00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef OMC_PROCESS_TIMER_H
00015 #define OMC_PROCESS_TIMER_H
00016
00017
00018 #define HAVE_SYS_RESOURCE_H 1
00019 #define HAVE_SYS_TIME_H 1
00020
00021 #ifdef HAVE_SYS_RESOURCE_H
00022 #include <sys/resource.h>
00023 #endif
00024
00025 #ifdef HAVE_SYS_TIME_H
00026 #include <sys/time.h>
00027 #endif
00028
00029 namespace omc
00030 {
00031 class ProcessTimer
00032 {
00033 public:
00034 ProcessTimer() {}
00035
00036 ~ProcessTimer() {}
00037
00038 void start();
00039 void stop();
00040
00041 double elapsed() const;
00042 double elapsedUser() const;
00043 double elapsedSys() const;
00044
00045 private:
00046 #ifdef HAVE_SYS_RESOURCE_H
00047 struct rusage m_start;
00048 struct rusage m_stop;
00049 #endif
00050 };
00051
00052
00053
00054 inline void ProcessTimer::start() {
00055 #ifdef HAVE_SYS_RESOURCE_H
00056 getrusage (RUSAGE_SELF, &m_start);
00057 #endif
00058 }
00059
00060
00061 inline void ProcessTimer::stop() {
00062 #ifdef HAVE_SYS_RESOURCE_H
00063 getrusage (RUSAGE_SELF,&m_stop);
00064 #endif
00065 }
00066
00067
00068 inline double ProcessTimer::elapsed() const {
00069 #ifdef HAVE_SYS_RESOURCE_H
00070 return elapsedUser() + elapsedSys();
00071 #else
00072 return 0.0;
00073 #endif
00074 }
00075
00076
00077 inline double ProcessTimer::elapsedUser() const {
00078 #ifdef HAVE_SYS_RESOURCE_H
00079 return double (m_stop.ru_utime.tv_sec * 10e6 +
00080 m_stop.ru_utime.tv_usec -
00081 m_start.ru_utime.tv_sec * 10e6 -
00082 m_start.ru_utime.tv_usec) / 10e6;
00083 #else
00084 return 0.0;
00085 #endif
00086 }
00087
00088
00089 inline double ProcessTimer::elapsedSys() const {
00090 #ifdef HAVE_SYS_RESOURCE_H
00091 return double (m_stop.ru_stime.tv_sec * 10e6 +
00092 m_stop.ru_stime.tv_usec -
00093 m_start.ru_stime.tv_sec * 10e6 -
00094 m_start.ru_stime.tv_usec) / 10e6;
00095 #else
00096 return 0.0;
00097 #endif
00098 }
00099
00100 }
00101
00102 #endif // OMC_PROCESS_TIMER_H