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