Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 1 | /* |
Jens Axboe | f5cc024 | 2006-11-24 10:47:40 +0100 | [diff] [blame] | 2 | * Clock functions |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 3 | */ |
Jens Axboe | f5cc024 | 2006-11-24 10:47:40 +0100 | [diff] [blame] | 4 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | #include <sys/time.h> |
| 7 | |
| 8 | #include "fio.h" |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 9 | #include "smalloc.h" |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 10 | |
| 11 | #include "hash.h" |
| 12 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 13 | static int clock_gettime_works; |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 14 | static struct timeval last_tv; |
| 15 | static int last_tv_valid; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 16 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 17 | static struct timeval *fio_tv; |
| 18 | int fio_gtod_offload = 0; |
| 19 | int fio_gtod_cpu = -1; |
| 20 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 21 | #ifdef FIO_DEBUG_TIME |
| 22 | |
| 23 | #define HASH_BITS 8 |
| 24 | #define HASH_SIZE (1 << HASH_BITS) |
| 25 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 26 | static struct flist_head hash[HASH_SIZE]; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 27 | static int gtod_inited; |
| 28 | |
| 29 | struct gtod_log { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 30 | struct flist_head list; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 31 | void *caller; |
| 32 | unsigned long calls; |
| 33 | }; |
| 34 | |
| 35 | static struct gtod_log *find_hash(void *caller) |
| 36 | { |
| 37 | unsigned long h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 38 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 39 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 40 | flist_for_each(entry, &hash[h]) { |
| 41 | struct gtod_log *log = flist_entry(entry, struct gtod_log, |
| 42 | list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 43 | |
| 44 | if (log->caller == caller) |
| 45 | return log; |
| 46 | } |
| 47 | |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | static struct gtod_log *find_log(void *caller) |
| 52 | { |
| 53 | struct gtod_log *log = find_hash(caller); |
| 54 | |
| 55 | if (!log) { |
| 56 | unsigned long h; |
| 57 | |
| 58 | log = malloc(sizeof(*log)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 59 | INIT_FLIST_HEAD(&log->list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 60 | log->caller = caller; |
| 61 | log->calls = 0; |
| 62 | |
| 63 | h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 64 | flist_add_tail(&log->list, &hash[h]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return log; |
| 68 | } |
| 69 | |
| 70 | static void gtod_log_caller(void *caller) |
| 71 | { |
| 72 | if (gtod_inited) { |
| 73 | struct gtod_log *log = find_log(caller); |
| 74 | |
| 75 | log->calls++; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void fio_exit fio_dump_gtod(void) |
| 80 | { |
| 81 | unsigned long total_calls = 0; |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; i < HASH_SIZE; i++) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 85 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 86 | struct gtod_log *log; |
| 87 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 88 | flist_for_each(entry, &hash[i]) { |
| 89 | log = flist_entry(entry, struct gtod_log, list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 90 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 91 | printf("function %p, calls %lu\n", log->caller, |
| 92 | log->calls); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 93 | total_calls += log->calls; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | printf("Total %lu gettimeofday\n", total_calls); |
| 98 | } |
| 99 | |
| 100 | static void fio_init gtod_init(void) |
| 101 | { |
| 102 | int i; |
| 103 | |
| 104 | for (i = 0; i < HASH_SIZE; i++) |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 105 | INIT_FLIST_HEAD(&hash[i]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 106 | |
| 107 | gtod_inited = 1; |
| 108 | } |
| 109 | |
| 110 | #endif /* FIO_DEBUG_TIME */ |
| 111 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 112 | #ifdef FIO_DEBUG_TIME |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 113 | void fio_gettime(struct timeval *tp, void *caller) |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 114 | #else |
| 115 | void fio_gettime(struct timeval *tp, void fio_unused *caller) |
| 116 | #endif |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 117 | { |
| 118 | #ifdef FIO_DEBUG_TIME |
| 119 | if (!caller) |
| 120 | caller = __builtin_return_address(0); |
| 121 | |
| 122 | gtod_log_caller(caller); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 123 | #endif |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 124 | if (fio_tv) { |
| 125 | memcpy(tp, fio_tv, sizeof(*tp)); |
| 126 | return; |
| 127 | } else if (!clock_gettime_works) { |
Jens Axboe | 7e326f3 | 2007-01-13 15:17:22 +0100 | [diff] [blame] | 128 | gtod: |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 129 | gettimeofday(tp, NULL); |
Jens Axboe | 7e326f3 | 2007-01-13 15:17:22 +0100 | [diff] [blame] | 130 | } else { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 131 | struct timespec ts; |
| 132 | |
| 133 | if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) { |
| 134 | clock_gettime_works = 0; |
Jens Axboe | 7e326f3 | 2007-01-13 15:17:22 +0100 | [diff] [blame] | 135 | goto gtod; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | tp->tv_sec = ts.tv_sec; |
| 139 | tp->tv_usec = ts.tv_nsec / 1000; |
| 140 | } |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * If Linux is using the tsc clock on non-synced processors, |
| 144 | * sometimes time can appear to drift backwards. Fix that up. |
| 145 | */ |
| 146 | if (last_tv_valid) { |
| 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; |
| 152 | } |
| 153 | last_tv_valid = 1; |
| 154 | memcpy(&last_tv, tp, sizeof(*tp)); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 155 | } |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 156 | |
| 157 | void fio_gtod_init(void) |
| 158 | { |
| 159 | fio_tv = smalloc(sizeof(struct timeval)); |
| 160 | assert(fio_tv); |
| 161 | } |
| 162 | |
| 163 | void fio_gtod_update(void) |
| 164 | { |
| 165 | gettimeofday(fio_tv, NULL); |
| 166 | } |