spot 2.16
Loading...
Searching...
No Matches
timer.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) by the Spot authors, see the AUTHORS file for details.
3//
4// This file is part of Spot, a model checking library.
5//
6// Spot is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 3 of the License, or
9// (at your option) any later version.
10//
11// Spot is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14// License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19#pragma once
20
21#include <spot/misc/common.hh>
22#include <spot/misc/_config.h>
23#include <cassert>
24#include <iosfwd>
25#include <string>
26#include <map>
27#include <chrono>
28#if __has_include(<sys/times.h>)
29# include <sys/times.h>
30#endif
31#include <ctime>
32#include <chrono>
33
34namespace spot
35{
38
39
41 struct stopwatch
42 {
43 protected:
45 typedef std::chrono::steady_clock clock;
46 clock::time_point start_;
47 public:
49 void start()
50 {
51 start_ = clock::now();
52 }
53
58 double stop()
59 {
60 auto t = clock::now();
61 typedef std::chrono::duration<double> seconds;
62 return std::chrono::duration_cast<seconds>(t - start_).count();
63 }
64 };
65
67 struct time_info
68 {
69 time_info()
70 : utime(0), stime(0), cutime(0), cstime(0)
71 {
72 }
73 clock_t utime;
74 clock_t stime;
75 clock_t cutime;
76 clock_t cstime;
77 };
78
82 class timer
83 {
84 public:
85 timer()
86 : running(false)
87 {
88 }
89
91 void
93 {
94 SPOT_ASSERT(!running);
95 running = true;
96 wall_start_ = std::chrono::steady_clock::now();
97#ifdef SPOT_HAVE_TIMES
98 struct tms tmp;
99 times(&tmp);
100 start_.utime = tmp.tms_utime;
101 start_.cutime = tmp.tms_cutime;
102 start_.stime = tmp.tms_stime;
103 start_.cstime = tmp.tms_cstime;
104#else
105 start_.utime = clock();
106#endif
107 }
108
110 void
112 {
113 auto end = std::chrono::steady_clock::now();
114 wall_cumul_ += std::chrono::duration_cast
115 <std::chrono::milliseconds>(end - wall_start_).count();
116#ifdef SPOT_HAVE_TIMES
117 struct tms tmp;
118 times(&tmp);
119 total_.utime += tmp.tms_utime - start_.utime;
120 total_.cutime += tmp.tms_cutime - start_.cutime;
121 total_.stime += tmp.tms_stime - start_.stime;
122 total_.cstime += tmp.tms_cstime - start_.cstime;
123#else
124 total_.utime += clock() - start_.utime;
125#endif
126 SPOT_ASSERT(running);
127 running = false;
128 }
129
135 clock_t
136 utime() const
137 {
138 return total_.utime;
139 }
140
145 clock_t
146 cutime() const
147 {
148 return total_.cutime;
149 }
150
156 clock_t
157 stime() const
158 {
159 return total_.stime;
160 }
161
166 clock_t
167 cstime() const
168 {
169 return total_.cstime;
170 }
171
176 clock_t get_uscp(bool user, bool system, bool children, bool parent) const
177 {
178 clock_t res = 0;
179
180 if (user && parent)
181 res += utime();
182
183 if (user && children)
184 res += cutime();
185
186 if (system && parent)
187 res += stime();
188
189 if (system && children)
190 res += cstime();
191
192 return res;
193 }
194
196 bool
198 {
199 return running;
200 }
201
207 std::chrono::milliseconds::rep
208 walltime() const
209 {
210 return wall_cumul_;
211 }
212
213 protected:
216 bool running;
218 std::chrono::steady_clock::time_point wall_start_;
219 std::chrono::milliseconds::rep wall_cumul_ = 0;
220 };
221
222 // This function declared here must be implemented in each file
223 // that includes this header, well, only if this operator is needed!
224 inline std::ostream& operator<<(std::ostream& os, const timer& dt);
225
231 {
232 public:
233
239 void
240 start(const std::string& name)
241 {
242 item_type& it = tm[name];
243 it.first.start();
244 ++it.second;
245 }
246
250 void
251 stop(const std::string& name)
252 {
253 tm[name].first.stop();
254 }
255
263 void
264 cancel(const std::string& name)
265 {
266 tm_type::iterator i = tm.find(name);
267 if (SPOT_UNLIKELY(i == tm.end()))
268 throw std::invalid_argument("timer_map::cancel(): unknown name");
269 SPOT_ASSERT(0 < i->second.second);
270 if (0 == --i->second.second)
271 tm.erase(i);
272 }
273
275 const spot::timer&
276 timer(const std::string& name) const
277 {
278 tm_type::const_iterator i = tm.find(name);
279 if (SPOT_UNLIKELY(i == tm.end()))
280 throw std::invalid_argument("timer_map::timer(): unknown name");
281 return i->second.first;
282 }
283
289 bool
290 empty() const
291 {
292 return tm.empty();
293 }
294
296 SPOT_API std::ostream&
297 print(std::ostream& os) const;
298
300 void
302 {
303 tm.clear();
304 }
305
306 protected:
307 typedef std::pair<spot::timer, int> item_type;
308 typedef std::map<std::string, item_type> tm_type;
310 };
311
313 typedef struct process_timer
314 {
316 void start()
317 {
318 walltimer.start();
319 cputimer.start();
320 }
321 // sw.stop() --> It always returns the duration since the last call to
322 // start(). Therefore, it won't stop timing, moreover, it can be called
323 // multiple times.
325 void stop()
326 {
327 walltime_lap_ = walltimer.stop();
328 cputimer.stop();
329 }
330
332 double walltime() const
333 {
334 return walltime_lap_;
335 }
336
338 clock_t cputime(bool user, bool system, bool children, bool parent) const
339 {
340 return cputimer.get_uscp(user, system, children, parent);
341 }
342
343 private:
344 spot::timer cputimer;
345 spot::stopwatch walltimer;
346 double walltime_lap_ = 0;
347 } process_timer;
348
350}
A map of timer, where each timer has a name.
Definition timer.hh:231
std::ostream & print(std::ostream &os) const
Format information about all timers in a table.
std::pair< spot::timer, int > item_type
Timer + count pair.
Definition timer.hh:307
bool empty() const
Whether there is no timer in the map.
Definition timer.hh:290
void stop(const std::string &name)
Stop timer name.
Definition timer.hh:251
void start(const std::string &name)
Start a timer with name name.
Definition timer.hh:240
tm_type tm
The map of named timers.
Definition timer.hh:309
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition timer.hh:276
void cancel(const std::string &name)
Cancel timer name.
Definition timer.hh:264
void reset_all()
Remove information about all timers.
Definition timer.hh:301
std::map< std::string, item_type > tm_type
Timer map type.
Definition timer.hh:308
Definition timer.hh:83
clock_t stime() const
Return the system time of the current process (without children) of all accumulated interval.
Definition timer.hh:157
std::chrono::milliseconds::rep walltime() const
Return cumulative wall time.
Definition timer.hh:208
clock_t cutime() const
Return the user time of children of all accumulated interval.
Definition timer.hh:146
void stop()
Stop a time interval and update the sum of all intervals.
Definition timer.hh:111
bool is_running() const
Whether the timer is running.
Definition timer.hh:197
time_info total_
Accumulated time across all intervals.
Definition timer.hh:215
clock_t cstime() const
Return the system time of children of all accumulated interval.
Definition timer.hh:167
void start()
Start a time interval.
Definition timer.hh:92
time_info start_
Start time of the current interval.
Definition timer.hh:214
clock_t get_uscp(bool user, bool system, bool children, bool parent) const
Definition timer.hh:176
std::chrono::steady_clock::time_point wall_start_
Wall-clock start time of the current interval.
Definition timer.hh:218
clock_t utime() const
Return the user time of the current process (without children) of all accumulated interval.
Definition timer.hh:136
bool running
Definition timer.hh:216
std::chrono::milliseconds::rep wall_cumul_
Wall time sum.
Definition timer.hh:219
Definition automata.hh:26
std::ostream & operator<<(std::ostream &os, const mc_algorithm &ma)
Print an mc_algorithm value to a stream.
Definition mc.hh:74
Struct used to start and stop both timer and stopwatch clocks.
Definition timer.hh:314
void stop()
Stop both timers and record elapsed times.
Definition timer.hh:325
double walltime() const
Return elapsed wall time in seconds since the last start().
Definition timer.hh:332
void start()
Start both wall-clock and CPU timers.
Definition timer.hh:316
clock_t cputime(bool user, bool system, bool children, bool parent) const
Return accumulated CPU ticks for selected time components.
Definition timer.hh:338
A simple stopwatch.
Definition timer.hh:42
void start()
Marks the start of the measurement.
Definition timer.hh:49
std::chrono::steady_clock clock
Clock type used by the stopwatch.
Definition timer.hh:45
clock::time_point start_
Recorded start time point.
Definition timer.hh:46
double stop()
Returns the elapsed duration in seconds.
Definition timer.hh:58
A structure to record elapsed time in clock ticks.
Definition timer.hh:68
clock_t cstime
System time of child processes.
Definition timer.hh:76
clock_t cutime
User time of child processes.
Definition timer.hh:75
clock_t stime
System time of the process.
Definition timer.hh:74
clock_t utime
User time of the process.
Definition timer.hh:73

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.8