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> |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 6 | #include <math.h> |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 7 | #include <sys/time.h> |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 8 | #include <time.h> |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 9 | |
| 10 | #include "fio.h" |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 11 | #include "smalloc.h" |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 12 | |
| 13 | #include "hash.h" |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 14 | #include "os/os.h" |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 15 | |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 16 | #if defined(ARCH_HAVE_CPU_CLOCK) |
| 17 | #ifndef ARCH_CPU_CLOCK_CYCLES_PER_USEC |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 18 | static unsigned long cycles_per_usec; |
Jens Axboe | 7133911 | 2012-12-21 22:54:56 +0100 | [diff] [blame] | 19 | static unsigned long inv_cycles_per_usec; |
Jens Axboe | 400531d | 2014-12-16 22:40:37 -0700 | [diff] [blame] | 20 | static uint64_t max_cycles_for_mult; |
Christian Ehrhardt | bbc1f06 | 2015-02-27 08:20:46 -0700 | [diff] [blame] | 21 | #endif |
| 22 | #ifdef ARCH_CPU_CLOCK_WRAPS |
Jens Axboe | 1e62c3f | 2014-12-17 08:51:23 -0700 | [diff] [blame] | 23 | static unsigned long long cycles_start, cycles_wrap; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 24 | #endif |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 25 | #endif |
Jens Axboe | 4de98eb | 2013-01-01 10:59:04 +0100 | [diff] [blame] | 26 | int tsc_reliable = 0; |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 27 | |
| 28 | struct tv_valid { |
Jens Axboe | ba458c2 | 2013-01-21 05:38:22 -0700 | [diff] [blame] | 29 | uint64_t last_cycles; |
Jens Axboe | 02c7e29 | 2014-12-16 15:37:25 -0700 | [diff] [blame] | 30 | int last_tv_valid; |
| 31 | int warned; |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 32 | }; |
Jens Axboe | d8fc0cd | 2014-12-17 09:08:01 -0700 | [diff] [blame] | 33 | #ifdef ARCH_HAVE_CPU_CLOCK |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 34 | #ifdef CONFIG_TLS_THREAD |
Jens Axboe | b4ea84d | 2013-04-11 14:20:33 +0200 | [diff] [blame] | 35 | static __thread struct tv_valid static_tv_valid; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 36 | #else |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 37 | static pthread_key_t tv_tls_key; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 38 | #endif |
Jens Axboe | d8fc0cd | 2014-12-17 09:08:01 -0700 | [diff] [blame] | 39 | #endif |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 40 | |
Bruce Cran | 16de1bf | 2012-02-20 17:07:32 +0000 | [diff] [blame] | 41 | enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 42 | int fio_clock_source_set = 0; |
Jens Axboe | 10aa136 | 2014-04-01 21:10:36 -0600 | [diff] [blame] | 43 | static enum fio_cs fio_clock_source_inited = CS_INVAL; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 44 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 45 | #ifdef FIO_DEBUG_TIME |
| 46 | |
| 47 | #define HASH_BITS 8 |
| 48 | #define HASH_SIZE (1 << HASH_BITS) |
| 49 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 50 | static struct flist_head hash[HASH_SIZE]; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 51 | static int gtod_inited; |
| 52 | |
| 53 | struct gtod_log { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 54 | struct flist_head list; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 55 | void *caller; |
| 56 | unsigned long calls; |
| 57 | }; |
| 58 | |
| 59 | static struct gtod_log *find_hash(void *caller) |
| 60 | { |
| 61 | unsigned long h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 62 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 63 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 64 | flist_for_each(entry, &hash[h]) { |
| 65 | struct gtod_log *log = flist_entry(entry, struct gtod_log, |
| 66 | list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 67 | |
| 68 | if (log->caller == caller) |
| 69 | return log; |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Jens Axboe | 5194e34 | 2014-12-16 23:03:54 -0700 | [diff] [blame] | 75 | static void inc_caller(void *caller) |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 76 | { |
| 77 | struct gtod_log *log = find_hash(caller); |
| 78 | |
| 79 | if (!log) { |
| 80 | unsigned long h; |
| 81 | |
| 82 | log = malloc(sizeof(*log)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 83 | INIT_FLIST_HEAD(&log->list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 84 | log->caller = caller; |
| 85 | log->calls = 0; |
| 86 | |
| 87 | h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 88 | flist_add_tail(&log->list, &hash[h]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Jens Axboe | 5194e34 | 2014-12-16 23:03:54 -0700 | [diff] [blame] | 91 | log->calls++; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void gtod_log_caller(void *caller) |
| 95 | { |
Jens Axboe | 5194e34 | 2014-12-16 23:03:54 -0700 | [diff] [blame] | 96 | if (gtod_inited) |
| 97 | inc_caller(caller); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void fio_exit fio_dump_gtod(void) |
| 101 | { |
| 102 | unsigned long total_calls = 0; |
| 103 | int i; |
| 104 | |
| 105 | for (i = 0; i < HASH_SIZE; i++) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 106 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 107 | struct gtod_log *log; |
| 108 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 109 | flist_for_each(entry, &hash[i]) { |
| 110 | log = flist_entry(entry, struct gtod_log, list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 111 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 112 | printf("function %p, calls %lu\n", log->caller, |
| 113 | log->calls); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 114 | total_calls += log->calls; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | printf("Total %lu gettimeofday\n", total_calls); |
| 119 | } |
| 120 | |
| 121 | static void fio_init gtod_init(void) |
| 122 | { |
| 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < HASH_SIZE; i++) |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 126 | INIT_FLIST_HEAD(&hash[i]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 127 | |
| 128 | gtod_inited = 1; |
| 129 | } |
| 130 | |
| 131 | #endif /* FIO_DEBUG_TIME */ |
| 132 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 133 | #ifdef CONFIG_CLOCK_GETTIME |
Jens Axboe | 9ff1c07 | 2012-12-20 15:17:57 +0100 | [diff] [blame] | 134 | static int fill_clock_gettime(struct timespec *ts) |
| 135 | { |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 136 | #if defined(CONFIG_CLOCK_MONOTONIC_RAW) |
| 137 | return clock_gettime(CLOCK_MONOTONIC_RAW, ts); |
| 138 | #elif defined(CONFIG_CLOCK_MONOTONIC) |
Jens Axboe | 9ff1c07 | 2012-12-20 15:17:57 +0100 | [diff] [blame] | 139 | return clock_gettime(CLOCK_MONOTONIC, ts); |
| 140 | #else |
| 141 | return clock_gettime(CLOCK_REALTIME, ts); |
| 142 | #endif |
| 143 | } |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 144 | #endif |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 145 | |
Jens Axboe | a7d78f2 | 2014-12-16 15:13:45 -0700 | [diff] [blame] | 146 | static void __fio_gettime(struct timeval *tp) |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 147 | { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 148 | switch (fio_clock_source) { |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 149 | #ifdef CONFIG_GETTIMEOFDAY |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 150 | case CS_GTOD: |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 151 | gettimeofday(tp, NULL); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 152 | break; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 153 | #endif |
| 154 | #ifdef CONFIG_CLOCK_GETTIME |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 155 | case CS_CGETTIME: { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 156 | struct timespec ts; |
| 157 | |
Jens Axboe | 9ff1c07 | 2012-12-20 15:17:57 +0100 | [diff] [blame] | 158 | if (fill_clock_gettime(&ts) < 0) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 159 | log_err("fio: clock_gettime fails\n"); |
| 160 | assert(0); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | tp->tv_sec = ts.tv_sec; |
| 164 | tp->tv_usec = ts.tv_nsec / 1000; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 165 | break; |
| 166 | } |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 167 | #endif |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 168 | #ifdef ARCH_HAVE_CPU_CLOCK |
| 169 | case CS_CPUCLOCK: { |
Jens Axboe | ba458c2 | 2013-01-21 05:38:22 -0700 | [diff] [blame] | 170 | uint64_t usecs, t; |
Jens Axboe | d8fc0cd | 2014-12-17 09:08:01 -0700 | [diff] [blame] | 171 | struct tv_valid *tv; |
| 172 | |
| 173 | #ifdef CONFIG_TLS_THREAD |
| 174 | tv = &static_tv_valid; |
| 175 | #else |
| 176 | tv = pthread_getspecific(tv_tls_key); |
| 177 | #endif |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 178 | |
| 179 | t = get_cpu_clock(); |
Christian Ehrhardt | bbc1f06 | 2015-02-27 08:20:46 -0700 | [diff] [blame] | 180 | #ifdef ARCH_CPU_CLOCK_WRAPS |
Jens Axboe | 1e62c3f | 2014-12-17 08:51:23 -0700 | [diff] [blame] | 181 | if (t < cycles_start && !cycles_wrap) |
| 182 | cycles_wrap = 1; |
Jens Axboe | d8fc0cd | 2014-12-17 09:08:01 -0700 | [diff] [blame] | 183 | else if (cycles_wrap && t >= cycles_start && !tv->warned) { |
| 184 | log_err("fio: double CPU clock wrap\n"); |
| 185 | tv->warned = 1; |
Jens Axboe | 02c7e29 | 2014-12-16 15:37:25 -0700 | [diff] [blame] | 186 | } |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 187 | |
Jens Axboe | 1e62c3f | 2014-12-17 08:51:23 -0700 | [diff] [blame] | 188 | t -= cycles_start; |
Christian Ehrhardt | bbc1f06 | 2015-02-27 08:20:46 -0700 | [diff] [blame] | 189 | #endif |
Jens Axboe | a7d78f2 | 2014-12-16 15:13:45 -0700 | [diff] [blame] | 190 | tv->last_cycles = t; |
| 191 | tv->last_tv_valid = 1; |
Christian Ehrhardt | 919e789 | 2014-04-08 17:52:00 +0200 | [diff] [blame] | 192 | #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC |
| 193 | usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC; |
| 194 | #else |
Jens Axboe | 400531d | 2014-12-16 22:40:37 -0700 | [diff] [blame] | 195 | if (t < max_cycles_for_mult) |
| 196 | usecs = (t * inv_cycles_per_usec) / 16777216UL; |
| 197 | else |
| 198 | usecs = t / cycles_per_usec; |
Christian Ehrhardt | 919e789 | 2014-04-08 17:52:00 +0200 | [diff] [blame] | 199 | #endif |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 200 | tp->tv_sec = usecs / 1000000; |
| 201 | tp->tv_usec = usecs % 1000000; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | #endif |
| 205 | default: |
| 206 | log_err("fio: invalid clock source %d\n", fio_clock_source); |
| 207 | break; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 208 | } |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | #ifdef FIO_DEBUG_TIME |
| 212 | void fio_gettime(struct timeval *tp, void *caller) |
| 213 | #else |
| 214 | void fio_gettime(struct timeval *tp, void fio_unused *caller) |
| 215 | #endif |
| 216 | { |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 217 | #ifdef FIO_DEBUG_TIME |
| 218 | if (!caller) |
| 219 | caller = __builtin_return_address(0); |
| 220 | |
| 221 | gtod_log_caller(caller); |
| 222 | #endif |
Jens Axboe | 0419419 | 2014-12-16 19:43:55 -0700 | [diff] [blame] | 223 | if (fio_unlikely(fio_gettime_offload(tp))) |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 224 | return; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 225 | |
Jens Axboe | a7d78f2 | 2014-12-16 15:13:45 -0700 | [diff] [blame] | 226 | __fio_gettime(tp); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 227 | } |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 228 | |
Christian Ehrhardt | 919e789 | 2014-04-08 17:52:00 +0200 | [diff] [blame] | 229 | #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 230 | static unsigned long get_cycles_per_usec(void) |
| 231 | { |
| 232 | struct timeval s, e; |
Jens Axboe | ba458c2 | 2013-01-21 05:38:22 -0700 | [diff] [blame] | 233 | uint64_t c_s, c_e; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 234 | enum fio_cs old_cs = fio_clock_source; |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 235 | uint64_t elapsed; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 236 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 237 | #ifdef CONFIG_CLOCK_GETTIME |
| 238 | fio_clock_source = CS_CGETTIME; |
| 239 | #else |
| 240 | fio_clock_source = CS_GTOD; |
| 241 | #endif |
| 242 | __fio_gettime(&s); |
Jens Axboe | 9ff1c07 | 2012-12-20 15:17:57 +0100 | [diff] [blame] | 243 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 244 | c_s = get_cpu_clock(); |
| 245 | do { |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 246 | __fio_gettime(&e); |
Jens Axboe | 9ff1c07 | 2012-12-20 15:17:57 +0100 | [diff] [blame] | 247 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 248 | elapsed = utime_since(&s, &e); |
Jens Axboe | 486332e | 2012-12-10 08:07:14 +0100 | [diff] [blame] | 249 | if (elapsed >= 1280) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 250 | c_e = get_cpu_clock(); |
| 251 | break; |
| 252 | } |
| 253 | } while (1); |
| 254 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 255 | fio_clock_source = old_cs; |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 256 | return (c_e - c_s) / elapsed; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 259 | #define NR_TIME_ITERS 50 |
| 260 | |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 261 | static int calibrate_cpu_clock(void) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 262 | { |
| 263 | double delta, mean, S; |
Jens Axboe | fa52915 | 2015-01-06 09:36:25 -0700 | [diff] [blame] | 264 | uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS]; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 265 | int i, samples; |
| 266 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 267 | cycles[0] = get_cycles_per_usec(); |
| 268 | S = delta = mean = 0.0; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 269 | for (i = 0; i < NR_TIME_ITERS; i++) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 270 | cycles[i] = get_cycles_per_usec(); |
| 271 | delta = cycles[i] - mean; |
| 272 | if (delta) { |
| 273 | mean += delta / (i + 1.0); |
| 274 | S += delta * (cycles[i] - mean); |
| 275 | } |
| 276 | } |
| 277 | |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 278 | /* |
| 279 | * The most common platform clock breakage is returning zero |
| 280 | * indefinitely. Check for that and return failure. |
| 281 | */ |
| 282 | if (!cycles[0] && !cycles[NR_TIME_ITERS - 1]) |
| 283 | return 1; |
| 284 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 285 | S = sqrt(S / (NR_TIME_ITERS - 1.0)); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 286 | |
Jens Axboe | fa52915 | 2015-01-06 09:36:25 -0700 | [diff] [blame] | 287 | minc = -1ULL; |
| 288 | maxc = samples = avg = 0; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 289 | for (i = 0; i < NR_TIME_ITERS; i++) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 290 | double this = cycles[i]; |
| 291 | |
Jens Axboe | fa52915 | 2015-01-06 09:36:25 -0700 | [diff] [blame] | 292 | minc = min(cycles[i], minc); |
| 293 | maxc = max(cycles[i], maxc); |
| 294 | |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 295 | if ((fmax(this, mean) - fmin(this, mean)) > S) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 296 | continue; |
| 297 | samples++; |
| 298 | avg += this; |
| 299 | } |
| 300 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 301 | S /= (double) NR_TIME_ITERS; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 302 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 303 | for (i = 0; i < NR_TIME_ITERS; i++) |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 304 | dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 305 | |
Jens Axboe | d7abad3 | 2012-12-10 10:15:59 +0100 | [diff] [blame] | 306 | avg /= samples; |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 307 | dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg); |
Jens Axboe | fa52915 | 2015-01-06 09:36:25 -0700 | [diff] [blame] | 308 | dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n", |
| 309 | (unsigned long long) minc, |
| 310 | (unsigned long long) maxc, mean, S); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 311 | |
| 312 | cycles_per_usec = avg; |
Jens Axboe | 7133911 | 2012-12-21 22:54:56 +0100 | [diff] [blame] | 313 | inv_cycles_per_usec = 16777216UL / cycles_per_usec; |
Jens Axboe | 400531d | 2014-12-16 22:40:37 -0700 | [diff] [blame] | 314 | max_cycles_for_mult = ~0ULL / inv_cycles_per_usec; |
Jens Axboe | 7133911 | 2012-12-21 22:54:56 +0100 | [diff] [blame] | 315 | dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec); |
Christian Ehrhardt | bbc1f06 | 2015-02-27 08:20:46 -0700 | [diff] [blame] | 316 | #ifdef ARCH_CPU_CLOCK_WRAPS |
Jens Axboe | 1e62c3f | 2014-12-17 08:51:23 -0700 | [diff] [blame] | 317 | cycles_start = get_cpu_clock(); |
| 318 | dprint(FD_TIME, "cycles_start=%llu\n", cycles_start); |
Christian Ehrhardt | bbc1f06 | 2015-02-27 08:20:46 -0700 | [diff] [blame] | 319 | #endif |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 320 | return 0; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 321 | } |
| 322 | #else |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 323 | static int calibrate_cpu_clock(void) |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 324 | { |
Christian Ehrhardt | 919e789 | 2014-04-08 17:52:00 +0200 | [diff] [blame] | 325 | #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC |
| 326 | return 0; |
| 327 | #else |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 328 | return 1; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 329 | #endif |
Christian Ehrhardt | 919e789 | 2014-04-08 17:52:00 +0200 | [diff] [blame] | 330 | } |
| 331 | #endif // ARCH_HAVE_CPU_CLOCK |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 332 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 333 | #ifndef CONFIG_TLS_THREAD |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 334 | void fio_local_clock_init(int is_thread) |
| 335 | { |
| 336 | struct tv_valid *t; |
| 337 | |
Jens Axboe | 572cfb3 | 2013-12-06 12:02:10 -0700 | [diff] [blame] | 338 | t = calloc(1, sizeof(*t)); |
Jens Axboe | a7d78f2 | 2014-12-16 15:13:45 -0700 | [diff] [blame] | 339 | if (pthread_setspecific(tv_tls_key, t)) { |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 340 | log_err("fio: can't set TLS key\n"); |
Jens Axboe | a7d78f2 | 2014-12-16 15:13:45 -0700 | [diff] [blame] | 341 | assert(0); |
| 342 | } |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static void kill_tv_tls_key(void *data) |
| 346 | { |
| 347 | free(data); |
| 348 | } |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 349 | #else |
| 350 | void fio_local_clock_init(int is_thread) |
| 351 | { |
| 352 | } |
| 353 | #endif |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 354 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 355 | void fio_clock_init(void) |
| 356 | { |
Jens Axboe | 01423ea | 2012-12-14 20:37:06 +0100 | [diff] [blame] | 357 | if (fio_clock_source == fio_clock_source_inited) |
| 358 | return; |
| 359 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 360 | #ifndef CONFIG_TLS_THREAD |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 361 | if (pthread_key_create(&tv_tls_key, kill_tv_tls_key)) |
| 362 | log_err("fio: can't create TLS key\n"); |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 363 | #endif |
Jens Axboe | 5d87939 | 2012-12-18 09:12:27 +0100 | [diff] [blame] | 364 | |
Jens Axboe | 01423ea | 2012-12-14 20:37:06 +0100 | [diff] [blame] | 365 | fio_clock_source_inited = fio_clock_source; |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 366 | |
| 367 | if (calibrate_cpu_clock()) |
| 368 | tsc_reliable = 0; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 369 | |
| 370 | /* |
| 371 | * If the arch sets tsc_reliable != 0, then it must be good enough |
| 372 | * to use as THE clock source. For x86 CPUs, this means the TSC |
| 373 | * runs at a constant rate and is synced across CPU cores. |
| 374 | */ |
| 375 | if (tsc_reliable) { |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 376 | if (!fio_clock_source_set && !fio_monotonic_clocktest(0)) |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 377 | fio_clock_source = CS_CPUCLOCK; |
| 378 | } else if (fio_clock_source == CS_CPUCLOCK) |
| 379 | log_info("fio: clocksource=cpu may not be reliable\n"); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Jens Axboe | 0cbbc39 | 2014-09-30 16:04:12 -0600 | [diff] [blame] | 382 | uint64_t utime_since(const struct timeval *s, const struct timeval *e) |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 383 | { |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 384 | int64_t sec, usec; |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 385 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 386 | sec = e->tv_sec - s->tv_sec; |
| 387 | usec = e->tv_usec - s->tv_usec; |
| 388 | if (sec > 0 && usec < 0) { |
| 389 | sec--; |
| 390 | usec += 1000000; |
| 391 | } |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 392 | |
| 393 | /* |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 394 | * time warp bug on some kernels? |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 395 | */ |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 396 | if (sec < 0 || (sec == 0 && usec < 0)) |
| 397 | return 0; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 398 | |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 399 | return usec + (sec * 1000000); |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Jens Axboe | 0cbbc39 | 2014-09-30 16:04:12 -0600 | [diff] [blame] | 402 | uint64_t utime_since_now(const struct timeval *s) |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 403 | { |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 404 | struct timeval t; |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 405 | #ifdef FIO_DEBUG_TIME |
| 406 | void *p = __builtin_return_address(0); |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 407 | |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 408 | fio_gettime(&t, p); |
| 409 | #else |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 410 | fio_gettime(&t, NULL); |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 411 | #endif |
| 412 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 413 | return utime_since(s, &t); |
| 414 | } |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 415 | |
Jens Axboe | 0cbbc39 | 2014-09-30 16:04:12 -0600 | [diff] [blame] | 416 | uint64_t mtime_since(const struct timeval *s, const struct timeval *e) |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 417 | { |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 418 | long sec, usec; |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 419 | |
| 420 | sec = e->tv_sec - s->tv_sec; |
| 421 | usec = e->tv_usec - s->tv_usec; |
| 422 | if (sec > 0 && usec < 0) { |
| 423 | sec--; |
| 424 | usec += 1000000; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 427 | if (sec < 0 || (sec == 0 && usec < 0)) |
| 428 | return 0; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 429 | |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 430 | sec *= 1000; |
| 431 | usec /= 1000; |
| 432 | return sec + usec; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 433 | } |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 434 | |
Jens Axboe | 0cbbc39 | 2014-09-30 16:04:12 -0600 | [diff] [blame] | 435 | uint64_t mtime_since_now(const struct timeval *s) |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 436 | { |
| 437 | struct timeval t; |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 438 | #ifdef FIO_DEBUG_TIME |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 439 | void *p = __builtin_return_address(0); |
| 440 | |
| 441 | fio_gettime(&t, p); |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 442 | #else |
| 443 | fio_gettime(&t, NULL); |
| 444 | #endif |
| 445 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 446 | return mtime_since(s, &t); |
| 447 | } |
| 448 | |
Jens Axboe | 0cbbc39 | 2014-09-30 16:04:12 -0600 | [diff] [blame] | 449 | uint64_t time_since_now(const struct timeval *s) |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 450 | { |
| 451 | return mtime_since_now(s) / 1000; |
| 452 | } |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 453 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 454 | #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \ |
| 455 | defined(CONFIG_SFAA) |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 456 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 457 | #define CLOCK_ENTRIES_DEBUG 100000 |
| 458 | #define CLOCK_ENTRIES_TEST 10000 |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 459 | |
| 460 | struct clock_entry { |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 461 | uint32_t seq; |
| 462 | uint32_t cpu; |
Jens Axboe | ba458c2 | 2013-01-21 05:38:22 -0700 | [diff] [blame] | 463 | uint64_t tsc; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 464 | }; |
| 465 | |
| 466 | struct clock_thread { |
| 467 | pthread_t thread; |
| 468 | int cpu; |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 469 | int debug; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 470 | pthread_mutex_t lock; |
| 471 | pthread_mutex_t started; |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 472 | unsigned long nr_entries; |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 473 | uint32_t *seq; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 474 | struct clock_entry *entries; |
| 475 | }; |
| 476 | |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 477 | static inline uint32_t atomic32_inc_return(uint32_t *seq) |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 478 | { |
| 479 | return 1 + __sync_fetch_and_add(seq, 1); |
| 480 | } |
| 481 | |
| 482 | static void *clock_thread_fn(void *data) |
| 483 | { |
| 484 | struct clock_thread *t = data; |
| 485 | struct clock_entry *c; |
| 486 | os_cpu_mask_t cpu_mask; |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 487 | uint32_t last_seq; |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 488 | unsigned long long first; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 489 | int i; |
| 490 | |
Jens Axboe | 1af4be1 | 2015-01-28 14:42:10 -0700 | [diff] [blame] | 491 | if (fio_cpuset_init(&cpu_mask)) { |
Jens Axboe | aa28ce0 | 2015-01-29 08:32:28 -0800 | [diff] [blame] | 492 | int __err = errno; |
Jens Axboe | 1af4be1 | 2015-01-28 14:42:10 -0700 | [diff] [blame] | 493 | |
| 494 | log_err("clock cpuset init failed: %s\n", strerror(__err)); |
| 495 | goto err_out; |
| 496 | } |
| 497 | |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 498 | fio_cpu_set(&cpu_mask, t->cpu); |
| 499 | |
| 500 | if (fio_setaffinity(gettid(), cpu_mask) == -1) { |
Jens Axboe | 4c64ff7 | 2015-01-28 14:34:10 -0700 | [diff] [blame] | 501 | int __err = errno; |
| 502 | |
| 503 | log_err("clock setaffinity failed: %s\n", strerror(__err)); |
Jens Axboe | 1af4be1 | 2015-01-28 14:42:10 -0700 | [diff] [blame] | 504 | goto err; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 505 | } |
| 506 | |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 507 | pthread_mutex_lock(&t->lock); |
Jens Axboe | b9b3498 | 2012-12-17 14:23:47 +0100 | [diff] [blame] | 508 | pthread_mutex_unlock(&t->started); |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 509 | |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 510 | first = get_cpu_clock(); |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 511 | last_seq = 0; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 512 | c = &t->entries[0]; |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 513 | for (i = 0; i < t->nr_entries; i++, c++) { |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 514 | uint32_t seq; |
| 515 | uint64_t tsc; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 516 | |
| 517 | c->cpu = t->cpu; |
| 518 | do { |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 519 | seq = atomic32_inc_return(t->seq); |
| 520 | if (seq < last_seq) |
| 521 | break; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 522 | tsc = get_cpu_clock(); |
| 523 | } while (seq != *t->seq); |
| 524 | |
| 525 | c->seq = seq; |
| 526 | c->tsc = tsc; |
| 527 | } |
| 528 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 529 | if (t->debug) { |
| 530 | unsigned long long clocks; |
| 531 | |
| 532 | clocks = t->entries[i - 1].tsc - t->entries[0].tsc; |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 533 | log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu, |
| 534 | clocks, first); |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 535 | } |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 536 | |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 537 | /* |
| 538 | * The most common platform clock breakage is returning zero |
| 539 | * indefinitely. Check for that and return failure. |
| 540 | */ |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 541 | if (!t->entries[i - 1].tsc && !t->entries[0].tsc) |
Jens Axboe | 1af4be1 | 2015-01-28 14:42:10 -0700 | [diff] [blame] | 542 | goto err; |
Jens Axboe | e259879 | 2013-02-24 21:29:35 +0100 | [diff] [blame] | 543 | |
Jens Axboe | 90881c8 | 2015-01-29 09:23:39 -0800 | [diff] [blame] | 544 | fio_cpuset_exit(&cpu_mask); |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 545 | return NULL; |
Jens Axboe | 1af4be1 | 2015-01-28 14:42:10 -0700 | [diff] [blame] | 546 | err: |
| 547 | fio_cpuset_exit(&cpu_mask); |
| 548 | err_out: |
| 549 | return (void *) 1; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static int clock_cmp(const void *p1, const void *p2) |
| 553 | { |
| 554 | const struct clock_entry *c1 = p1; |
| 555 | const struct clock_entry *c2 = p2; |
| 556 | |
Jens Axboe | b9b3498 | 2012-12-17 14:23:47 +0100 | [diff] [blame] | 557 | if (c1->seq == c2->seq) |
| 558 | log_err("cs: bug in atomic sequence!\n"); |
| 559 | |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 560 | return c1->seq - c2->seq; |
| 561 | } |
| 562 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 563 | int fio_monotonic_clocktest(int debug) |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 564 | { |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 565 | struct clock_thread *cthreads; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 566 | unsigned int nr_cpus = cpus_online(); |
| 567 | struct clock_entry *entries; |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 568 | unsigned long nr_entries, tentries, failed = 0; |
Bruce Cran | 80da8a8 | 2013-02-21 14:16:17 +0100 | [diff] [blame] | 569 | struct clock_entry *prev, *this; |
Jens Axboe | 58002f9 | 2013-02-25 10:23:58 +0100 | [diff] [blame] | 570 | uint32_t seq = 0; |
Jens Axboe | caa3eb1 | 2014-04-14 09:05:46 -0600 | [diff] [blame] | 571 | unsigned int i; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 572 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 573 | if (debug) { |
| 574 | log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no"); |
Jens Axboe | d5e3f5d | 2013-01-18 20:13:45 +0100 | [diff] [blame] | 575 | |
Jens Axboe | fc3f3e4 | 2014-09-24 09:54:24 -0600 | [diff] [blame] | 576 | #ifdef FIO_INC_DEBUG |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 577 | fio_debug |= 1U << FD_TIME; |
Jens Axboe | fc3f3e4 | 2014-09-24 09:54:24 -0600 | [diff] [blame] | 578 | #endif |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 579 | nr_entries = CLOCK_ENTRIES_DEBUG; |
| 580 | } else |
| 581 | nr_entries = CLOCK_ENTRIES_TEST; |
| 582 | |
Jens Axboe | 4f1d43c | 2012-12-17 14:29:28 +0100 | [diff] [blame] | 583 | calibrate_cpu_clock(); |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 584 | |
| 585 | if (debug) { |
Jens Axboe | fc3f3e4 | 2014-09-24 09:54:24 -0600 | [diff] [blame] | 586 | #ifdef FIO_INC_DEBUG |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 587 | fio_debug &= ~(1U << FD_TIME); |
Jens Axboe | fc3f3e4 | 2014-09-24 09:54:24 -0600 | [diff] [blame] | 588 | #endif |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 589 | } |
Jens Axboe | 4f1d43c | 2012-12-17 14:29:28 +0100 | [diff] [blame] | 590 | |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 591 | cthreads = malloc(nr_cpus * sizeof(struct clock_thread)); |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 592 | tentries = nr_entries * nr_cpus; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 593 | entries = malloc(tentries * sizeof(struct clock_entry)); |
| 594 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 595 | if (debug) |
| 596 | log_info("cs: Testing %u CPUs\n", nr_cpus); |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 597 | |
| 598 | for (i = 0; i < nr_cpus; i++) { |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 599 | struct clock_thread *t = &cthreads[i]; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 600 | |
| 601 | t->cpu = i; |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 602 | t->debug = debug; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 603 | t->seq = &seq; |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 604 | t->nr_entries = nr_entries; |
| 605 | t->entries = &entries[i * nr_entries]; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 606 | pthread_mutex_init(&t->lock, NULL); |
| 607 | pthread_mutex_init(&t->started, NULL); |
| 608 | pthread_mutex_lock(&t->lock); |
Jens Axboe | 6b0110c | 2014-04-14 12:14:17 -0600 | [diff] [blame] | 609 | if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) { |
| 610 | failed++; |
| 611 | nr_cpus = i; |
| 612 | break; |
| 613 | } |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | for (i = 0; i < nr_cpus; i++) { |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 617 | struct clock_thread *t = &cthreads[i]; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 618 | |
| 619 | pthread_mutex_lock(&t->started); |
| 620 | } |
| 621 | |
| 622 | for (i = 0; i < nr_cpus; i++) { |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 623 | struct clock_thread *t = &cthreads[i]; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 624 | |
| 625 | pthread_mutex_unlock(&t->lock); |
| 626 | } |
| 627 | |
Jens Axboe | 814917b | 2014-04-14 12:20:04 -0600 | [diff] [blame] | 628 | for (i = 0; i < nr_cpus; i++) { |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 629 | struct clock_thread *t = &cthreads[i]; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 630 | void *ret; |
| 631 | |
| 632 | pthread_join(t->thread, &ret); |
| 633 | if (ret) |
| 634 | failed++; |
| 635 | } |
Jens Axboe | 0f7f9a9 | 2014-11-06 09:21:10 -0700 | [diff] [blame] | 636 | free(cthreads); |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 637 | |
| 638 | if (failed) { |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 639 | if (debug) |
| 640 | log_err("Clocksource test: %lu threads failed\n", failed); |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 641 | goto err; |
| 642 | } |
| 643 | |
| 644 | qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp); |
| 645 | |
Elliott Hughes | eda3a60 | 2017-05-19 18:53:02 -0700 | [diff] [blame] | 646 | /* silence silly gcc */ |
| 647 | prev = NULL; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 648 | for (failed = i = 0; i < tentries; i++) { |
Bruce Cran | 80da8a8 | 2013-02-21 14:16:17 +0100 | [diff] [blame] | 649 | this = &entries[i]; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 650 | |
| 651 | if (!i) { |
| 652 | prev = this; |
| 653 | continue; |
| 654 | } |
| 655 | |
| 656 | if (prev->tsc > this->tsc) { |
| 657 | uint64_t diff = prev->tsc - this->tsc; |
| 658 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 659 | if (!debug) { |
| 660 | failed++; |
| 661 | break; |
| 662 | } |
| 663 | |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 664 | log_info("cs: CPU clock mismatch (diff=%llu):\n", |
| 665 | (unsigned long long) diff); |
| 666 | log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq); |
| 667 | log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq); |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 668 | failed++; |
| 669 | } |
| 670 | |
| 671 | prev = this; |
| 672 | } |
| 673 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 674 | if (debug) { |
| 675 | if (failed) |
| 676 | log_info("cs: Failed: %lu\n", failed); |
| 677 | else |
| 678 | log_info("cs: Pass!\n"); |
| 679 | } |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 680 | err: |
| 681 | free(entries); |
| 682 | return !!failed; |
| 683 | } |
| 684 | |
| 685 | #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */ |
| 686 | |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 687 | int fio_monotonic_clocktest(int debug) |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 688 | { |
Jens Axboe | 1056413 | 2014-12-23 10:41:34 -0700 | [diff] [blame] | 689 | if (debug) |
| 690 | log_info("cs: current platform does not support CPU clocks\n"); |
| 691 | return 1; |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | #endif |