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