55 #include <unordered_map>
58 #include <boost/functional/hash/hash.hpp>
59 #include <boost/ref.hpp>
60 #include <boost/tuple/detail/tuple_basic.hpp>
62 #include <nlohmann/json.hpp>
86 #define CONSTEXPR_ constexpr
92 #define _PASTE(a,b) a ## b
93 #define PASTE(a,b) _PASTE(a, b)
112 #ifdef FLOW123D_DEBUG_PROFILER
113 #define START_TIMER(tag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT(tag); TimerFrame PASTE(timer_,__LINE__) = TimerFrame( PASTE(cp_,__LINE__) )
115 #define START_TIMER(tag)
133 #ifdef FLOW123D_DEBUG_PROFILER
134 #define START_TIMER_EXT(tag, subtag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT_EXT(tag, subtag); TimerFrame PASTE(timer_,__LINE__) = TimerFrame( PASTE(cp_,__LINE__) )
136 #define START_TIMER_EXT(tag, subtag)
146 #ifdef FLOW123D_DEBUG_PROFILER
147 #define END_TIMER(tag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT(tag); Profiler::instance()->stop_timer( PASTE(cp_,__LINE__) )
149 #define END_TIMER(tag)
157 #ifdef FLOW123D_DEBUG_PROFILER
158 #define START_MEMORY_MONITORING Profiler::instance()->start_memory_monitoring()
160 #define START_MEMORY_MONITORING
168 #ifdef FLOW123D_DEBUG_PROFILER
169 #define END_START_TIMER(tag) Profiler::instance()->stop_timer(); START_TIMER(tag);
171 #define END_START_TIMER(tag)
194 #ifdef FLOW123D_DEBUG_PROFILER
195 #define ADD_CALLS(n_calls) Profiler::instance()->add_calls(n_calls)
197 #define ADD_CALLS(n_calls)
201 #ifdef FLOW123D_DEBUG_PROFILER
202 #define CUMUL_TIMER(tag) Profiler::instance()->find_timer(tag).cumulative_time()
204 #define CUMUL_TIMER(tag) 0
208 #ifdef FLOW123D_DEBUG_PROFILER
213 #define PROFILER_EMPTY_SUBTAG ""
218 #define PROFILER_HASH_DEFAULT 0
227 inline CONSTEXPR_ unsigned int str_hash(
const char * str,
unsigned int default_value) {
229 return (*str == 0 ? SALT : default_value + str_hash(str+1, PROFILER_HASH_DEFAULT) * 101 + (
unsigned int)(*str) );
235 #define CODE_POINT(tag) CodePoint(tag, __FILE__, __func__, __LINE__)
240 #define CODE_POINT_EXT(tag, subtag) CodePoint(tag, subtag, __FILE__, __func__, __LINE__)
255 CONSTEXPR_ CodePoint(
const char *tag,
const char * file,
const char * func,
const unsigned int line)
256 : tag_(tag), subtag_(PROFILER_EMPTY_SUBTAG), file_(file), func_(func), line_(line),
257 hash_(str_hash(tag, PROFILER_HASH_DEFAULT)),
258 hash_idx_( str_hash(tag, PROFILER_HASH_DEFAULT)%max_n_timer_childs )
260 CONSTEXPR_ CodePoint(
const char *tag,
const char *subtag,
const char * file,
const char * func,
const unsigned int line)
261 : tag_(tag), subtag_(subtag), file_(file), func_(func), line_(line),
262 hash_(str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))),
263 hash_idx_( str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))%max_n_timer_childs )
267 static const unsigned int max_n_timer_childs=13;
270 const char *
const tag_;
273 const char *
const subtag_;
276 const char *
const file_;
279 const char *
const func_;
282 const unsigned int line_;
288 unsigned int hash_idx_;
310 static const unsigned int max_n_childs=CodePoint::max_n_timer_childs;
315 Timer(
const CodePoint &cp,
int parent);
329 bool stop(
bool forced =
false);
333 inline string tag()
const {
334 string buf(code_point_->tag_);
335 buf.append(code_point_->subtag_);
340 inline bool running()
const
341 {
return start_count >0; }
344 std::string code_point_str()
const;
349 double cumulative_time()
const;
354 void add_child(
int child_index,
const Timer &child);
359 void start_memory_monitoring();
361 inline bool memory_monitor_on()
const {
362 return memory_monitor_on_;
393 unsigned int call_count;
397 unsigned int start_count;
404 const CodePoint *code_point_;
406 unsigned int full_hash_;
408 unsigned int hash_idx_;
417 int child_timers[max_n_childs];
423 size_t total_allocated_;
428 size_t total_deallocated_;
434 size_t max_allocated_;
441 size_t current_allocated_;
454 bool memory_monitor_on_;
456 #ifdef FLOW123D_HAVE_PETSC
460 PetscLogDouble petsc_start_memory;
464 PetscLogDouble petsc_end_memory;
468 PetscLogDouble petsc_memory_difference;
476 PetscLogDouble petsc_peak_memory;
482 PetscLogDouble petsc_local_peak_memory;
554 static Profiler* instance(
bool clear =
false);
561 void set_task_info(
string description,
int size);
567 void set_program_info(
string program_name,
string program_version,
string branch,
string revision,
string build);
575 int start_timer(
const CodePoint &cp);
583 void stop_timer(
const CodePoint &cp);
591 void stop_timer(
int timer_index = -1);
598 void start_memory_monitoring();
604 void add_calls(
unsigned int n_calls);
609 void notify_malloc(
const size_t size,
const long p);
614 void notify_free(
const long p);
620 static double get_resolution ();
626 Timer find_timer(
string tag);
631 #ifdef FLOW123D_HAVE_MPI
643 void output(
MPI_Comm comm, std::ostream &os);
649 string output(
MPI_Comm comm,
string profiler_path =
"");
662 void output(std::ostream &os);
668 string output(
string profiler_path =
"");
678 static void uninitialize();
684 static void*
operator new (
size_t sz);
690 static void operator delete (
void* p);
692 static void operator delete (
void* p, std::size_t);
699 void static set_memory_monitoring(
const bool global_monitor);
705 inline bool static get_global_memory_monitoring() {
706 return global_monitor_memory;
718 double calibration_time() {
719 if (calibration_time_ < 0) calibrate();
720 return calibration_time_;
735 static bool global_monitor_memory;
740 static bool petsc_monitor_memory;
748 static const long malloc_map_reserve;
754 void propagate_timers ();
759 void accept_from_child (
Timer &parent,
Timer &child);
765 int find_child(
const CodePoint &cp);
785 unsigned int actual_node;
803 string task_description_;
810 string flow_version_;
814 string flow_revision_;
823 double calibration_time_;
831 template<
typename ReduceFunctor>
832 void add_timer_info(ReduceFunctor reduce,
nlohmann::json* node,
int timer_idx,
double parent_time);
863 int const timer_index_;
865 inline TimerFrame(
const CodePoint &cp)
866 : timer_index_(
Profiler::instance()->start_timer(cp) )
889 static unordered_map_with_alloc & malloc_map();
901 static Profiler* instance(
bool clear =
false);
933 static void uninitialize();
static int sum(int *val, MPI_Comm comm)
static int min(int *val, MPI_Comm comm)
static int max(int *val, MPI_Comm comm)
double calibration_time()
void notify_malloc(const size_t)
void output(MPI_Comm, ostream &)
void notify_free(const size_t)
static void set_memory_monitoring(bool)
static Profiler * instance(bool clear=false)
double actual_cumulative_time() const
void output(std::ostream &)
unsigned int actual_count() const
void set_program_info(string, string, string, string, string)
const char * actual_tag() const
string output(MPI_Comm, string)
void set_task_info(string, int)
double get_resolution() const
a class to store JSON values
Global macros to enhance readability and debugging, general constants.
STREAM & operator<<(STREAM &s, UpdateFlags u)