MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hrt_utils.h
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 /*
20  * hrt_utils.h
21  *
22  */
23 
24 #ifndef _utils_hrt_utils
25 #define _utils_hrt_utils
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /*
31  * High-Resolution Time Measurement Utilities
32  *
33  * This module provides fuctions for measuring the system's real time
34  * and the current process's cpu time.
35  *
36  * In the Unix universe, various system functions exist of different
37  * resolution for measuring resources, such as real and cpu time.
38  * While ANSI C provides for functions time() and clock(), they are
39  * of limited resolution and use. Unix standardization efforts
40  * ("POSIX" et al) over time specified a number of functions, namely:
41  * clock_gettime(), gettimeofday(), getrusage(), and times().
42  *
43  * While these functions have different characteristics, they are not
44  * equally available on all systems. Therefore, determining the best
45  * time function available for various systems becomes a task; and it
46  * then introduces dependencies.
47  *
48  * This module provides
49  * - an abstraction from the chosen function for measuring times,
50  * - a default method of measuring real and cpu times by selecting
51  * the function with highest resolution available, and
52  * - functions to calculate the amount of time between measurements
53  * at microsecond resolution (but not neccessarily accuracy).
54  *
55  * The choice of the measurement function is controlled by the macros
56  * HRT_REALTIME_METHOD for real time and
57  * HRT_CPUTIME_METHOD for cpu time.
58  * If these macros haven't been defined, a default is chosen.
59  *
60  * Supported values for these macros are (by descending accuracy)
61  * HRT_REALTIME_METHOD:
62  * HRT_USE_CLOCK_GETTIME
63  * HRT_USE_GETTIMEOFDAY
64  * HRT_USE_TIMES
65  * HRT_USE_ANSI_TIME
66  * HRT_CPUTIME_METHOD:
67  * HRT_USE_CLOCK_GETTIME
68  * HRT_USE_GETRUSAGE
69  * HRT_USE_TIMES
70  * HRT_USE_ANSI_CLOCK
71  *
72  * Some information on the individual methods is given below; for
73  * detailed information, however, consult the system's man pages.
74  */
75 
76 /*
77  * For now, require all subsequent system header files.
78  *
79  * Alternatives:
80  * - Determine the availability of APIs by the Unix standards macros
81  * http://predef.sourceforge.net/prestd.html
82  * POSIX.1-2001: _POSIX_VERSION = 200112L
83  * SUSv2: _XOPEN_VERSION = 500
84  * SUSv3: _XOPEN_VERSION = 600
85  * from <unistd.h> for their existence/value before including headers.
86  * - Check autoconf-generated macros:
87  * #include "config.h"
88  * #if HAVE_UNISTD_H
89  * # include <unistd.h>
90  * #endif
91  * #if HAVE_SYS_TIMES_H
92  * # include <sys/times.h>
93  * #endif
94  * #if HAVE_GETRUSAGE
95  * # include <sys/time.h>
96  * #endif
97  * ...
98  */
99 #include <unistd.h>
100 #include <stdlib.h>
101 #include <time.h>
102 #include <sys/time.h>
103 #include <sys/resource.h>
104 #include <sys/times.h>
105 
106 
107 /*
108  * Method definitions for measuring real and cpu times.
109  */
110 
123 #define HRT_USE_CLOCK_GETTIME 1
124 
130 #define HRT_USE_GETRUSAGE 2
131 
139 #define HRT_USE_GETTIMEOFDAY 3
140 
153 #define HRT_USE_TIMES 4
154 
161 #define HRT_USE_ANSI_TIME 5
162 
174 #define HRT_USE_ANSI_CLOCK 6
175 
176 
177 /*
178  * Default method selection of measuring real and cpu times.
179  */
180 
181 #ifdef HRT_REALTIME_METHOD
182 # if !(HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME \
183  || HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY \
184  || HRT_REALTIME_METHOD==HRT_USE_TIMES \
185  || HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
186 # error "unsupported HRT_REALTIME_METHOD: " HRT_REALTIME_METHOD
187 # endif
188 #else
189 # if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS+0 >= 0))
190 # define HRT_REALTIME_METHOD HRT_USE_CLOCK_GETTIME
191 # else
192 # define HRT_REALTIME_METHOD HRT_USE_GETTIMEOFDAY
193 # endif
194 #endif
195 #ifdef HRT_CPUTIME_METHOD
196 # if !(HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME \
197  || HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE \
198  || HRT_CPUTIME_METHOD==HRT_USE_TIMES \
199  || HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
200 # error "unsupported HRT_CPUTIME_METHOD: " HRT_CPUTIME_METHOD
201 # endif
202 #else
203 # if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS+0 >= 0) \
204  && defined(_POSIX_CPUTIME) && (_POSIX_CPUTIME+0 >= 0))
205 # define HRT_CPUTIME_METHOD HRT_USE_CLOCK_GETTIME
206 # else
207 # define HRT_CPUTIME_METHOD HRT_USE_GETRUSAGE
208 # endif
209 #endif
210 
211 /*
212  * Timestamp types for real and cpu time.
213  */
214 
218 typedef struct {
219 #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
220  struct timespec time;
221 #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
222  struct timeval time;
223 #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
224  clock_t time;
225 #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
226  time_t time;
227 #endif
228 } hrt_rtstamp;
229 
233 typedef struct {
234 #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
235  struct timespec time;
236 #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
237  struct rusage time;
238 #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
239  struct tms time;
240 #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
241  clock_t time;
242 #endif
243 } hrt_ctstamp;
244 
248 typedef struct hrt_tstamp {
249  hrt_rtstamp rtstamp;
250  hrt_ctstamp ctstamp;
251 } hrt_tstamp;
252 
253 /*
254  * Functions for time snapshots.
255  */
256 
263 extern int
264 hrt_rtnow(hrt_rtstamp* x);
265 
272 extern int
273 hrt_ctnow(hrt_ctstamp* x);
274 
281 extern int
282 hrt_tnow(hrt_tstamp* x);
283 
288 extern double
289 hrt_rtmicros(const hrt_rtstamp* y, const hrt_rtstamp* x);
290 
295 extern double
296 hrt_ctmicros(const hrt_ctstamp* y, const hrt_ctstamp* x);
297 
298 /*
299  * Functions for Debugging.
300  */
301 
305 extern void
306 hrt_rtnull(hrt_rtstamp* x);
307 
311 extern void
312 hrt_ctnull(hrt_ctstamp* x);
313 
317 extern void
318 hrt_tnull(hrt_tstamp* x);
319 
320 #ifdef __cplusplus
321 }
322 #endif
323 #endif