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 (C) 2019-2020, 2024 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
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
49{
50public:
57 PROF_TIMER( const std::string& aName, bool aAutostart = true ) :
58 m_name( aName ), m_running( false )
59 {
60 if( aAutostart )
61 Start();
62 }
63
70 {
71 Start();
72 }
73
77 void Start()
78 {
79 m_running = true;
80 m_starttime = CLOCK::now();
82 }
83
84
88 void Stop()
89 {
90 if( !m_running )
91 return;
92
93 m_stoptime = CLOCK::now();
94 m_running = false;
95 }
96
105 void Show( std::ostream& aStream = std::cerr )
106 {
107 using DURATION = std::chrono::duration<double, std::nano>;
108
109 const auto duration = SinceStart<DURATION>();
110 const double cnt = duration.count();
111
112 if( m_name.size() )
113 {
114 aStream << m_name << " took ";
115 }
116
117 if( cnt < 1e3 )
118 aStream << cnt << "ns";
119 else if( cnt < 1e6 )
120 aStream << cnt / 1e3 << "µs";
121 else if( cnt < 1e9 )
122 aStream << cnt / 1e6 << "ms";
123 else
124 aStream << cnt / 1e9 << "s";
125
126 aStream << std::endl;
127 }
128
134 template <typename DURATION>
135 DURATION SinceStart( bool aSinceLast = false )
136 {
137 const TIME_POINT stoptime = m_running ? CLOCK::now() : m_stoptime;
138 const TIME_POINT starttime = aSinceLast ? m_lasttime : m_starttime;
139
140 m_lasttime = stoptime;
141
142 return std::chrono::duration_cast<DURATION>( stoptime - starttime );
143 }
144
149 double msecs( bool aSinceLast = false )
150 {
151 using DUR_MS = std::chrono::duration<double, std::milli>;
152 return SinceStart<DUR_MS>( aSinceLast ).count();
153 }
154
155 std::string to_string()
156 {
157 std::string retv;
158
159 if( !m_name.empty() )
160 retv = m_name + ": ";
161
162 std::stringstream time;
163
164 Show( time );
165
166 retv += time.get();
167
168 return retv;
169 }
170
171private:
172 std::string m_name; // a string printed in message
174
175 using CLOCK = std::chrono::high_resolution_clock;
176 using TIME_POINT = std::chrono::time_point<CLOCK>;
177
179};
180
181
200template <typename DURATION>
202{
203public:
204 SCOPED_PROF_TIMER( DURATION& aDuration ) : PROF_TIMER(), m_duration( aDuration )
205 {
206 }
207
209 {
210 // update the output
211 m_duration = m_counter.SinceStart<DURATION>();
212 }
213
214private:
217
219 DURATION& m_duration;
220};
221
222
231
232
237{
238public:
240 m_name( "Anonymous" ),
241 m_count( 0 )
242 {
243 }
244
245 PROF_COUNTER( const std::string& aName ) :
246 m_name( aName ),
247 m_count( 0 )
248 {
249 }
250
251 unsigned long long Count() const
252 {
253 return m_count.load();
254 }
255
256 void Reset()
257 {
258 m_count.store( 0 );
259 }
260
261 unsigned long long operator++( int )
262 {
263 return m_count++;
264 }
265
266 void Show( std::ostream& aStream = std::cerr )
267 {
268 if( m_name.size() )
269 aStream << m_name << ": ";
270
271 aStream << m_count.load();
272 aStream << std::endl;
273 }
274
275private:
276 std::string m_name;
277 std::atomic_ullong m_count;
278};
279
280#endif // TPROFILE_H
A thread-safe event counter.
Definition: profile.h:237
unsigned long long operator++(int)
Definition: profile.h:261
PROF_COUNTER(const std::string &aName)
Definition: profile.h:245
unsigned long long Count() const
Definition: profile.h:251
void Show(std::ostream &aStream=std::cerr)
Definition: profile.h:266
std::atomic_ullong m_count
Definition: profile.h:277
void Reset()
Definition: profile.h:256
PROF_COUNTER()
Definition: profile.h:239
std::string m_name
Definition: profile.h:276
A small class to help profiling.
Definition: profile.h:49
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
Definition: profile.h:105
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition: profile.h:88
std::chrono::high_resolution_clock CLOCK
Definition: profile.h:175
DURATION SinceStart(bool aSinceLast=false)
Definition: profile.h:135
void Start()
Start or restart the counter.
Definition: profile.h:77
TIME_POINT m_lasttime
Definition: profile.h:178
std::string to_string()
Definition: profile.h:155
TIME_POINT m_stoptime
Definition: profile.h:178
bool m_running
Definition: profile.h:173
PROF_TIMER()
Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition: profile.h:69
std::chrono::time_point< CLOCK > TIME_POINT
Definition: profile.h:176
TIME_POINT m_starttime
Definition: profile.h:178
PROF_TIMER(const std::string &aName, bool aAutostart=true)
Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
Definition: profile.h:57
std::string m_name
Definition: profile.h:172
double msecs(bool aSinceLast=false)
Definition: profile.h:149
A simple RAII class to measure the time of an operation.
Definition: profile.h:202
DURATION & m_duration
Definition: profile.h:219
SCOPED_PROF_TIMER(DURATION &aDuration)
Definition: profile.h:204
PROF_TIMER m_counter
< The counter to use to do the profiling
Definition: profile.h:216
int64_t GetRunningMicroSecs()
An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER.