spot  2.5
timer.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012, 2013, 2014, 2015, 2016 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 SPOT_HAVE_SYS_TIMES_H
33 # include <sys/times.h>
34 #endif
35 #include <ctime>
36 
37 
38 namespace spot
39 {
42 
43 
45  struct stopwatch
46  {
47  protected:
48  typedef std::chrono::high_resolution_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 #ifdef SPOT_HAVE_TIMES
100  struct tms tmp;
101  times(&tmp);
102  start_.utime = tmp.tms_utime;
103  start_.cutime = tmp.tms_cutime;
104  start_.stime = tmp.tms_stime;
105  start_.cstime = tmp.tms_cstime;
106 #else
107  start_.utime = clock();
108 #endif
109  }
110 
112  void
114  {
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 
197  protected:
198  time_info start_;
199  time_info total_;
200  bool running;
201  };
202 
203  // This function declared here must be implemented in each file
204  // that includes this header, well, only if this operator is needed!
205  inline std::ostream& operator<<(std::ostream& os, const timer& dt);
206 
211  class timer_map
212  {
213  public:
214 
220  void
221  start(const std::string& name)
222  {
223  item_type& it = tm[name];
224  it.first.start();
225  ++it.second;
226  }
227 
231  void
232  stop(const std::string& name)
233  {
234  tm[name].first.stop();
235  }
236 
244  void
245  cancel(const std::string& name)
246  {
247  tm_type::iterator i = tm.find(name);
248  if (SPOT_UNLIKELY(i == tm.end()))
249  throw std::invalid_argument("timer_map::cancel(): unknown name");
250  SPOT_ASSERT(0 < i->second.second);
251  if (0 == --i->second.second)
252  tm.erase(i);
253  }
254 
256  const spot::timer&
257  timer(const std::string& name) const
258  {
259  tm_type::const_iterator i = tm.find(name);
260  if (SPOT_UNLIKELY(i == tm.end()))
261  throw std::invalid_argument("timer_map::timer(): unknown name");
262  return i->second.first;
263  }
264 
270  bool
271  empty() const
272  {
273  return tm.empty();
274  }
275 
277  SPOT_API std::ostream&
278  print(std::ostream& os) const;
279 
281  void
283  {
284  tm.clear();
285  }
286 
287  protected:
288  typedef std::pair<spot::timer, int> item_type;
289  typedef std::map<std::string, item_type> tm_type;
290  tm_type tm;
291  };
292 
294  typedef struct process_timer
295  {
296  void start()
297  {
298  walltimer.start();
299  cputimer.start();
300  }
301  // sw.stop() --> It always returns the duration since the last call to
302  // start(). Therefore, it wont't stop timing, moreover, it can be called
303  // multiple times.
304  void stop()
305  {
306  walltime_lap_ = walltimer.stop();
307  cputimer.stop();
308  }
309 
310  double walltime() const
311  {
312  return walltime_lap_;
313  }
314 
315  clock_t cputime(bool user, bool system, bool children, bool parent) const
316  {
317  return cputimer.get_uscp(user, system, children, parent);
318  }
319 
320  private:
321  spot::timer cputimer;
322  spot::stopwatch walltimer;
323  double walltime_lap_ = 0;
324  } process_timer;
325 
327 }
Definition: automata.hh:26
clock_t cutime() const
Return the user time of children of all accumulated interval.
Definition: timer.hh:145
clock_t stime() const
Return the system time of the current process (whithout children) of all accumulated interval...
Definition: timer.hh:156
void start()
Marks the start if the measurement.
Definition: timer.hh:52
A map of timer, where each timer has a name.
Definition: timer.hh:211
void start(const std::string &name)
Start a timer with name name.
Definition: timer.hh:221
void stop(const std::string &name)
Stop timer name.
Definition: timer.hh:232
clock_t utime() const
Return the user time of the current process (without children) of all accumulated interval...
Definition: timer.hh:135
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition: timer.hh:257
clock_t cstime() const
Return the system time of children of all accumulated interval.
Definition: timer.hh:166
bool empty() const
Whether there is no timer in the map.
Definition: timer.hh:271
Struct used to start and stop both timer and stopwatch clocks.
Definition: timer.hh:294
void cancel(const std::string &name)
Cancel timer name.
Definition: timer.hh:245
double stop()
Returns the elapsed duration in seconds.
Definition: timer.hh:61
bool is_running() const
Whether the timer is running.
Definition: timer.hh:192
void start()
Start a time interval.
Definition: timer.hh:95
void stop()
Stop a time interval and update the sum of all intervals.
Definition: timer.hh:113
A simple stopwatch.
Definition: timer.hh:45
void reset_all()
Remove information about all timers.
Definition: timer.hh:282
Definition: timer.hh:85
A structure to record elapsed time in clock ticks.
Definition: timer.hh:70

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