KiCad PCB EDA Suite
Loading...
Searching...
No Matches
profile.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
30
31#ifndef TPROFILE_H
32#define TPROFILE_H
33
34#include <atomic>
35#include <chrono>
36#include <sstream>
37#include <string>
38#include <iostream>
39#include <iomanip>
40#include <cstdint>
41#include <vector>
42
50{
51public:
58 PROF_TIMER( const std::string& aName, bool aAutostart = true ) :
59 m_name( aName ), m_running( false )
60 {
61 if( aAutostart )
62 Start();
63 }
64
71 {
72 Start();
73 }
74
78 void Start()
79 {
80 m_started = true;
81 m_running = true;
82 m_starttime = CLOCK::now();
84 }
85
86
90 void Stop()
91 {
92 if( !m_running )
93 return;
94
95 m_stoptime = CLOCK::now();
96 m_running = false;
97 }
98
107 void Show( std::ostream& aStream = std::cerr )
108 {
109 using DURATION = std::chrono::duration<double, std::nano>;
110
111 const auto duration = SinceStart<DURATION>();
112 const double cnt = duration.count();
113
114 if( m_name.size() )
115 {
116 aStream << m_name << " took ";
117 }
118
119 if( cnt < 1e3 )
120 aStream << cnt << "ns";
121 else if( cnt < 1e6 )
122 aStream << ( cnt / 1e3 ) << "µs";
123 else if( cnt < 1e9 )
124 aStream << ( cnt / 1e6 ) << "ms";
125 else
126 aStream << ( cnt / 1e9 ) << "s";
127
128 aStream << std::endl;
129 }
130
136 template <typename DURATION>
137 DURATION SinceStart( bool aSinceLast = false )
138 {
139 const TIME_POINT stoptime = m_running ? CLOCK::now() : m_stoptime;
140 const TIME_POINT starttime = aSinceLast ? m_lasttime : m_starttime;
141
142 m_lasttime = stoptime;
143
144 return std::chrono::duration_cast<DURATION>( stoptime - starttime );
145 }
146
151 double msecs( bool aSinceLast = false )
152 {
153 using DUR_MS = std::chrono::duration<double, std::milli>;
154 return SinceStart<DUR_MS>( aSinceLast ).count();
155 }
156
157 std::string to_string()
158 {
159 using DURATION = std::chrono::duration<double, std::nano>;
160
161 const auto duration = SinceStart<DURATION>();
162 const double cnt = duration.count();
163 std::string retv;
164
165 if( !m_name.empty() )
166 retv = m_name + ": ";
167
168 if( !m_started )
169 {
170 retv += "none";
171 return retv;
172 }
173
174 std::stringstream time;
175
176 if( cnt < 1e3 )
177 time << cnt << "ns";
178 else if( cnt < 1e6 )
179 time << ( cnt / 1e3 ) << "µs";
180 else if( cnt < 1e9 )
181 time << ( cnt / 1e6 ) << "ms";
182 else
183 time << ( cnt / 1e9 ) << "s";
184
185 retv += time.str();
186
187 return retv;
188 }
189
190 const std::string& GetName() const { return m_name; }
191
192private:
193 std::string m_name; // a string printed in message
196
197 using CLOCK = std::chrono::high_resolution_clock;
198 using TIME_POINT = std::chrono::time_point<CLOCK>;
199
201};
202
203
222template <typename DURATION>
224{
225public:
226 SCOPED_PROF_TIMER( DURATION& aDuration ) : PROF_TIMER(), m_duration( aDuration )
227 {
228 }
229
231 {
232 // update the output
233 m_duration = m_counter.SinceStart<DURATION>();
234 }
235
236private:
239
241 DURATION& m_duration;
242};
243
244
253
254
259{
260public:
262 m_name( "Anonymous" ),
263 m_count( 0 )
264 {
265 }
266
267 PROF_COUNTER( const std::string& aName ) :
268 m_name( aName ),
269 m_count( 0 )
270 {
271 }
272
273 unsigned long long Count() const
274 {
275 return m_count.load();
276 }
277
278 void Reset()
279 {
280 m_count.store( 0 );
281 }
282
283 unsigned long long operator++( int )
284 {
285 return m_count++;
286 }
287
288 void Show( std::ostream& aStream = std::cerr )
289 {
290 if( m_name.size() )
291 aStream << m_name << ": ";
292
293 aStream << m_count.load();
294 aStream << std::endl;
295 }
296
297private:
298 std::string m_name;
299 std::atomic_ullong m_count;
300};
301
303{
304 public:
305 using CLOCK = std::chrono::high_resolution_clock;
306 using TIME_POINT = std::chrono::time_point<CLOCK>;
307
309 {
310 std::string name;
312 bool isTimer = false;
313 double timerDelta = 0;
314 };
315
316 LATENCY_PROBE( const std::string& aName, int aStages = 8 ) :
317 m_name( aName )
318 {
319 m_checkpoints.reserve( aStages );
320 }
321
322 void Reset()
323 {
324 m_start = CLOCK::now();
325 m_checkpoints.clear();
326 }
327
328 void Checkpoint( const std::string& aName )
329 {
330 CHECKPOINT chk;
331 chk.name = aName;
332 chk.timestamp = CLOCK::now();
333 m_checkpoints.push_back( chk );
334 }
335
336 void AddTimer( PROF_TIMER& aTimer )
337 {
338 CHECKPOINT chk;
339 chk.name = aTimer.GetName();
340 chk.isTimer = true;
341 chk.timerDelta = aTimer.msecs();
342 m_checkpoints.push_back( chk );
343 }
344
345 std::string to_string();
346
348 {
349 using DUR_MS = std::chrono::duration<double, std::milli>;
350 return static_cast<DUR_MS>( a - b ).count();
351 }
352
353 private:
354
356 std::string m_name;
357 std::vector<CHECKPOINT> m_checkpoints;
358};
359
360#endif // TPROFILE_H
std::string to_string()
Definition profile.cpp:79
void AddTimer(PROF_TIMER &aTimer)
Definition profile.h:336
TIME_POINT m_start
Definition profile.h:355
void Checkpoint(const std::string &aName)
Definition profile.h:328
std::chrono::high_resolution_clock CLOCK
Definition profile.h:305
std::vector< CHECKPOINT > m_checkpoints
Definition profile.h:357
double delta_ms(TIME_POINT a, TIME_POINT b)
Definition profile.h:347
void Reset()
Definition profile.h:322
std::chrono::time_point< CLOCK > TIME_POINT
Definition profile.h:306
std::string m_name
Definition profile.h:356
LATENCY_PROBE(const std::string &aName, int aStages=8)
Definition profile.h:316
unsigned long long operator++(int)
Definition profile.h:283
PROF_COUNTER(const std::string &aName)
Definition profile.h:267
unsigned long long Count() const
Definition profile.h:273
void Show(std::ostream &aStream=std::cerr)
Definition profile.h:288
std::atomic_ullong m_count
Definition profile.h:299
void Reset()
Definition profile.h:278
std::string m_name
Definition profile.h:298
A small class to help profiling.
Definition profile.h:50
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
Definition profile.h:107
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:90
std::chrono::high_resolution_clock CLOCK
Definition profile.h:197
const std::string & GetName() const
Definition profile.h:190
DURATION SinceStart(bool aSinceLast=false)
Definition profile.h:137
void Start()
Start or restart the counter.
Definition profile.h:78
TIME_POINT m_lasttime
Definition profile.h:200
std::string to_string()
Definition profile.h:157
bool m_started
Definition profile.h:195
TIME_POINT m_stoptime
Definition profile.h:200
bool m_running
Definition profile.h:194
PROF_TIMER()
Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition profile.h:70
std::chrono::time_point< CLOCK > TIME_POINT
Definition profile.h:198
TIME_POINT m_starttime
Definition profile.h:200
PROF_TIMER(const std::string &aName, bool aAutostart=true)
Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition profile.h:58
std::string m_name
Definition profile.h:193
double msecs(bool aSinceLast=false)
Definition profile.h:151
DURATION & m_duration
Definition profile.h:241
SCOPED_PROF_TIMER(DURATION &aDuration)
Definition profile.h:226
PROF_TIMER m_counter
< The counter to use to do the profiling
Definition profile.h:238
int64_t GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.