util/time.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #ifndef ZORBA_UTIL_TIME_H
18 #define ZORBA_UTIL_TIME_H
19 
20 #include <zorba/config.h>
21 
22 /**
23  * This header includes utility functions for certain timing-related
24  * operations, namely getting current wall-clock time and current
25  * CPU-used time values in a platform-dependent fashion, and computing
26  * deltas for both types.
27  *
28  * Types:
29  * cputime - type representing CPU time utilized thus far by this process
30  * walltime - type representing wall-clock time since some
31  * platform-dependent epoch
32  *
33  * Function signatures:
34  * void get_current_cputime(cputime& t) - returns current CPU time
35  *
36  * double get_cputime_elapsed(const cputime& t0, const cputime& t1) -
37  * calculates elapsed CPU time (in ms) between two cputimes
38  *
39  * void get_current_walltime(walltime& t) - returns current wall-clock time
40  *
41  * double get_walltime_elapsed(const walltime& t0, const walltime& t1) -
42  * calculates elapsed wall-clock time (in ms) between two walltimes
43  */
44 
45 /**
46  * TODO These functions should probably be defined in a .cpp file
47  * somewhere rather than here in time.h; as it is they will be
48  * compiled into every .o that uses them. So far, though, this is only
49  * zorbacmd and the implementation of fn:doc(), so it's not too bad.
50  */
51 
52 namespace zorba
53 {
54 
55  namespace time
56  {
57 
58  // Large enough to hold number of milliseconds since epoch.
59 #if ZORBA_SIZEOF_LONG <= 4
60  typedef long long msec_type;
61 #else
62  typedef long msec_type;
63 #endif /* ZORBA_SIZEOF_LONG */
64 
65  //
66  //
67  // Types and functions for CPU time
68  //
69  //
70 
71 #if (defined(ZORBA_HAVE_CLOCKGETTIME_FUNCTION) & defined(_POSIX_CPUTIME))
72 
73 #include <time.h>
74 
75  typedef struct timespec cputime;
76 
77  inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
78  {
79  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
80  ((t1.tv_nsec - t0.tv_nsec) / 1000000.0);
81  }
82 
83  inline void get_current_cputime (cputime& t)
84  {
85  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
86  }
87 
88 #elif defined(ZORBA_HAVE_RUSAGE_FUNCTION)
89 
90 #include <sys/time.h>
91 #include <sys/resource.h>
92 
93  typedef struct timeval cputime;
94 
95  inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
96  {
97  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
98  ((t1.tv_usec - t0.tv_usec) / 1000.0);
99  }
100 
101  inline void get_current_cputime (cputime& t)
102  {
103  struct rusage ru;
104  getrusage (RUSAGE_SELF, &ru);
105  t = ru.ru_utime;
106  }
107 
108 #else /* no rusage, no clock_gettime */
109 
110 #include <time.h>
111 
112  typedef clock_t cputime;
113 
114  inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
115  {
116  return (double) (t1 - t0) / (CLOCKS_PER_SEC / 1000);
117  }
118 
119  inline void get_current_cputime (cputime& t)
120  {
121  t = clock ();
122  }
123 
124 #endif /* ZORBA_HAVE_CLOCKGETTIME_FUNCTION */
125 
126 
127  //
128  //
129  // Types and functions for wall-clock time
130  //
131  //
132 
133 #if defined(ZORBA_HAVE_CLOCKGETTIME_FUNCTION)
134 
135 #include <time.h>
136 
137  typedef struct timespec walltime;
138 
139  inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
140  {
141  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
142  ((t1.tv_nsec - t0.tv_nsec) / 1000000.0);
143  }
144 
145  inline void get_current_walltime (walltime& t)
146  {
147 #ifdef _POSIX_MONOTONIC_CLOCK
148  clock_gettime(CLOCK_MONOTONIC, &t);
149 #else
150  clock_gettime(CLOCK_REALTIME, &t);
151 #endif /* _POSIX_MONOTONIC_CLOCK */
152  }
153 
154  inline msec_type get_walltime_in_millis(const walltime& t)
155  {
156  return t.tv_sec * (msec_type)1000 + t.tv_nsec / 1000000;
157  }
158 
159 #elif defined(WIN32)
160 
161  // TODO: Should maybe use QueryPerformanceCounter() or
162  // GetSystemTimeAsFileTime() for this, rather than ftime(), but I
163  // don't know enough about any of these alternatives to choose
164  // one. See http://msdn.microsoft.com/en-us/magazine/cc163996.aspx .
165 
166 #include <sys/timeb.h>
167 
168 #ifdef WINCE
169  typedef struct timeb walltime;
170 #else
171  typedef struct _timeb walltime;
172 #endif /* WINCE */
173 
174  inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
175  {
176  return ((t1.time - t0.time) * 1000.0) + (t1.millitm - t0.millitm);
177  }
178 
179  inline void get_current_walltime (walltime& t)
180  {
181 #ifdef WINCE
182  ftime(&t);
183 #else
184  _ftime_s(&t);
185 #endif /* WINCE */
186  }
187 
188  inline msec_type get_walltime_in_millis(const walltime& t)
189  {
190  return t.time * (msec_type)1000 + t.millitm;
191  }
192 
193 #else /* not Windows, and no clock_gettime() */
194 
195 #include <time.h>
196 #include <sys/time.h>
197 
198  typedef struct timeval walltime;
199 
200  inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
201  {
202  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
203  ((t1.tv_usec - t0.tv_usec) / 1000.0);
204  }
205 
207  {
208  gettimeofday(&t, NULL);
209  }
210 
212  {
213  return t.tv_sec * (msec_type)1000 + t.tv_usec / 1000;
214  }
215 
216 #endif /* ZORBA_HAVE_CLOCKGETTIME_FUNCTION */
217 
218  } // namespace time
219 } // namesace zorba
220 
221 #endif /* ZORBA_UTIL_TIME_H */
222 /*
223  * Local variables:
224  * mode: c++
225  * End:
226  */
227 /* vim:set et sw=2 ts=2: */
msec_type get_walltime_in_millis(const walltime &t)
Definition: util/time.h:211
struct timeval walltime
Definition: util/time.h:198
void get_current_walltime(walltime &t)
Definition: util/time.h:206
void get_current_cputime(cputime &t)
Definition: util/time.h:119
clock_t cputime
Definition: util/time.h:112
long long msec_type
Definition: util/time.h:60
double get_walltime_elapsed(const walltime &t0, const walltime &t1)
Definition: util/time.h:200
double get_cputime_elapsed(const cputime &t0, const cputime &t1)
Definition: util/time.h:114