blob: 2753faecb02d0596b1898346a46bf7f1d9e44873 [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
Jens Axboe01743ee2008-06-02 12:19:19 +020019static struct flist_head hash[HASH_SIZE];
Jens Axboe02bcaa82006-11-24 10:42:00 +010020static int gtod_inited;
21
22struct gtod_log {
Jens Axboe01743ee2008-06-02 12:19:19 +020023 struct flist_head list;
Jens Axboe02bcaa82006-11-24 10:42:00 +010024 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);
Jens Axboe01743ee2008-06-02 12:19:19 +020031 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010032
Jens Axboe01743ee2008-06-02 12:19:19 +020033 flist_for_each(entry, &hash[h]) {
34 struct gtod_log *log = flist_entry(entry, struct gtod_log,
35 list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010036
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));
Jens Axboe01743ee2008-06-02 12:19:19 +020052 INIT_FLIST_HEAD(&log->list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010053 log->caller = caller;
54 log->calls = 0;
55
56 h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020057 flist_add_tail(&log->list, &hash[h]);
Jens Axboe02bcaa82006-11-24 10:42:00 +010058 }
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++) {
Jens Axboe01743ee2008-06-02 12:19:19 +020078 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010079 struct gtod_log *log;
80
Jens Axboe01743ee2008-06-02 12:19:19 +020081 flist_for_each(entry, &hash[i]) {
82 log = flist_entry(entry, struct gtod_log, list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010083
Jens Axboe5ec10ea2008-03-06 15:42:00 +010084 printf("function %p, calls %lu\n", log->caller,
85 log->calls);
Jens Axboe02bcaa82006-11-24 10:42:00 +010086 total_calls += log->calls;
87 }
88 }
89
90 printf("Total %lu gettimeofday\n", total_calls);
91}
92
93static void fio_init gtod_init(void)
94{
95 int i;
96
97 for (i = 0; i < HASH_SIZE; i++)
Jens Axboe01743ee2008-06-02 12:19:19 +020098 INIT_FLIST_HEAD(&hash[i]);
Jens Axboe02bcaa82006-11-24 10:42:00 +010099
100 gtod_inited = 1;
101}
102
103#endif /* FIO_DEBUG_TIME */
104
Jens Axboe1e97cce2006-12-05 11:44:16 +0100105#ifdef FIO_DEBUG_TIME
Jens Axboe02bcaa82006-11-24 10:42:00 +0100106void fio_gettime(struct timeval *tp, void *caller)
Jens Axboe1e97cce2006-12-05 11:44:16 +0100107#else
108void fio_gettime(struct timeval *tp, void fio_unused *caller)
109#endif
Jens Axboe02bcaa82006-11-24 10:42:00 +0100110{
111#ifdef FIO_DEBUG_TIME
112 if (!caller)
113 caller = __builtin_return_address(0);
114
115 gtod_log_caller(caller);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100116#endif
Jens Axboe7e326f32007-01-13 15:17:22 +0100117 if (!clock_gettime_works) {
118gtod:
Jens Axboe02bcaa82006-11-24 10:42:00 +0100119 gettimeofday(tp, NULL);
Jens Axboe7e326f32007-01-13 15:17:22 +0100120 } else {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100121 struct timespec ts;
122
123 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
124 clock_gettime_works = 0;
Jens Axboe7e326f32007-01-13 15:17:22 +0100125 goto gtod;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100126 }
127
128 tp->tv_sec = ts.tv_sec;
129 tp->tv_usec = ts.tv_nsec / 1000;
130 }
131}