00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00048 #ifndef UNIXSTL_INCL_UNIXSTL_PERFORMANCE_HPP_PERFORMANCE_COUNTER
00049 #define UNIXSTL_INCL_UNIXSTL_PERFORMANCE_HPP_PERFORMANCE_COUNTER
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PERFORMANCE_COUNTER_MAJOR 4
00053 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PERFORMANCE_COUNTER_MINOR 1
00054 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PERFORMANCE_COUNTER_REVISION 8
00055 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PERFORMANCE_COUNTER_EDIT 65
00056 #endif
00057
00058
00059
00060
00061
00062 #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
00063 # include <unixstl/unixstl.h>
00064 #endif
00065
00066 #ifndef STLSOFT_INCL_SYS_H_TIME
00067 # define STLSOFT_INCL_SYS_H_TIME
00068 # include <sys/time.h>
00069 #endif
00070
00071
00072
00073
00074
00075
00076 #ifdef _STLSOFT_NO_NAMESPACES
00077 # define _UNIXSTL_NO_NAMESPACES
00078 #endif
00079
00080
00081 #ifdef _UNIXSTL_NO_NAMESPACES
00082 # define _UNIXSTL_NO_NAMESPACE
00083 #endif
00084
00085 #ifndef _UNIXSTL_NO_NAMESPACE
00086 # if defined(_STLSOFT_NO_NAMESPACE) || \
00087 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00088
00089 namespace unixstl
00090 {
00091 # else
00092
00093
00094 namespace stlsoft
00095 {
00096
00097 namespace unixstl_project
00098 {
00099
00100 # endif
00101 #endif
00102
00103
00104
00105
00106
00114 class performance_counter
00115 {
00118 public:
00122 typedef struct timeval epoch_type;
00126 typedef us_sint64_t interval_type;
00128 typedef performance_counter class_type;
00130
00133 public:
00134 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00135 performance_counter()
00136 {}
00137 #endif
00139
00142 public:
00146 void start();
00150 void stop();
00155 void restart();
00157
00160 public:
00162 static epoch_type get_epoch();
00163
00165 static interval_type get_seconds(epoch_type start, epoch_type end);
00167 static interval_type get_milliseconds(epoch_type start, epoch_type end);
00169 static interval_type get_microseconds(epoch_type start, epoch_type end);
00170
00174 interval_type get_period_count() const;
00178 interval_type get_seconds() const;
00182 interval_type get_milliseconds() const;
00186 interval_type get_microseconds() const;
00187
00190 interval_type stop_get_period_count_and_restart();
00191
00194 interval_type stop_get_seconds_and_restart();
00195
00198 interval_type stop_get_milliseconds_and_restart();
00199
00202 interval_type stop_get_microseconds_and_restart();
00204
00207 private:
00208 static void measure_(epoch_type &epoch);
00210
00213 private:
00214 epoch_type m_start;
00215 epoch_type m_end;
00217 };
00218
00219
00220
00221
00222
00223 #ifdef STLSOFT_UNITTEST
00224 # include "./unittest/performance_counter_unittest_.h"
00225 #endif
00226
00227
00228
00229
00230
00231 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00232
00233 inline void performance_counter::measure_(performance_counter::epoch_type &epoch)
00234 {
00235 ::gettimeofday(&epoch, NULL);
00236 }
00237
00238 inline void performance_counter::start()
00239 {
00240 measure_(m_start);
00241
00242 m_end = m_start;
00243 }
00244
00245 inline void performance_counter::stop()
00246 {
00247 measure_(m_end);
00248 }
00249
00250 inline void performance_counter::restart()
00251 {
00252 measure_(m_start);
00253 m_end = m_start;
00254 }
00255
00256 inline performance_counter::epoch_type performance_counter::get_epoch()
00257 {
00258 epoch_type epoch;
00259
00260 measure_(epoch);
00261
00262 return epoch;
00263 }
00264
00265 inline performance_counter::interval_type performance_counter::get_seconds(performance_counter::epoch_type start, performance_counter::epoch_type end)
00266 {
00267 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", start.tv_sec <= end.tv_sec);
00268
00269 long secs = end.tv_sec - start.tv_sec;
00270 long usecs = end.tv_usec - start.tv_usec;
00271
00272 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00273
00274 return secs + usecs / (1000 * 1000);
00275 }
00276
00277 inline performance_counter::interval_type performance_counter::get_milliseconds(performance_counter::epoch_type start, performance_counter::epoch_type end)
00278 {
00279 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", start.tv_sec <= end.tv_sec);
00280
00281 long secs = end.tv_sec - start.tv_sec;
00282 long usecs = end.tv_usec - start.tv_usec;
00283
00284 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00285
00286 return secs * 1000 + usecs / 1000;
00287 }
00288
00289 inline performance_counter::interval_type performance_counter::get_microseconds(performance_counter::epoch_type start, performance_counter::epoch_type end)
00290 {
00291 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", start.tv_sec <= end.tv_sec);
00292
00293 long secs = end.tv_sec - start.tv_sec;
00294 long usecs = end.tv_usec - start.tv_usec;
00295
00296 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00297
00298 return secs * (1000 * 1000) + usecs;
00299 }
00300
00301 inline performance_counter::interval_type performance_counter::get_period_count() const
00302 {
00303 return get_microseconds();
00304 }
00305
00306 inline performance_counter::interval_type performance_counter::get_seconds() const
00307 {
00308 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_start.tv_sec <= m_end.tv_sec);
00309
00310 long secs = m_end.tv_sec - m_start.tv_sec;
00311 long usecs = m_end.tv_usec - m_start.tv_usec;
00312
00313 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00314
00315 return secs + usecs / (1000 * 1000);
00316 }
00317
00318 inline performance_counter::interval_type performance_counter::get_milliseconds() const
00319 {
00320 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_start.tv_sec <= m_end.tv_sec);
00321
00322 long secs = m_end.tv_sec - m_start.tv_sec;
00323 long usecs = m_end.tv_usec - m_start.tv_usec;
00324
00325 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00326
00327 return secs * 1000 + usecs / 1000;
00328 }
00329
00330 inline performance_counter::interval_type performance_counter::get_microseconds() const
00331 {
00332 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_start.tv_sec <= m_end.tv_sec);
00333
00334 long secs = m_end.tv_sec - m_start.tv_sec;
00335 long usecs = m_end.tv_usec - m_start.tv_usec;
00336
00337 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00338
00339 return secs * (1000 * 1000) + usecs;
00340 }
00341
00342 #endif
00343
00344
00345
00346 #ifndef _UNIXSTL_NO_NAMESPACE
00347 # if defined(_STLSOFT_NO_NAMESPACE) || \
00348 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00349 }
00350 # else
00351 }
00352 }
00353 # endif
00354 #endif
00355
00356
00357
00358 #endif
00359
00360