12 static int clock_gettime_works;
13 static struct timeval last_tv;
14 static int last_tv_valid;
19 #define HASH_SIZE (1 << HASH_BITS)
21 static struct flist_head hash[HASH_SIZE];
22 static int gtod_inited;
25 struct flist_head list;
30 static struct gtod_log *find_hash(void *caller)
32 unsigned long h = hash_ptr(caller, HASH_BITS);
33 struct flist_head *entry;
35 flist_for_each(entry, &hash[h]) {
36 struct gtod_log *log = flist_entry(entry, struct gtod_log,
39 if (log->caller == caller)
46 static struct gtod_log *find_log(void *caller)
48 struct gtod_log *log = find_hash(caller);
53 log = malloc(sizeof(*log));
54 INIT_FLIST_HEAD(&log->list);
58 h = hash_ptr(caller, HASH_BITS);
59 flist_add_tail(&log->list, &hash[h]);
65 static void gtod_log_caller(void *caller)
68 struct gtod_log *log = find_log(caller);
74 static void fio_exit fio_dump_gtod(void)
76 unsigned long total_calls = 0;
79 for (i = 0; i < HASH_SIZE; i++) {
80 struct flist_head *entry;
83 flist_for_each(entry, &hash[i]) {
84 log = flist_entry(entry, struct gtod_log, list);
86 printf("function %p, calls %lu\n", log->caller,
88 total_calls += log->calls;
92 printf("Total %lu gettimeofday\n", total_calls);
95 static void fio_init gtod_init(void)
99 for (i = 0; i < HASH_SIZE; i++)
100 INIT_FLIST_HEAD(&hash[i]);
105 #endif /* FIO_DEBUG_TIME */
107 #ifdef FIO_DEBUG_TIME
108 void fio_gettime(struct timeval *tp, void *caller)
110 void fio_gettime(struct timeval *tp, void fio_unused *caller)
113 #ifdef FIO_DEBUG_TIME
115 caller = __builtin_return_address(0);
117 gtod_log_caller(caller);
119 if (!clock_gettime_works) {
121 gettimeofday(tp, NULL);
125 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
126 clock_gettime_works = 0;
130 tp->tv_sec = ts.tv_sec;
131 tp->tv_usec = ts.tv_nsec / 1000;
135 * If Linux is using the tsc clock on non-synced processors,
136 * sometimes time can appear to drift backwards. Fix that up.
139 if (tp->tv_sec < last_tv.tv_sec)
140 tp->tv_sec = last_tv.tv_sec;
141 else if (last_tv.tv_sec == tp->tv_sec &&
142 tp->tv_usec < last_tv.tv_usec)
143 tp->tv_usec = last_tv.tv_usec;
146 memcpy(&last_tv, tp, sizeof(*tp));