clock: ensure that we re-init if the clocksource changes from the default
[fio.git] / gettime.c
... / ...
CommitLineData
1/*
2 * Clock functions
3 */
4
5#include <unistd.h>
6#include <math.h>
7#include <sys/time.h>
8#include <time.h>
9
10#include "fio.h"
11#include "smalloc.h"
12
13#include "hash.h"
14
15#ifdef ARCH_HAVE_CPU_CLOCK
16static unsigned long cycles_per_usec;
17static unsigned long last_cycles;
18int tsc_reliable = 0;
19#endif
20static struct timeval last_tv;
21static int last_tv_valid;
22
23enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
24int fio_clock_source_set = 0;
25enum fio_cs fio_clock_source_inited = CS_INVAL;
26
27#ifdef FIO_DEBUG_TIME
28
29#define HASH_BITS 8
30#define HASH_SIZE (1 << HASH_BITS)
31
32static struct flist_head hash[HASH_SIZE];
33static int gtod_inited;
34
35struct gtod_log {
36 struct flist_head list;
37 void *caller;
38 unsigned long calls;
39};
40
41static struct gtod_log *find_hash(void *caller)
42{
43 unsigned long h = hash_ptr(caller, HASH_BITS);
44 struct flist_head *entry;
45
46 flist_for_each(entry, &hash[h]) {
47 struct gtod_log *log = flist_entry(entry, struct gtod_log,
48 list);
49
50 if (log->caller == caller)
51 return log;
52 }
53
54 return NULL;
55}
56
57static struct gtod_log *find_log(void *caller)
58{
59 struct gtod_log *log = find_hash(caller);
60
61 if (!log) {
62 unsigned long h;
63
64 log = malloc(sizeof(*log));
65 INIT_FLIST_HEAD(&log->list);
66 log->caller = caller;
67 log->calls = 0;
68
69 h = hash_ptr(caller, HASH_BITS);
70 flist_add_tail(&log->list, &hash[h]);
71 }
72
73 return log;
74}
75
76static void gtod_log_caller(void *caller)
77{
78 if (gtod_inited) {
79 struct gtod_log *log = find_log(caller);
80
81 log->calls++;
82 }
83}
84
85static void fio_exit fio_dump_gtod(void)
86{
87 unsigned long total_calls = 0;
88 int i;
89
90 for (i = 0; i < HASH_SIZE; i++) {
91 struct flist_head *entry;
92 struct gtod_log *log;
93
94 flist_for_each(entry, &hash[i]) {
95 log = flist_entry(entry, struct gtod_log, list);
96
97 printf("function %p, calls %lu\n", log->caller,
98 log->calls);
99 total_calls += log->calls;
100 }
101 }
102
103 printf("Total %lu gettimeofday\n", total_calls);
104}
105
106static void fio_init gtod_init(void)
107{
108 int i;
109
110 for (i = 0; i < HASH_SIZE; i++)
111 INIT_FLIST_HEAD(&hash[i]);
112
113 gtod_inited = 1;
114}
115
116#endif /* FIO_DEBUG_TIME */
117
118#ifdef FIO_DEBUG_TIME
119void fio_gettime(struct timeval *tp, void *caller)
120#else
121void fio_gettime(struct timeval *tp, void fio_unused *caller)
122#endif
123{
124#ifdef FIO_DEBUG_TIME
125 if (!caller)
126 caller = __builtin_return_address(0);
127
128 gtod_log_caller(caller);
129#endif
130 if (fio_tv) {
131 memcpy(tp, fio_tv, sizeof(*tp));
132 return;
133 }
134
135 switch (fio_clock_source) {
136 case CS_GTOD:
137 gettimeofday(tp, NULL);
138 break;
139 case CS_CGETTIME: {
140 struct timespec ts;
141
142#ifdef FIO_HAVE_CLOCK_MONOTONIC
143 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
144#else
145 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
146#endif
147 log_err("fio: clock_gettime fails\n");
148 assert(0);
149 }
150
151 tp->tv_sec = ts.tv_sec;
152 tp->tv_usec = ts.tv_nsec / 1000;
153 break;
154 }
155#ifdef ARCH_HAVE_CPU_CLOCK
156 case CS_CPUCLOCK: {
157 unsigned long long usecs, t;
158
159 t = get_cpu_clock();
160 if (t < last_cycles) {
161 dprint(FD_TIME, "CPU clock going back in time\n");
162 t = last_cycles;
163 }
164
165 usecs = t / cycles_per_usec;
166 tp->tv_sec = usecs / 1000000;
167 tp->tv_usec = usecs % 1000000;
168 last_cycles = t;
169 break;
170 }
171#endif
172 default:
173 log_err("fio: invalid clock source %d\n", fio_clock_source);
174 break;
175 }
176
177 /*
178 * If Linux is using the tsc clock on non-synced processors,
179 * sometimes time can appear to drift backwards. Fix that up.
180 */
181 if (last_tv_valid) {
182 if (tp->tv_sec < last_tv.tv_sec)
183 tp->tv_sec = last_tv.tv_sec;
184 else if (last_tv.tv_sec == tp->tv_sec &&
185 tp->tv_usec < last_tv.tv_usec)
186 tp->tv_usec = last_tv.tv_usec;
187 }
188 last_tv_valid = 1;
189 memcpy(&last_tv, tp, sizeof(*tp));
190}
191
192#ifdef ARCH_HAVE_CPU_CLOCK
193static unsigned long get_cycles_per_usec(void)
194{
195 struct timeval s, e;
196 unsigned long long c_s, c_e;
197
198 gettimeofday(&s, NULL);
199 c_s = get_cpu_clock();
200 do {
201 unsigned long long elapsed;
202
203 gettimeofday(&e, NULL);
204 elapsed = utime_since(&s, &e);
205 if (elapsed >= 1280) {
206 c_e = get_cpu_clock();
207 break;
208 }
209 } while (1);
210
211 return (c_e - c_s + 127) >> 7;
212}
213
214#define NR_TIME_ITERS 50
215
216static void calibrate_cpu_clock(void)
217{
218 double delta, mean, S;
219 unsigned long avg, cycles[NR_TIME_ITERS];
220 int i, samples;
221
222 cycles[0] = get_cycles_per_usec();
223 S = delta = mean = 0.0;
224 for (i = 0; i < NR_TIME_ITERS; i++) {
225 cycles[i] = get_cycles_per_usec();
226 delta = cycles[i] - mean;
227 if (delta) {
228 mean += delta / (i + 1.0);
229 S += delta * (cycles[i] - mean);
230 }
231 }
232
233 S = sqrt(S / (NR_TIME_ITERS - 1.0));
234
235 samples = avg = 0;
236 for (i = 0; i < NR_TIME_ITERS; i++) {
237 double this = cycles[i];
238
239 if ((fmax(this, mean) - fmin(this, mean)) > S)
240 continue;
241 samples++;
242 avg += this;
243 }
244
245 S /= (double) NR_TIME_ITERS;
246 mean /= 10.0;
247
248 for (i = 0; i < NR_TIME_ITERS; i++)
249 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
250
251 avg /= samples;
252 avg = (avg + 9) / 10;
253 dprint(FD_TIME, "avg: %lu\n", avg);
254 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
255
256 cycles_per_usec = avg;
257}
258#else
259static void calibrate_cpu_clock(void)
260{
261}
262#endif
263
264void fio_clock_init(void)
265{
266 if (fio_clock_source == fio_clock_source_inited)
267 return;
268
269 last_tv_valid = 0;
270 fio_clock_source_inited = fio_clock_source;
271 calibrate_cpu_clock();
272
273 /*
274 * If the arch sets tsc_reliable != 0, then it must be good enough
275 * to use as THE clock source. For x86 CPUs, this means the TSC
276 * runs at a constant rate and is synced across CPU cores.
277 */
278 if (tsc_reliable) {
279 if (!fio_clock_source_set)
280 fio_clock_source = CS_CPUCLOCK;
281 } else if (fio_clock_source == CS_CPUCLOCK)
282 log_info("fio: clocksource=cpu may not be reliable\n");
283}
284
285unsigned long long utime_since(struct timeval *s, struct timeval *e)
286{
287 long sec, usec;
288 unsigned long long ret;
289
290 sec = e->tv_sec - s->tv_sec;
291 usec = e->tv_usec - s->tv_usec;
292 if (sec > 0 && usec < 0) {
293 sec--;
294 usec += 1000000;
295 }
296
297 /*
298 * time warp bug on some kernels?
299 */
300 if (sec < 0 || (sec == 0 && usec < 0))
301 return 0;
302
303 ret = sec * 1000000ULL + usec;
304
305 return ret;
306}
307
308unsigned long long utime_since_now(struct timeval *s)
309{
310 struct timeval t;
311
312 fio_gettime(&t, NULL);
313 return utime_since(s, &t);
314}
315
316unsigned long mtime_since(struct timeval *s, struct timeval *e)
317{
318 long sec, usec, ret;
319
320 sec = e->tv_sec - s->tv_sec;
321 usec = e->tv_usec - s->tv_usec;
322 if (sec > 0 && usec < 0) {
323 sec--;
324 usec += 1000000;
325 }
326
327 if (sec < 0 || (sec == 0 && usec < 0))
328 return 0;
329
330 sec *= 1000UL;
331 usec /= 1000UL;
332 ret = sec + usec;
333
334 return ret;
335}
336
337unsigned long mtime_since_now(struct timeval *s)
338{
339 struct timeval t;
340 void *p = __builtin_return_address(0);
341
342 fio_gettime(&t, p);
343 return mtime_since(s, &t);
344}
345
346unsigned long time_since_now(struct timeval *s)
347{
348 return mtime_since_now(s) / 1000;
349}