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 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
27 #define HASH_SIZE (1 << HASH_BITS)
29 static struct flist_head hash[HASH_SIZE];
30 static int gtod_inited;
33 struct flist_head list;
38 static struct gtod_log *find_hash(void *caller)
40 unsigned long h = hash_ptr(caller, HASH_BITS);
41 struct flist_head *entry;
43 flist_for_each(entry, &hash[h]) {
44 struct gtod_log *log = flist_entry(entry, struct gtod_log,
47 if (log->caller == caller)
54 static struct gtod_log *find_log(void *caller)
56 struct gtod_log *log = find_hash(caller);
61 log = malloc(sizeof(*log));
62 INIT_FLIST_HEAD(&log->list);
66 h = hash_ptr(caller, HASH_BITS);
67 flist_add_tail(&log->list, &hash[h]);
73 static void gtod_log_caller(void *caller)
76 struct gtod_log *log = find_log(caller);
82 static void fio_exit fio_dump_gtod(void)
84 unsigned long total_calls = 0;
87 for (i = 0; i < HASH_SIZE; i++) {
88 struct flist_head *entry;
91 flist_for_each(entry, &hash[i]) {
92 log = flist_entry(entry, struct gtod_log, list);
94 printf("function %p, calls %lu\n", log->caller,
96 total_calls += log->calls;
100 printf("Total %lu gettimeofday\n", total_calls);
103 static void fio_init gtod_init(void)
107 for (i = 0; i < HASH_SIZE; i++)
108 INIT_FLIST_HEAD(&hash[i]);
113 #endif /* FIO_DEBUG_TIME */
115 #ifdef FIO_DEBUG_TIME
116 void fio_gettime(struct timeval *tp, void *caller)
118 void fio_gettime(struct timeval *tp, void fio_unused *caller)
121 #ifdef FIO_DEBUG_TIME
123 caller = __builtin_return_address(0);
125 gtod_log_caller(caller);
128 memcpy(tp, fio_tv, sizeof(*tp));
132 switch (fio_clock_source) {
134 gettimeofday(tp, NULL);
139 #ifdef FIO_HAVE_CLOCK_MONOTONIC
140 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
142 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 unsigned long long utime_since(struct timeval *s, struct timeval *e)
268 unsigned long long ret;
270 sec = e->tv_sec - s->tv_sec;
271 usec = e->tv_usec - s->tv_usec;
272 if (sec > 0 && usec < 0) {
278 * time warp bug on some kernels?
280 if (sec < 0 || (sec == 0 && usec < 0))
283 ret = sec * 1000000ULL + usec;
288 unsigned long long utime_since_now(struct timeval *s)
292 fio_gettime(&t, NULL);
293 return utime_since(s, &t);
296 unsigned long mtime_since(struct timeval *s, struct timeval *e)
300 sec = e->tv_sec - s->tv_sec;
301 usec = e->tv_usec - s->tv_usec;
302 if (sec > 0 && usec < 0) {
307 if (sec < 0 || (sec == 0 && usec < 0))
317 unsigned long mtime_since_now(struct timeval *s)
320 void *p = __builtin_return_address(0);
323 return mtime_since(s, &t);
326 unsigned long time_since_now(struct timeval *s)
328 return mtime_since_now(s) / 1000;