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 WINSTL_INCL_UNIXSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER
00049 #define UNIXSTL_INCL_UNIXSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_MAJOR 1
00053 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_MINOR 0
00054 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_REVISION 7
00055 # define UNIXSTL_VER_UNIXSTL_PERFORMANCE_HPP_PROCESSTIMES_COUNTER_EDIT 13
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 #ifndef STLSOFT_INCL_SYS_H_RESOURCE
00071 # define STLSOFT_INCL_SYS_H_RESOURCE
00072 # include <sys/resource.h>
00073 #endif
00074
00075
00076
00077
00078
00079 #ifndef _UNIXSTL_NO_NAMESPACE
00080 # if defined(_STLSOFT_NO_NAMESPACE) || \
00081 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00082
00083 namespace unixstl
00084 {
00085 # else
00086
00087
00088 namespace stlsoft
00089 {
00090
00091 namespace unixstl_project
00092 {
00093
00094 # endif
00095 #endif
00096
00097
00098
00099
00100
00101
00111 class processtimes_counter
00112 {
00113 public:
00114 typedef processtimes_counter class_type;
00115 typedef us_sint64_t epoch_type;
00116
00117 public:
00121 typedef us_sint64_t interval_type;
00122
00123
00124 public:
00125 processtimes_counter();
00126
00127
00128 public:
00132 void start();
00136 void stop();
00137
00138
00139 public:
00140
00141
00145 interval_type get_kernel_period_count() const;
00149 interval_type get_kernel_seconds() const;
00153 interval_type get_kernel_milliseconds() const;
00157 interval_type get_kernel_microseconds() const;
00158
00159
00160
00164 interval_type get_user_period_count() const;
00168 interval_type get_user_seconds() const;
00172 interval_type get_user_milliseconds() const;
00176 interval_type get_user_microseconds() const;
00177
00178
00179
00183 interval_type get_period_count() const;
00187 interval_type get_seconds() const;
00191 interval_type get_milliseconds() const;
00195 interval_type get_microseconds() const;
00196
00197
00198 private:
00199 typedef struct timeval timeval_t;
00200
00201 timeval_t m_kernelStart;
00202 timeval_t m_kernelEnd;
00203 timeval_t m_userStart;
00204 timeval_t m_userEnd;
00205 };
00206
00208
00209
00210 #ifdef STLSOFT_UNITTEST
00211 # include "./unittest/processtimes_counter_unittest_.h"
00212 #endif
00213
00214
00215
00216 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00217
00218 inline processtimes_counter::processtimes_counter()
00219 {
00220
00221
00222
00223 }
00224
00225
00226 inline void processtimes_counter::start()
00227 {
00228 struct rusage r_usage;
00229
00230 ::getrusage(RUSAGE_SELF, &r_usage);
00231
00232 m_kernelStart = r_usage.ru_stime;
00233 m_userStart = r_usage.ru_utime;
00234 }
00235
00236 inline void processtimes_counter::stop()
00237 {
00238 struct rusage r_usage;
00239
00240 ::getrusage(RUSAGE_SELF, &r_usage);
00241
00242 m_kernelEnd = r_usage.ru_stime;
00243 m_userEnd = r_usage.ru_utime;
00244 }
00245
00246
00247 inline processtimes_counter::interval_type processtimes_counter::get_kernel_period_count() const
00248 {
00249 return get_kernel_microseconds();
00250 }
00251
00252 inline processtimes_counter::interval_type processtimes_counter::get_kernel_seconds() const
00253 {
00254 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_kernelStart.tv_sec <= m_kernelEnd.tv_sec);
00255
00256 long secs = m_kernelEnd.tv_sec - m_kernelStart.tv_sec;
00257 long usecs = m_kernelEnd.tv_usec - m_kernelStart.tv_usec;
00258
00259 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00260
00261 return secs + usecs / (1000 * 1000);
00262 }
00263
00264 inline processtimes_counter::interval_type processtimes_counter::get_kernel_milliseconds() const
00265 {
00266 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_kernelStart.tv_sec <= m_kernelEnd.tv_sec);
00267
00268 long secs = m_kernelEnd.tv_sec - m_kernelStart.tv_sec;
00269 long usecs = m_kernelEnd.tv_usec - m_kernelStart.tv_usec;
00270
00271 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00272
00273 return secs * 1000 + usecs / 1000;
00274 }
00275
00276 inline processtimes_counter::interval_type processtimes_counter::get_kernel_microseconds() const
00277 {
00278 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_kernelStart.tv_sec <= m_kernelEnd.tv_sec);
00279
00280 long secs = m_kernelEnd.tv_sec - m_kernelStart.tv_sec;
00281 long usecs = m_kernelEnd.tv_usec - m_kernelStart.tv_usec;
00282
00283 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00284
00285 return secs * (1000 * 1000) + usecs;
00286 }
00287
00288
00289 inline processtimes_counter::interval_type processtimes_counter::get_user_period_count() const
00290 {
00291 return get_user_microseconds();
00292 }
00293
00294 inline processtimes_counter::interval_type processtimes_counter::get_user_seconds() const
00295 {
00296 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_userStart.tv_sec <= m_userEnd.tv_sec);
00297
00298 long secs = m_userEnd.tv_sec - m_userStart.tv_sec;
00299 long usecs = m_userEnd.tv_usec - m_userStart.tv_usec;
00300
00301 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00302
00303 return secs + usecs / (1000 * 1000);
00304 }
00305
00306 inline processtimes_counter::interval_type processtimes_counter::get_user_milliseconds() const
00307 {
00308 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_userStart.tv_sec <= m_userEnd.tv_sec);
00309
00310 long secs = m_userEnd.tv_sec - m_userStart.tv_sec;
00311 long usecs = m_userEnd.tv_usec - m_userStart.tv_usec;
00312
00313 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00314
00315 return secs * 1000 + usecs / 1000;
00316 }
00317
00318 inline processtimes_counter::interval_type processtimes_counter::get_user_microseconds() const
00319 {
00320 UNIXSTL_MESSAGE_ASSERT("end before start: stop() must be called after start()", m_userStart.tv_sec <= m_userEnd.tv_sec);
00321
00322 long secs = m_userEnd.tv_sec - m_userStart.tv_sec;
00323 long usecs = m_userEnd.tv_usec - m_userStart.tv_usec;
00324
00325 UNIXSTL_ASSERT(usecs >= 0 || secs > 0);
00326
00327 return secs * (1000 * 1000) + usecs;
00328 }
00329
00330
00331 inline processtimes_counter::interval_type processtimes_counter::get_period_count() const
00332 {
00333 return get_kernel_period_count() + get_user_period_count();
00334 }
00335
00336 inline processtimes_counter::interval_type processtimes_counter::get_seconds() const
00337 {
00338 return get_period_count() / interval_type(10000000);
00339 }
00340
00341 inline processtimes_counter::interval_type processtimes_counter::get_milliseconds() const
00342 {
00343 return get_period_count() / interval_type(10000);
00344 }
00345
00346 inline processtimes_counter::interval_type processtimes_counter::get_microseconds() const
00347 {
00348 return get_period_count() / interval_type(10);
00349 }
00350
00351 #endif
00352
00353
00354
00355 #ifndef _UNIXSTL_NO_NAMESPACE
00356 # if defined(_STLSOFT_NO_NAMESPACE) || \
00357 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00358 }
00359 # else
00360 }
00361 }
00362 # endif
00363 #endif
00364
00365
00366
00367 #endif
00368
00369