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; |
| 7 | |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 8 | unsigned long utime_since(struct timeval *s, struct timeval *e) |
| 9 | { |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame^] | 10 | long sec, usec; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 11 | |
| 12 | sec = e->tv_sec - s->tv_sec; |
| 13 | usec = e->tv_usec - s->tv_usec; |
| 14 | if (sec > 0 && usec < 0) { |
| 15 | sec--; |
| 16 | usec += 1000000; |
| 17 | } |
| 18 | |
| 19 | sec *= (double) 1000000; |
| 20 | |
| 21 | return sec + usec; |
| 22 | } |
| 23 | |
Jens Axboe | 6900899 | 2006-11-24 13:12:56 +0100 | [diff] [blame] | 24 | unsigned long utime_since_now(struct timeval *s) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 25 | { |
| 26 | struct timeval t; |
| 27 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 28 | fio_gettime(&t, NULL); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 29 | return utime_since(s, &t); |
| 30 | } |
| 31 | |
| 32 | unsigned long mtime_since(struct timeval *s, struct timeval *e) |
| 33 | { |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame^] | 34 | long sec, usec; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 35 | |
| 36 | sec = e->tv_sec - s->tv_sec; |
| 37 | usec = e->tv_usec - s->tv_usec; |
| 38 | if (sec > 0 && usec < 0) { |
| 39 | sec--; |
| 40 | usec += 1000000; |
| 41 | } |
| 42 | |
| 43 | sec *= (double) 1000; |
| 44 | usec /= (double) 1000; |
| 45 | |
| 46 | return sec + usec; |
| 47 | } |
| 48 | |
| 49 | unsigned long mtime_since_now(struct timeval *s) |
| 50 | { |
| 51 | struct timeval t; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 52 | void *p = __builtin_return_address(0); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 53 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 54 | fio_gettime(&t, p); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 55 | return mtime_since(s, &t); |
| 56 | } |
| 57 | |
| 58 | unsigned long time_since_now(struct timeval *s) |
| 59 | { |
| 60 | return mtime_since_now(s) / 1000; |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * busy looping version for the last few usec |
| 65 | */ |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 66 | void __usec_sleep(unsigned int usec) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 67 | { |
| 68 | struct timeval start; |
| 69 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 70 | fio_gettime(&start, NULL); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 71 | while (utime_since_now(&start) < usec) |
| 72 | nop; |
| 73 | } |
| 74 | |
| 75 | void usec_sleep(struct thread_data *td, unsigned long usec) |
| 76 | { |
| 77 | struct timespec req, rem; |
| 78 | |
| 79 | req.tv_sec = usec / 1000000; |
| 80 | req.tv_nsec = usec * 1000 - req.tv_sec * 1000000; |
| 81 | |
| 82 | do { |
| 83 | if (usec < 5000) { |
| 84 | __usec_sleep(usec); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | rem.tv_sec = rem.tv_nsec = 0; |
| 89 | if (nanosleep(&req, &rem) < 0) |
| 90 | break; |
| 91 | |
| 92 | if ((rem.tv_sec + rem.tv_nsec) == 0) |
| 93 | break; |
| 94 | |
| 95 | req.tv_nsec = rem.tv_nsec; |
| 96 | req.tv_sec = rem.tv_sec; |
| 97 | |
| 98 | usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000; |
| 99 | } while (!td->terminate); |
| 100 | } |
| 101 | |
| 102 | void rate_throttle(struct thread_data *td, unsigned long time_spent, |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 103 | unsigned int bytes, int ddir) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 104 | { |
| 105 | unsigned long usec_cycle; |
| 106 | |
| 107 | if (!td->rate) |
| 108 | return; |
| 109 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 110 | usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs[ddir]); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 111 | |
| 112 | if (time_spent < usec_cycle) { |
| 113 | unsigned long s = usec_cycle - time_spent; |
| 114 | |
| 115 | td->rate_pending_usleep += s; |
| 116 | if (td->rate_pending_usleep >= 100000) { |
| 117 | usec_sleep(td, td->rate_pending_usleep); |
| 118 | td->rate_pending_usleep = 0; |
| 119 | } |
| 120 | } else { |
| 121 | long overtime = time_spent - usec_cycle; |
| 122 | |
| 123 | td->rate_pending_usleep -= overtime; |
| 124 | } |
| 125 | } |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 126 | |
| 127 | unsigned long mtime_since_genesis(void) |
| 128 | { |
| 129 | return mtime_since_now(&genesis); |
| 130 | } |
| 131 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 132 | static void fio_init time_init(void) |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 133 | { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 134 | fio_gettime(&genesis, NULL); |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 135 | } |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 136 | |
| 137 | void fill_start_time(struct timeval *t) |
| 138 | { |
| 139 | memcpy(t, &genesis, sizeof(genesis)); |
| 140 | } |