spot
0.9.1
|
00001 // -*- coding: utf-8 -*- 00002 // Copyright (C) 2009, 2011, 2012 Laboratoire de Recherche et 00003 // Développement de l'Epita (LRDE). 00004 // Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6), 00005 // département Systèmes Répartis Coopératifs (SRC), Université Pierre 00006 // et Marie Curie. 00007 // 00008 // This file is part of Spot, a model checking library. 00009 // 00010 // Spot is free software; you can redistribute it and/or modify it 00011 // under the terms of the GNU General Public License as published by 00012 // the Free Software Foundation; either version 2 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // Spot is distributed in the hope that it will be useful, but WITHOUT 00016 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00017 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 00018 // License for more details. 00019 // 00020 // You should have received a copy of the GNU General Public License 00021 // along with Spot; see the file COPYING. If not, write to the Free 00022 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00023 // 02111-1307, USA. 00024 00025 #ifndef SPOT_MISC_TIMER_HH 00026 # define SPOT_MISC_TIMER_HH 00027 00028 # include "misc/_config.h" 00029 # include <cassert> 00030 # include <iosfwd> 00031 # include <string> 00032 # include <map> 00033 # if SPOT_HAVE_SYS_TIMES_H 00034 # include <sys/times.h> 00035 # endif 00036 # include <ctime> 00037 00038 00039 namespace spot 00040 { 00043 00045 struct time_info 00046 { 00047 time_info() 00048 : utime(0), stime(0) 00049 { 00050 } 00051 clock_t utime; 00052 clock_t stime; 00053 }; 00054 00056 class timer 00057 { 00058 public: 00059 timer() 00060 : running(false) 00061 { 00062 } 00063 00065 void 00066 start() 00067 { 00068 assert(!running); 00069 running = true; 00070 #ifdef SPOT_HAVE_TIMES 00071 struct tms tmp; 00072 times(&tmp); 00073 start_.utime = tmp.tms_utime; 00074 start_.stime = tmp.tms_stime; 00075 #else 00076 start_.utime = clock(); 00077 #endif 00078 } 00079 00081 void 00082 stop() 00083 { 00084 #ifdef SPOT_HAVE_TIMES 00085 struct tms tmp; 00086 times(&tmp); 00087 total_.utime += tmp.tms_utime - start_.utime; 00088 total_.stime += tmp.tms_stime - start_.stime; 00089 #else 00090 total_.utime += clock() - start_.utime; 00091 #endif 00092 assert(running); 00093 running = false; 00094 } 00095 00100 clock_t 00101 utime() const 00102 { 00103 return total_.utime; 00104 } 00105 00110 clock_t 00111 stime() const 00112 { 00113 return total_.stime; 00114 } 00115 00116 00118 bool 00119 is_running() const 00120 { 00121 return running; 00122 } 00123 00124 protected: 00125 time_info start_; 00126 time_info total_; 00127 bool running; 00128 }; 00129 00134 class timer_map 00135 { 00136 public: 00137 00143 void 00144 start(const std::string& name) 00145 { 00146 item_type& it = tm[name]; 00147 it.first.start(); 00148 ++it.second; 00149 } 00150 00154 void 00155 stop(const std::string& name) 00156 { 00157 tm[name].first.stop(); 00158 } 00159 00167 void 00168 cancel(const std::string& name) 00169 { 00170 tm_type::iterator i = tm.find(name); 00171 assert(i != tm.end()); 00172 assert(0 < i->second.second); 00173 if (0 == --i->second.second) 00174 tm.erase(i); 00175 } 00176 00178 const spot::timer& 00179 timer(const std::string& name) const 00180 { 00181 tm_type::const_iterator i = tm.find(name); 00182 assert(i != tm.end()); 00183 return i->second.first; 00184 } 00185 00187 spot::timer& 00188 timer(const std::string& name) 00189 { 00190 return tm[name].first; 00191 } 00192 00198 bool 00199 empty() const 00200 { 00201 return tm.empty(); 00202 } 00203 00205 std::ostream& 00206 print(std::ostream& os) const; 00207 00209 void 00210 reset_all() 00211 { 00212 tm.clear(); 00213 } 00214 00215 protected: 00216 typedef std::pair<spot::timer, int> item_type; 00217 typedef std::map<std::string, item_type> tm_type; 00218 tm_type tm; 00219 }; 00220 00222 } 00223 00224 #endif // SPOT_MISC_TIMER_HH