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