blob: 161240eeedbbcee0107bfc92a5c1fa7e94954915 [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"
9#include "os.h"
10
11#include "hash.h"
12
13static int clock_gettime_works = 1;
14
15#ifdef FIO_DEBUG_TIME
16
17#define HASH_BITS 8
18#define HASH_SIZE (1 << HASH_BITS)
19
20static struct list_head hash[HASH_SIZE];
21static int gtod_inited;
22
23struct gtod_log {
24 struct list_head list;
25 void *caller;
26 unsigned long calls;
27};
28
29static struct gtod_log *find_hash(void *caller)
30{
31 unsigned long h = hash_ptr(caller, HASH_BITS);
32 struct list_head *entry;
33
34 list_for_each(entry, &hash[h]) {
35 struct gtod_log *log = list_entry(entry, struct gtod_log, list);
36
37 if (log->caller == caller)
38 return log;
39 }
40
41 return NULL;
42}
43
44static struct gtod_log *find_log(void *caller)
45{
46 struct gtod_log *log = find_hash(caller);
47
48 if (!log) {
49 unsigned long h;
50
51 log = malloc(sizeof(*log));
52 INIT_LIST_HEAD(&log->list);
53 log->caller = caller;
54 log->calls = 0;
55
56 h = hash_ptr(caller, HASH_BITS);
57 list_add_tail(&log->list, &hash[h]);
58 }
59
60 return log;
61}
62
63static void gtod_log_caller(void *caller)
64{
65 if (gtod_inited) {
66 struct gtod_log *log = find_log(caller);
67
68 log->calls++;
69 }
70}
71
72static void fio_exit fio_dump_gtod(void)
73{
74 unsigned long total_calls = 0;
75 int i;
76
77 for (i = 0; i < HASH_SIZE; i++) {
78 struct list_head *entry;
79 struct gtod_log *log;
80
81 list_for_each(entry, &hash[i]) {
82 log = list_entry(entry, struct gtod_log, list);
83
84 printf("function %p, calls %lu\n", log->caller, log->calls);
85 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
116repeat:
117 if (!clock_gettime_works)
118 gettimeofday(tp, NULL);
119 else {
120 struct timespec ts;
121
122 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
123 clock_gettime_works = 0;
124 goto repeat;
125 }
126
127 tp->tv_sec = ts.tv_sec;
128 tp->tv_usec = ts.tv_nsec / 1000;
129 }
130}
131
132static void fio_init check_clock(void)
133{
134 struct timespec ts;
135
136 if (clock_getres(CLOCK_MONOTONIC, &ts) < 0)
137 clock_gettime_works = 0;
138}