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 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 16 | #ifdef ARCH_HAVE_CPU_CLOCK |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 17 | static unsigned long cycles_per_usec; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 18 | static unsigned long last_cycles; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 19 | int tsc_reliable = 0; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 20 | #endif |
| 21 | static struct timeval last_tv; |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 22 | static int last_tv_valid; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 23 | |
Bruce Cran | 16de1bf | 2012-02-20 17:07:32 +0000 | [diff] [blame] | 24 | enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 25 | int fio_clock_source_set = 0; |
Jens Axboe | 01423ea | 2012-12-14 20:37:06 +0100 | [diff] [blame] | 26 | enum fio_cs fio_clock_source_inited = CS_INVAL; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 27 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 28 | #ifdef FIO_DEBUG_TIME |
| 29 | |
| 30 | #define HASH_BITS 8 |
| 31 | #define HASH_SIZE (1 << HASH_BITS) |
| 32 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 33 | static struct flist_head hash[HASH_SIZE]; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 34 | static int gtod_inited; |
| 35 | |
| 36 | struct gtod_log { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 37 | struct flist_head list; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 38 | void *caller; |
| 39 | unsigned long calls; |
| 40 | }; |
| 41 | |
| 42 | static struct gtod_log *find_hash(void *caller) |
| 43 | { |
| 44 | unsigned long h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 45 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 46 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 47 | flist_for_each(entry, &hash[h]) { |
| 48 | struct gtod_log *log = flist_entry(entry, struct gtod_log, |
| 49 | list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 50 | |
| 51 | if (log->caller == caller) |
| 52 | return log; |
| 53 | } |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | static struct gtod_log *find_log(void *caller) |
| 59 | { |
| 60 | struct gtod_log *log = find_hash(caller); |
| 61 | |
| 62 | if (!log) { |
| 63 | unsigned long h; |
| 64 | |
| 65 | log = malloc(sizeof(*log)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 66 | INIT_FLIST_HEAD(&log->list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 67 | log->caller = caller; |
| 68 | log->calls = 0; |
| 69 | |
| 70 | h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 71 | flist_add_tail(&log->list, &hash[h]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return log; |
| 75 | } |
| 76 | |
| 77 | static void gtod_log_caller(void *caller) |
| 78 | { |
| 79 | if (gtod_inited) { |
| 80 | struct gtod_log *log = find_log(caller); |
| 81 | |
| 82 | log->calls++; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void fio_exit fio_dump_gtod(void) |
| 87 | { |
| 88 | unsigned long total_calls = 0; |
| 89 | int i; |
| 90 | |
| 91 | for (i = 0; i < HASH_SIZE; i++) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 92 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 93 | struct gtod_log *log; |
| 94 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 95 | flist_for_each(entry, &hash[i]) { |
| 96 | log = flist_entry(entry, struct gtod_log, list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 97 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 98 | printf("function %p, calls %lu\n", log->caller, |
| 99 | log->calls); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 100 | total_calls += log->calls; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | printf("Total %lu gettimeofday\n", total_calls); |
| 105 | } |
| 106 | |
| 107 | static void fio_init gtod_init(void) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 111 | for (i = 0; i < HASH_SIZE; i++) |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 112 | INIT_FLIST_HEAD(&hash[i]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 113 | |
| 114 | gtod_inited = 1; |
| 115 | } |
| 116 | |
| 117 | #endif /* FIO_DEBUG_TIME */ |
| 118 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 119 | #ifdef FIO_DEBUG_TIME |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 120 | void fio_gettime(struct timeval *tp, void *caller) |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 121 | #else |
| 122 | void fio_gettime(struct timeval *tp, void fio_unused *caller) |
| 123 | #endif |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 124 | { |
| 125 | #ifdef FIO_DEBUG_TIME |
| 126 | if (!caller) |
| 127 | caller = __builtin_return_address(0); |
| 128 | |
| 129 | gtod_log_caller(caller); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 130 | #endif |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 131 | if (fio_tv) { |
| 132 | memcpy(tp, fio_tv, sizeof(*tp)); |
| 133 | return; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | switch (fio_clock_source) { |
| 137 | case CS_GTOD: |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 138 | gettimeofday(tp, NULL); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 139 | break; |
| 140 | case CS_CGETTIME: { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 141 | struct timespec ts; |
| 142 | |
Jens Axboe | 5bd9c78 | 2012-02-03 12:48:16 +0100 | [diff] [blame] | 143 | #ifdef FIO_HAVE_CLOCK_MONOTONIC |
| 144 | if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) { |
| 145 | #else |
Jens Axboe | d481e00 | 2009-12-04 09:56:32 +0100 | [diff] [blame] | 146 | if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { |
Jens Axboe | 5bd9c78 | 2012-02-03 12:48:16 +0100 | [diff] [blame] | 147 | #endif |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 148 | log_err("fio: clock_gettime fails\n"); |
| 149 | assert(0); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | tp->tv_sec = ts.tv_sec; |
| 153 | tp->tv_usec = ts.tv_nsec / 1000; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | #ifdef ARCH_HAVE_CPU_CLOCK |
| 157 | case CS_CPUCLOCK: { |
| 158 | unsigned long long usecs, t; |
| 159 | |
| 160 | t = get_cpu_clock(); |
| 161 | if (t < last_cycles) { |
| 162 | dprint(FD_TIME, "CPU clock going back in time\n"); |
| 163 | t = last_cycles; |
| 164 | } |
| 165 | |
| 166 | usecs = t / cycles_per_usec; |
| 167 | tp->tv_sec = usecs / 1000000; |
| 168 | tp->tv_usec = usecs % 1000000; |
| 169 | last_cycles = t; |
| 170 | break; |
| 171 | } |
| 172 | #endif |
| 173 | default: |
| 174 | log_err("fio: invalid clock source %d\n", fio_clock_source); |
| 175 | break; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 176 | } |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * If Linux is using the tsc clock on non-synced processors, |
| 180 | * sometimes time can appear to drift backwards. Fix that up. |
| 181 | */ |
| 182 | if (last_tv_valid) { |
| 183 | if (tp->tv_sec < last_tv.tv_sec) |
| 184 | tp->tv_sec = last_tv.tv_sec; |
| 185 | else if (last_tv.tv_sec == tp->tv_sec && |
| 186 | tp->tv_usec < last_tv.tv_usec) |
| 187 | tp->tv_usec = last_tv.tv_usec; |
| 188 | } |
| 189 | last_tv_valid = 1; |
| 190 | memcpy(&last_tv, tp, sizeof(*tp)); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 191 | } |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 192 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 193 | #ifdef ARCH_HAVE_CPU_CLOCK |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 194 | static unsigned long get_cycles_per_usec(void) |
| 195 | { |
| 196 | struct timeval s, e; |
| 197 | unsigned long long c_s, c_e; |
| 198 | |
| 199 | gettimeofday(&s, NULL); |
| 200 | c_s = get_cpu_clock(); |
| 201 | do { |
| 202 | unsigned long long elapsed; |
| 203 | |
| 204 | gettimeofday(&e, NULL); |
| 205 | elapsed = utime_since(&s, &e); |
Jens Axboe | 486332e | 2012-12-10 08:07:14 +0100 | [diff] [blame] | 206 | if (elapsed >= 1280) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 207 | c_e = get_cpu_clock(); |
| 208 | break; |
| 209 | } |
| 210 | } while (1); |
| 211 | |
Jens Axboe | c011000 | 2012-12-10 08:29:03 +0100 | [diff] [blame] | 212 | return (c_e - c_s + 127) >> 7; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 215 | #define NR_TIME_ITERS 50 |
| 216 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 217 | static void calibrate_cpu_clock(void) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 218 | { |
| 219 | double delta, mean, S; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 220 | unsigned long avg, cycles[NR_TIME_ITERS]; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 221 | int i, samples; |
| 222 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 223 | cycles[0] = get_cycles_per_usec(); |
| 224 | S = delta = mean = 0.0; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 225 | for (i = 0; i < NR_TIME_ITERS; i++) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 226 | cycles[i] = get_cycles_per_usec(); |
| 227 | delta = cycles[i] - mean; |
| 228 | if (delta) { |
| 229 | mean += delta / (i + 1.0); |
| 230 | S += delta * (cycles[i] - mean); |
| 231 | } |
| 232 | } |
| 233 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 234 | S = sqrt(S / (NR_TIME_ITERS - 1.0)); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 235 | |
| 236 | samples = avg = 0; |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 237 | for (i = 0; i < NR_TIME_ITERS; i++) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 238 | double this = cycles[i]; |
| 239 | |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 240 | if ((fmax(this, mean) - fmin(this, mean)) > S) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 241 | continue; |
| 242 | samples++; |
| 243 | avg += this; |
| 244 | } |
| 245 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 246 | S /= (double) NR_TIME_ITERS; |
Jens Axboe | 89db727 | 2012-12-10 08:29:56 +0100 | [diff] [blame] | 247 | mean /= 10.0; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 248 | |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 249 | for (i = 0; i < NR_TIME_ITERS; i++) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 250 | dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10); |
| 251 | |
Jens Axboe | d7abad3 | 2012-12-10 10:15:59 +0100 | [diff] [blame] | 252 | avg /= samples; |
| 253 | avg = (avg + 9) / 10; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 254 | dprint(FD_TIME, "avg: %lu\n", avg); |
| 255 | dprint(FD_TIME, "mean=%f, S=%f\n", mean, S); |
| 256 | |
| 257 | cycles_per_usec = avg; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 258 | } |
| 259 | #else |
| 260 | static void calibrate_cpu_clock(void) |
| 261 | { |
| 262 | } |
| 263 | #endif |
| 264 | |
| 265 | void fio_clock_init(void) |
| 266 | { |
Jens Axboe | 01423ea | 2012-12-14 20:37:06 +0100 | [diff] [blame] | 267 | if (fio_clock_source == fio_clock_source_inited) |
| 268 | return; |
| 269 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 270 | last_tv_valid = 0; |
Jens Axboe | 01423ea | 2012-12-14 20:37:06 +0100 | [diff] [blame] | 271 | fio_clock_source_inited = fio_clock_source; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 272 | calibrate_cpu_clock(); |
Jens Axboe | fa80fea | 2012-12-09 20:29:00 +0100 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * If the arch sets tsc_reliable != 0, then it must be good enough |
| 276 | * to use as THE clock source. For x86 CPUs, this means the TSC |
| 277 | * runs at a constant rate and is synced across CPU cores. |
| 278 | */ |
| 279 | if (tsc_reliable) { |
| 280 | if (!fio_clock_source_set) |
| 281 | fio_clock_source = CS_CPUCLOCK; |
| 282 | } else if (fio_clock_source == CS_CPUCLOCK) |
| 283 | log_info("fio: clocksource=cpu may not be reliable\n"); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 286 | unsigned long long utime_since(struct timeval *s, struct timeval *e) |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 287 | { |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 288 | long sec, usec; |
| 289 | unsigned long long ret; |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 290 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 291 | sec = e->tv_sec - s->tv_sec; |
| 292 | usec = e->tv_usec - s->tv_usec; |
| 293 | if (sec > 0 && usec < 0) { |
| 294 | sec--; |
| 295 | usec += 1000000; |
| 296 | } |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 297 | |
| 298 | /* |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 299 | * time warp bug on some kernels? |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 300 | */ |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 301 | if (sec < 0 || (sec == 0 && usec < 0)) |
| 302 | return 0; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 303 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 304 | ret = sec * 1000000ULL + usec; |
| 305 | |
| 306 | return ret; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 309 | unsigned long long utime_since_now(struct timeval *s) |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 310 | { |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 311 | struct timeval t; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 312 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 313 | fio_gettime(&t, NULL); |
| 314 | return utime_since(s, &t); |
| 315 | } |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 316 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 317 | unsigned long mtime_since(struct timeval *s, struct timeval *e) |
| 318 | { |
| 319 | long sec, usec, ret; |
| 320 | |
| 321 | sec = e->tv_sec - s->tv_sec; |
| 322 | usec = e->tv_usec - s->tv_usec; |
| 323 | if (sec > 0 && usec < 0) { |
| 324 | sec--; |
| 325 | usec += 1000000; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 328 | if (sec < 0 || (sec == 0 && usec < 0)) |
| 329 | return 0; |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 330 | |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 331 | sec *= 1000UL; |
| 332 | usec /= 1000UL; |
| 333 | ret = sec + usec; |
| 334 | |
Jens Axboe | 783a3eb | 2012-02-09 10:27:10 +0100 | [diff] [blame] | 335 | return ret; |
| 336 | } |
Jens Axboe | 39ab7da | 2012-11-06 22:10:43 +0100 | [diff] [blame] | 337 | |
| 338 | unsigned long mtime_since_now(struct timeval *s) |
| 339 | { |
| 340 | struct timeval t; |
| 341 | void *p = __builtin_return_address(0); |
| 342 | |
| 343 | fio_gettime(&t, p); |
| 344 | return mtime_since(s, &t); |
| 345 | } |
| 346 | |
| 347 | unsigned long time_since_now(struct timeval *s) |
| 348 | { |
| 349 | return mtime_since_now(s) / 1000; |
| 350 | } |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 351 | |
| 352 | #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) |
| 353 | |
| 354 | #define CLOCK_ENTRIES 100000 |
| 355 | |
| 356 | struct clock_entry { |
| 357 | unsigned long seq; |
| 358 | unsigned long tsc; |
| 359 | unsigned long cpu; |
| 360 | }; |
| 361 | |
| 362 | struct clock_thread { |
| 363 | pthread_t thread; |
| 364 | int cpu; |
| 365 | pthread_mutex_t lock; |
| 366 | pthread_mutex_t started; |
| 367 | uint64_t *seq; |
| 368 | struct clock_entry *entries; |
| 369 | }; |
| 370 | |
| 371 | static inline uint64_t atomic64_inc_return(uint64_t *seq) |
| 372 | { |
| 373 | return 1 + __sync_fetch_and_add(seq, 1); |
| 374 | } |
| 375 | |
| 376 | static void *clock_thread_fn(void *data) |
| 377 | { |
| 378 | struct clock_thread *t = data; |
| 379 | struct clock_entry *c; |
| 380 | os_cpu_mask_t cpu_mask; |
| 381 | int i; |
| 382 | |
| 383 | memset(&cpu_mask, 0, sizeof(cpu_mask)); |
| 384 | fio_cpu_set(&cpu_mask, t->cpu); |
| 385 | |
| 386 | if (fio_setaffinity(gettid(), cpu_mask) == -1) { |
| 387 | log_err("clock setaffinity failed\n"); |
| 388 | return (void *) 1; |
| 389 | } |
| 390 | |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 391 | pthread_mutex_lock(&t->lock); |
Jens Axboe | b9b3498 | 2012-12-17 14:23:47 +0100 | [diff] [blame^] | 392 | pthread_mutex_unlock(&t->started); |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 393 | |
| 394 | c = &t->entries[0]; |
| 395 | for (i = 0; i < CLOCK_ENTRIES; i++, c++) { |
| 396 | uint64_t seq, tsc; |
| 397 | |
| 398 | c->cpu = t->cpu; |
| 399 | do { |
| 400 | seq = atomic64_inc_return(t->seq); |
| 401 | tsc = get_cpu_clock(); |
| 402 | } while (seq != *t->seq); |
| 403 | |
| 404 | c->seq = seq; |
| 405 | c->tsc = tsc; |
| 406 | } |
| 407 | |
| 408 | log_info("cs: cpu%3d: %lu clocks seen\n", t->cpu, t->entries[CLOCK_ENTRIES - 1].tsc - t->entries[0].tsc); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | static int clock_cmp(const void *p1, const void *p2) |
| 413 | { |
| 414 | const struct clock_entry *c1 = p1; |
| 415 | const struct clock_entry *c2 = p2; |
| 416 | |
Jens Axboe | b9b3498 | 2012-12-17 14:23:47 +0100 | [diff] [blame^] | 417 | if (c1->seq == c2->seq) |
| 418 | log_err("cs: bug in atomic sequence!\n"); |
| 419 | |
Jens Axboe | 7d11f87 | 2012-12-17 12:03:29 +0100 | [diff] [blame] | 420 | return c1->seq - c2->seq; |
| 421 | } |
| 422 | |
| 423 | int fio_monotonic_clocktest(void) |
| 424 | { |
| 425 | struct clock_thread *threads; |
| 426 | unsigned int nr_cpus = cpus_online(); |
| 427 | struct clock_entry *entries; |
| 428 | unsigned long tentries, failed; |
| 429 | uint64_t seq = 0; |
| 430 | int i; |
| 431 | |
| 432 | threads = malloc(nr_cpus * sizeof(struct clock_thread)); |
| 433 | tentries = CLOCK_ENTRIES * nr_cpus; |
| 434 | entries = malloc(tentries * sizeof(struct clock_entry)); |
| 435 | |
| 436 | log_info("cs: Testing %u CPUs\n", nr_cpus); |
| 437 | |
| 438 | for (i = 0; i < nr_cpus; i++) { |
| 439 | struct clock_thread *t = &threads[i]; |
| 440 | |
| 441 | t->cpu = i; |
| 442 | t->seq = &seq; |
| 443 | t->entries = &entries[i * CLOCK_ENTRIES]; |
| 444 | pthread_mutex_init(&t->lock, NULL); |
| 445 | pthread_mutex_init(&t->started, NULL); |
| 446 | pthread_mutex_lock(&t->lock); |
| 447 | pthread_create(&t->thread, NULL, clock_thread_fn, t); |
| 448 | } |
| 449 | |
| 450 | for (i = 0; i < nr_cpus; i++) { |
| 451 | struct clock_thread *t = &threads[i]; |
| 452 | |
| 453 | pthread_mutex_lock(&t->started); |
| 454 | } |
| 455 | |
| 456 | for (i = 0; i < nr_cpus; i++) { |
| 457 | struct clock_thread *t = &threads[i]; |
| 458 | |
| 459 | pthread_mutex_unlock(&t->lock); |
| 460 | } |
| 461 | |
| 462 | for (failed = i = 0; i < nr_cpus; i++) { |
| 463 | struct clock_thread *t = &threads[i]; |
| 464 | void *ret; |
| 465 | |
| 466 | pthread_join(t->thread, &ret); |
| 467 | if (ret) |
| 468 | failed++; |
| 469 | } |
| 470 | free(threads); |
| 471 | |
| 472 | if (failed) { |
| 473 | log_err("Clocksource test: %u threads failed\n", failed); |
| 474 | goto err; |
| 475 | } |
| 476 | |
| 477 | qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp); |
| 478 | |
| 479 | for (failed = i = 0; i < tentries; i++) { |
| 480 | struct clock_entry *prev, *this = &entries[i]; |
| 481 | |
| 482 | if (!i) { |
| 483 | prev = this; |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | if (prev->tsc > this->tsc) { |
| 488 | uint64_t diff = prev->tsc - this->tsc; |
| 489 | |
| 490 | log_info("cs: CPU clock mismatch (diff=%lu):\n", diff); |
| 491 | log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", prev->cpu, prev->tsc, prev->seq); |
| 492 | log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", this->cpu, this->tsc, this->seq); |
| 493 | failed++; |
| 494 | } |
| 495 | |
| 496 | prev = this; |
| 497 | } |
| 498 | |
| 499 | if (failed) |
| 500 | log_info("cs: Failed: %lu\n", failed); |
| 501 | else |
| 502 | log_info("cs: Pass!\n"); |
| 503 | |
| 504 | err: |
| 505 | free(entries); |
| 506 | return !!failed; |
| 507 | } |
| 508 | |
| 509 | #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */ |
| 510 | |
| 511 | int fio_monotonic_clocktest(void) |
| 512 | { |
| 513 | log_info("cs: current platform does not support CPU clocks\n"); |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | #endif |