14 #ifdef ARCH_HAVE_CPU_CLOCK
15 static unsigned long cycles_per_usec;
16 static unsigned long last_cycles;
18 static struct timeval last_tv;
19 static int last_tv_valid;
21 static struct timeval *fio_tv;
22 int fio_gtod_offload = 0;
23 int fio_gtod_cpu = -1;
25 enum fio_cs fio_clock_source = CS_GTOD;
30 #define HASH_SIZE (1 << HASH_BITS)
32 static struct flist_head hash[HASH_SIZE];
33 static int gtod_inited;
36 struct flist_head list;
41 static struct gtod_log *find_hash(void *caller)
43 unsigned long h = hash_ptr(caller, HASH_BITS);
44 struct flist_head *entry;
46 flist_for_each(entry, &hash[h]) {
47 struct gtod_log *log = flist_entry(entry, struct gtod_log,
50 if (log->caller == caller)
57 static struct gtod_log *find_log(void *caller)
59 struct gtod_log *log = find_hash(caller);
64 log = malloc(sizeof(*log));
65 INIT_FLIST_HEAD(&log->list);
69 h = hash_ptr(caller, HASH_BITS);
70 flist_add_tail(&log->list, &hash[h]);
76 static void gtod_log_caller(void *caller)
79 struct gtod_log *log = find_log(caller);
85 static void fio_exit fio_dump_gtod(void)
87 unsigned long total_calls = 0;
90 for (i = 0; i < HASH_SIZE; i++) {
91 struct flist_head *entry;
94 flist_for_each(entry, &hash[i]) {
95 log = flist_entry(entry, struct gtod_log, list);
97 printf("function %p, calls %lu\n", log->caller,
99 total_calls += log->calls;
103 printf("Total %lu gettimeofday\n", total_calls);
106 static void fio_init gtod_init(void)
110 for (i = 0; i < HASH_SIZE; i++)
111 INIT_FLIST_HEAD(&hash[i]);
116 #endif /* FIO_DEBUG_TIME */
118 #ifdef FIO_DEBUG_TIME
119 void fio_gettime(struct timeval *tp, void *caller)
121 void fio_gettime(struct timeval *tp, void fio_unused *caller)
124 #ifdef FIO_DEBUG_TIME
126 caller = __builtin_return_address(0);
128 gtod_log_caller(caller);
131 memcpy(tp, fio_tv, sizeof(*tp));
135 switch (fio_clock_source) {
137 gettimeofday(tp, NULL);
142 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
143 log_err("fio: clock_gettime fails\n");
147 tp->tv_sec = ts.tv_sec;
148 tp->tv_usec = ts.tv_nsec / 1000;
151 #ifdef ARCH_HAVE_CPU_CLOCK
153 unsigned long long usecs, t;
156 if (t < last_cycles) {
157 dprint(FD_TIME, "CPU clock going back in time\n");
161 usecs = t / cycles_per_usec;
162 tp->tv_sec = usecs / 1000000;
163 tp->tv_usec = usecs % 1000000;
169 log_err("fio: invalid clock source %d\n", fio_clock_source);
174 * If Linux is using the tsc clock on non-synced processors,
175 * sometimes time can appear to drift backwards. Fix that up.
178 if (tp->tv_sec < last_tv.tv_sec)
179 tp->tv_sec = last_tv.tv_sec;
180 else if (last_tv.tv_sec == tp->tv_sec &&
181 tp->tv_usec < last_tv.tv_usec)
182 tp->tv_usec = last_tv.tv_usec;
185 memcpy(&last_tv, tp, sizeof(*tp));
188 #ifdef ARCH_HAVE_CPU_CLOCK
189 static unsigned long get_cycles_per_usec(void)
192 unsigned long long c_s, c_e;
194 gettimeofday(&s, NULL);
195 c_s = get_cpu_clock();
197 unsigned long long elapsed;
199 gettimeofday(&e, NULL);
200 elapsed = utime_since(&s, &e);
202 c_e = get_cpu_clock();
210 static void calibrate_cpu_clock(void)
212 double delta, mean, S;
213 unsigned long avg, cycles[10];
216 cycles[0] = get_cycles_per_usec();
217 S = delta = mean = 0.0;
218 for (i = 0; i < 10; i++) {
219 cycles[i] = get_cycles_per_usec();
220 delta = cycles[i] - mean;
222 mean += delta / (i + 1.0);
223 S += delta * (cycles[i] - mean);
227 S = sqrt(S / (10 - 1.0));
230 for (i = 0; i < 10; i++) {
231 double this = cycles[i];
233 if ((max(this, mean) - min(this, mean)) > S)
242 for (i = 0; i < 10; i++)
243 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
245 avg /= (samples * 10);
246 dprint(FD_TIME, "avg: %lu\n", avg);
247 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
249 cycles_per_usec = avg;
253 static void calibrate_cpu_clock(void)
258 void fio_clock_init(void)
261 calibrate_cpu_clock();
264 void fio_gtod_init(void)
266 fio_tv = smalloc(sizeof(struct timeval));
270 void fio_gtod_update(void)
272 gettimeofday(fio_tv, NULL);