| 1 | #include <time.h> |
| 2 | #include <sys/time.h> |
| 3 | |
| 4 | #include "fio.h" |
| 5 | |
| 6 | static struct timeval genesis; |
| 7 | static unsigned long ns_granularity; |
| 8 | |
| 9 | /* |
| 10 | * busy looping version for the last few usec |
| 11 | */ |
| 12 | uint64_t usec_spin(unsigned int usec) |
| 13 | { |
| 14 | struct timeval start; |
| 15 | uint64_t t; |
| 16 | |
| 17 | fio_gettime(&start, NULL); |
| 18 | while ((t = utime_since_now(&start)) < usec) |
| 19 | nop; |
| 20 | |
| 21 | return t; |
| 22 | } |
| 23 | |
| 24 | uint64_t usec_sleep(struct thread_data *td, unsigned long usec) |
| 25 | { |
| 26 | struct timespec req; |
| 27 | struct timeval tv; |
| 28 | uint64_t t = 0; |
| 29 | |
| 30 | do { |
| 31 | unsigned long ts = usec; |
| 32 | |
| 33 | if (usec < ns_granularity) { |
| 34 | t += usec_spin(usec); |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | ts = usec - ns_granularity; |
| 39 | |
| 40 | if (ts >= 1000000) { |
| 41 | req.tv_sec = ts / 1000000; |
| 42 | ts -= 1000000 * req.tv_sec; |
| 43 | } else |
| 44 | req.tv_sec = 0; |
| 45 | |
| 46 | req.tv_nsec = ts * 1000; |
| 47 | fio_gettime(&tv, NULL); |
| 48 | |
| 49 | if (nanosleep(&req, NULL) < 0) |
| 50 | break; |
| 51 | |
| 52 | ts = utime_since_now(&tv); |
| 53 | t += ts; |
| 54 | if (ts >= usec) |
| 55 | break; |
| 56 | |
| 57 | usec -= ts; |
| 58 | } while (!td->terminate); |
| 59 | |
| 60 | return t; |
| 61 | } |
| 62 | |
| 63 | uint64_t time_since_genesis(void) |
| 64 | { |
| 65 | return time_since_now(&genesis); |
| 66 | } |
| 67 | |
| 68 | uint64_t mtime_since_genesis(void) |
| 69 | { |
| 70 | return mtime_since_now(&genesis); |
| 71 | } |
| 72 | |
| 73 | uint64_t utime_since_genesis(void) |
| 74 | { |
| 75 | return utime_since_now(&genesis); |
| 76 | } |
| 77 | |
| 78 | int in_ramp_time(struct thread_data *td) |
| 79 | { |
| 80 | return td->o.ramp_time && !td->ramp_time_over; |
| 81 | } |
| 82 | |
| 83 | static void parent_update_ramp(struct thread_data *td) |
| 84 | { |
| 85 | struct thread_data *parent = td->parent; |
| 86 | |
| 87 | if (!parent || parent->ramp_time_over) |
| 88 | return; |
| 89 | |
| 90 | reset_all_stats(parent); |
| 91 | parent->ramp_time_over = 1; |
| 92 | td_set_runstate(parent, TD_RAMP); |
| 93 | } |
| 94 | |
| 95 | int ramp_time_over(struct thread_data *td) |
| 96 | { |
| 97 | struct timeval tv; |
| 98 | |
| 99 | if (!td->o.ramp_time || td->ramp_time_over) |
| 100 | return 1; |
| 101 | |
| 102 | fio_gettime(&tv, NULL); |
| 103 | if (utime_since(&td->epoch, &tv) >= td->o.ramp_time) { |
| 104 | td->ramp_time_over = 1; |
| 105 | reset_all_stats(td); |
| 106 | td_set_runstate(td, TD_RAMP); |
| 107 | parent_update_ramp(td); |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | void fio_time_init(void) |
| 115 | { |
| 116 | int i; |
| 117 | |
| 118 | fio_clock_init(); |
| 119 | |
| 120 | /* |
| 121 | * Check the granularity of the nanosleep function |
| 122 | */ |
| 123 | for (i = 0; i < 10; i++) { |
| 124 | struct timeval tv; |
| 125 | struct timespec ts; |
| 126 | unsigned long elapsed; |
| 127 | |
| 128 | fio_gettime(&tv, NULL); |
| 129 | ts.tv_sec = 0; |
| 130 | ts.tv_nsec = 1000; |
| 131 | |
| 132 | nanosleep(&ts, NULL); |
| 133 | elapsed = utime_since_now(&tv); |
| 134 | |
| 135 | if (elapsed > ns_granularity) |
| 136 | ns_granularity = elapsed; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void set_genesis_time(void) |
| 141 | { |
| 142 | fio_gettime(&genesis, NULL); |
| 143 | } |
| 144 | |
| 145 | void fill_start_time(struct timeval *t) |
| 146 | { |
| 147 | memcpy(t, &genesis, sizeof(genesis)); |
| 148 | } |