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 | /* |
| 10 | * busy looping version for the last few usec |
| 11 | */ |
Jens Axboe | 6dd6f2c | 2008-12-10 13:24:12 +0100 | [diff] [blame] | 12 | void usec_spin(unsigned int usec) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 13 | { |
| 14 | struct timeval start; |
| 15 | |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 16 | fio_gettime(&start, NULL); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 17 | while (utime_since_now(&start) < usec) |
| 18 | nop; |
| 19 | } |
| 20 | |
| 21 | void usec_sleep(struct thread_data *td, unsigned long usec) |
| 22 | { |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 23 | struct timespec req; |
| 24 | struct timeval tv; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 25 | |
| 26 | do { |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 27 | unsigned long ts = usec; |
| 28 | |
| 29 | if (usec < ns_granularity) { |
Jens Axboe | 6dd6f2c | 2008-12-10 13:24:12 +0100 | [diff] [blame] | 30 | usec_spin(usec); |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 31 | break; |
| 32 | } |
| 33 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 34 | ts = usec - ns_granularity; |
| 35 | |
| 36 | if (ts >= 1000000) { |
| 37 | req.tv_sec = ts / 1000000; |
| 38 | ts -= 1000000 * req.tv_sec; |
| 39 | } else |
| 40 | req.tv_sec = 0; |
| 41 | |
| 42 | req.tv_nsec = ts * 1000; |
| 43 | fio_gettime(&tv, NULL); |
| 44 | |
| 45 | if (nanosleep(&req, NULL) < 0) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 46 | break; |
| 47 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 48 | ts = utime_since_now(&tv); |
| 49 | if (ts >= usec) |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 50 | break; |
| 51 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 52 | usec -= ts; |
Jens Axboe | 3c39a37 | 2006-06-06 20:56:12 +0200 | [diff] [blame] | 53 | } while (!td->terminate); |
| 54 | } |
| 55 | |
Jens Axboe | aa60bc5 | 2013-01-04 13:24:52 +0100 | [diff] [blame] | 56 | uint64_t mtime_since_genesis(void) |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 57 | { |
| 58 | return mtime_since_now(&genesis); |
| 59 | } |
| 60 | |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 61 | int in_ramp_time(struct thread_data *td) |
| 62 | { |
| 63 | return td->o.ramp_time && !td->ramp_time_over; |
| 64 | } |
| 65 | |
Jens Axboe | 721938a | 2008-09-10 09:46:16 +0200 | [diff] [blame] | 66 | int ramp_time_over(struct thread_data *td) |
| 67 | { |
| 68 | struct timeval tv; |
| 69 | |
| 70 | if (!td->o.ramp_time || td->ramp_time_over) |
| 71 | return 1; |
| 72 | |
| 73 | fio_gettime(&tv, NULL); |
Christian Ehrhardt | 74454ce | 2014-02-20 14:20:01 +0100 | [diff] [blame] | 74 | if (mtime_since(&td->epoch, &tv) >= td->o.ramp_time ) { |
Jens Axboe | 721938a | 2008-09-10 09:46:16 +0200 | [diff] [blame] | 75 | td->ramp_time_over = 1; |
Jens Axboe | b29ee5b | 2008-09-11 10:17:26 +0200 | [diff] [blame] | 76 | reset_all_stats(td); |
| 77 | td_set_runstate(td, TD_RAMP); |
Jens Axboe | 721938a | 2008-09-10 09:46:16 +0200 | [diff] [blame] | 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 84 | void fio_time_init(void) |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 85 | { |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 86 | int i; |
| 87 | |
Jens Axboe | c223da8 | 2010-03-24 13:23:53 +0100 | [diff] [blame] | 88 | fio_clock_init(); |
| 89 | |
Jens Axboe | fd84146 | 2007-01-13 12:20:30 +0100 | [diff] [blame] | 90 | /* |
| 91 | * Check the granularity of the nanosleep function |
| 92 | */ |
| 93 | for (i = 0; i < 10; i++) { |
| 94 | struct timeval tv; |
| 95 | struct timespec ts; |
| 96 | unsigned long elapsed; |
| 97 | |
| 98 | fio_gettime(&tv, NULL); |
| 99 | ts.tv_sec = 0; |
| 100 | ts.tv_nsec = 1000; |
| 101 | |
| 102 | nanosleep(&ts, NULL); |
| 103 | elapsed = utime_since_now(&tv); |
| 104 | |
| 105 | if (elapsed > ns_granularity) |
| 106 | ns_granularity = elapsed; |
| 107 | } |
Jens Axboe | 263e529 | 2006-10-18 15:37:01 +0200 | [diff] [blame] | 108 | } |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 109 | |
Jens Axboe | a2f77c9 | 2007-02-22 11:53:00 +0100 | [diff] [blame] | 110 | void set_genesis_time(void) |
| 111 | { |
| 112 | fio_gettime(&genesis, NULL); |
| 113 | } |
| 114 | |
Jens Axboe | 6043c57 | 2006-11-03 11:37:47 +0100 | [diff] [blame] | 115 | void fill_start_time(struct timeval *t) |
| 116 | { |
| 117 | memcpy(t, &genesis, sizeof(genesis)); |
| 118 | } |