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;
26 enum fio_cs fio_clock_source = CS_GTOD;
31 #define HASH_SIZE (1 << HASH_BITS)
33 static struct flist_head hash[HASH_SIZE];
34 static int gtod_inited;
37 struct flist_head list;
42 static struct gtod_log *find_hash(void *caller)
44 unsigned long h = hash_ptr(caller, HASH_BITS);
45 struct flist_head *entry;
47 flist_for_each(entry, &hash[h]) {
48 struct gtod_log *log = flist_entry(entry, struct gtod_log,
51 if (log->caller == caller)
58 static struct gtod_log *find_log(void *caller)
60 struct gtod_log *log = find_hash(caller);
65 log = malloc(sizeof(*log));
66 INIT_FLIST_HEAD(&log->list);
70 h = hash_ptr(caller, HASH_BITS);
71 flist_add_tail(&log->list, &hash[h]);
77 static void gtod_log_caller(void *caller)
80 struct gtod_log *log = find_log(caller);
86 static void fio_exit fio_dump_gtod(void)
88 unsigned long total_calls = 0;
91 for (i = 0; i < HASH_SIZE; i++) {
92 struct flist_head *entry;
95 flist_for_each(entry, &hash[i]) {
96 log = flist_entry(entry, struct gtod_log, list);
98 printf("function %p, calls %lu\n", log->caller,
100 total_calls += log->calls;
104 printf("Total %lu gettimeofday\n", total_calls);
107 static void fio_init gtod_init(void)
111 for (i = 0; i < HASH_SIZE; i++)
112 INIT_FLIST_HEAD(&hash[i]);
117 #endif /* FIO_DEBUG_TIME */
119 #ifdef FIO_DEBUG_TIME
120 void fio_gettime(struct timeval *tp, void *caller)
122 void fio_gettime(struct timeval *tp, void fio_unused *caller)
125 #ifdef FIO_DEBUG_TIME
127 caller = __builtin_return_address(0);
129 gtod_log_caller(caller);
132 memcpy(tp, fio_tv, sizeof(*tp));
136 switch (fio_clock_source) {
138 gettimeofday(tp, NULL);
143 #ifdef FIO_HAVE_CLOCK_MONOTONIC
144 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
146 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
148 log_err("fio: clock_gettime fails\n");
152 tp->tv_sec = ts.tv_sec;
153 tp->tv_usec = ts.tv_nsec / 1000;
156 #ifdef ARCH_HAVE_CPU_CLOCK
158 unsigned long long usecs, t;
161 if (t < last_cycles) {
162 dprint(FD_TIME, "CPU clock going back in time\n");
166 usecs = t / cycles_per_usec;
167 tp->tv_sec = usecs / 1000000;
168 tp->tv_usec = usecs % 1000000;
174 log_err("fio: invalid clock source %d\n", fio_clock_source);
179 * If Linux is using the tsc clock on non-synced processors,
180 * sometimes time can appear to drift backwards. Fix that up.
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;
190 memcpy(&last_tv, tp, sizeof(*tp));
193 #ifdef ARCH_HAVE_CPU_CLOCK
194 static unsigned long get_cycles_per_usec(void)
197 unsigned long long c_s, c_e;
199 gettimeofday(&s, NULL);
200 c_s = get_cpu_clock();
202 unsigned long long elapsed;
204 gettimeofday(&e, NULL);
205 elapsed = utime_since(&s, &e);
207 c_e = get_cpu_clock();
215 static void calibrate_cpu_clock(void)
217 double delta, mean, S;
218 unsigned long avg, cycles[10];
221 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;
227 mean += delta / (i + 1.0);
228 S += delta * (cycles[i] - mean);
232 S = sqrt(S / (10 - 1.0));
235 for (i = 0; i < 10; i++) {
236 double this = cycles[i];
238 if ((fmax(this, mean) - fmin(this, mean)) > S)
247 for (i = 0; i < 10; i++)
248 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
250 avg /= (samples * 10);
251 dprint(FD_TIME, "avg: %lu\n", avg);
252 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
254 cycles_per_usec = avg;
258 static void calibrate_cpu_clock(void)
263 void fio_clock_init(void)
266 calibrate_cpu_clock();
269 void fio_gtod_init(void)
271 fio_tv = smalloc(sizeof(struct timeval));
275 void fio_gtod_update(void)
277 gettimeofday(fio_tv, NULL);