Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 1 | #include <time.h> |
| 2 | #include <sys/time.h> |
| 3 | |
| 4 | #include "fio.h" |
| 5 | |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 6 | static struct timeval genesis; |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 7 | static unsigned long ns_granularity; |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 8 | |
Shawn Lewis | 1092731 | 2007-11-21 09:38:13 +0100 | [diff] [blame] | 9 | unsigned long long utime_since(struct timeval *s, struct timeval *e) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 10 | { |
Shawn Lewis | 1092731 | 2007-11-21 09:38:13 +0100 | [diff] [blame] | 11 | long sec, usec; |
| 12 | unsigned long long ret; |
| 13 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 14 | sec = e->tv_sec - s->tv_sec; |
| 15 | usec = e->tv_usec - s->tv_usec; |
| 16 | if (sec > 0 && usec < 0) { |
| 17 | sec--; |
| 18 | usec += 1000000; |
| 19 | } |
| 20 | |
Jens Axboe | 2bfe24b | 2007-05-21 14:27:32 +0200 | [diff] [blame] | 21 | /* |
| 22 | * time warp bug on some kernels? |
| 23 | */ |
Shawn Lewis | 1092731 | 2007-11-21 09:38:13 +0100 | [diff] [blame] | 24 | if (sec < 0 || (sec == 0 && usec < 0)) |
| 25 | return 0; |
| 26 | |
| 27 | ret = sec * 1000000ULL + usec; |
| 28 | |
Jens Axboe | 2bfe24b | 2007-05-21 14:27:32 +0200 | [diff] [blame] | 29 | return ret; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Shawn Lewis | 1092731 | 2007-11-21 09:38:13 +0100 | [diff] [blame] | 32 | unsigned long long utime_since_now(struct timeval *s) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 33 | { |
| 34 | struct timeval t; |
| 35 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 36 | fio_gettime(&t, NULL); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 37 | return utime_since(s, &t); |
| 38 | } |
| 39 | |
| 40 | unsigned long mtime_since(struct timeval *s, struct timeval *e) |
| 41 | { |
Jens Axboe | 2bfe24b | 2007-05-21 14:27:32 +0200 | [diff] [blame] | 42 | long sec, usec, ret; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 43 | |
| 44 | sec = e->tv_sec - s->tv_sec; |
| 45 | usec = e->tv_usec - s->tv_usec; |
| 46 | if (sec > 0 && usec < 0) { |
| 47 | sec--; |
| 48 | usec += 1000000; |
| 49 | } |
| 50 | |
Jens Axboe | 64b18e1 | 2007-03-27 10:03:09 +0200 | [diff] [blame] | 51 | sec *= 1000UL; |
| 52 | usec /= 1000UL; |
Jens Axboe | 2bfe24b | 2007-05-21 14:27:32 +0200 | [diff] [blame] | 53 | ret = sec + usec; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 54 | |
Jens Axboe | 2bfe24b | 2007-05-21 14:27:32 +0200 | [diff] [blame] | 55 | /* |
| 56 | * time warp bug on some kernels? |
| 57 | */ |
| 58 | if (ret < 0) |
| 59 | ret = 0; |
| 60 | |
| 61 | return ret; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | unsigned long mtime_since_now(struct timeval *s) |
| 65 | { |
| 66 | struct timeval t; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 67 | void *p = __builtin_return_address(0); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 68 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 69 | fio_gettime(&t, p); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 70 | return mtime_since(s, &t); |
| 71 | } |
| 72 | |
| 73 | unsigned long time_since_now(struct timeval *s) |
| 74 | { |
| 75 | return mtime_since_now(s) / 1000; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * busy looping version for the last few usec |
| 80 | */ |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 81 | void __usec_sleep(unsigned int usec) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 82 | { |
| 83 | struct timeval start; |
| 84 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 85 | fio_gettime(&start, NULL); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 86 | while (utime_since_now(&start) < usec) |
| 87 | nop; |
| 88 | } |
| 89 | |
| 90 | void usec_sleep(struct thread_data *td, unsigned long usec) |
| 91 | { |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 92 | struct timespec req; |
| 93 | struct timeval tv; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 94 | |
| 95 | do { |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 96 | unsigned long ts = usec; |
| 97 | |
| 98 | if (usec < ns_granularity) { |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 99 | __usec_sleep(usec); |
| 100 | break; |
| 101 | } |
| 102 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 103 | ts = usec - ns_granularity; |
| 104 | |
| 105 | if (ts >= 1000000) { |
| 106 | req.tv_sec = ts / 1000000; |
| 107 | ts -= 1000000 * req.tv_sec; |
| 108 | } else |
| 109 | req.tv_sec = 0; |
| 110 | |
| 111 | req.tv_nsec = ts * 1000; |
| 112 | fio_gettime(&tv, NULL); |
| 113 | |
| 114 | if (nanosleep(&req, NULL) < 0) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 115 | break; |
| 116 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 117 | ts = utime_since_now(&tv); |
| 118 | if (ts >= usec) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 119 | break; |
| 120 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 121 | usec -= ts; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 122 | } while (!td->terminate); |
| 123 | } |
| 124 | |
| 125 | void rate_throttle(struct thread_data *td, unsigned long time_spent, |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 126 | unsigned int bytes) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 127 | { |
| 128 | unsigned long usec_cycle; |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 129 | unsigned int bs; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 130 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 131 | if (!td->o.rate && !td->o.rate_iops) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 132 | return; |
| 133 | |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 134 | if (td_rw(td)) |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 135 | bs = td->o.rw_min_bs; |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 136 | else if (td_read(td)) |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 137 | bs = td->o.min_bs[DDIR_READ]; |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 138 | else |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 139 | bs = td->o.min_bs[DDIR_WRITE]; |
Jens Axboe | c3852ae | 2007-02-23 10:47:46 +0100 | [diff] [blame] | 140 | |
| 141 | usec_cycle = td->rate_usec_cycle * (bytes / bs); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 142 | |
| 143 | if (time_spent < usec_cycle) { |
| 144 | unsigned long s = usec_cycle - time_spent; |
| 145 | |
| 146 | td->rate_pending_usleep += s; |
Jens Axboe | a998c86 | 2007-03-15 12:09:39 +0100 | [diff] [blame] | 147 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 148 | if (td->rate_pending_usleep >= 100000) { |
Jens Axboe | a998c86 | 2007-03-15 12:09:39 +0100 | [diff] [blame] | 149 | struct timeval t; |
| 150 | |
| 151 | fio_gettime(&t, NULL); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 152 | usec_sleep(td, td->rate_pending_usleep); |
Jens Axboe | a998c86 | 2007-03-15 12:09:39 +0100 | [diff] [blame] | 153 | td->rate_pending_usleep -= utime_since_now(&t); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 154 | } |
| 155 | } else { |
| 156 | long overtime = time_spent - usec_cycle; |
| 157 | |
| 158 | td->rate_pending_usleep -= overtime; |
| 159 | } |
| 160 | } |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 161 | |
| 162 | unsigned long mtime_since_genesis(void) |
| 163 | { |
| 164 | return mtime_since_now(&genesis); |
| 165 | } |
| 166 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 167 | static void fio_init time_init(void) |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 168 | { |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 169 | int i; |
| 170 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 171 | /* |
| 172 | * Check the granularity of the nanosleep function |
| 173 | */ |
| 174 | for (i = 0; i < 10; i++) { |
| 175 | struct timeval tv; |
| 176 | struct timespec ts; |
| 177 | unsigned long elapsed; |
| 178 | |
| 179 | fio_gettime(&tv, NULL); |
| 180 | ts.tv_sec = 0; |
| 181 | ts.tv_nsec = 1000; |
| 182 | |
| 183 | nanosleep(&ts, NULL); |
| 184 | elapsed = utime_since_now(&tv); |
| 185 | |
| 186 | if (elapsed > ns_granularity) |
| 187 | ns_granularity = elapsed; |
| 188 | } |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 189 | } |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 190 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 191 | void set_genesis_time(void) |
| 192 | { |
| 193 | fio_gettime(&genesis, NULL); |
| 194 | } |
| 195 | |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 196 | void fill_start_time(struct timeval *t) |
| 197 | { |
| 198 | memcpy(t, &genesis, sizeof(genesis)); |
| 199 | } |