14 static unsigned long cycles_per_usec;
15 static struct timeval last_tv;
16 static unsigned long last_cycles;
17 static int last_tv_valid;
19 static struct timeval *fio_tv;
20 int fio_gtod_offload = 0;
21 int fio_gtod_cpu = -1;
23 enum fio_cs fio_clock_source = CS_GTOD;
28 #define HASH_SIZE (1 << HASH_BITS)
30 static struct flist_head hash[HASH_SIZE];
31 static int gtod_inited;
34 struct flist_head list;
39 static struct gtod_log *find_hash(void *caller)
41 unsigned long h = hash_ptr(caller, HASH_BITS);
42 struct flist_head *entry;
44 flist_for_each(entry, &hash[h]) {
45 struct gtod_log *log = flist_entry(entry, struct gtod_log,
48 if (log->caller == caller)
55 static struct gtod_log *find_log(void *caller)
57 struct gtod_log *log = find_hash(caller);
62 log = malloc(sizeof(*log));
63 INIT_FLIST_HEAD(&log->list);
67 h = hash_ptr(caller, HASH_BITS);
68 flist_add_tail(&log->list, &hash[h]);
74 static void gtod_log_caller(void *caller)
77 struct gtod_log *log = find_log(caller);
83 static void fio_exit fio_dump_gtod(void)
85 unsigned long total_calls = 0;
88 for (i = 0; i < HASH_SIZE; i++) {
89 struct flist_head *entry;
92 flist_for_each(entry, &hash[i]) {
93 log = flist_entry(entry, struct gtod_log, list);
95 printf("function %p, calls %lu\n", log->caller,
97 total_calls += log->calls;
101 printf("Total %lu gettimeofday\n", total_calls);
104 static void fio_init gtod_init(void)
108 for (i = 0; i < HASH_SIZE; i++)
109 INIT_FLIST_HEAD(&hash[i]);
114 #endif /* FIO_DEBUG_TIME */
116 #ifdef FIO_DEBUG_TIME
117 void fio_gettime(struct timeval *tp, void *caller)
119 void fio_gettime(struct timeval *tp, void fio_unused *caller)
122 #ifdef FIO_DEBUG_TIME
124 caller = __builtin_return_address(0);
126 gtod_log_caller(caller);
129 memcpy(tp, fio_tv, sizeof(*tp));
133 switch (fio_clock_source) {
135 gettimeofday(tp, NULL);
140 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
141 log_err("fio: clock_gettime fails\n");
145 tp->tv_sec = ts.tv_sec;
146 tp->tv_usec = ts.tv_nsec / 1000;
149 #ifdef ARCH_HAVE_CPU_CLOCK
151 unsigned long long usecs, t;
154 if (t < last_cycles) {
155 dprint(FD_TIME, "CPU clock going back in time\n");
159 usecs = t / cycles_per_usec;
160 tp->tv_sec = usecs / 1000000;
161 tp->tv_usec = usecs % 1000000;
167 log_err("fio: invalid clock source %d\n", fio_clock_source);
172 * If Linux is using the tsc clock on non-synced processors,
173 * sometimes time can appear to drift backwards. Fix that up.
176 if (tp->tv_sec < last_tv.tv_sec)
177 tp->tv_sec = last_tv.tv_sec;
178 else if (last_tv.tv_sec == tp->tv_sec &&
179 tp->tv_usec < last_tv.tv_usec)
180 tp->tv_usec = last_tv.tv_usec;
183 memcpy(&last_tv, tp, sizeof(*tp));
186 static unsigned long get_cycles_per_usec(void)
189 unsigned long long c_s, c_e;
191 gettimeofday(&s, NULL);
192 c_s = get_cpu_clock();
194 unsigned long long elapsed;
196 gettimeofday(&e, NULL);
197 elapsed = utime_since(&s, &e);
199 c_e = get_cpu_clock();
207 void fio_clock_init(void)
209 double delta, mean, S;
210 unsigned long avg, cycles[10];
215 cycles[0] = get_cycles_per_usec();
216 S = delta = mean = 0.0;
217 for (i = 0; i < 10; i++) {
218 cycles[i] = get_cycles_per_usec();
219 delta = cycles[i] - mean;
221 mean += delta / (i + 1.0);
222 S += delta * (cycles[i] - mean);
226 S = sqrt(S / (10 - 1.0));
229 for (i = 0; i < 10; i++) {
230 double this = cycles[i];
232 if ((max(this, mean) - min(this, mean)) > S)
241 for (i = 0; i < 10; i++)
242 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
244 avg /= (samples * 10);
245 dprint(FD_TIME, "avg: %lu\n", avg);
246 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
248 cycles_per_usec = avg;
251 void fio_gtod_init(void)
253 fio_tv = smalloc(sizeof(struct timeval));
257 void fio_gtod_update(void)
259 gettimeofday(fio_tv, NULL);