00001 // OSB library ************************************************ -*- C++ -*- 00006 /* 00007 AUTHOR(S): Stephan Broennimann (vb) 00008 00009 RCS information 00010 $Name: OSB_060808 $ 00011 $Revision: 1.7 $ 00012 00013 License 00014 OSB rating and billing library for communication networks 00015 Copyright (C) 2004, 2005, 2006 OSB systems 00016 00017 This file may be distributed and/or modify under the terms of the 00018 GNU General Public License (GPL) as published by the Free Software 00019 Foundation which is provided in the file LICENSE.GPL included in the 00020 packaging of this file. 00021 00022 The file is distributed in the hope that it will be useful, but WITHOUT 00023 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00024 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00025 for more details. 00026 00027 Holders of a OSB Commercial License may use this file under the terms 00028 and conditions of this commercial license. 00029 */ 00030 #ifndef _STOPWATCH_H_ 00031 #define _STOPWATCH_H_ 00032 00033 // ************************************************************************ 00034 // included header files 00035 // + standard includes 00036 #include <sys/times.h> 00037 #include <errno.h> 00038 00039 // + libraries 00040 00041 // + local headers 00042 #include "osberror.h" 00043 00044 // + class declarations 00045 00046 // ************************************************************************ 00047 // namespace extensions 00048 namespace OSB_LIB { 00049 00050 // ************************************************************************ 00051 // forward declarations 00052 00053 // ************************************************************************ 00054 // type definitions 00055 00056 // ************************************************************************ 00057 // class definitions 00058 00062 class Stopwatch { 00063 public: 00065 Stopwatch(); 00066 public: 00068 struct Interval { 00070 Interval() 00071 : count_(0), real_(0.0), 00072 sys_(0.0), user_(0.0), csys_(0.0), cuser_(0.0) 00073 { 00074 // empty 00075 } 00077 long count_; 00079 double real_; 00081 double sys_; 00083 double user_; 00088 double csys_; 00093 double cuser_; 00094 }; 00095 00097 void start(); 00099 void stop(); 00101 void reset(); 00103 void read(Interval*) const; 00104 00105 private: 00107 struct Sws { 00109 Sws() 00110 : rtime_(0), stime_(0), utime_(0), cstime_(0), cutime_(0) 00111 { 00112 // empty 00113 } 00115 clock_t rtime_; 00117 clock_t stime_; 00119 clock_t utime_; 00121 clock_t cstime_; 00123 clock_t cutime_; 00124 }; 00125 00127 long cnt_; 00129 Sws accu_; 00131 Sws run_; 00133 struct tms tbuf_; 00135 static clock_t clk_tck_; 00136 }; // class Stopwatch 00137 } // namespace OSB_LIB 00138 00139 // ************************************************************************ 00140 // inline definitions 00141 namespace OSB_LIB { 00142 inline void Stopwatch::start() 00143 { 00144 if (-1 == (run_.rtime_ = times(&tbuf_))) { 00145 throw DefException(54) << "times" << errno; 00146 } 00147 run_.stime_ = tbuf_.tms_stime; 00148 run_.utime_ = tbuf_.tms_utime; 00149 run_.cstime_ = tbuf_.tms_cstime; 00150 run_.cutime_ = tbuf_.tms_cutime; 00151 } // Stopwatch::start() 00152 00153 inline void Stopwatch::stop() 00154 { 00155 clock_t rtime; 00156 if (-1 == (rtime = times(&tbuf_))) { 00157 throw DefException(54) << "times" << errno; 00158 } 00159 cnt_ += 1; 00160 accu_.rtime_ += rtime - run_.rtime_; 00161 accu_.stime_ += tbuf_.tms_stime - run_.stime_; 00162 accu_.utime_ += tbuf_.tms_utime - run_.utime_; 00163 accu_.cstime_ += tbuf_.tms_cstime - run_.cstime_; 00164 accu_.cutime_ += tbuf_.tms_cutime - run_.cutime_; 00165 } // Stopwatch::stop() 00166 } // namespace OSB_LIB 00167 #endif // #ifndef _STOPWATCH_H_