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" |
| 14 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 15 | #ifdef ARCH_HAVE_CPU_CLOCK |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 16 | static unsigned long cycles_per_usec; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 17 | static unsigned long last_cycles; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 18 | #endif |
| 19 | static struct timeval last_tv; |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 20 | static int last_tv_valid; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 21 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 22 | static struct timeval *fio_tv; |
| 23 | int fio_gtod_offload = 0; |
| 24 | int fio_gtod_cpu = -1; |
| 25 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 26 | enum fio_cs fio_clock_source = CS_GTOD; |
| 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 | d481e00 | 2009-12-04 09:56:32 +0100 | [diff] [blame] | 143 | if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 144 | log_err("fio: clock_gettime fails\n"); |
| 145 | assert(0); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | tp->tv_sec = ts.tv_sec; |
| 149 | tp->tv_usec = ts.tv_nsec / 1000; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | #ifdef ARCH_HAVE_CPU_CLOCK |
| 153 | case CS_CPUCLOCK: { |
| 154 | unsigned long long usecs, t; |
| 155 | |
| 156 | t = get_cpu_clock(); |
| 157 | if (t < last_cycles) { |
| 158 | dprint(FD_TIME, "CPU clock going back in time\n"); |
| 159 | t = last_cycles; |
| 160 | } |
| 161 | |
| 162 | usecs = t / cycles_per_usec; |
| 163 | tp->tv_sec = usecs / 1000000; |
| 164 | tp->tv_usec = usecs % 1000000; |
| 165 | last_cycles = t; |
| 166 | break; |
| 167 | } |
| 168 | #endif |
| 169 | default: |
| 170 | log_err("fio: invalid clock source %d\n", fio_clock_source); |
| 171 | break; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 172 | } |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * If Linux is using the tsc clock on non-synced processors, |
| 176 | * sometimes time can appear to drift backwards. Fix that up. |
| 177 | */ |
| 178 | if (last_tv_valid) { |
| 179 | if (tp->tv_sec < last_tv.tv_sec) |
| 180 | tp->tv_sec = last_tv.tv_sec; |
| 181 | else if (last_tv.tv_sec == tp->tv_sec && |
| 182 | tp->tv_usec < last_tv.tv_usec) |
| 183 | tp->tv_usec = last_tv.tv_usec; |
| 184 | } |
| 185 | last_tv_valid = 1; |
| 186 | memcpy(&last_tv, tp, sizeof(*tp)); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 187 | } |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 188 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 189 | #ifdef ARCH_HAVE_CPU_CLOCK |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 190 | static unsigned long get_cycles_per_usec(void) |
| 191 | { |
| 192 | struct timeval s, e; |
| 193 | unsigned long long c_s, c_e; |
| 194 | |
| 195 | gettimeofday(&s, NULL); |
| 196 | c_s = get_cpu_clock(); |
| 197 | do { |
| 198 | unsigned long long elapsed; |
| 199 | |
| 200 | gettimeofday(&e, NULL); |
| 201 | elapsed = utime_since(&s, &e); |
| 202 | if (elapsed >= 10) { |
| 203 | c_e = get_cpu_clock(); |
| 204 | break; |
| 205 | } |
| 206 | } while (1); |
| 207 | |
| 208 | return c_e - c_s; |
| 209 | } |
| 210 | |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 211 | static void calibrate_cpu_clock(void) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 212 | { |
| 213 | double delta, mean, S; |
| 214 | unsigned long avg, cycles[10]; |
| 215 | int i, samples; |
| 216 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 217 | cycles[0] = get_cycles_per_usec(); |
| 218 | S = delta = mean = 0.0; |
| 219 | for (i = 0; i < 10; i++) { |
| 220 | cycles[i] = get_cycles_per_usec(); |
| 221 | delta = cycles[i] - mean; |
| 222 | if (delta) { |
| 223 | mean += delta / (i + 1.0); |
| 224 | S += delta * (cycles[i] - mean); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | S = sqrt(S / (10 - 1.0)); |
| 229 | |
| 230 | samples = avg = 0; |
| 231 | for (i = 0; i < 10; i++) { |
| 232 | double this = cycles[i]; |
| 233 | |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 234 | if ((fmax(this, mean) - fmin(this, mean)) > S) |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 235 | continue; |
| 236 | samples++; |
| 237 | avg += this; |
| 238 | } |
| 239 | |
| 240 | S /= 10.0; |
| 241 | mean /= 10.0; |
| 242 | |
| 243 | for (i = 0; i < 10; i++) |
| 244 | dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10); |
| 245 | |
| 246 | avg /= (samples * 10); |
| 247 | dprint(FD_TIME, "avg: %lu\n", avg); |
| 248 | dprint(FD_TIME, "mean=%f, S=%f\n", mean, S); |
| 249 | |
| 250 | cycles_per_usec = avg; |
Jens Axboe | 09a3240 | 2010-08-15 15:01:51 -0400 | [diff] [blame] | 251 | |
| 252 | } |
| 253 | #else |
| 254 | static void calibrate_cpu_clock(void) |
| 255 | { |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | void fio_clock_init(void) |
| 260 | { |
| 261 | last_tv_valid = 0; |
| 262 | calibrate_cpu_clock(); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 265 | void fio_gtod_init(void) |
| 266 | { |
| 267 | fio_tv = smalloc(sizeof(struct timeval)); |
| 268 | assert(fio_tv); |
| 269 | } |
| 270 | |
| 271 | void fio_gtod_update(void) |
| 272 | { |
| 273 | gettimeofday(fio_tv, NULL); |
| 274 | } |