DABC (Data Acquisition Backbone Core)  2.9.9
Profiler.h
Go to the documentation of this file.
1 // $Id: Profiler.h 4472 2020-04-15 13:33:18Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Profiler
17 #define DABC_Profiler
18 
19 #ifndef DABC_timing
20 #include "dabc/timing.h"
21 #endif
22 
23 #include <vector>
24 
25 namespace dabc {
26 
27  class ProfilerGuard;
28 
29  class Profiler {
30 
31  friend class ProfilerGuard;
32 
33  typedef unsigned long long clock_t;
34 
35  bool fActive{true};
36 
38 
40 
41  struct Entry {
42  clock_t fSum{0}; // sum of used time
43  double fRatio{0.}; // rel time for this slot
44  std::string fName;
45  };
46 
47  std::vector<Entry> fEntries;
48 
49  public:
50 
51  Profiler() { Reserve(10); }
52 
53  void Reserve(unsigned num = 10)
54  {
55  while (fEntries.size() < num)
56  fEntries.emplace_back();
57  }
58 
59  void SetActive(bool on = true) { fActive = on; }
60 
61  void MakeStatistic();
62 
63  std::string Format();
64 
65  };
66 
67  class ProfilerGuard {
69  unsigned fCnt{0};
71 
72  public:
73  ProfilerGuard(Profiler &prof, const char *name = nullptr, unsigned lvl = 0) : fProfiler(prof), fCnt(lvl)
74  {
75  if (!fProfiler.fActive || (fCnt >= fProfiler.fEntries.size()))
76  return;
77 
79 
80  if (name && fProfiler.fEntries[fCnt].fName.empty())
81  fProfiler.fEntries[fCnt].fName = name;
82  }
83 
85  {
86  Next();
87  }
88 
89  void Next(const char *name = nullptr, unsigned lvl = 0)
90  {
91  if (!fProfiler.fActive || (fCnt >= fProfiler.fEntries.size()))
92  return;
93 
94  auto now = fProfiler.GetClock();
95 
96  fProfiler.fEntries[fCnt].fSum += (now - fLast);
97 
98  fLast = now;
99 
100  if (lvl) fCnt = lvl; else fCnt++;
101 
102  if (name && (fCnt < fProfiler.fEntries.size()) && fProfiler.fEntries[fCnt].fName.empty())
103  fProfiler.fEntries[fCnt].fName = name;
104  }
105 
106  };
107 
108 } // namespace dabc
109 
110 #endif
void Next(const char *name=nullptr, unsigned lvl=0)
Definition: Profiler.h:89
ProfilerGuard(Profiler &prof, const char *name=nullptr, unsigned lvl=0)
Definition: Profiler.h:73
Profiler::clock_t fLast
Definition: Profiler.h:70
Profiler & fProfiler
Definition: Profiler.h:68
clock_t GetClock()
Definition: Profiler.h:37
clock_t fLast
Definition: Profiler.h:39
unsigned long long clock_t
Definition: Profiler.h:33
void Reserve(unsigned num=10)
Definition: Profiler.h:53
void SetActive(bool on=true)
Definition: Profiler.h:59
std::vector< Entry > fEntries
Definition: Profiler.h:47
bool fActive
Definition: Profiler.h:35
std::string Format()
Definition: Profiler.cxx:36
void MakeStatistic()
Definition: Profiler.cxx:18
Event manipulation API.
Definition: api.h:23
std::string fName
Definition: Profiler.h:44
static fastclock_t GetFastClock()
Definition: timing.h:81