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 /**
21  * This header includes utility functions for certain timing-related
22  * operations, namely getting current wall-clock time and current
23  * CPU-used time values in a platform-dependent fashion, and computing
24  * deltas for both types.
25  *
26  * Types:
27  * cputime - type representing CPU time utilized thus far by this process
28  * walltime - type representing wall-clock time since some
29  * platform-dependent epoch
30  *
31  * Function signatures:
32  * void get_current_cputime(cputime& t) - returns current CPU time
33  *
34  * double get_cputime_elapsed(const cputime& t0, const cputime& t1) -
35  * calculates elapsed CPU time (in ms) between two cputimes
36  *
37  * void get_current_walltime(walltime& t) - returns current wall-clock time
38  *
39  * double get_walltime_elapsed(const walltime& t0, const walltime& t1) -
40  * calculates elapsed wall-clock time (in ms) between two walltimes
41  */
42 
43 /**
44  * TODO These functions should probably be defined in a .cpp file
45  * somewhere rather than here in time.h; as it is they will be
46  * compiled into every .o that uses them. So far, though, this is only
47  * zorbacmd and the implementation of fn:doc(), so it's not too bad.
48  */
49 
50 namespace zorba
51 {
52 
53  namespace time
54  {
55 
56  //
57  //
58  // Types and functions for CPU time
59  //
60  //
61 
62 #if (defined(ZORBA_HAVE_CLOCKGETTIME_FUNCTION) & defined(_POSIX_CPUTIME))
63 
64 #include <time.h>
65 
66  typedef struct timespec cputime;
67 
68  inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
69  {
70  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
71  ((t1.tv_nsec - t0.tv_nsec) / 1000000.0);
72  }
73 
74  inline void get_current_cputime (cputime& t)
75  {
76  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t);
77  }
78 
79 #elif defined(ZORBA_HAVE_RUSAGE_FUNCTION)
80 
81 #include <sys/time.h>
82 #include <sys/resource.h>
83 
84  typedef struct timeval cputime;
85 
86  inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
87  {
88  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
89  ((t1.tv_usec - t0.tv_usec) / 1000.0);
90  }
91 
92  inline void get_current_cputime (cputime& t)
93  {
94  struct rusage ru;
95  getrusage (RUSAGE_SELF, &ru);
96  t = ru.ru_utime;
97  }
98 
99 #else /* no rusage, no clock_gettime */
100 
101 #include <time.h>
102 
103  typedef clock_t cputime;
104 
105  inline double get_cputime_elapsed (const cputime& t0, const cputime& t1)
106  {
107  return (double) (t1 - t0) / (CLOCKS_PER_SEC / 1000);
108  }
109 
110  inline void get_current_cputime (cputime& t)
111  {
112  t = clock ();
113  }
114 
115 #endif /* ZORBA_HAVE_CLOCKGETTIME_FUNCTION */
116 
117 
118  //
119  //
120  // Types and functions for wall-clock time
121  //
122  //
123 
124 #if defined(ZORBA_HAVE_CLOCKGETTIME_FUNCTION)
125 
126 #include <time.h>
127 
128  typedef struct timespec walltime;
129 
130  inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
131  {
132  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
133  ((t1.tv_nsec - t0.tv_nsec) / 1000000.0);
134  }
135 
136  inline void get_current_walltime (walltime& t)
137  {
138 #ifdef _POSIX_MONOTONIC_CLOCK
139  clock_gettime(CLOCK_MONOTONIC, &t);
140 #else
141  clock_gettime(CLOCK_REALTIME, &t);
142 #endif
143  }
144 
145  inline long get_walltime_in_millis(const walltime& t)
146  {
147  return t.tv_sec * 1000 + t.tv_nsec / 1000000;
148  }
149 
150 #elif defined(WIN32)
151 
152  // TODO: Should maybe use QueryPerformanceCounter() or
153  // GetSystemTimeAsFileTime() for this, rather than ftime(), but I
154  // don't know enough about any of these alternatives to choose
155  // one. See http://msdn.microsoft.com/en-us/magazine/cc163996.aspx .
156 
157 #include <sys/timeb.h>
158 
159 #ifdef WINCE
160  typedef struct timeb walltime;
161 #else
162  typedef struct _timeb walltime;
163 #endif
164 
165  inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
166  {
167  return ((t1.time - t0.time) * 1000.0) + (t1.millitm - t0.millitm);
168  }
169 
170  inline void get_current_walltime (walltime& t)
171  {
172 #ifdef WINCE
173  ftime(&t);
174 #else
175  _ftime_s(&t);
176 #endif
177  }
178 
179  inline long get_walltime_in_millis(const walltime& t)
180  {
181  return (long)(t.time * 1000 + t.millitm);
182  }
183 
184 #else /* not Windows, and no clock_gettime() */
185 
186 #include <time.h>
187 #include <sys/time.h>
188 
189  typedef struct timeval walltime;
190 
191  inline double get_walltime_elapsed (const walltime& t0, const walltime& t1)
192  {
193  return ((t1.tv_sec - t0.tv_sec) * 1000.0) +
194  ((t1.tv_usec - t0.tv_usec) / 1000.0);
195  }
196 
198  {
199  gettimeofday(&t, NULL);
200  }
201 
202  inline long get_walltime_in_millis(const walltime& t)
203  {
204  return t.tv_sec * 1000 + t.tv_usec / 1000;
205  }
206 
207 #endif /* ZORBA_HAVE_CLOCKGETTIME_FUNCTION */
208 
209  } // ::time
210 
211 } // ::zorba
212 
213 #endif
214 
215 /*
216  * Local variables:
217  * mode: c++
218  * End:
219  */
220 /* vim:set et sw=2 ts=2: */
blog comments powered by Disqus