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