13 static int clock_gettime_works;
14 static struct timeval last_tv;
15 static int last_tv_valid;
17 static struct timeval *fio_tv;
18 int fio_gtod_offload = 0;
19 int fio_gtod_cpu = -1;
24 #define HASH_SIZE (1 << HASH_BITS)
26 static struct flist_head hash[HASH_SIZE];
27 static int gtod_inited;
30 struct flist_head list;
35 static struct gtod_log *find_hash(void *caller)
37 unsigned long h = hash_ptr(caller, HASH_BITS);
38 struct flist_head *entry;
40 flist_for_each(entry, &hash[h]) {
41 struct gtod_log *log = flist_entry(entry, struct gtod_log,
44 if (log->caller == caller)
51 static struct gtod_log *find_log(void *caller)
53 struct gtod_log *log = find_hash(caller);
58 log = malloc(sizeof(*log));
59 INIT_FLIST_HEAD(&log->list);
63 h = hash_ptr(caller, HASH_BITS);
64 flist_add_tail(&log->list, &hash[h]);
70 static void gtod_log_caller(void *caller)
73 struct gtod_log *log = find_log(caller);
79 static void fio_exit fio_dump_gtod(void)
81 unsigned long total_calls = 0;
84 for (i = 0; i < HASH_SIZE; i++) {
85 struct flist_head *entry;
88 flist_for_each(entry, &hash[i]) {
89 log = flist_entry(entry, struct gtod_log, list);
91 printf("function %p, calls %lu\n", log->caller,
93 total_calls += log->calls;
97 printf("Total %lu gettimeofday\n", total_calls);
100 static void fio_init gtod_init(void)
104 for (i = 0; i < HASH_SIZE; i++)
105 INIT_FLIST_HEAD(&hash[i]);
110 #endif /* FIO_DEBUG_TIME */
112 #ifdef FIO_DEBUG_TIME
113 void fio_gettime(struct timeval *tp, void *caller)
115 void fio_gettime(struct timeval *tp, void fio_unused *caller)
118 #ifdef FIO_DEBUG_TIME
120 caller = __builtin_return_address(0);
122 gtod_log_caller(caller);
125 memcpy(tp, fio_tv, sizeof(*tp));
127 } else if (!clock_gettime_works) {
129 gettimeofday(tp, NULL);
133 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
134 clock_gettime_works = 0;
138 tp->tv_sec = ts.tv_sec;
139 tp->tv_usec = ts.tv_nsec / 1000;
143 * If Linux is using the tsc clock on non-synced processors,
144 * sometimes time can appear to drift backwards. Fix that up.
147 if (tp->tv_sec < last_tv.tv_sec)
148 tp->tv_sec = last_tv.tv_sec;
149 else if (last_tv.tv_sec == tp->tv_sec &&
150 tp->tv_usec < last_tv.tv_usec)
151 tp->tv_usec = last_tv.tv_usec;
154 memcpy(&last_tv, tp, sizeof(*tp));
157 void fio_gtod_init(void)
159 fio_tv = smalloc(sizeof(struct timeval));
163 void fio_gtod_update(void)
165 gettimeofday(fio_tv, NULL);