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 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
144 log_err("fio: clock_gettime fails\n");
148 tp->tv_sec = ts.tv_sec;
149 tp->tv_usec = ts.tv_nsec / 1000;
152 #ifdef ARCH_HAVE_CPU_CLOCK
154 unsigned long long usecs, t;
157 if (t < last_cycles) {
158 dprint(FD_TIME, "CPU clock going back in time\n");
162 usecs = t / cycles_per_usec;
163 tp->tv_sec = usecs / 1000000;
164 tp->tv_usec = usecs % 1000000;
170 log_err("fio: invalid clock source %d\n", fio_clock_source);
175 * If Linux is using the tsc clock on non-synced processors,
176 * sometimes time can appear to drift backwards. Fix that up.
179 if (tp->tv_sec < last_tv.tv_sec)
180 tp->tv_sec = last_tv.tv_sec;
181 else if (last_tv.tv_sec == tp->tv_sec &&
182 tp->tv_usec < last_tv.tv_usec)
183 tp->tv_usec = last_tv.tv_usec;
186 memcpy(&last_tv, tp, sizeof(*tp));
189 #ifdef ARCH_HAVE_CPU_CLOCK
190 static unsigned long get_cycles_per_usec(void)
193 unsigned long long c_s, c_e;
195 gettimeofday(&s, NULL);
196 c_s = get_cpu_clock();
198 unsigned long long elapsed;
200 gettimeofday(&e, NULL);
201 elapsed = utime_since(&s, &e);
203 c_e = get_cpu_clock();
211 static void calibrate_cpu_clock(void)
213 double delta, mean, S;
214 unsigned long avg, cycles[10];
217 cycles[0] = get_cycles_per_usec();
218 S = delta = mean = 0.0;
219 for (i = 0; i < 10; i++) {
220 cycles[i] = get_cycles_per_usec();
221 delta = cycles[i] - mean;
223 mean += delta / (i + 1.0);
224 S += delta * (cycles[i] - mean);
228 S = sqrt(S / (10 - 1.0));
231 for (i = 0; i < 10; i++) {
232 double this = cycles[i];
234 if ((fmax(this, mean) - fmin(this, mean)) > S)
243 for (i = 0; i < 10; i++)
244 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
246 avg /= (samples * 10);
247 dprint(FD_TIME, "avg: %lu\n", avg);
248 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
250 cycles_per_usec = avg;
254 static void calibrate_cpu_clock(void)
259 void fio_clock_init(void)
262 calibrate_cpu_clock();
265 void fio_gtod_init(void)
267 fio_tv = smalloc(sizeof(struct timeval));
271 void fio_gtod_update(void)
273 gettimeofday(fio_tv, NULL);