15 #ifdef ARCH_HAVE_CPU_CLOCK
16 static unsigned long cycles_per_usec;
17 static unsigned long last_cycles;
20 static struct timeval last_tv;
21 static int last_tv_valid;
23 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
24 int fio_clock_source_set = 0;
29 #define HASH_SIZE (1 << HASH_BITS)
31 static struct flist_head hash[HASH_SIZE];
32 static int gtod_inited;
35 struct flist_head list;
40 static struct gtod_log *find_hash(void *caller)
42 unsigned long h = hash_ptr(caller, HASH_BITS);
43 struct flist_head *entry;
45 flist_for_each(entry, &hash[h]) {
46 struct gtod_log *log = flist_entry(entry, struct gtod_log,
49 if (log->caller == caller)
56 static struct gtod_log *find_log(void *caller)
58 struct gtod_log *log = find_hash(caller);
63 log = malloc(sizeof(*log));
64 INIT_FLIST_HEAD(&log->list);
68 h = hash_ptr(caller, HASH_BITS);
69 flist_add_tail(&log->list, &hash[h]);
75 static void gtod_log_caller(void *caller)
78 struct gtod_log *log = find_log(caller);
84 static void fio_exit fio_dump_gtod(void)
86 unsigned long total_calls = 0;
89 for (i = 0; i < HASH_SIZE; i++) {
90 struct flist_head *entry;
93 flist_for_each(entry, &hash[i]) {
94 log = flist_entry(entry, struct gtod_log, list);
96 printf("function %p, calls %lu\n", log->caller,
98 total_calls += log->calls;
102 printf("Total %lu gettimeofday\n", total_calls);
105 static void fio_init gtod_init(void)
109 for (i = 0; i < HASH_SIZE; i++)
110 INIT_FLIST_HEAD(&hash[i]);
115 #endif /* FIO_DEBUG_TIME */
117 #ifdef FIO_DEBUG_TIME
118 void fio_gettime(struct timeval *tp, void *caller)
120 void fio_gettime(struct timeval *tp, void fio_unused *caller)
123 #ifdef FIO_DEBUG_TIME
125 caller = __builtin_return_address(0);
127 gtod_log_caller(caller);
130 memcpy(tp, fio_tv, sizeof(*tp));
134 switch (fio_clock_source) {
136 gettimeofday(tp, NULL);
141 #ifdef FIO_HAVE_CLOCK_MONOTONIC
142 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
144 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
146 log_err("fio: clock_gettime fails\n");
150 tp->tv_sec = ts.tv_sec;
151 tp->tv_usec = ts.tv_nsec / 1000;
154 #ifdef ARCH_HAVE_CPU_CLOCK
156 unsigned long long usecs, t;
159 if (t < last_cycles) {
160 dprint(FD_TIME, "CPU clock going back in time\n");
164 usecs = t / cycles_per_usec;
165 tp->tv_sec = usecs / 1000000;
166 tp->tv_usec = usecs % 1000000;
172 log_err("fio: invalid clock source %d\n", fio_clock_source);
177 * If Linux is using the tsc clock on non-synced processors,
178 * sometimes time can appear to drift backwards. Fix that up.
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;
188 memcpy(&last_tv, tp, sizeof(*tp));
191 #ifdef ARCH_HAVE_CPU_CLOCK
192 static unsigned long get_cycles_per_usec(void)
195 unsigned long long c_s, c_e;
197 gettimeofday(&s, NULL);
198 c_s = get_cpu_clock();
200 unsigned long long elapsed;
202 gettimeofday(&e, NULL);
203 elapsed = utime_since(&s, &e);
204 if (elapsed >= 1280) {
205 c_e = get_cpu_clock();
210 return (c_e - c_s + 127) >> 7;
213 #define NR_TIME_ITERS 50
215 static void calibrate_cpu_clock(void)
217 double delta, mean, S;
218 unsigned long avg, cycles[NR_TIME_ITERS];
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;
227 mean += delta / (i + 1.0);
228 S += delta * (cycles[i] - mean);
232 S = sqrt(S / (NR_TIME_ITERS - 1.0));
235 for (i = 0; i < NR_TIME_ITERS; i++) {
236 double this = cycles[i];
238 if ((fmax(this, mean) - fmin(this, mean)) > S)
244 S /= (double) NR_TIME_ITERS;
247 for (i = 0; i < NR_TIME_ITERS; i++)
248 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
251 avg = (avg + 9) / 10;
252 dprint(FD_TIME, "avg: %lu\n", avg);
253 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
255 cycles_per_usec = avg;
258 static void calibrate_cpu_clock(void)
263 void fio_clock_init(void)
266 calibrate_cpu_clock();
269 * If the arch sets tsc_reliable != 0, then it must be good enough
270 * to use as THE clock source. For x86 CPUs, this means the TSC
271 * runs at a constant rate and is synced across CPU cores.
274 if (!fio_clock_source_set)
275 fio_clock_source = CS_CPUCLOCK;
276 } else if (fio_clock_source == CS_CPUCLOCK)
277 log_info("fio: clocksource=cpu may not be reliable\n");
280 unsigned long long utime_since(struct timeval *s, struct timeval *e)
283 unsigned long long ret;
285 sec = e->tv_sec - s->tv_sec;
286 usec = e->tv_usec - s->tv_usec;
287 if (sec > 0 && usec < 0) {
293 * time warp bug on some kernels?
295 if (sec < 0 || (sec == 0 && usec < 0))
298 ret = sec * 1000000ULL + usec;
303 unsigned long long utime_since_now(struct timeval *s)
307 fio_gettime(&t, NULL);
308 return utime_since(s, &t);
311 unsigned long mtime_since(struct timeval *s, struct timeval *e)
315 sec = e->tv_sec - s->tv_sec;
316 usec = e->tv_usec - s->tv_usec;
317 if (sec > 0 && usec < 0) {
322 if (sec < 0 || (sec == 0 && usec < 0))
332 unsigned long mtime_since_now(struct timeval *s)
335 void *p = __builtin_return_address(0);
338 return mtime_since(s, &t);
341 unsigned long time_since_now(struct timeval *s)
343 return mtime_since_now(s) / 1000;