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_;
397 unsigned int call_count;
401 unsigned int start_count;
408 const CodePoint *code_point_;
410 unsigned int full_hash_;
412 unsigned int hash_idx_;
421 int child_timers[max_n_childs];
427 size_t total_allocated_;
432 size_t total_deallocated_;
438 size_t max_allocated_;
445 size_t current_allocated_;
458 bool memory_monitor_on_;
460 #ifdef FLOW123D_HAVE_PETSC
464 PetscLogDouble petsc_start_memory;
468 PetscLogDouble petsc_end_memory;
472 PetscLogDouble petsc_memory_difference;
480 PetscLogDouble petsc_peak_memory;
486 PetscLogDouble petsc_local_peak_memory;
558 static Profiler* instance(
bool clear =
false);
565 void set_task_info(
string description,
int size);
571 void set_program_info(
string program_name,
string program_version,
string branch,
string revision,
string build);
579 int start_timer(
const CodePoint &cp);
587 void stop_timer(
const CodePoint &cp);
595 void stop_timer(
int timer_index = -1);
602 void start_memory_monitoring();
608 void add_calls(
unsigned int n_calls);
613 void notify_malloc(
const size_t size,
const long p);
618 void notify_free(
const long p);
624 static double get_resolution ();
630 Timer find_timer(
string tag);
635 #ifdef FLOW123D_HAVE_MPI
647 void output(
MPI_Comm comm, std::ostream &os);
653 string output(
MPI_Comm comm,
string profiler_path =
"");
666 void output(std::ostream &os);
672 string output(
string profiler_path =
"");
682 static void uninitialize();
688 static void*
operator new (
size_t sz);
694 static void operator delete (
void* p);
696 static void operator delete (
void* p, std::size_t);
703 void static set_memory_monitoring(
const bool global_monitor);
709 inline bool static get_global_memory_monitoring() {
710 return global_monitor_memory;
722 double calibration_time() {
723 if (calibration_time_ < 0) calibrate();
724 return calibration_time_;
739 static bool global_monitor_memory;
744 static bool petsc_monitor_memory;
752 static const long malloc_map_reserve;
758 void propagate_timers ();
763 void accept_from_child (
Timer &parent,
Timer &child);
769 int find_child(
const CodePoint &cp);
789 unsigned int actual_node;
807 string task_description_;
814 string flow_version_;
818 string flow_revision_;
827 double calibration_time_;
835 template<
typename ReduceFunctor>
836 void add_timer_info(ReduceFunctor reduce,
nlohmann::json* node,
int timer_idx,
double parent_time);
867 int const timer_index_;
869 inline TimerFrame(
const CodePoint &cp)
870 : timer_index_(
Profiler::instance()->start_timer(cp) )
893 static unordered_map_with_alloc & malloc_map();
905 static Profiler* instance(
bool clear =
false);
937 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)