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 END_START_TIMER(tag) Profiler::instance()->stop_timer(); START_TIMER(tag);
160 #define END_START_TIMER(tag)
183 #ifdef FLOW123D_DEBUG_PROFILER
184 #define ADD_CALLS(n_calls) Profiler::instance()->add_calls(n_calls)
186 #define ADD_CALLS(n_calls)
190 #ifdef FLOW123D_DEBUG_PROFILER
191 #define CUMUL_TIMER(tag) Profiler::instance()->find_timer(tag).cumulative_time()
193 #define CUMUL_TIMER(tag)
197 #ifdef FLOW123D_DEBUG_PROFILER
202 #define PROFILER_EMPTY_SUBTAG ""
207 #define PROFILER_HASH_DEFAULT 0
216 inline CONSTEXPR_ unsigned int str_hash(
const char * str,
unsigned int default_value) {
217 #define SALT 0 //0xef50e38f
218 return (*str == 0 ? SALT : default_value + str_hash(str+1, PROFILER_HASH_DEFAULT) * 101 + (
unsigned int)(*str) );
224 #define CODE_POINT(tag) CodePoint(tag, __FILE__, __func__, __LINE__)
229 #define CODE_POINT_EXT(tag, subtag) CodePoint(tag, subtag, __FILE__, __func__, __LINE__)
244 CONSTEXPR_ CodePoint(
const char *tag,
const char * file,
const char * func,
const unsigned int line)
245 : tag_(tag), subtag_(PROFILER_EMPTY_SUBTAG), file_(file), func_(func), line_(line),
246 hash_(str_hash(tag, PROFILER_HASH_DEFAULT)),
247 hash_idx_( str_hash(tag, PROFILER_HASH_DEFAULT)%max_n_timer_childs )
249 CONSTEXPR_ CodePoint(
const char *tag,
const char *subtag,
const char * file,
const char * func,
const unsigned int line)
250 : tag_(tag), subtag_(subtag), file_(file), func_(func), line_(line),
251 hash_(str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))),
252 hash_idx_( str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))%max_n_timer_childs )
256 static const unsigned int max_n_timer_childs=13;
259 const char *
const tag_;
262 const char *
const subtag_;
265 const char *
const file_;
268 const char *
const func_;
271 const unsigned int line_;
277 unsigned int hash_idx_;
299 static const unsigned int max_n_childs=CodePoint::max_n_timer_childs;
304 Timer(
const CodePoint &cp,
int parent);
318 bool stop(
bool forced =
false);
322 inline string tag()
const {
323 string buf(code_point_->tag_);
324 buf.append(code_point_->subtag_);
329 inline bool running()
const
330 {
return start_count >0; }
333 std::string code_point_str()
const;
338 double cumulative_time()
const;
343 void add_child(
int child_index,
const Timer &child);
373 unsigned int call_count;
377 unsigned int start_count;
384 const CodePoint *code_point_;
386 unsigned int full_hash_;
388 unsigned int hash_idx_;
397 int child_timers[max_n_childs];
403 size_t total_allocated_;
408 size_t total_deallocated_;
414 size_t max_allocated_;
421 size_t current_allocated_;
432 #ifdef FLOW123D_HAVE_PETSC
436 PetscLogDouble petsc_start_memory;
440 PetscLogDouble petsc_end_memory;
444 PetscLogDouble petsc_memory_difference;
452 PetscLogDouble petsc_peak_memory;
458 PetscLogDouble petsc_local_peak_memory;
459 #endif // FLOW123D_HAVE_PETSC
530 static Profiler* instance(
bool clear =
false);
537 void set_task_info(
string description,
int size);
543 void set_program_info(
string program_name,
string program_version,
string branch,
string revision,
string build);
551 int start_timer(
const CodePoint &cp);
559 void stop_timer(
const CodePoint &cp);
567 void stop_timer(
int timer_index = -1);
573 void add_calls(
unsigned int n_calls);
578 void notify_malloc(
const size_t size,
const long p);
583 void notify_free(
const long p);
589 static double get_resolution ();
595 Timer find_timer(
string tag);
600 #ifdef FLOW123D_HAVE_MPI
612 void output(
MPI_Comm comm, std::ostream &os);
618 void output(
MPI_Comm comm,
string profiler_path =
"");
631 void output(std::ostream &os);
637 void output(
string profiler_path =
"");
642 void transform_profiler_data (
const string &output_file_suffix,
const string &formatter);
647 static void uninitialize();
653 static void*
operator new (
size_t sz);
659 static void operator delete (
void* p);
661 static void operator delete (
void* p, std::size_t);
668 void static set_memory_monitoring(
const bool global_monitor,
const bool petsc_monitor);
674 inline bool static get_global_memory_monitoring() {
675 return global_monitor_memory;
682 inline bool static get_petsc_memory_monitoring() {
683 return petsc_monitor_memory;
695 double calibration_time() {
696 if (calibration_time_ < 0) calibrate();
697 return calibration_time_;
712 static bool global_monitor_memory;
717 static bool petsc_monitor_memory;
725 static const long malloc_map_reserve;
731 void propagate_timers ();
736 void accept_from_child (
Timer &parent,
Timer &child);
742 int find_child(
const CodePoint &cp);
756 std::shared_ptr<std::ostream> get_default_output_stream();
762 unsigned int actual_node;
780 string task_description_;
787 string flow_version_;
791 string flow_revision_;
795 string json_filepath;
800 double calibration_time_;
808 template<
typename ReduceFunctor>
809 void add_timer_info(ReduceFunctor reduce,
nlohmann::json* node,
int timer_idx,
double parent_time);
840 int const timer_index_;
842 inline TimerFrame(
const CodePoint &cp)
843 : timer_index_(
Profiler::instance()->start_timer(cp) )
866 static unordered_map_with_alloc & malloc_map();
872 #else // FLOW123D_DEBUG_PROFILER
878 static Profiler* instance(
bool clear =
false);
910 static void uninitialize();