spot  1.2.1a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
timer.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2009, 2011, 2012, 2013 Laboratoire de Recherche et
3 // 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 #ifndef SPOT_MISC_TIMER_HH
24 # define SPOT_MISC_TIMER_HH
25 
26 # include "common.hh"
27 # include "misc/_config.h"
28 # include <cassert>
29 # include <iosfwd>
30 # include <string>
31 # include <map>
32 # if SPOT_HAVE_SYS_TIMES_H
33 # include <sys/times.h>
34 # endif
35 # include <ctime>
36 
37 #include <chrono>
38 #include <ctime>
39 
40 namespace spot
41 {
44 
46  struct time_info
47  {
48  time_info()
49  : utime(0), stime(0)
50  {
51  }
52  clock_t utime;
53  clock_t stime;
54  };
55 
57  class timer
58  {
59  public:
60  timer()
61  : running(false)
62  {
63  }
64 
66  void
68  {
69  assert(!running);
70  running = true;
71  wall_start_ = std::chrono::high_resolution_clock::now();
72 #ifdef SPOT_HAVE_TIMES
73  struct tms tmp;
74  times(&tmp);
75  start_.utime = tmp.tms_utime;
76  start_.stime = tmp.tms_stime;
77 #else
78  start_.utime = clock();
79 #endif
80  }
81 
83  void
84  stop()
85  {
86  auto end = std::chrono::high_resolution_clock::now();
87  wall_cumul_ = std::chrono::duration_cast
88  <std::chrono::milliseconds>(end - wall_start_).count();
89 #ifdef SPOT_HAVE_TIMES
90  struct tms tmp;
91  times(&tmp);
92  total_.utime += tmp.tms_utime - start_.utime;
93  total_.stime += tmp.tms_stime - start_.stime;
94 #else
95  total_.utime += clock() - start_.utime;
96 #endif
97  assert(running);
98  running = false;
99  }
100 
105  clock_t
106  utime() const
107  {
108  return total_.utime;
109  }
110 
115  clock_t
116  stime() const
117  {
118  return total_.stime;
119  }
120 
121 
123  bool
124  is_running() const
125  {
126  return running;
127  }
128 
134  std::chrono::milliseconds::rep
135  walltime () const
136  {
137  return wall_cumul_;
138  }
139 
140  protected:
141  time_info start_;
142  time_info total_;
143  bool running;
144  std::chrono::high_resolution_clock::time_point wall_start_;
145  std::chrono::milliseconds::rep wall_cumul_;
146  };
147 
152  class timer_map
153  {
154  public:
155 
161  void
162  start(const std::string& name)
163  {
164  item_type& it = tm[name];
165  it.first.start();
166  ++it.second;
167  }
168 
172  void
173  stop(const std::string& name)
174  {
175  tm[name].first.stop();
176  }
177 
185  void
186  cancel(const std::string& name)
187  {
188  tm_type::iterator i = tm.find(name);
189  assert(i != tm.end());
190  assert(0 < i->second.second);
191  if (0 == --i->second.second)
192  tm.erase(i);
193  }
194 
196  const spot::timer&
197  timer(const std::string& name) const
198  {
199  tm_type::const_iterator i = tm.find(name);
200  assert(i != tm.end());
201  return i->second.first;
202  }
203 
205  spot::timer&
206  timer(const std::string& name)
207  {
208  return tm[name].first;
209  }
210 
216  bool
217  empty() const
218  {
219  return tm.empty();
220  }
221 
223  SPOT_API std::ostream&
224  print(std::ostream& os) const;
225 
227  void
229  {
230  tm.clear();
231  }
232 
233  protected:
234  typedef std::pair<spot::timer, int> item_type;
235  typedef std::map<std::string, item_type> tm_type;
236  tm_type tm;
237  };
238 
240 }
241 
242 #endif // SPOT_MISC_TIMER_HH
clock_t stime() const
Return the system time of all accumulated interval.
Definition: timer.hh:116
spot::timer & timer(const std::string &name)
Return the timer name.
Definition: timer.hh:206
std::chrono::milliseconds::rep walltime() const
Return cumulative wall time.
Definition: timer.hh:135
A map of timer, where each timer has a name.
Definition: timer.hh:152
void start(const std::string &name)
Start a timer with name name.
Definition: timer.hh:162
SPOT_API std::ostream & print(std::ostream &os) const
Format information about all timers in a table.
void stop(const std::string &name)
Stop timer name.
Definition: timer.hh:173
clock_t utime() const
Return the user time of all accumulated interval.
Definition: timer.hh:106
bool is_running() const
Whether the timer is running.
Definition: timer.hh:124
const spot::timer & timer(const std::string &name) const
Return the timer name.
Definition: timer.hh:197
void cancel(const std::string &name)
Cancel timer name.
Definition: timer.hh:186
void start()
Start a time interval.
Definition: timer.hh:67
bool empty() const
Whether there is no timer in the map.
Definition: timer.hh:217
void stop()
Stop a time interval and update the sum of all intervals.
Definition: timer.hh:84
void reset_all()
Remove information about all timers.
Definition: timer.hh:228
A timekeeper that accumulate interval of time.
Definition: timer.hh:57
A structure to record elapsed time in clock ticks.
Definition: timer.hh:46

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Tue Jan 21 2014 16:52:01 for spot by doxygen 1.8.5