horometer.h
1 // $Id$
2 // Author: John Wu <John.Wu at ACM.org>
3 // Copyright (c) 2000-2016 the Regents of the University of California
4 #ifndef IBIS_HOROMETER_H
5 #define IBIS_HOROMETER_H
6 #include <stdio.h>
7 #include <time.h> // clock, clock_gettime
8 #if defined(__sun) || defined(__linux__) || defined(__HOS_AIX__) || \
9  defined(__CYGWIN__) || defined(__APPLE__) || defined(__FreeBSD__)
10 # include <limits.h> // CLK_TCK
11 # include <sys/time.h> // gettimeofday, timeval
12 # include <sys/times.h> // times, struct tms
13 # include <sys/resource.h> // getrusage
14 # ifndef RUSAGE_SELF
15 # define RUSAGE_SELF 0
16 # endif
17 # ifndef RUSAGE_CHILDREN
18 # define RUSAGE_CHILDRED -1
19 # endif
20 #elif defined(CRAY)
21 # include <sys/times.h> // times
22 #elif defined(sgi)
23 # include <limits.h> // CLK_TCK
24 # define RUSAGE_SELF 0 /* calling process */
25 # define RUSAGE_CHILDREN -1 /* terminated child processes */
26 # include <sys/times.h> // times
27 //# include <sys/types.h> // struct tms
28 # include <sys/time.h> // gettimeofday, getrusage
29 # include <sys/resource.h> // getrusage
30 #elif defined(__MINGW32__)
31 # include <limits.h> // CLK_TCK
32 # include <sys/time.h> // gettimeofday, timeval
33 #elif defined(_WIN32)
34 # include <windows.h>
35 #elif defined(VMS)
36 # include <unistd.h>
37 #endif
38 
41 namespace ibis {
42  class horometer;
43 }
44 
61 
63 public:
64  horometer() : startRealTime(0), totalRealTime(0),
65  startCPUTime(0), totalCPUTime(0) {
66 #if defined(_WIN32) && defined(_MSC_VER)
67  // the frequency of the high-resolution performance counter
68  LARGE_INTEGER lFrequency;
69  BOOL ret = QueryPerformanceFrequency(&lFrequency);
70  if (ret != 0 && lFrequency.QuadPart != 0)
71  countPeriod = 1.0/static_cast<double>(lFrequency.QuadPart);
72  else
73  countPeriod = 0.0;
74 #endif
75  };
77  void start() {
78  startRealTime = readWallClock();
79  startCPUTime = readCPUClock();
80  totalRealTime = 0.0;
81  totalCPUTime = 0.0;
82  };
84  void stop() {
85  double tmpr = readWallClock() - startRealTime;
86  double tmpc = readCPUClock() - startCPUTime;
87  if (tmpr > 0.0)
88  totalRealTime += tmpr;
89  if (tmpc > 0.0)
90  totalCPUTime += tmpc;
91  };
93  void resume() {
94  startRealTime = readWallClock();
95  startCPUTime = readCPUClock();
96  }
98  double realTime() const {return totalRealTime;}
100  double CPUTime() const {return totalCPUTime;}
101 
102 private:
103  double startRealTime; // wall clock start time
104  double totalRealTime; // total real time
105  double startCPUTime; // cpu start time
106  double totalCPUTime; // total cpu time
107 #if defined(_WIN32) && defined(_MSC_VER)
108  double countPeriod; // time of one high-resolution count
109 #endif
110 
111  inline double readWallClock();
112  inline double readCPUClock();
113 };
114 
117 inline double ibis::horometer::readWallClock() {
118 #if defined(CLOCK_MONOTONIC) && !defined(__CYGWIN__)
119  struct timespec tb;
120  if (0 == clock_gettime(CLOCK_MONOTONIC, &tb)) {
121  return static_cast<double>(tb.tv_sec) + (1e-9 * tb.tv_nsec);
122  }
123  else {
124  struct timeval cpt;
125  gettimeofday(&cpt, 0);
126  return static_cast<double>(cpt.tv_sec) + (1e-6 * cpt.tv_usec);
127  }
128 #elif defined(HAVE_GETTIMEOFDAY) || defined(__unix__) || defined(CRAY) || \
129  defined(__linux__) || defined(__HOS_AIX__) || defined(__APPLE__) || \
130  defined(__FreeBSD__)
131  struct timeval cpt;
132  gettimeofday(&cpt, 0);
133  return static_cast<double>(cpt.tv_sec) + (1e-6 * cpt.tv_usec);
134 #elif defined(_WIN32) && defined(_MSC_VER)
135  double ret = 0.0;
136  if (countPeriod != 0) {
137  LARGE_INTEGER cnt;
138  if (QueryPerformanceCounter(&cnt)) {
139  ret = countPeriod * cnt.QuadPart;
140  }
141  }
142  if (ret == 0.0) { // fallback option -- use GetSystemTime
143  union {
144  FILETIME ftFileTime;
145  __int64 ftInt64;
146  } ftRealTime;
147  GetSystemTimeAsFileTime(&ftRealTime.ftFileTime);
148  ret = (double) ftRealTime.ftInt64 * 1e-7;
149  }
150  return ret;
151 #elif defined(VMS)
152  return (double) clock() * 0.001;
153 #else
154  return (double) clock() / CLOCKS_PER_SEC;
155 #endif
156 } // ibis::horometer::readWallClock
157 
160 inline double ibis::horometer::readCPUClock() {
161 #if defined(__sun) || defined(sgi) || defined(__linux__) || defined(__APPLE__) \
162  || defined(__HOS_AIX__) || defined(__CYGWIN__) || defined(__FreeBSD__)
163  // on sun and linux, we can access getrusage to get more accurate time
164  double time=0;
165  struct rusage ruse;
166  if (0 == getrusage(RUSAGE_SELF, &ruse)) {
167  time = (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) * 1e-6 +
168  ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec;
169  }
170  else {
171  fputs("Warning -- horometer::readCPUClock(): getrusage failed "
172  "on RUSAGE_SELF", stderr);
173  }
174  if (0 == getrusage(RUSAGE_CHILDREN, &ruse)) {
175  time += (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) * 1e-6 +
176  ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec;
177  }
178  else {
179  fputs("Warning -- horometer::readCPUClock(): getrusage failed on "
180  "RUSAGE_CHILDRED", stderr);
181  }
182  return time;
183 #elif defined(__unix__) || defined(CRAY)
184 #if defined(__STDC__)
185  struct tms cpt;
186  times(&cpt);
187  return (cpt.tms_utime + cpt.tms_stime + cpt.tms_cutime +
188  (double)cpt.tms_cstime) / CLK_TCK;
189 #else
190  return (double) times() / CLK_TCK;
191 #endif
192 #elif defined(_WIN32)
193  return (double) clock() / CLOCKS_PER_SEC;
194 #elif defined(VMS)
195  return (double) clock() * 0.001;
196 #else
197  return (double) clock() / CLOCKS_PER_SEC;
198 #endif
199 } // ibis::horometer::readCPUClock
200 #endif // IBIS_HOROMETER_H
void resume()
Continue after being stopped.
Definition: horometer.h:93
void start()
Start the timer. Clear the internal counters.
Definition: horometer.h:77
The current implementation of FastBit is code named IBIS; most data structures and functions are in t...
Definition: bord.h:16
Horometer – a primitive timing instrument.
Definition: horometer.h:62
void stop()
Stop the timer. Record the duration. May resume later.
Definition: horometer.h:84
double CPUTime() const
Return the CPU time in seconds.
Definition: horometer.h:100
double realTime() const
Return the elapsed time in seconds.
Definition: horometer.h:98

Make It A Bit Faster
Contact us
Disclaimers
FastBit source code
FastBit mailing list archive