15 #ifdef ARCH_HAVE_CPU_CLOCK
16 static unsigned long cycles_per_usec;
17 static unsigned long last_cycles;
19 static struct timeval last_tv;
20 static int last_tv_valid;
22 static struct timeval *fio_tv;
23 int fio_gtod_offload = 0;
24 int fio_gtod_cpu = -1;
25 static pthread_t gtod_thread;
27 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
32 #define HASH_SIZE (1 << HASH_BITS)
34 static struct flist_head hash[HASH_SIZE];
35 static int gtod_inited;
38 struct flist_head list;
43 static struct gtod_log *find_hash(void *caller)
45 unsigned long h = hash_ptr(caller, HASH_BITS);
46 struct flist_head *entry;
48 flist_for_each(entry, &hash[h]) {
49 struct gtod_log *log = flist_entry(entry, struct gtod_log,
52 if (log->caller == caller)
59 static struct gtod_log *find_log(void *caller)
61 struct gtod_log *log = find_hash(caller);
66 log = malloc(sizeof(*log));
67 INIT_FLIST_HEAD(&log->list);
71 h = hash_ptr(caller, HASH_BITS);
72 flist_add_tail(&log->list, &hash[h]);
78 static void gtod_log_caller(void *caller)
81 struct gtod_log *log = find_log(caller);
87 static void fio_exit fio_dump_gtod(void)
89 unsigned long total_calls = 0;
92 for (i = 0; i < HASH_SIZE; i++) {
93 struct flist_head *entry;
96 flist_for_each(entry, &hash[i]) {
97 log = flist_entry(entry, struct gtod_log, list);
99 printf("function %p, calls %lu\n", log->caller,
101 total_calls += log->calls;
105 printf("Total %lu gettimeofday\n", total_calls);
108 static void fio_init gtod_init(void)
112 for (i = 0; i < HASH_SIZE; i++)
113 INIT_FLIST_HEAD(&hash[i]);
118 #endif /* FIO_DEBUG_TIME */
120 #ifdef FIO_DEBUG_TIME
121 void fio_gettime(struct timeval *tp, void *caller)
123 void fio_gettime(struct timeval *tp, void fio_unused *caller)
126 #ifdef FIO_DEBUG_TIME
128 caller = __builtin_return_address(0);
130 gtod_log_caller(caller);
133 memcpy(tp, fio_tv, sizeof(*tp));
137 switch (fio_clock_source) {
139 gettimeofday(tp, NULL);
144 #ifdef FIO_HAVE_CLOCK_MONOTONIC
145 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
147 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
149 log_err("fio: clock_gettime fails\n");
153 tp->tv_sec = ts.tv_sec;
154 tp->tv_usec = ts.tv_nsec / 1000;
157 #ifdef ARCH_HAVE_CPU_CLOCK
159 unsigned long long usecs, t;
162 if (t < last_cycles) {
163 dprint(FD_TIME, "CPU clock going back in time\n");
167 usecs = t / cycles_per_usec;
168 tp->tv_sec = usecs / 1000000;
169 tp->tv_usec = usecs % 1000000;
175 log_err("fio: invalid clock source %d\n", fio_clock_source);
180 * If Linux is using the tsc clock on non-synced processors,
181 * sometimes time can appear to drift backwards. Fix that up.
184 if (tp->tv_sec < last_tv.tv_sec)
185 tp->tv_sec = last_tv.tv_sec;
186 else if (last_tv.tv_sec == tp->tv_sec &&
187 tp->tv_usec < last_tv.tv_usec)
188 tp->tv_usec = last_tv.tv_usec;
191 memcpy(&last_tv, tp, sizeof(*tp));
194 #ifdef ARCH_HAVE_CPU_CLOCK
195 static unsigned long get_cycles_per_usec(void)
198 unsigned long long c_s, c_e;
200 gettimeofday(&s, NULL);
201 c_s = get_cpu_clock();
203 unsigned long long elapsed;
205 gettimeofday(&e, NULL);
206 elapsed = utime_since(&s, &e);
208 c_e = get_cpu_clock();
216 static void calibrate_cpu_clock(void)
218 double delta, mean, S;
219 unsigned long avg, cycles[10];
222 cycles[0] = get_cycles_per_usec();
223 S = delta = mean = 0.0;
224 for (i = 0; i < 10; i++) {
225 cycles[i] = get_cycles_per_usec();
226 delta = cycles[i] - mean;
228 mean += delta / (i + 1.0);
229 S += delta * (cycles[i] - mean);
233 S = sqrt(S / (10 - 1.0));
236 for (i = 0; i < 10; i++) {
237 double this = cycles[i];
239 if ((fmax(this, mean) - fmin(this, mean)) > S)
248 for (i = 0; i < 10; i++)
249 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
251 avg /= (samples * 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;
259 static void calibrate_cpu_clock(void)
264 void fio_clock_init(void)
267 calibrate_cpu_clock();
270 void fio_gtod_init(void)
272 fio_tv = smalloc(sizeof(struct timeval));
276 static void fio_gtod_update(void)
278 gettimeofday(fio_tv, NULL);
281 static void *gtod_thread_main(void *data)
283 struct fio_mutex *mutex = data;
288 * As long as we have jobs around, update the clock. It would be nice
289 * to have some way of NOT hammering that CPU with gettimeofday(),
290 * but I'm not sure what to use outside of a simple CPU nop to relax
291 * it - we don't want to lose precision.
301 int fio_start_gtod_thread(void)
303 struct fio_mutex *mutex;
307 mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
311 pthread_attr_init(&attr);
312 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
313 ret = pthread_create(>od_thread, &attr, gtod_thread_main, NULL);
314 pthread_attr_destroy(&attr);
316 log_err("Can't create gtod thread: %s\n", strerror(ret));
320 ret = pthread_detach(gtod_thread);
322 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
326 dprint(FD_MUTEX, "wait on startup_mutex\n");
327 fio_mutex_down(mutex);
328 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
330 fio_mutex_remove(mutex);