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 inline void set_turn_off_memory_monitoring() {
360 turn_off_memory_monitoring_ =
true;
391 unsigned int call_count;
395 unsigned int start_count;
402 const CodePoint *code_point_;
404 unsigned int full_hash_;
406 unsigned int hash_idx_;
415 int child_timers[max_n_childs];
421 size_t total_allocated_;
426 size_t total_deallocated_;
432 size_t max_allocated_;
439 size_t current_allocated_;
452 bool turn_off_memory_monitoring_;
454 #ifdef FLOW123D_HAVE_PETSC
458 PetscLogDouble petsc_start_memory;
462 PetscLogDouble petsc_end_memory;
466 PetscLogDouble petsc_memory_difference;
474 PetscLogDouble petsc_peak_memory;
480 PetscLogDouble petsc_local_peak_memory;
552 static Profiler* instance(
bool clear =
false);
559 void set_task_info(
string description,
int size);
565 void set_program_info(
string program_name,
string program_version,
string branch,
string revision,
string build);
573 int start_timer(
const CodePoint &cp);
581 void stop_timer(
const CodePoint &cp);
589 void stop_timer(
int timer_index = -1);
596 void start_memory_monitoring();
602 void add_calls(
unsigned int n_calls);
607 void notify_malloc(
const size_t size,
const long p);
612 void notify_free(
const long p);
618 static double get_resolution ();
624 Timer find_timer(
string tag);
629 #ifdef FLOW123D_HAVE_MPI
641 void output(
MPI_Comm comm, std::ostream &os);
647 string output(
MPI_Comm comm,
string profiler_path =
"");
660 void output(std::ostream &os);
666 string output(
string profiler_path =
"");
676 static void uninitialize();
682 static void*
operator new (
size_t sz);
688 static void operator delete (
void* p);
690 static void operator delete (
void* p, std::size_t);
697 void static set_memory_monitoring(
const bool global_monitor);
703 inline bool static get_global_memory_monitoring() {
704 return global_monitor_memory;
711 inline bool static get_petsc_memory_monitoring() {
712 return petsc_monitor_memory;
724 double calibration_time() {
725 if (calibration_time_ < 0) calibrate();
726 return calibration_time_;
741 static bool global_monitor_memory;
746 static bool petsc_monitor_memory;
754 static const long malloc_map_reserve;
760 void propagate_timers ();
765 void accept_from_child (
Timer &parent,
Timer &child);
771 int find_child(
const CodePoint &cp);
791 unsigned int actual_node;
809 string task_description_;
816 string flow_version_;
820 string flow_revision_;
829 double calibration_time_;
837 template<
typename ReduceFunctor>
838 void add_timer_info(ReduceFunctor reduce,
nlohmann::json* node,
int timer_idx,
double parent_time);
869 int const timer_index_;
871 inline TimerFrame(
const CodePoint &cp)
872 : timer_index_(
Profiler::instance()->start_timer(cp) )
895 static unordered_map_with_alloc & malloc_map();
907 static Profiler* instance(
bool clear =
false);
939 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)