blob: d78cf6d0ba9281a36ec286c5e6e87843fb49e360 [file] [log] [blame]
Jens Axboe02bcaa82006-11-24 10:42:00 +01001/*
Jens Axboef5cc0242006-11-24 10:47:40 +01002 * Clock functions
Jens Axboe02bcaa82006-11-24 10:42:00 +01003 */
Jens Axboef5cc0242006-11-24 10:47:40 +01004
Jens Axboe02bcaa82006-11-24 10:42:00 +01005#include <unistd.h>
6#include <sys/time.h>
7
8#include "fio.h"
Jens Axboe02bcaa82006-11-24 10:42:00 +01009
10#include "hash.h"
11
Jens Axboe5ec10ea2008-03-06 15:42:00 +010012static int clock_gettime_works;
Jens Axboe02bcaa82006-11-24 10:42:00 +010013
14#ifdef FIO_DEBUG_TIME
15
16#define HASH_BITS 8
17#define HASH_SIZE (1 << HASH_BITS)
18
19static struct list_head hash[HASH_SIZE];
20static int gtod_inited;
21
22struct gtod_log {
23 struct list_head list;
24 void *caller;
25 unsigned long calls;
26};
27
28static struct gtod_log *find_hash(void *caller)
29{
30 unsigned long h = hash_ptr(caller, HASH_BITS);
31 struct list_head *entry;
32
33 list_for_each(entry, &hash[h]) {
34 struct gtod_log *log = list_entry(entry, struct gtod_log, list);
35
36 if (log->caller == caller)
37 return log;
38 }
39
40 return NULL;
41}
42
43static struct gtod_log *find_log(void *caller)
44{
45 struct gtod_log *log = find_hash(caller);
46
47 if (!log) {
48 unsigned long h;
49
50 log = malloc(sizeof(*log));
51 INIT_LIST_HEAD(&log->list);
52 log->caller = caller;
53 log->calls = 0;
54
55 h = hash_ptr(caller, HASH_BITS);
56 list_add_tail(&log->list, &hash[h]);
57 }
58
59 return log;
60}
61
62static void gtod_log_caller(void *caller)
63{
64 if (gtod_inited) {
65 struct gtod_log *log = find_log(caller);
66
67 log->calls++;
68 }
69}
70
71static void fio_exit fio_dump_gtod(void)
72{
73 unsigned long total_calls = 0;
74 int i;
75
76 for (i = 0; i < HASH_SIZE; i++) {
77 struct list_head *entry;
78 struct gtod_log *log;
79
80 list_for_each(entry, &hash[i]) {
81 log = list_entry(entry, struct gtod_log, list);
82
Jens Axboe5ec10ea2008-03-06 15:42:00 +010083 printf("function %p, calls %lu\n", log->caller,
84 log->calls);
Jens Axboe02bcaa82006-11-24 10:42:00 +010085 total_calls += log->calls;
86 }
87 }
88
89 printf("Total %lu gettimeofday\n", total_calls);
90}
91
92static void fio_init gtod_init(void)
93{
94 int i;
95
96 for (i = 0; i < HASH_SIZE; i++)
97 INIT_LIST_HEAD(&hash[i]);
98
99 gtod_inited = 1;
100}
101
102#endif /* FIO_DEBUG_TIME */
103
Jens Axboe1e97cce2006-12-05 11:44:16 +0100104#ifdef FIO_DEBUG_TIME
Jens Axboe02bcaa82006-11-24 10:42:00 +0100105void fio_gettime(struct timeval *tp, void *caller)
Jens Axboe1e97cce2006-12-05 11:44:16 +0100106#else
107void fio_gettime(struct timeval *tp, void fio_unused *caller)
108#endif
Jens Axboe02bcaa82006-11-24 10:42:00 +0100109{
110#ifdef FIO_DEBUG_TIME
111 if (!caller)
112 caller = __builtin_return_address(0);
113
114 gtod_log_caller(caller);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100115#endif
Jens Axboe7e326f32007-01-13 15:17:22 +0100116 if (!clock_gettime_works) {
117gtod:
Jens Axboe02bcaa82006-11-24 10:42:00 +0100118 gettimeofday(tp, NULL);
Jens Axboe7e326f32007-01-13 15:17:22 +0100119 } else {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100120 struct timespec ts;
121
122 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
123 clock_gettime_works = 0;
Jens Axboe7e326f32007-01-13 15:17:22 +0100124 goto gtod;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100125 }
126
127 tp->tv_sec = ts.tv_sec;
128 tp->tv_usec = ts.tv_nsec / 1000;
129 }
130}