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> |
| 8 | |
| 9 | #include "fio.h" |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 10 | #include "smalloc.h" |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 11 | |
| 12 | #include "hash.h" |
| 13 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 14 | static unsigned long cycles_per_usec; |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 15 | static struct timeval last_tv; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 16 | static unsigned long last_cycles; |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 17 | static int last_tv_valid; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 18 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 19 | static struct timeval *fio_tv; |
| 20 | int fio_gtod_offload = 0; |
| 21 | int fio_gtod_cpu = -1; |
| 22 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 23 | enum fio_cs fio_clock_source = CS_GTOD; |
| 24 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 25 | #ifdef FIO_DEBUG_TIME |
| 26 | |
| 27 | #define HASH_BITS 8 |
| 28 | #define HASH_SIZE (1 << HASH_BITS) |
| 29 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 30 | static struct flist_head hash[HASH_SIZE]; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 31 | static int gtod_inited; |
| 32 | |
| 33 | struct gtod_log { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 34 | struct flist_head list; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 35 | void *caller; |
| 36 | unsigned long calls; |
| 37 | }; |
| 38 | |
| 39 | static struct gtod_log *find_hash(void *caller) |
| 40 | { |
| 41 | unsigned long h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 42 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 43 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 44 | flist_for_each(entry, &hash[h]) { |
| 45 | struct gtod_log *log = flist_entry(entry, struct gtod_log, |
| 46 | list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 47 | |
| 48 | if (log->caller == caller) |
| 49 | return log; |
| 50 | } |
| 51 | |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | static struct gtod_log *find_log(void *caller) |
| 56 | { |
| 57 | struct gtod_log *log = find_hash(caller); |
| 58 | |
| 59 | if (!log) { |
| 60 | unsigned long h; |
| 61 | |
| 62 | log = malloc(sizeof(*log)); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 63 | INIT_FLIST_HEAD(&log->list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 64 | log->caller = caller; |
| 65 | log->calls = 0; |
| 66 | |
| 67 | h = hash_ptr(caller, HASH_BITS); |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 68 | flist_add_tail(&log->list, &hash[h]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return log; |
| 72 | } |
| 73 | |
| 74 | static void gtod_log_caller(void *caller) |
| 75 | { |
| 76 | if (gtod_inited) { |
| 77 | struct gtod_log *log = find_log(caller); |
| 78 | |
| 79 | log->calls++; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void fio_exit fio_dump_gtod(void) |
| 84 | { |
| 85 | unsigned long total_calls = 0; |
| 86 | int i; |
| 87 | |
| 88 | for (i = 0; i < HASH_SIZE; i++) { |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 89 | struct flist_head *entry; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 90 | struct gtod_log *log; |
| 91 | |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 92 | flist_for_each(entry, &hash[i]) { |
| 93 | log = flist_entry(entry, struct gtod_log, list); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 94 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 95 | printf("function %p, calls %lu\n", log->caller, |
| 96 | log->calls); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 97 | total_calls += log->calls; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | printf("Total %lu gettimeofday\n", total_calls); |
| 102 | } |
| 103 | |
| 104 | static void fio_init gtod_init(void) |
| 105 | { |
| 106 | int i; |
| 107 | |
| 108 | for (i = 0; i < HASH_SIZE; i++) |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 109 | INIT_FLIST_HEAD(&hash[i]); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 110 | |
| 111 | gtod_inited = 1; |
| 112 | } |
| 113 | |
| 114 | #endif /* FIO_DEBUG_TIME */ |
| 115 | |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 116 | #ifdef FIO_DEBUG_TIME |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 117 | void fio_gettime(struct timeval *tp, void *caller) |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 118 | #else |
| 119 | void fio_gettime(struct timeval *tp, void fio_unused *caller) |
| 120 | #endif |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 121 | { |
| 122 | #ifdef FIO_DEBUG_TIME |
| 123 | if (!caller) |
| 124 | caller = __builtin_return_address(0); |
| 125 | |
| 126 | gtod_log_caller(caller); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 127 | #endif |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 128 | if (fio_tv) { |
| 129 | memcpy(tp, fio_tv, sizeof(*tp)); |
| 130 | return; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 131 | } |
| 132 | |
| 133 | switch (fio_clock_source) { |
| 134 | case CS_GTOD: |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 135 | gettimeofday(tp, NULL); |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 136 | break; |
| 137 | case CS_CGETTIME: { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 138 | struct timespec ts; |
| 139 | |
Jens Axboe | d481e00 | 2009-12-04 09:56:32 +0100 | [diff] [blame] | 140 | if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 141 | log_err("fio: clock_gettime fails\n"); |
| 142 | assert(0); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | tp->tv_sec = ts.tv_sec; |
| 146 | tp->tv_usec = ts.tv_nsec / 1000; |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 147 | break; |
| 148 | } |
| 149 | #ifdef ARCH_HAVE_CPU_CLOCK |
| 150 | case CS_CPUCLOCK: { |
| 151 | unsigned long long usecs, t; |
| 152 | |
| 153 | t = get_cpu_clock(); |
| 154 | if (t < last_cycles) { |
| 155 | dprint(FD_TIME, "CPU clock going back in time\n"); |
| 156 | t = last_cycles; |
| 157 | } |
| 158 | |
| 159 | usecs = t / cycles_per_usec; |
| 160 | tp->tv_sec = usecs / 1000000; |
| 161 | tp->tv_usec = usecs % 1000000; |
| 162 | last_cycles = t; |
| 163 | break; |
| 164 | } |
| 165 | #endif |
| 166 | default: |
| 167 | log_err("fio: invalid clock source %d\n", fio_clock_source); |
| 168 | break; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 169 | } |
Jens Axboe | 3e48892 | 2008-11-13 12:07:20 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * If Linux is using the tsc clock on non-synced processors, |
| 173 | * sometimes time can appear to drift backwards. Fix that up. |
| 174 | */ |
| 175 | if (last_tv_valid) { |
| 176 | if (tp->tv_sec < last_tv.tv_sec) |
| 177 | tp->tv_sec = last_tv.tv_sec; |
| 178 | else if (last_tv.tv_sec == tp->tv_sec && |
| 179 | tp->tv_usec < last_tv.tv_usec) |
| 180 | tp->tv_usec = last_tv.tv_usec; |
| 181 | } |
| 182 | last_tv_valid = 1; |
| 183 | memcpy(&last_tv, tp, sizeof(*tp)); |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 184 | } |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 185 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame^] | 186 | static unsigned long get_cycles_per_usec(void) |
| 187 | { |
| 188 | struct timeval s, e; |
| 189 | unsigned long long c_s, c_e; |
| 190 | |
| 191 | gettimeofday(&s, NULL); |
| 192 | c_s = get_cpu_clock(); |
| 193 | do { |
| 194 | unsigned long long elapsed; |
| 195 | |
| 196 | gettimeofday(&e, NULL); |
| 197 | elapsed = utime_since(&s, &e); |
| 198 | if (elapsed >= 10) { |
| 199 | c_e = get_cpu_clock(); |
| 200 | break; |
| 201 | } |
| 202 | } while (1); |
| 203 | |
| 204 | return c_e - c_s; |
| 205 | } |
| 206 | |
| 207 | void fio_clock_init(void) |
| 208 | { |
| 209 | double delta, mean, S; |
| 210 | unsigned long avg, cycles[10]; |
| 211 | int i, samples; |
| 212 | |
| 213 | last_tv_valid = 0; |
| 214 | |
| 215 | cycles[0] = get_cycles_per_usec(); |
| 216 | S = delta = mean = 0.0; |
| 217 | for (i = 0; i < 10; i++) { |
| 218 | cycles[i] = get_cycles_per_usec(); |
| 219 | delta = cycles[i] - mean; |
| 220 | if (delta) { |
| 221 | mean += delta / (i + 1.0); |
| 222 | S += delta * (cycles[i] - mean); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | S = sqrt(S / (10 - 1.0)); |
| 227 | |
| 228 | samples = avg = 0; |
| 229 | for (i = 0; i < 10; i++) { |
| 230 | double this = cycles[i]; |
| 231 | |
| 232 | if ((max(this, mean) - min(this, mean)) > S) |
| 233 | continue; |
| 234 | samples++; |
| 235 | avg += this; |
| 236 | } |
| 237 | |
| 238 | S /= 10.0; |
| 239 | mean /= 10.0; |
| 240 | |
| 241 | for (i = 0; i < 10; i++) |
| 242 | dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10); |
| 243 | |
| 244 | avg /= (samples * 10); |
| 245 | dprint(FD_TIME, "avg: %lu\n", avg); |
| 246 | dprint(FD_TIME, "mean=%f, S=%f\n", mean, S); |
| 247 | |
| 248 | cycles_per_usec = avg; |
| 249 | } |
| 250 | |
Jens Axboe | be4ecfd | 2008-12-08 14:10:52 +0100 | [diff] [blame] | 251 | void fio_gtod_init(void) |
| 252 | { |
| 253 | fio_tv = smalloc(sizeof(struct timeval)); |
| 254 | assert(fio_tv); |
| 255 | } |
| 256 | |
| 257 | void fio_gtod_update(void) |
| 258 | { |
| 259 | gettimeofday(fio_tv, NULL); |
| 260 | } |