spot  2.3.3.dev
timer.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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 namespace spot
38 {
41 
42 
44  struct stopwatch
45  {
46  protected:
47  typedef std::chrono::high_resolution_clock clock;
48  clock::time_point start_;
49  public:
51  void start()
52  {
53  start_ = clock::now();
54  }
55 
60  double stop()
61  {
62  auto t = clock::now();
63  typedef std::chrono::duration<double> seconds;
64  return std::chrono::duration_cast<seconds>(t - start_).count();
65  }
66  };
67 
69  struct time_info
70  {
71  time_info()
72  : utime(0), stime(0), cutime(0), cstime(0)
73  {
74  }
75  clock_t utime;
76  clock_t stime;
77  clock_t cutime;
78  clock_t cstime;
79  };
80 
84  class timer
85  {
86  public:
87  timer()
88  : running(false)
89  {
90  }
91 
93  void
95  {
96  SPOT_ASSERT(!running);
97  running = true;
98  wall_start_ = std::chrono::high_resolution_clock::now();
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  auto end = std::chrono::high_resolution_clock::now();
116  wall_cumul_ = std::chrono::duration_cast
117  <std::chrono::milliseconds>(end - wall_start_).count();
118 #ifdef SPOT_HAVE_TIMES
119  struct tms tmp;
120  times(&tmp);
121  total_.utime += tmp.tms_utime - start_.utime;
122  total_.cutime += tmp.tms_cutime - start_.cutime;
123  total_.stime += tmp.tms_stime - start_.stime;
124  total_.cstime += tmp.tms_cstime - start_.cstime;
125 #else
126  total_.utime += clock() - start_.utime;
127 #endif
128  SPOT_ASSERT(running);
129  running = false;
130  }
131 
137  clock_t
138  utime() const
139  {
140  return total_.utime;
141  }
142 
147  clock_t
148  cutime() const
149  {
150  return total_.cutime;
151  }
152 
158  clock_t
159  stime() const
160  {
161  return total_.stime;
162  }
163 
168  clock_t
169  cstime() const
170  {
171  return total_.cstime;
172  }
173 
174  clock_t get_uscp(bool user, bool system, bool children, bool parent) const
175  {
176  clock_t res = 0;
177 
178  if (user && parent)
179  res += utime();
180 
181  if (user && children)
182  res += cutime();
183 
184  if (system && parent)
185  res += stime();
186 
187  if (system && children)
188  res += cstime();
189 
190  return res;
191  }
192 
194  bool
195  is_running() const
196  {
197  return running;
198  }
199 
205  std::chrono::milliseconds::rep
206  walltime() const
207  {
208  return wall_cumul_;
209  }
210 
211  protected:
212  time_info start_;
213  time_info total_;
214  bool running;
215  std::chrono::high_resolution_clock::time_point wall_start_;
216  std::chrono::milliseconds::rep wall_cumul_;
217  };
218 
219  // This function declared here must be implemented in each file
220  // that includes this header, well, only if this operator is needed!
221  inline std::ostream& operator<<(std::ostream& os, const timer& dt);
222 
227  class timer_map
228  {
229  public:
230 
236  void
237  start(const std::string& name)
238  {
239  item_type& it = tm[name];
240  it.first.start();
241  ++it.second;
242  }
243 
247  void
248  stop(const std::string& name)
249  {
250  tm[name].first.stop();
251  }
252 
260  void
261  cancel(const std::string& name)
262  {
263  tm_type::iterator i = tm.find(name);
264  if (SPOT_UNLIKELY(i == tm.end()))
265  throw std::invalid_argument("timer_map::cancel(): unknown name");
266  SPOT_ASSERT(0 < i->second.second);
267  if (0 == --i->second.second)
268  tm.erase(i);
269  }
270 
272  const spot::timer&
273  timer(const std::string& name) const
274  {
275  tm_type::const_iterator i = tm.find(name);
276  if (SPOT_UNLIKELY(i == tm.end()))
277  throw std::invalid_argument("timer_map::timer(): unknown name");
278  return i->second.first;
279  }
280 
286  bool
287  empty() const
288  {
289  return tm.empty();
290  }
291 
293  SPOT_API std::ostream&
294  print(std::ostream& os) const;
295 
297  void
299  {
300  tm.clear();
301  }
302 
303  protected:
304  typedef std::pair<spot::timer, int> item_type;
305  typedef std::map<std::string, item_type> tm_type;
306  tm_type tm;
307  };
308 
310 }
Definition: graph.hh:33
clock_t cutime() const
Return the user time of children of all accumulated interval.
Definition: timer.hh:148
clock_t stime() const
Return the system time of the current process (whithout children) of all accumulated interval...
Definition: timer.hh:159
void start()
Marks the start if the measurement.
Definition: timer.hh:51
A map of timer, where each timer has a name.
Definition: timer.hh:227
void start(const std::string &name)
Start a timer with name name.
Definition: timer.hh:237
std::chrono::milliseconds::rep walltime() const
Return cumulative wall time.
Definition: timer.hh:206
void stop(const std::string &name)
Stop timer name.
Definition: timer.hh:248
clock_t utime() const
Return the user time of the current process (without children) of all accumulated interval...
Definition: timer.hh:138
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition: timer.hh:273
clock_t cstime() const
Return the system time of children of all accumulated interval.
Definition: timer.hh:169
bool empty() const
Whether there is no timer in the map.
Definition: timer.hh:287
void cancel(const std::string &name)
Cancel timer name.
Definition: timer.hh:261
double stop()
Returns the elapsed duration in seconds.
Definition: timer.hh:60
bool is_running() const
Whether the timer is running.
Definition: timer.hh:195
void start()
Start a time interval.
Definition: timer.hh:94
std::ostream & operator<<(std::ostream &os, const formula &f)
Print a formula.
void stop()
Stop a time interval and update the sum of all intervals.
Definition: timer.hh:113
A simple stopwatch.
Definition: timer.hh:44
void reset_all()
Remove information about all timers.
Definition: timer.hh:298
Definition: timer.hh:84
A structure to record elapsed time in clock ticks.
Definition: timer.hh:69

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Tue Apr 18 2017 14:42:56 for spot by doxygen 1.8.13