MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hrt_utils.c
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.c
21  *
22  */
23 
24 #include <hrt_utils.h>
25 #include <assert.h>
26 
27 /*
28  * High-Resolution Time Utilities -- Implementation
29  */
30 
31 int
32 hrt_rtnow(hrt_rtstamp* x)
33 {
34  int ret;
35 
36 #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
37 # if (defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK+0 >= 0))
38 # define REAL_CLOCK_TYPE CLOCK_MONOTONIC
39 # else
40 # define REAL_CLOCK_TYPE CLOCK_REALTIME
41 # endif
42  ret = clock_gettime(REAL_CLOCK_TYPE, &x->time);
43 #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
44  ret = gettimeofday(&x->time, NULL);
45 #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
46  {
47  clock_t r = times(NULL);
48  if (r == -1) {
49  ret = r;
50  } else {
51  ret = 0;
52  x->time = r;
53  }
54  }
55 #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
56  {
57  time_t r = time(NULL);
58  if (r == -1) {
59  ret = r;
60  } else {
61  ret = 0;
62  x->time = r;
63  }
64  }
65 #endif
66 
67  return ret;
68 }
69 
70 int
71 hrt_ctnow(hrt_ctstamp* x)
72 {
73  int ret;
74 
75 #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
76  ret = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &x->time);
77 #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
78  ret = getrusage(RUSAGE_SELF, &x->time);
79 #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
80  {
81  clock_t r = times(&x->time);
82  ret = (r == -1 ? r : 0);
83  }
84 #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
85  {
86  clock_t r = clock();
87  if (r == -1) {
88  ret = r;
89  } else {
90  ret = 0;
91  x->time = r;
92  }
93  }
94 #endif
95 
96  return ret;
97 }
98 
99 int
100 hrt_tnow(hrt_tstamp* x)
101 {
102  int ret;
103  ret = hrt_rtnow(&x->rtstamp);
104  if (ret != 0)
105  return ret;
106  ret = hrt_ctnow(&x->ctstamp);
107  return ret;
108 }
109 
110 #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME \
111  || HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
112 static inline double
113 timespec_diff(const struct timespec *y, const struct timespec *x)
114 {
115  return (((y->tv_sec - x->tv_sec) * 1000000.0)
116  + ((y->tv_nsec - x->tv_nsec) / 1000.0));
117 }
118 #endif
119 
120 #if (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY \
121  || HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
122 static inline double
123 timeval_diff(const struct timeval *y, const struct timeval *x)
124 {
125  return (((y->tv_sec - x->tv_sec) * 1000000.0)
126  + (y->tv_usec - x->tv_usec));
127 }
128 #endif
129 
130 #if (HRT_REALTIME_METHOD==HRT_USE_TIMES \
131  || HRT_CPUTIME_METHOD==HRT_USE_TIMES \
132  || HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
133 static inline double
134 clock_t_diff(clock_t y, clock_t x)
135 {
136  /* ignored: loosing precision if clock_t is specified as long double */
137  return (double)(y - x);
138 }
139 #endif
140 
141 double
142 hrt_rtmicros(const hrt_rtstamp* y, const hrt_rtstamp* x)
143 {
144 #if (HRT_REALTIME_METHOD==HRT_USE_TIMES)
145  static long clock_ticks = 0;
146  if (clock_ticks == 0) {
147  clock_ticks = sysconf(_SC_CLK_TCK);
148  assert (clock_ticks > 0);
149  }
150 #endif
151 
152 #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
153  return timespec_diff(&y->time, &x->time);
154 #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
155  return timeval_diff(&y->time, &x->time);
156 #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
157  return ((clock_t_diff(y->time, x->time) * 1000000.0) / clock_ticks);
158 #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
159  return (difftime(y->time, x->time) * 1000000.0);
160 #endif
161 }
162 
163 double
164 hrt_ctmicros(const hrt_ctstamp* y, const hrt_ctstamp* x)
165 {
166 #if (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
167  static long clock_ticks = 0;
168  if (clock_ticks == 0) {
169  clock_ticks = sysconf(_SC_CLK_TCK);
170  assert (clock_ticks > 0);
171  }
172 #endif
173 
174 #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
175  return timespec_diff(&y->time, &x->time);
176 #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
177  return (timeval_diff(&y->time.ru_utime, &x->time.ru_utime)
178  + timeval_diff(&y->time.ru_stime, &x->time.ru_stime));
179 #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
180  return (((clock_t_diff(y->time.tms_utime, x->time.tms_utime)
181  + clock_t_diff(y->time.tms_stime, x->time.tms_stime))
182  * 1000000.0) / clock_ticks);
183 #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
184  return ((clock_t_diff(y->time, x->time) * 1000000.0) / CLOCKS_PER_SEC);
185 #endif
186 }
187 
188 void
189 hrt_rtnull(hrt_rtstamp* x)
190 {
191 #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
192  x->time.tv_sec = 0;
193  x->time.tv_nsec = 0;
194 #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
195  x->time.tv_sec = 0;
196  x->time.tv_usec = 0;
197 #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
198  x->time = 0;
199 #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
200  x->time = 0;
201 #endif
202 }
203 
204 void
205 hrt_ctnull(hrt_ctstamp* x)
206 {
207 #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
208  x->time.tv_sec = 0;
209  x->time.tv_nsec = 0;
210 #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
211  x->time.ru_utime.tv_sec = 0;
212  x->time.ru_utime.tv_usec = 0;
213  x->time.ru_stime.tv_sec = 0;
214  x->time.ru_stime.tv_usec = 0;
215 #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
216  x->time.tms_utime = 0;
217  x->time.tms_stime = 0;
218 #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
219  x->time = 0;
220 #endif
221 }
222 
223 void
224 hrt_tnull(hrt_tstamp* x)
225 {
226  hrt_rtnull(&x->rtstamp);
227  hrt_ctnull(&x->ctstamp);
228 }