spot  2.11.6
timer.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012, 2013, 2014, 2015, 2016, 2022 Laboratoire de
3 // Recherche et Développement de l'Epita (LRDE).
4 // Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
5 // département Systèmes Répartis Coopératifs (SRC), Université Pierre
6 // et Marie Curie.
7 //
8 // This file is part of Spot, a model checking library.
9 //
10 // Spot is free software; you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Spot is distributed in the hope that it will be useful, but WITHOUT
16 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18 // License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 #pragma once
24 
25 #include <spot/misc/common.hh>
26 #include <spot/misc/_config.h>
27 #include <cassert>
28 #include <iosfwd>
29 #include <string>
30 #include <map>
31 #include <chrono>
32 #if __has_include(<sys/times.h>)
33 # include <sys/times.h>
34 #endif
35 #include <ctime>
36 #include <chrono>
37 
38 namespace spot
39 {
42 
43 
45  struct stopwatch
46  {
47  protected:
48  typedef std::chrono::steady_clock clock;
49  clock::time_point start_;
50  public:
52  void start()
53  {
54  start_ = clock::now();
55  }
56 
61  double stop()
62  {
63  auto t = clock::now();
64  typedef std::chrono::duration<double> seconds;
65  return std::chrono::duration_cast<seconds>(t - start_).count();
66  }
67  };
68 
70  struct time_info
71  {
72  time_info()
73  : utime(0), stime(0), cutime(0), cstime(0)
74  {
75  }
76  clock_t utime;
77  clock_t stime;
78  clock_t cutime;
79  clock_t cstime;
80  };
81 
85  class timer
86  {
87  public:
88  timer()
89  : running(false)
90  {
91  }
92 
94  void
96  {
97  SPOT_ASSERT(!running);
98  running = true;
99  wall_start_ = std::chrono::steady_clock::now();
100 #ifdef SPOT_HAVE_TIMES
101  struct tms tmp;
102  times(&tmp);
103  start_.utime = tmp.tms_utime;
104  start_.cutime = tmp.tms_cutime;
105  start_.stime = tmp.tms_stime;
106  start_.cstime = tmp.tms_cstime;
107 #else
108  start_.utime = clock();
109 #endif
110  }
111 
113  void
115  {
116  auto end = std::chrono::steady_clock::now();
117  wall_cumul_ += std::chrono::duration_cast
118  <std::chrono::milliseconds>(end - wall_start_).count();
119 #ifdef SPOT_HAVE_TIMES
120  struct tms tmp;
121  times(&tmp);
122  total_.utime += tmp.tms_utime - start_.utime;
123  total_.cutime += tmp.tms_cutime - start_.cutime;
124  total_.stime += tmp.tms_stime - start_.stime;
125  total_.cstime += tmp.tms_cstime - start_.cstime;
126 #else
127  total_.utime += clock() - start_.utime;
128 #endif
129  SPOT_ASSERT(running);
130  running = false;
131  }
132 
138  clock_t
139  utime() const
140  {
141  return total_.utime;
142  }
143 
148  clock_t
149  cutime() const
150  {
151  return total_.cutime;
152  }
153 
159  clock_t
160  stime() const
161  {
162  return total_.stime;
163  }
164 
169  clock_t
170  cstime() const
171  {
172  return total_.cstime;
173  }
174 
175  clock_t get_uscp(bool user, bool system, bool children, bool parent) const
176  {
177  clock_t res = 0;
178 
179  if (user && parent)
180  res += utime();
181 
182  if (user && children)
183  res += cutime();
184 
185  if (system && parent)
186  res += stime();
187 
188  if (system && children)
189  res += cstime();
190 
191  return res;
192  }
193 
195  bool
196  is_running() const
197  {
198  return running;
199  }
200 
206  std::chrono::milliseconds::rep
207  walltime() const
208  {
209  return wall_cumul_;
210  }
211 
212  protected:
213  time_info start_;
214  time_info total_;
215  bool running;
216  std::chrono::steady_clock::time_point wall_start_;
217  std::chrono::milliseconds::rep wall_cumul_ = 0;
218  };
219 
220  // This function declared here must be implemented in each file
221  // that includes this header, well, only if this operator is needed!
222  inline std::ostream& operator<<(std::ostream& os, const timer& dt);
223 
228  class timer_map
229  {
230  public:
231 
237  void
238  start(const std::string& name)
239  {
240  item_type& it = tm[name];
241  it.first.start();
242  ++it.second;
243  }
244 
248  void
249  stop(const std::string& name)
250  {
251  tm[name].first.stop();
252  }
253 
261  void
262  cancel(const std::string& name)
263  {
264  tm_type::iterator i = tm.find(name);
265  if (SPOT_UNLIKELY(i == tm.end()))
266  throw std::invalid_argument("timer_map::cancel(): unknown name");
267  SPOT_ASSERT(0 < i->second.second);
268  if (0 == --i->second.second)
269  tm.erase(i);
270  }
271 
273  const spot::timer&
274  timer(const std::string& name) const
275  {
276  tm_type::const_iterator i = tm.find(name);
277  if (SPOT_UNLIKELY(i == tm.end()))
278  throw std::invalid_argument("timer_map::timer(): unknown name");
279  return i->second.first;
280  }
281 
287  bool
288  empty() const
289  {
290  return tm.empty();
291  }
292 
294  SPOT_API std::ostream&
295  print(std::ostream& os) const;
296 
298  void
300  {
301  tm.clear();
302  }
303 
304  protected:
305  typedef std::pair<spot::timer, int> item_type;
306  typedef std::map<std::string, item_type> tm_type;
307  tm_type tm;
308  };
309 
311  typedef struct process_timer
312  {
313  void start()
314  {
315  walltimer.start();
316  cputimer.start();
317  }
318  // sw.stop() --> It always returns the duration since the last call to
319  // start(). Therefore, it wont't stop timing, moreover, it can be called
320  // multiple times.
321  void stop()
322  {
323  walltime_lap_ = walltimer.stop();
324  cputimer.stop();
325  }
326 
327  double walltime() const
328  {
329  return walltime_lap_;
330  }
331 
332  clock_t cputime(bool user, bool system, bool children, bool parent) const
333  {
334  return cputimer.get_uscp(user, system, children, parent);
335  }
336 
337  private:
338  spot::timer cputimer;
339  spot::stopwatch walltimer;
340  double walltime_lap_ = 0;
342 
344 }
A map of timer, where each timer has a name.
Definition: timer.hh:229
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:288
void stop(const std::string &name)
Stop timer name.
Definition: timer.hh:249
void start(const std::string &name)
Start a timer with name name.
Definition: timer.hh:238
void cancel(const std::string &name)
Cancel timer name.
Definition: timer.hh:262
void reset_all()
Remove information about all timers.
Definition: timer.hh:299
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition: timer.hh:274
Definition: timer.hh:86
clock_t stime() const
Return the system time of the current process (whithout children) of all accumulated interval.
Definition: timer.hh:160
std::chrono::milliseconds::rep walltime() const
Return cumulative wall time.
Definition: timer.hh:207
clock_t cutime() const
Return the user time of children of all accumulated interval.
Definition: timer.hh:149
void stop()
Stop a time interval and update the sum of all intervals.
Definition: timer.hh:114
bool is_running() const
Whether the timer is running.
Definition: timer.hh:196
clock_t cstime() const
Return the system time of children of all accumulated interval.
Definition: timer.hh:170
void start()
Start a time interval.
Definition: timer.hh:95
clock_t utime() const
Return the user time of the current process (without children) of all accumulated interval.
Definition: timer.hh:139
Definition: automata.hh:27
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:312
A simple stopwatch.
Definition: timer.hh:46
void start()
Marks the start if the measurement.
Definition: timer.hh:52
double stop()
Returns the elapsed duration in seconds.
Definition: timer.hh:61
A structure to record elapsed time in clock ticks.
Definition: timer.hh:71

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