Free Electron
Profiler.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2015 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __core_Profiler_h__
8 #define __core_Profiler_h__
9 
10 #define FE_PROFILER_ON (FE_CODEGEN<=FE_PROFILE)
11 #define FE_PROFILER_DEBUG FALSE
12 
13 namespace fe
14 {
15 
16 /**************************************************************************//**
17  @brief Group of tick-based precision profilers
18 
19  @ingroup core
20 
21 *//***************************************************************************/
22 class FE_DL_EXPORT Profiler: public Handled<Profiler>
23 {
24  public:
25  Profiler(String name): m_name(name)
26  {
27 #if FE_PROFILER_ON
29 #if FE_PROFILER_DEBUG
30  feLog("Profiler calibrate %.6G us/tick\n",
32 #endif
33 #endif
34  }
35 virtual ~Profiler(void) {}
36 
37  /// @brief Tick-based precision time meter
38  class FE_DL_PUBLIC Profile: public Handled<Profile>
39  {
40  public:
41  Profile(sp<Profiler> spProfiler,String name):
42 #if FE_PROFILER_ON
43  m_spProfiler(spProfiler),
44 #endif
45  m_name(name),
46  m_count(0),
47  m_us(0.0)
48  {
49 #if FE_PROFILER_ON
50  hp<Profile> hpProfile(this);
51  spProfiler->registerProfile(hpProfile);
52 #endif
53  }
54 
55  const String& name(void) const { return m_name; }
56  F64 microseconds(void) const { return m_us; }
57  U32 count(void) const { return m_count; }
58 
59  void start(void)
60  {
61 #if FE_PROFILER_ON
62  m_startTick=systemTick();
63  m_count++;
64 #if FE_PROFILER_DEBUG
65  feLog("Profile %s start %u\n",m_name.c_str(),
66  m_startTick);
67 #endif
68 #endif
69  }
70  void finish(void)
71  {
72 #if FE_PROFILER_ON
73  unsigned long tick=systemTick();
76  m_startTick,tick);
77 
78 #if FE_PROFILER_DEBUG
79  unsigned long diff=tick-m_startTick;
81  feLog("Profile %s finish %u diff %u\n",
82  m_name.c_str(),
83  tick,tick-m_startTick,us);
84 #endif
85 #endif
86  }
87  void replace(sp<Profile>& rspProfile)
88  {
89 #if FE_PROFILER_ON
90  m_startTick=systemTick();
91  m_count++;
92 
93  rspProfile->m_us+=
96  rspProfile->m_startTick,m_startTick);
97 
98 #if FE_PROFILER_DEBUG
99  unsigned long diff=
100  m_startTick-rspProfile->m_startTick;
101  F64 us=diff*SystemTicker::microsecondsPerTick();
102  feLog("Profile %s replace %s %u diff %u"
103  " (%.6G us)\n",
104  m_name.c_str(),
105  rspProfile->name().c_str(),
106  m_startTick,
107  m_startTick-rspProfile->m_startTick,us);
108 #endif
109 #endif
110  }
111  String report(void) const;
112 
113  private:
114 #if FE_PROFILER_ON
115  sp<Profiler> m_spProfiler;
116 #endif
117  String m_name;
118  U32 m_count;
119  F64 m_us;
120  unsigned long m_startTick;
121  };
122 
123  /// @brief Guard-style scope control for a Profiler
124  class FE_DL_PUBLIC Stage
125  {
126  public:
127  Stage(sp<Profile>& rspProfile)
128 #if FE_PROFILER_ON
129  :m_spProfile(rspProfile)
130 #endif
131  {
132 #if FE_PROFILER_ON
133  m_spProfile->start();
134 #endif
135  }
136  ~Stage(void)
137  {
138 #if FE_PROFILER_ON
139  m_spProfile->finish();
140 #endif
141  }
142 
143  private:
144 #if FE_PROFILER_ON
145  sp<Profile> m_spProfile;
146 #endif
147  };
148 
149 const String& name(void) const { return m_name; }
150 
151 #if FE_PROFILER_ON
152  void registerProfile(hp<Profile> hpProfile)
153  { m_profileList.append(hpProfile); }
154 #endif
155 
156  void begin(void)
157  {
158 #if FE_PROFILER_DEBUG
159  feLog("Profiler %s begin\n",m_name.c_str());
160 #endif
161  }
162  void end(void)
163  {
164 #if FE_PROFILER_DEBUG
165  feLog("Profiler %s end\n",m_name.c_str());
166 #endif
167  }
168 
169  /// @brief Generate a report of current profiles
170  String report(void);
171 
172  private:
173 
174 #if FE_PROFILER_ON
175  List< hp<Profile> > m_profileList;
176 #endif
177 
178  String m_name;
179 };
180 
181 } /* namespace */
182 
183 #endif /* __core_Profiler_h__ */
184 
Fully Bidirectional Doubly-Linked List.
Definition: List.h:493
static void FE_CDECL calibrate(void)
Perform a self-contained recalibration.
Definition: SystemTicker.h:179
kernel
Definition: namespace.dox:3
unsigned long systemTick(void)
Sleep the current thread for the given number of seconds and nanoseconds.
Definition: SystemTicker.h:102
Guard-style scope control for a Profiler.
Definition: Profiler.h:124
Group of tick-based precision profilers.
Definition: Profiler.h:22
static float microsecondsPerTick()
Returns the fractional number of microseonds for each processor clock tick.
Definition: SystemTicker.h:202
Tick-based precision time meter.
Definition: Profiler.h:38
Automatically reference-counted string container.
Definition: String.h:121
static unsigned long tickDifference(unsigned long start, unsigned long finish)
Return the reltive tick change between two absolute tick values.
Definition: SystemTicker.h:190
Intrusive Smart Pointer.
Definition: ptr.h:53
Base class providing an fe::Handle to the derived class.
Definition: Handled.h:209