Flow123d  release_2.2.0-36-g163dc99
sys_profiler.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file sys_profiler.hh
15  * @brief
16  * @todo
17  * - START_GLOBAL_TIMER(tag) - this calls the start_timer, which creates local timer on the correct place in the hierarchy,
18  * further this timer is added to the list of global timers, this contains groups of timers with same tag, and
19  * collect/sum data from these timers in the report.
20  *
21  * - Allow output even during calculation (not complete, but at least some thing)
22  * Report should contain time of start as well as time of creation of the report or time from start of the program.
23  *
24  * - When generating report we has to deal with possibly different trees at every MPI process.
25  *
26  * - test memory profiling
27  * in our own new and xmalloc functions - register allocatied and deallocated memory to active Profiler frame.
28  *
29  * - test in parallel
30  * - extended output:
31  * cas na jedno volani (jina redukce nez pro kumulativni cas, pokud je pocet volani ruzny)
32  * procenta vuci predkovi
33  * code point (az nekde na konci radky)
34  *
35  *
36  * !!! Unfortunately using constexpr is worse (without optimization).
37  * This is probably due to use of static variable for
38  * CodePoint, the access could be slow, and computation of hash is done only once. Actually timing results
39  * are:
40  *
41  * OPTIONS OVERHEAD (compared to call 2x clock())
42  * -g, no c++11 : 18%
43  * -g, c++11 : 60%
44  * -O3,no c++11 : 6%
45  * -O3, c++11 : 6%
46  */
47 
48 #ifndef PROFILER_H
49 #define PROFILER_H
50 
51 #include "global_defs.h"
52 
53 #include <mpi.h>
54 #include <ostream>
55 namespace boost { template <class T> struct hash; }
56 #include <boost/functional/hash/hash.hpp> // for hash
57 #include <boost/property_tree/ptree_fwd.hpp> // for ptree, property_tree
58 #include <boost/ref.hpp>
59 #include <boost/tuple/detail/tuple_basic.hpp> // for get
60 #include <boost/unordered/unordered_map.hpp> // for unordered_map
61 
62 #include "time_point.hh"
63 #include "petscsys.h"
64 #include "simple_allocator.hh"
65 
66 
67 // namespace alias
68 namespace property_tree = boost::property_tree;
69 
70 //instead of #include "mpi.h"
71 //mpi declarations follows:
73 public:
74  static int sum(int* val, MPI_Comm comm);
75  static double sum(double* val, MPI_Comm comm);
76  static long sum(long* val, MPI_Comm comm);
77 
78  static int min(int* val, MPI_Comm comm);
79  static double min(double* val, MPI_Comm comm);
80  static long min(long* val, MPI_Comm comm);
81 
82  static int max(int* val, MPI_Comm comm);
83  static double max(double* val, MPI_Comm comm);
84  static long max(long* val, MPI_Comm comm);
85 };
86 
87 // Assuming all compilers support constexpr
88 #define CONSTEXPR_ constexpr
89 
90 using namespace std;
91 
92 
93 // These helper macros are necessary due to use of _LINE_ variable in START_TIMER macro.
94 #define _PASTE(a,b) a ## b
95 #define PASTE(a,b) _PASTE(a, b)
96 
97 
98 
99 /**
100  * \def START_TIMER(tag)
101  *
102  * @brief Starts a timer with specified tag.
103  *
104  * In fact it creates an static constant expression that identifies the point in the code and
105  * contains tag of the involved timer and its hash. Then it creates local variable that
106  * calls @p Profiler::start_timer() in constructor and @p Profiler::stop_timer() in destructor.
107  * This way the timer is automatically closed at the end of current block.
108  *
109  * ATTENTION: This macro expands to two statements so following code is illegal:
110  * @code
111  * if (some_condition) START_TIMER(tag);
112  * @endcode
113  */
114 #ifdef FLOW123D_DEBUG_PROFILER
115 #define START_TIMER(tag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT(tag); TimerFrame PASTE(timer_,__LINE__) = TimerFrame( PASTE(cp_,__LINE__) )
116 #else
117 #define START_TIMER(tag)
118 #endif
119 
120 /**
121  * \def START_TIMER_EXT (tag, subtag)
122  *
123  * @brief Starts a timer with specified tag and subtag.
124  *
125  * In fact it creates an static constant expression that identifies the point in the code and
126  * contains tag and subtag of the involved timer and its hash. Then it creates local variable that
127  * calls @p Profiler::start_timer() in constructor and @p Profiler::stop_timer() in destructor.
128  * This way the timer is automatically closed at the end of current block.
129  *
130  * ATTENTION: This macro expands to two statements so following code is illegal:
131  * @code
132  * if (some_condition) START_TIMER_EXT(tag, subtag);
133  * @endcode
134  */
135 #ifdef FLOW123D_DEBUG_PROFILER
136 #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__) )
137 #else
138 #define START_TIMER_EXT(tag, subtag)
139 #endif
140 
141 /**
142  * \def END_TIMER(tag)
143  *
144  * @brief Ends a timer with specified tag.
145  *
146  * Use only if you want to end timer before the end of block. Again this expands into two lines, see ATTENTION in previous macro.
147  */
148 #ifdef FLOW123D_DEBUG_PROFILER
149 #define END_TIMER(tag) static CONSTEXPR_ CodePoint PASTE(cp_,__LINE__) = CODE_POINT(tag); Profiler::instance()->stop_timer( PASTE(cp_,__LINE__) )
150 #else
151 #define END_TIMER(tag)
152 #endif
153 
154 /**
155  * \def END_START_TIMER(tag)
156  *
157  * Ends current timer and starts the new one with given tag. Again this expands into two lines, see ATTENTION in previous macro.
158  */
159 #ifdef FLOW123D_DEBUG_PROFILER
160 #define END_START_TIMER(tag) Profiler::instance()->stop_timer(); START_TIMER(tag);
161 #else
162 #define END_START_TIMER(tag)
163 #endif
164 
165 
166 /**
167  * \def ADD_CALLS(n_calls)
168  *
169  * @brief Increase number of calls in actual timer.
170  *
171  * Some time you want to measure a performance of a cycle with body that is below resolution of the Timer implementation.
172  * If you know number of cycles, you can use this macro in following way:
173  *
174  * @code
175  * START_TIMER("cycle");
176  * unsigned int i;
177  * for(i =0; i<1000000; i++) i*i*i;
178  * ADD_CALLS(i);
179  * END_TIMER("cycle");
180  * @endcode
181  *
182  * In the profiler report you get the total time spent in the cycle, and time per one call which will be average
183  * time spent in the body of the cycle.
184  */
185 #ifdef FLOW123D_DEBUG_PROFILER
186 #define ADD_CALLS(n_calls) Profiler::instance()->add_calls(n_calls)
187 #else
188 #define ADD_CALLS(n_calls)
189 #endif
190 
191 
192 
193 
194 //////////////////////////////////////////////////////////////////////////////////////////////
195 #ifdef FLOW123D_DEBUG_PROFILER
196 
197 /**
198  * Variable which represents value when no subtag was specified in CodePoint class
199  */
200 #define PROFILER_EMPTY_SUBTAG ""
201 
202 /**
203  * Variable used for default value in hash process
204  */
205 #define PROFILER_HASH_DEFAULT 0
206 
207 /**
208  * @brief Function for compile-time hash computation. (Needs C++x11 standard.)
209  * Input, @p str, is constant null terminated string, result is unsigned int (usually 4 bytes).
210  * Function has to be recursive, since standard requires that the body consists only from the return statement.
211  *
212  * SALT is hash for the empty string. Currently zero for simpler testing.
213  */
214 inline CONSTEXPR_ unsigned int str_hash(const char * str, unsigned int default_value) {
215  #define SALT 0 //0xef50e38f
216  return (*str == 0 ? SALT : default_value + str_hash(str+1, PROFILER_HASH_DEFAULT) * 101 + (unsigned int)(*str) );
217 }
218 
219 /**
220  * Macro to generate constexpr CodePoint object.
221  */
222 #define CODE_POINT(tag) CodePoint(tag, __FILE__, __func__, __LINE__)
223 
224 /**
225  * Macro to generate constexpr CodePoint object.
226  */
227 #define CODE_POINT_EXT(tag, subtag) CodePoint(tag, subtag, __FILE__, __func__, __LINE__)
228 
229 
230 
231 
232 /**
233  * @brief Class that represents point in the code.
234  *
235  * This class allow construction at compile time. And includes the information about the code point as well
236  * as the 'tag' of the timer and cimpile-time computed hashes of this 'tag'. The @p hash_ is long one with
237  * very small probability of collisions - this we use for comparison of tags. The @p hash_idx_ is the long hash modulo
238  * length of the array of Timer's children, this is used for fast loop up into this array that servers as a simple hash table.
239  */
240 class CodePoint {
241 public:
242  CONSTEXPR_ CodePoint(const char *tag, const char * file, const char * func, const unsigned int line)
243  : tag_(tag), subtag_(PROFILER_EMPTY_SUBTAG), file_(file), func_(func), line_(line),
244  hash_(str_hash(tag, PROFILER_HASH_DEFAULT)),
245  hash_idx_( str_hash(tag, PROFILER_HASH_DEFAULT)%max_n_timer_childs )
246  {};
247  CONSTEXPR_ CodePoint(const char *tag, const char *subtag, const char * file, const char * func, const unsigned int line)
248  : tag_(tag), subtag_(subtag), file_(file), func_(func), line_(line),
249  hash_(str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))),
250  hash_idx_( str_hash(subtag, str_hash(tag, PROFILER_HASH_DEFAULT))%max_n_timer_childs )
251  {};
252 
253  /// Size of child arrays in timer nodes.
254  static const unsigned int max_n_timer_childs=13;
255 
256  /// Tag of the code point.
257  const char * const tag_;
258 
259  /// Subtag of the code point.
260  const char * const subtag_;
261 
262  /// file name of the code point
263  const char * const file_;
264 
265  /// file name of the code point
266  const char * const func_;
267 
268  /// file name of the code point
269  const unsigned int line_;
270 
271  /// Full 32-bit hash of the tag ( practically no chance of collision)
272  unsigned int hash_;
273 
274  /// Hash modulo size of array of timer childs ( we have to check full hash to prevent collision)
275  unsigned int hash_idx_;
276 };
277 
278 
279 
280 /**
281  * @brief Class for profiling tree nodes.
282  *
283  * One Timer represents one particular time frame in the execution tree.
284  * It collects information about total time, number of calls, allocated and deallocated memory.
285  *
286  * It should be accessed only through Profiler, which is its friend class.
287  *
288  * TODO: for better performance: move copy hash_ and hash_idx_ into Timer since CodePoint are in static
289  * variables, that may be slower to acces.
290  *
291  */
292 class Timer {
293 
294 
295 public:
296  /// Size of array @p child_timers, the hash table containing descendants in the call tree.
297  static const unsigned int max_n_childs=CodePoint::max_n_timer_childs;
298 
299  /**
300  * Creates the timer node object. Should not be called directly, but through the START_TIMER macro.
301  */
302  Timer(const CodePoint &cp, int parent);
303 
304 
305  /**
306  * Start the timer. If it is already started, just increase number of starts (recursions) and calls.
307  */
308  void start();
309 
310  /**
311  * If number of starts (recursions) drop back to zero, we stop the timer and add the period to the cumulative time.
312  * This method do not take care of its childs (it has no access to the other timers).
313  * When the parameter 2p forced is 'true', we stop the timer immediately regardless the number of recursions.
314  * Returns true if the timer is not closed (recursions didn't drop to zero yet).
315  */
316  bool stop(bool forced = false);
317 
318 
319  /// Getter for the 'tag'.
320  inline string tag() const {
321  string buf(code_point_->tag_);
322  buf.append(code_point_->subtag_);
323  return buf;
324  }
325 
326  /// Returns true if the timer is open, number of starts (recursions) is nonzero.
327  inline bool running() const
328  { return start_count >0; }
329 
330  /// Returns string with description of the code point where the timer was first started.
331  std::string code_point_str() const;
332 
333  /**
334  * Returns cumulative time of the timer in seconds.
335  */
336  double cumulative_time() const;
337 
338  /*
339  * Adds given index @p child_index of the timer @p child to the correct place in the hash table.
340  */
341  void add_child(int child_index, const Timer &child);
342 
343 
344 protected:
345 
346  /**
347  * Pauses current timer, save measured petsc memory information util resume.
348  * We get Petsc maximum memory usage via PetscMemoryGetMaximumUsage call
349  * and save this value into temp value. (we override local maximum if temp
350  * value is greater)
351  */
352  void pause();
353  /**
354  * Resume current timer. e tell Petsc to monitor the maximum memory
355  * usage once again. We call PetscMemorySetGetMaximumUsage so later in
356  * resume() method will PetscMemoryGetMaximumUsage method work.
357  */
358  void resume();
359 
360  /**
361  * Start time when frame opens.
362  */
363  TimePoint start_time;
364  /**
365  * Cumulative time spent in the frame.
366  */
367  double cumul_time;
368  /**
369  * Total number of opening of the frame.
370  */
371  unsigned int call_count;
372  /**
373  * Number of recursive openings.
374  */
375  unsigned int start_count;
376 
377 
378  /**
379  * Code point of the first START_TIMER for the particular tag. The 'tag' identifies timer
380  * and is used in reported profiler table.
381  */
382  const CodePoint *code_point_;
383  /// Full tag hash. Copy from code_point_.
384  unsigned int full_hash_;
385  /// Hash modulo size of array of timer childs. Copy from code_point_.
386  unsigned int hash_idx_;
387 
388  /**
389  * Index of the parent timer node in the tree. Negative value means 'not set'.
390  */
391  int parent_timer;
392  /**
393  * Indices of the child timers in the Profiler::timers_ vector. Negative values means 'not set'.
394  */
395  int child_timers[max_n_childs];
396 
397  /**
398  * Total number of bytes allocated in this frame. After
399  * Profiler::propagate_timers call will also contain values from children.
400  */
401  size_t total_allocated_;
402  /**
403  * Total number of bytes deallocated in this frame. After
404  * Profiler::propagate_timers call, will also contain values from children.
405  */
406  size_t total_deallocated_;
407  /**
408  * Maximum number of bytes allocated at one time in this frame. After
409  * Profiler::propagate_timers call, maximum value will be taken from this
410  * Timer and also from all children Timers.
411  */
412  size_t max_allocated_;
413  /**
414  * Current number of bytes allocated in this frame at the same time.
415  * This value is used to monitor maximum bytes allocated. When notify_free
416  * and notify_malloc is called this values is changed and new maximum
417  * is tested.
418  */
419  size_t current_allocated_;
420 
421  /**
422  * Number of times new/new[] operator was used in this scope
423  */
424  int alloc_called;
425  /**
426  * Number of times delete/delete[] operator was used in this scope
427  */
428  int dealloc_called;
429 
430  #ifdef FLOW123D_HAVE_PETSC
431  /**
432  * Number of bytes used by Petsc at the start of time-frame
433  */
434  PetscLogDouble petsc_start_memory;
435  /**
436  * Number of bytes used by Petsc at the end of time-frame
437  */
438  PetscLogDouble petsc_end_memory;
439  /**
440  * Difference between start and end of a petsc memory usage
441  */
442  PetscLogDouble petsc_memory_difference;
443  /**
444  * Maximum amount of memory used that was PetscMalloc()ed at any time
445  * during this run.
446  *
447  * The memory usage reported here includes all Fortran arrays (that may be
448  * used in application-defined sections of code).
449  */
450  PetscLogDouble petsc_peak_memory;
451  /**
452  * Local maximum amount of memory used that was PetscMalloc()ed
453  * used during time-frame pause/resume. Auxilary variable for storing
454  * local memory used when pause is called.
455  */
456  PetscLogDouble petsc_local_peak_memory;
457  #endif // FLOW123D_HAVE_PETSC
458 
459  friend class Profiler;
460  friend std::ostream & operator <<(std::ostream&, const Timer&);
461 
462  /**
463  * if under unit testing, specify friend so protected members can be tested
464  */
465  #ifdef __UNIT_TEST__
466  friend ProfilerTest;
467  #endif /* __UNIT_TEST__ */
468 
469 };
470 
471 /*
472 struct SimpleTranslator {
473  typedef std::string internal_type;
474  typedef int external_type;
475 
476  // Converts a string to int
477  boost::optional<external_type> get_value(const internal_type& str) {
478  return boost::optional<external_type>(std::stoi(str));
479  }
480 
481  // Converts a bool to string
482  boost::optional<internal_type> put_value(const external_type& i){
483  return boost::optional<internal_type>(std::to_string(i));
484  }
485 };
486 
487 namespace boost {
488 namespace property_tree {
489 
490 template<typename Ch, typename Traits, typename Alloc>
491 struct translator_between<std::basic_string< Ch, Traits, Alloc >, int> {
492  typedef SimpleTranslator type;
493 };
494 
495 
496 } // namespace property_tree
497 } // namespace boost
498 */
499 /**
500  *
501  * @brief Main class for profiling by measuring time intervals.
502  *
503  * These time intervals form a tree structure where each interval is represented
504  * by a Timer object. The root node of the tree is automatically created and
505  * started after creating the Profiler object and cannot be stopped manually.
506  *
507  * The class implements a singleton pattern and all the functions are accessible trough
508  * Profiler::instance(), but in most cases the programmer will access the profiler
509  * functions via the #START_TIMER and #END_TIMER macros. The #START_TIMER macro
510  * is responsible for the fact that we don't have to call #END_TIMER macro to stop the timer and
511  * the timer will be stopped at the end of the block in which #START_TIMER was used.
512  * These macros internally use the TimerFrame objects and the programmer should
513  * not use the TimerFrame objects directly.
514  *
515  * By using #SET_TIMER_SUBFRAMES macro, the programmer can specify the number of subframes (eg. iterations)
516  * for the currently active timer.
517  *
518  *
519  * Currently the Profiler system is not thread safe. No idea how to do this.
520  *
521  */
522 class Profiler {
523 public:
524 
525  /**
526  * Initializes the Profiler with specific MPI communicator object
527  */
528  //static void initialize(MPI_Comm communicator = MPI_COMM_WORLD);
529  static void initialize();
530  /**
531  * Returns unique Profiler object.
532  */
533  static Profiler* instance();
534  /**
535  * Sets task specific information. The string @p description with textual description of the task and the
536  * number of elements of the mesh (parameter @p size). This is used for weak scaling graphs so it should
537  * measure size of the task of the same type (same description).
538  *
539  */
540  void set_task_info(string description, int size);
541  /**
542  * Sets informations about program version. This consists of @p program_version (includes program name), @p branch in the repository or rather full URL of the branch,
543  * and SVN @p revision (or hash for GIT).
544  *
545  */
546  void set_program_info(string program_name, string program_version, string branch, string revision, string build);
547 
548 
549  /**
550  * Starts a timer with code point, tag and hashes specified by CodePoint object @p cp.
551  * If the timer is not already created, it creates a new one. It returns index of
552  * the actual timer.
553  */
554  int start_timer(const CodePoint &cp);
555  /**
556  * Stops actual timer. It check if the hash of given code point match hash of the
557  * tag of actual timer node. If not we print out warning and try to find the correct tag
558  * towards the tree root closing all nodes we pass through.
559  *
560  * If FLOW123D_DEBUG is set, we check that all children are closed.
561  */
562  void stop_timer(const CodePoint &cp);
563 
564  /**
565  * Stop timer with index given by @p timer_index. If this is not equal to @p actual_node, we
566  * traverse the tree towards root while force closing nodes by the way.
567  *
568  * Negative @p timer_index means close @p actual_node
569  */
570  void stop_timer(int timer_index = -1);
571 
572  /**
573  * Adds @p n_calls - 1 to the total number of calls of the current timer. Minus one, since one call is counted when
574  * timer was started. You should use macro ADD_CALLS above.
575  */
576  void add_calls(unsigned int n_calls);
577  /**
578  * Notification about allocation of given size.
579  * Increase total allocated memory in current profiler frame.
580  */
581  void notify_malloc(const size_t size, const long p);
582  /**
583  * Notification about freeing memory of given size.
584  * Increase total deallocated memory in current profiler frame.
585  */
586  void notify_free(const long p);
587 
588  /**
589  * Return average profiler timer resolution in seconds
590  * based on 100 measurements
591  */
592  static double get_resolution ();
593 
594 
595 #ifdef FLOW123D_HAVE_MPI
596  /**
597  * @brief Output current timing information into the given stream.
598  *
599  * COLECTIVE - all processes in the communicator have to call this
600  * method. All timers are finished, all processes are synchronized, collect
601  * profiling informations are collected and written to the given stream.
602  *
603  * Pass through the profiling tree (collective over processors)
604  * Print cumulative times average, balance (max/min), count (denote differences)
605  *
606  */
607  void output(MPI_Comm comm, std::ostream &os);
608  /**
609  * Same as previous, but output to the file with default name: "profiler_info_YYMMDD_HH::MM:SS.log".
610  * Empty body if macro FLOW123D_DEBUG_PROFILER is not defined.
611  */
612  void output(MPI_Comm comm);
613 #endif /* FLOW123D_HAVE_MPI */
614  /**
615  * @brief Output current timing information into the given stream.
616  *
617  * It temporally stops all timers, synchronize all processes, collect
618  * profiling informations and write it to the given stream.
619  *
620  * Pass through the profiling tree (collective over processors)
621  * Print cumulative times average, balance (max/min), count (denote differences)
622  *
623  */
624  void output(std::ostream &os);
625  /**
626  * Same as previous, but output to the file with default name: "profiler_info_YYMMDD_HH::MM:SS.log".
627  * Empty body if macro FLOW123D_DEBUG_PROFILER is not defined.
628  */
629  void output();
630  /**
631  * Method will transform last profiler json file to desired format
632  */
633  void transform_profiler_data (const string &output_file_suffix, const string &formatter);
634  /**
635  * Stop all timers and destroys the Profiler object.
636  * If you want some output call @p output method just before.
637  */
638  static void uninitialize();
639 
640  /**
641  * Class-specific allocation function new. Called by the usual
642  * single-object new-expressions if allocating an object of type Profiler.
643  */
644  static void* operator new (size_t sz);
645  /**
646  * Class-specific allocation function delete. Deallocates storage
647  * previously allocated by a matching operator new. These deallocation
648  * functions are called by delete-expressions.
649  */
650  static void operator delete (void* p);
651 
652  /**
653  * Public setter to turn on/off memory monitoring
654  * @param global_monitor whether to turn global monitoring on or off
655  * @param petsc_monitor petsc monitoring
656  */
657  void static set_memory_monitoring(const bool global_monitor, const bool petsc_monitor);
658 
659  /**
660  * Public getter to memory monitoring
661  * @return memory monitoring status
662  */
663  bool static get_global_memory_monitoring();
664 
665  /**
666  * Public getter to petsc memory monitoring
667  * @return memory monitoring status
668  */
669  bool static get_petsc_memory_monitoring();
670 
671  /**
672  * if under unit testing, specify friend so protected members can be tested
673  */
674  #ifdef __UNIT_TEST__
675  friend ProfilerTest;
676  #endif /* __UNIT_TEST__ */
677 
678 protected:
679 
680  /**
681  * Whether to monitor operator 'new/delete'
682  */
683  static bool global_monitor_memory;
684 
685  /**
686  * Whether to monitor petsc memory usage
687  */
688  static bool petsc_monitor_memory;
689 
690  /**
691  * When creating Profiler also reserve some bytes in malloc_map so overhead
692  * of creating single items is lowered. This value is passed as parameter in
693  * map.reserve() method so it indicates how many objects (pointers) are
694  * allocated at first.
695  */
696  static const long malloc_map_reserve;
697 
698  /**
699  * Method will propagate values from children timers to its parents
700  */
701  void propagate_timers ();
702 
703  /**
704  * Method for exchanging metrics from child timer to its parent timer
705  */
706  void accept_from_child (Timer &parent, Timer &child);
707 
708  /**
709  * Try to find timer with tag (in fact only its 32-bit hash) from given code point @p cp.
710  * Returns -1 if it is not found otherwise it returns its index.
711  */
712  int find_child(const CodePoint &cp);
713 
714 
715  /**
716  * Method will prepare construct specific details about the run (time start and time end)
717  * and write them along with basic informations about the run (name, description, ...)
718  * into ptree object
719  */
720  void output_header (property_tree::ptree &root, int mpi_size);
721 
722  /**
723  * Open a new file for profiler output with default name based on the
724  * actual time and date. Returns a pointer to the stream of the output file.
725  */
726  std::shared_ptr<std::ostream> get_default_output_stream();
727 
728  /// Default code point.
729  static CodePoint null_code_point;
730 
731  /// Pointer to the unique instance of singleton Profiler class.
732  static Profiler* _instance;
733 
734  /// Vector of all timers. Whole tree is stored in this array.
736 
737  /// Index of the actual timer node. Negative value means 'unset'.
738  unsigned int actual_node;
739 
740  /// MPI communicator used for final reduce of the timer node tree.
741  //MPI_Comm communicator_;
742  /// MPI_rank
743  //int mpi_rank_;
744 
745  /**
746  * flag indicating that collection of timer details will be
747  * using MPI
748  bool mpi_used;
749  */
750  // header informations
751 
752  /// Some measure of the size of the task in the set of the tasks that differs
753  /// only by size - used for scaling tests.
754  int task_size_;
755  /// Task description and identifier in possible database of all Profiler results.
756  string task_description_;
757  /// Time and date of the start of the task solution. In fact start of the Profiler.
758  time_t start_time;
759 
760  /// Name of the program.
761  string flow_name_;
762  /// Version of the program.
763  string flow_version_;
764  /// Http address of the branch in a repository.
765  string flow_branch_;
766  /// Revision or GIT hash.
767  string flow_revision_;
768  /// Build date and time.
769  string flow_build_;
770  /// Variable which stores last json log filepath
771  string json_filepath;
772 
773 
774  /**
775  * Use DFS to pass through the tree and collect information about all timers reduced from the processes in the communicator.
776  * For every timer the information strings are stored in the struct TimerInfo in order to pad fields correctly
777  * to have alligned columns on the output. The alligning is performed in the output() method.
778  */
779  template<typename ReduceFunctor>
780  void add_timer_info(ReduceFunctor reduce, property_tree::ptree* node, int timer_idx, double parent_time);
781 
782  //Profiler(MPI_Comm comm); // private constructor
783  Profiler(); // private constructor
784  Profiler(Profiler const&); // copy constructor is private
785  Profiler & operator=(Profiler const&); // assignment operator is private
786 };
787 
788 
789 
790 
791 
792 
793 /**
794  *
795  * @brief Class for automatic timer closing. This class is used by #START_TIMER macro
796  * and is responsible for the fact that we don't have to call #END_TIMER macro to stop the timer,
797  * the timer will be stopped at the end of the block in which #START_TIMER was used.
798  *
799  * The main idea of the approach described is that the TimerFrame variable will be destroyed
800  * at the end of the block where #START_TIMER macro was used. In order to work properly
801  * in situations where #END_TIMER was used to stop the timer manually before (but there is still the
802  * variable which will be later destroyed), we have to store references to these variables and
803  * destroy them on-demand.
804  *
805  * TODO:
806  * Should only contain pointer to the Timer. And destructor, that close the timer.
807  */
808 class TimerFrame {
809 private:
810  int const timer_index_;
811 public:
812  inline TimerFrame(const CodePoint &cp)
813  : timer_index_( Profiler::instance()->start_timer(cp) )
814  {}
815 
816  ~TimerFrame() {
817  Profiler::instance()->stop_timer(timer_index_);
818  }
819 };
820 
821 
822 /**
823  * Simple class providing static map variable storing address and alloc size
824  */
825 // gcc version 4.9 and lower has following bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59751
826 // fix in version 4.9: https://gcc.gnu.org/gcc-4.9/changes.html#cxx
827 // typedef unordered_map<long, int, hash<long>, equal_to<long>, internal::SimpleAllocator<pair<const long, int>>> unordered_map_with_alloc;
828 typedef boost::unordered_map<long, int, boost::hash<long>, equal_to<long>, internal::SimpleAllocator<std::pair<const long, int>>> unordered_map_with_alloc;
829 class MemoryAlloc {
830 public:
831  /**
832  * Create static map containing <allocation address, allocation size> pairs
833  * map is used for storing allocations and deallocations of all object not
834  * related to profiler after profiler initialization phase
835  */
836  static unordered_map_with_alloc & malloc_map();
837 };
838 
839 
840 
841 
842 #else // FLOW123D_DEBUG_PROFILER
843 
844 
845 // dummy declaration of Profiler class
846 class Profiler {
847 public:
848  static void initialize();
849  static Profiler* instance();
850 
851  void set_task_info(string description, int size)
852  {}
853  void set_program_info(string program_name, string program_version, string branch, string revision, string build)
854  {}
855  void notify_malloc(const size_t size )
856  {}
857  void notify_free(const size_t size )
858  {}
859  void output(MPI_Comm comm, ostream &os)
860  {}
861  void output(MPI_Comm comm)
862  {}
863  void transform_profiler_data(const string &output_file_suffix, const string &formatter)
864  {}
865  double get_resolution () const
866  { return 0.0; }
867  const char *actual_tag() const
868  { return NULL; }
869  inline unsigned int actual_count() const
870  { return 0; }
871  inline double actual_cumulative_time() const
872  { return 0.0; }
873  static void uninitialize();
874 private:
876  Profiler() {}
877 };
878 
879 
880 
881 
882 #endif
883 
884 
885 #endif
#define CONSTEXPR_
Definition: sys_profiler.hh:88
int MPI_Comm
Definition: mpi.h:141
double get_resolution() const
unsigned int actual_count() const
double actual_cumulative_time() const
void notify_free(const size_t size)
void output(MPI_Comm comm)
Global macros to enhance readability and debugging, general constants.
void set_program_info(string program_name, string program_version, string branch, string revision, string build)
STREAM & operator<<(STREAM &s, UpdateFlags u)
void transform_profiler_data(const string &output_file_suffix, const string &formatter)
static Profiler * instance()
static Profiler * _instance
Definition: memory.cc:33
void output(MPI_Comm comm, ostream &os)
void set_task_info(string description, int size)
void notify_malloc(const size_t size)
const char * actual_tag() const