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