spot  1.2.4
 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, 2014 Laboratoire de Recherche
3 // 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 #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 
38 namespace spot
39 {
42 
44  struct time_info
45  {
46  time_info()
47  : utime(0), stime(0)
48  {
49  }
50  clock_t utime;
51  clock_t stime;
52  };
53 
55  class timer
56  {
57  public:
58  timer()
59  : running(false)
60  {
61  }
62 
64  void
66  {
67  assert(!running);
68  running = true;
69 #ifdef SPOT_HAVE_TIMES
70  struct tms tmp;
71  times(&tmp);
72  start_.utime = tmp.tms_utime + tmp.tms_cutime;
73  start_.stime = tmp.tms_stime + tmp.tms_cstime;
74 #else
75  start_.utime = clock();
76 #endif
77  }
78 
80  void
81  stop()
82  {
83 #ifdef SPOT_HAVE_TIMES
84  struct tms tmp;
85  times(&tmp);
86  total_.utime += tmp.tms_utime + tmp.tms_cutime - start_.utime;
87  total_.stime += tmp.tms_stime + tmp.tms_cstime - start_.stime;
88 #else
89  total_.utime += clock() - start_.utime;
90 #endif
91  assert(running);
92  running = false;
93  }
94 
99  clock_t
100  utime() const
101  {
102  return total_.utime;
103  }
104 
109  clock_t
110  stime() const
111  {
112  return total_.stime;
113  }
114 
115 
117  bool
118  is_running() const
119  {
120  return running;
121  }
122 
123  protected:
124  time_info start_;
125  time_info total_;
126  bool running;
127  };
128 
133  class timer_map
134  {
135  public:
136 
142  void
143  start(const std::string& name)
144  {
145  item_type& it = tm[name];
146  it.first.start();
147  ++it.second;
148  }
149 
153  void
154  stop(const std::string& name)
155  {
156  tm[name].first.stop();
157  }
158 
166  void
167  cancel(const std::string& name)
168  {
169  tm_type::iterator i = tm.find(name);
170  assert(i != tm.end());
171  assert(0 < i->second.second);
172  if (0 == --i->second.second)
173  tm.erase(i);
174  }
175 
177  const spot::timer&
178  timer(const std::string& name) const
179  {
180  tm_type::const_iterator i = tm.find(name);
181  assert(i != tm.end());
182  return i->second.first;
183  }
184 
186  spot::timer&
187  timer(const std::string& name)
188  {
189  return tm[name].first;
190  }
191 
197  bool
198  empty() const
199  {
200  return tm.empty();
201  }
202 
204  SPOT_API std::ostream&
205  print(std::ostream& os) const;
206 
208  void
210  {
211  tm.clear();
212  }
213 
214  protected:
215  typedef std::pair<spot::timer, int> item_type;
216  typedef std::map<std::string, item_type> tm_type;
217  tm_type tm;
218  };
219 
221 }
222 
223 #endif // SPOT_MISC_TIMER_HH

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Thu May 15 2014 11:04:11 for spot by doxygen 1.8.4