spot
0.8.2
|
00001 // Copyright (C) 2009, 2011 Laboratoire de Recherche et Developpement 00002 // de l'Epita (LRDE). 00003 // Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6), 00004 // département Systèmes Répartis Coopératifs (SRC), Université Pierre 00005 // et Marie Curie. 00006 // 00007 // This file is part of Spot, a model checking library. 00008 // 00009 // Spot is free software; you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation; either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Spot is distributed in the hope that it will be useful, but WITHOUT 00015 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00016 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 00017 // License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Spot; see the file COPYING. If not, write to the Free 00021 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00022 // 02111-1307, USA. 00023 00024 #ifndef SPOT_MISC_TIMER_HH 00025 # define SPOT_MISC_TIMER_HH 00026 00027 # include <cassert> 00028 # include <iosfwd> 00029 # include <string> 00030 # include <map> 00031 # include <sys/times.h> 00032 00033 namespace spot 00034 { 00037 00039 struct time_info 00040 { 00041 time_info() 00042 : utime(), stime(0) 00043 { 00044 } 00045 clock_t utime; 00046 clock_t stime; 00047 }; 00048 00050 class timer 00051 { 00052 public: 00053 timer() 00054 : running(false) 00055 { 00056 } 00057 00059 void 00060 start() 00061 { 00062 assert(!running); 00063 running = true; 00064 struct tms tmp; 00065 times(&tmp); 00066 start_.utime = tmp.tms_utime; 00067 start_.stime = tmp.tms_stime; 00068 } 00069 00071 void 00072 stop() 00073 { 00074 struct tms tmp; 00075 times(&tmp); 00076 total_.utime += tmp.tms_utime - start_.utime; 00077 total_.stime += tmp.tms_stime - start_.stime; 00078 assert(running); 00079 running = false; 00080 } 00081 00086 clock_t 00087 utime() const 00088 { 00089 return total_.utime; 00090 } 00091 00096 clock_t 00097 stime() const 00098 { 00099 return total_.stime; 00100 } 00101 00102 00104 bool 00105 is_running() const 00106 { 00107 return running; 00108 } 00109 00110 protected: 00111 time_info start_; 00112 time_info total_; 00113 bool running; 00114 }; 00115 00120 class timer_map 00121 { 00122 public: 00123 00129 void 00130 start(const std::string& name) 00131 { 00132 item_type& it = tm[name]; 00133 it.first.start(); 00134 ++it.second; 00135 } 00136 00140 void 00141 stop(const std::string& name) 00142 { 00143 tm[name].first.stop(); 00144 } 00145 00153 void 00154 cancel(const std::string& name) 00155 { 00156 tm_type::iterator i = tm.find(name); 00157 assert(i != tm.end()); 00158 assert(0 < i->second.second); 00159 if (0 == --i->second.second) 00160 tm.erase(i); 00161 } 00162 00164 const spot::timer& 00165 timer(const std::string& name) const 00166 { 00167 tm_type::const_iterator i = tm.find(name); 00168 assert(i != tm.end()); 00169 return i->second.first; 00170 } 00171 00173 spot::timer& 00174 timer(const std::string& name) 00175 { 00176 return tm[name].first; 00177 } 00178 00184 bool 00185 empty() const 00186 { 00187 return tm.empty(); 00188 } 00189 00191 std::ostream& 00192 print(std::ostream& os) const; 00193 00195 void 00196 reset_all() 00197 { 00198 tm.clear(); 00199 } 00200 00201 protected: 00202 typedef std::pair<spot::timer, int> item_type; 00203 typedef std::map<std::string, item_type> tm_type; 00204 tm_type tm; 00205 }; 00206 00208 } 00209 00210 #endif // SPOT_MISC_TIMER_HH