blob: 15356b1b9227c2b92f7676aae124664647f0e5d1 [file] [log] [blame]
Jens Axboe02bcaa82006-11-24 10:42:00 +01001/*
Jens Axboef5cc0242006-11-24 10:47:40 +01002 * Clock functions
Jens Axboe02bcaa82006-11-24 10:42:00 +01003 */
Jens Axboef5cc0242006-11-24 10:47:40 +01004
Jens Axboe02bcaa82006-11-24 10:42:00 +01005#include <unistd.h>
Jens Axboec223da82010-03-24 13:23:53 +01006#include <math.h>
Jens Axboe02bcaa82006-11-24 10:42:00 +01007#include <sys/time.h>
Bruce Cran03e20d62011-01-02 20:14:54 +01008#include <time.h>
Jens Axboe02bcaa82006-11-24 10:42:00 +01009
10#include "fio.h"
Jens Axboebe4ecfd2008-12-08 14:10:52 +010011#include "smalloc.h"
Jens Axboe02bcaa82006-11-24 10:42:00 +010012
13#include "hash.h"
14
Jens Axboe09a32402010-08-15 15:01:51 -040015#ifdef ARCH_HAVE_CPU_CLOCK
Jens Axboec223da82010-03-24 13:23:53 +010016static unsigned long cycles_per_usec;
Jens Axboec223da82010-03-24 13:23:53 +010017static unsigned long last_cycles;
Jens Axboe09a32402010-08-15 15:01:51 -040018#endif
19static struct timeval last_tv;
Jens Axboe3e488922008-11-13 12:07:20 +010020static int last_tv_valid;
Jens Axboe02bcaa82006-11-24 10:42:00 +010021
Jens Axboebe4ecfd2008-12-08 14:10:52 +010022static struct timeval *fio_tv;
23int fio_gtod_offload = 0;
24int fio_gtod_cpu = -1;
25
Jens Axboec223da82010-03-24 13:23:53 +010026enum fio_cs fio_clock_source = CS_GTOD;
27
Jens Axboe02bcaa82006-11-24 10:42:00 +010028#ifdef FIO_DEBUG_TIME
29
30#define HASH_BITS 8
31#define HASH_SIZE (1 << HASH_BITS)
32
Jens Axboe01743ee2008-06-02 12:19:19 +020033static struct flist_head hash[HASH_SIZE];
Jens Axboe02bcaa82006-11-24 10:42:00 +010034static int gtod_inited;
35
36struct gtod_log {
Jens Axboe01743ee2008-06-02 12:19:19 +020037 struct flist_head list;
Jens Axboe02bcaa82006-11-24 10:42:00 +010038 void *caller;
39 unsigned long calls;
40};
41
42static struct gtod_log *find_hash(void *caller)
43{
44 unsigned long h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020045 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010046
Jens Axboe01743ee2008-06-02 12:19:19 +020047 flist_for_each(entry, &hash[h]) {
48 struct gtod_log *log = flist_entry(entry, struct gtod_log,
49 list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010050
51 if (log->caller == caller)
52 return log;
53 }
54
55 return NULL;
56}
57
58static struct gtod_log *find_log(void *caller)
59{
60 struct gtod_log *log = find_hash(caller);
61
62 if (!log) {
63 unsigned long h;
64
65 log = malloc(sizeof(*log));
Jens Axboe01743ee2008-06-02 12:19:19 +020066 INIT_FLIST_HEAD(&log->list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010067 log->caller = caller;
68 log->calls = 0;
69
70 h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020071 flist_add_tail(&log->list, &hash[h]);
Jens Axboe02bcaa82006-11-24 10:42:00 +010072 }
73
74 return log;
75}
76
77static void gtod_log_caller(void *caller)
78{
79 if (gtod_inited) {
80 struct gtod_log *log = find_log(caller);
81
82 log->calls++;
83 }
84}
85
86static void fio_exit fio_dump_gtod(void)
87{
88 unsigned long total_calls = 0;
89 int i;
90
91 for (i = 0; i < HASH_SIZE; i++) {
Jens Axboe01743ee2008-06-02 12:19:19 +020092 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010093 struct gtod_log *log;
94
Jens Axboe01743ee2008-06-02 12:19:19 +020095 flist_for_each(entry, &hash[i]) {
96 log = flist_entry(entry, struct gtod_log, list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010097
Jens Axboe5ec10ea2008-03-06 15:42:00 +010098 printf("function %p, calls %lu\n", log->caller,
99 log->calls);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100100 total_calls += log->calls;
101 }
102 }
103
104 printf("Total %lu gettimeofday\n", total_calls);
105}
106
107static void fio_init gtod_init(void)
108{
109 int i;
110
111 for (i = 0; i < HASH_SIZE; i++)
Jens Axboe01743ee2008-06-02 12:19:19 +0200112 INIT_FLIST_HEAD(&hash[i]);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100113
114 gtod_inited = 1;
115}
116
117#endif /* FIO_DEBUG_TIME */
118
Jens Axboe1e97cce2006-12-05 11:44:16 +0100119#ifdef FIO_DEBUG_TIME
Jens Axboe02bcaa82006-11-24 10:42:00 +0100120void fio_gettime(struct timeval *tp, void *caller)
Jens Axboe1e97cce2006-12-05 11:44:16 +0100121#else
122void fio_gettime(struct timeval *tp, void fio_unused *caller)
123#endif
Jens Axboe02bcaa82006-11-24 10:42:00 +0100124{
125#ifdef FIO_DEBUG_TIME
126 if (!caller)
127 caller = __builtin_return_address(0);
128
129 gtod_log_caller(caller);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100130#endif
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100131 if (fio_tv) {
132 memcpy(tp, fio_tv, sizeof(*tp));
133 return;
Jens Axboec223da82010-03-24 13:23:53 +0100134 }
135
136 switch (fio_clock_source) {
137 case CS_GTOD:
Jens Axboe02bcaa82006-11-24 10:42:00 +0100138 gettimeofday(tp, NULL);
Jens Axboec223da82010-03-24 13:23:53 +0100139 break;
140 case CS_CGETTIME: {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100141 struct timespec ts;
142
Jens Axboe5bd9c782012-02-03 12:48:16 +0100143#ifdef FIO_HAVE_CLOCK_MONOTONIC
144 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
145#else
Jens Axboed481e002009-12-04 09:56:32 +0100146 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
Jens Axboe5bd9c782012-02-03 12:48:16 +0100147#endif
Jens Axboec223da82010-03-24 13:23:53 +0100148 log_err("fio: clock_gettime fails\n");
149 assert(0);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100150 }
151
152 tp->tv_sec = ts.tv_sec;
153 tp->tv_usec = ts.tv_nsec / 1000;
Jens Axboec223da82010-03-24 13:23:53 +0100154 break;
155 }
156#ifdef ARCH_HAVE_CPU_CLOCK
157 case CS_CPUCLOCK: {
158 unsigned long long usecs, t;
159
160 t = get_cpu_clock();
161 if (t < last_cycles) {
162 dprint(FD_TIME, "CPU clock going back in time\n");
163 t = last_cycles;
164 }
165
166 usecs = t / cycles_per_usec;
167 tp->tv_sec = usecs / 1000000;
168 tp->tv_usec = usecs % 1000000;
169 last_cycles = t;
170 break;
171 }
172#endif
173 default:
174 log_err("fio: invalid clock source %d\n", fio_clock_source);
175 break;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100176 }
Jens Axboe3e488922008-11-13 12:07:20 +0100177
178 /*
179 * If Linux is using the tsc clock on non-synced processors,
180 * sometimes time can appear to drift backwards. Fix that up.
181 */
182 if (last_tv_valid) {
183 if (tp->tv_sec < last_tv.tv_sec)
184 tp->tv_sec = last_tv.tv_sec;
185 else if (last_tv.tv_sec == tp->tv_sec &&
186 tp->tv_usec < last_tv.tv_usec)
187 tp->tv_usec = last_tv.tv_usec;
188 }
189 last_tv_valid = 1;
190 memcpy(&last_tv, tp, sizeof(*tp));
Jens Axboe02bcaa82006-11-24 10:42:00 +0100191}
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100192
Jens Axboe09a32402010-08-15 15:01:51 -0400193#ifdef ARCH_HAVE_CPU_CLOCK
Jens Axboec223da82010-03-24 13:23:53 +0100194static unsigned long get_cycles_per_usec(void)
195{
196 struct timeval s, e;
197 unsigned long long c_s, c_e;
198
199 gettimeofday(&s, NULL);
200 c_s = get_cpu_clock();
201 do {
202 unsigned long long elapsed;
203
204 gettimeofday(&e, NULL);
205 elapsed = utime_since(&s, &e);
206 if (elapsed >= 10) {
207 c_e = get_cpu_clock();
208 break;
209 }
210 } while (1);
211
212 return c_e - c_s;
213}
214
Jens Axboe09a32402010-08-15 15:01:51 -0400215static void calibrate_cpu_clock(void)
Jens Axboec223da82010-03-24 13:23:53 +0100216{
217 double delta, mean, S;
218 unsigned long avg, cycles[10];
219 int i, samples;
220
Jens Axboec223da82010-03-24 13:23:53 +0100221 cycles[0] = get_cycles_per_usec();
222 S = delta = mean = 0.0;
223 for (i = 0; i < 10; 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 / (10 - 1.0));
233
234 samples = avg = 0;
235 for (i = 0; i < 10; i++) {
236 double this = cycles[i];
237
Bruce Cran03e20d62011-01-02 20:14:54 +0100238 if ((fmax(this, mean) - fmin(this, mean)) > S)
Jens Axboec223da82010-03-24 13:23:53 +0100239 continue;
240 samples++;
241 avg += this;
242 }
243
244 S /= 10.0;
245 mean /= 10.0;
246
247 for (i = 0; i < 10; 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;
Jens Axboe09a32402010-08-15 15:01:51 -0400255
256}
257#else
258static void calibrate_cpu_clock(void)
259{
260}
261#endif
262
263void fio_clock_init(void)
264{
265 last_tv_valid = 0;
266 calibrate_cpu_clock();
Jens Axboec223da82010-03-24 13:23:53 +0100267}
268
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100269void fio_gtod_init(void)
270{
271 fio_tv = smalloc(sizeof(struct timeval));
272 assert(fio_tv);
273}
274
275void fio_gtod_update(void)
276{
277 gettimeofday(fio_tv, NULL);
278}