spot  2.12.1
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 
34 namespace spot
35 {
38 
39 
41  struct stopwatch
42  {
43  protected:
44  typedef std::chrono::steady_clock clock;
45  clock::time_point start_;
46  public:
48  void start()
49  {
50  start_ = clock::now();
51  }
52 
57  double stop()
58  {
59  auto t = clock::now();
60  typedef std::chrono::duration<double> seconds;
61  return std::chrono::duration_cast<seconds>(t - start_).count();
62  }
63  };
64 
66  struct time_info
67  {
68  time_info()
69  : utime(0), stime(0), cutime(0), cstime(0)
70  {
71  }
72  clock_t utime;
73  clock_t stime;
74  clock_t cutime;
75  clock_t cstime;
76  };
77 
81  class timer
82  {
83  public:
84  timer()
85  : running(false)
86  {
87  }
88 
90  void
92  {
93  SPOT_ASSERT(!running);
94  running = true;
95  wall_start_ = std::chrono::steady_clock::now();
96 #ifdef SPOT_HAVE_TIMES
97  struct tms tmp;
98  times(&tmp);
99  start_.utime = tmp.tms_utime;
100  start_.cutime = tmp.tms_cutime;
101  start_.stime = tmp.tms_stime;
102  start_.cstime = tmp.tms_cstime;
103 #else
104  start_.utime = clock();
105 #endif
106  }
107 
109  void
111  {
112  auto end = std::chrono::steady_clock::now();
113  wall_cumul_ += std::chrono::duration_cast
114  <std::chrono::milliseconds>(end - wall_start_).count();
115 #ifdef SPOT_HAVE_TIMES
116  struct tms tmp;
117  times(&tmp);
118  total_.utime += tmp.tms_utime - start_.utime;
119  total_.cutime += tmp.tms_cutime - start_.cutime;
120  total_.stime += tmp.tms_stime - start_.stime;
121  total_.cstime += tmp.tms_cstime - start_.cstime;
122 #else
123  total_.utime += clock() - start_.utime;
124 #endif
125  SPOT_ASSERT(running);
126  running = false;
127  }
128 
134  clock_t
135  utime() const
136  {
137  return total_.utime;
138  }
139 
144  clock_t
145  cutime() const
146  {
147  return total_.cutime;
148  }
149 
155  clock_t
156  stime() const
157  {
158  return total_.stime;
159  }
160 
165  clock_t
166  cstime() const
167  {
168  return total_.cstime;
169  }
170 
171  clock_t get_uscp(bool user, bool system, bool children, bool parent) const
172  {
173  clock_t res = 0;
174 
175  if (user && parent)
176  res += utime();
177 
178  if (user && children)
179  res += cutime();
180 
181  if (system && parent)
182  res += stime();
183 
184  if (system && children)
185  res += cstime();
186 
187  return res;
188  }
189 
191  bool
192  is_running() const
193  {
194  return running;
195  }
196 
202  std::chrono::milliseconds::rep
203  walltime() const
204  {
205  return wall_cumul_;
206  }
207 
208  protected:
209  time_info start_;
210  time_info total_;
211  bool running;
212  std::chrono::steady_clock::time_point wall_start_;
213  std::chrono::milliseconds::rep wall_cumul_ = 0;
214  };
215 
216  // This function declared here must be implemented in each file
217  // that includes this header, well, only if this operator is needed!
218  inline std::ostream& operator<<(std::ostream& os, const timer& dt);
219 
224  class timer_map
225  {
226  public:
227 
233  void
234  start(const std::string& name)
235  {
236  item_type& it = tm[name];
237  it.first.start();
238  ++it.second;
239  }
240 
244  void
245  stop(const std::string& name)
246  {
247  tm[name].first.stop();
248  }
249 
257  void
258  cancel(const std::string& name)
259  {
260  tm_type::iterator i = tm.find(name);
261  if (SPOT_UNLIKELY(i == tm.end()))
262  throw std::invalid_argument("timer_map::cancel(): unknown name");
263  SPOT_ASSERT(0 < i->second.second);
264  if (0 == --i->second.second)
265  tm.erase(i);
266  }
267 
269  const spot::timer&
270  timer(const std::string& name) const
271  {
272  tm_type::const_iterator i = tm.find(name);
273  if (SPOT_UNLIKELY(i == tm.end()))
274  throw std::invalid_argument("timer_map::timer(): unknown name");
275  return i->second.first;
276  }
277 
283  bool
284  empty() const
285  {
286  return tm.empty();
287  }
288 
290  SPOT_API std::ostream&
291  print(std::ostream& os) const;
292 
294  void
296  {
297  tm.clear();
298  }
299 
300  protected:
301  typedef std::pair<spot::timer, int> item_type;
302  typedef std::map<std::string, item_type> tm_type;
303  tm_type tm;
304  };
305 
307  typedef struct process_timer
308  {
309  void start()
310  {
311  walltimer.start();
312  cputimer.start();
313  }
314  // sw.stop() --> It always returns the duration since the last call to
315  // start(). Therefore, it wont't stop timing, moreover, it can be called
316  // multiple times.
317  void stop()
318  {
319  walltime_lap_ = walltimer.stop();
320  cputimer.stop();
321  }
322 
323  double walltime() const
324  {
325  return walltime_lap_;
326  }
327 
328  clock_t cputime(bool user, bool system, bool children, bool parent) const
329  {
330  return cputimer.get_uscp(user, system, children, parent);
331  }
332 
333  private:
334  spot::timer cputimer;
335  spot::stopwatch walltimer;
336  double walltime_lap_ = 0;
338 
340 }
A map of timer, where each timer has a name.
Definition: timer.hh:225
std::ostream & print(std::ostream &os) const
Format information about all timers in a table.
bool empty() const
Whether there is no timer in the map.
Definition: timer.hh:284
void stop(const std::string &name)
Stop timer name.
Definition: timer.hh:245
void start(const std::string &name)
Start a timer with name name.
Definition: timer.hh:234
void cancel(const std::string &name)
Cancel timer name.
Definition: timer.hh:258
void reset_all()
Remove information about all timers.
Definition: timer.hh:295
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition: timer.hh:270
Definition: timer.hh:82
clock_t stime() const
Return the system time of the current process (without children) of all accumulated interval.
Definition: timer.hh:156
std::chrono::milliseconds::rep walltime() const
Return cumulative wall time.
Definition: timer.hh:203
clock_t cutime() const
Return the user time of children of all accumulated interval.
Definition: timer.hh:145
void stop()
Stop a time interval and update the sum of all intervals.
Definition: timer.hh:110
bool is_running() const
Whether the timer is running.
Definition: timer.hh:192
clock_t cstime() const
Return the system time of children of all accumulated interval.
Definition: timer.hh:166
void start()
Start a time interval.
Definition: timer.hh:91
clock_t utime() const
Return the user time of the current process (without children) of all accumulated interval.
Definition: timer.hh:135
Definition: automata.hh:26
struct spot::process_timer process_timer
Struct used to start and stop both timer and stopwatch clocks.
std::ostream & operator<<(std::ostream &os, const formula &f)
Print a formula.
Struct used to start and stop both timer and stopwatch clocks.
Definition: timer.hh:308
A simple stopwatch.
Definition: timer.hh:42
void start()
Marks the start if the measurement.
Definition: timer.hh:48
double stop()
Returns the elapsed duration in seconds.
Definition: timer.hh:57
A structure to record elapsed time in clock ticks.
Definition: timer.hh:67

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.1