Commit | Line | Data |
---|---|---|
3c39a379 JA |
1 | #include <time.h> |
2 | #include <sys/time.h> | |
3 | ||
4 | #include "fio.h" | |
5 | ||
8b6a404c | 6 | static struct timespec genesis; |
fd841467 | 7 | static unsigned long ns_granularity; |
263e529f | 8 | |
8b6a404c | 9 | void timespec_add_msec(struct timespec *ts, unsigned int msec) |
a47591e4 | 10 | { |
90e2cd1e | 11 | uint64_t adj_nsec = 1000000ULL * msec; |
d19251c4 | 12 | |
8b6a404c VF |
13 | ts->tv_nsec += adj_nsec; |
14 | if (adj_nsec >= 1000000000) { | |
90e2cd1e | 15 | uint64_t adj_sec = adj_nsec / 1000000000; |
d19251c4 | 16 | |
90e2cd1e | 17 | ts->tv_nsec -= adj_sec * 1000000000; |
8b6a404c | 18 | ts->tv_sec += adj_sec; |
d19251c4 | 19 | } |
90e2cd1e SW |
20 | if (ts->tv_nsec >= 1000000000){ |
21 | ts->tv_nsec -= 1000000000; | |
8b6a404c | 22 | ts->tv_sec++; |
a47591e4 JA |
23 | } |
24 | } | |
25 | ||
3c39a379 JA |
26 | /* |
27 | * busy looping version for the last few usec | |
28 | */ | |
9cc80b6d | 29 | uint64_t usec_spin(unsigned int usec) |
3c39a379 | 30 | { |
8b6a404c | 31 | struct timespec start; |
9cc80b6d | 32 | uint64_t t; |
3c39a379 | 33 | |
02bcaa8c | 34 | fio_gettime(&start, NULL); |
9cc80b6d | 35 | while ((t = utime_since_now(&start)) < usec) |
3c39a379 | 36 | nop; |
9cc80b6d JA |
37 | |
38 | return t; | |
3c39a379 JA |
39 | } |
40 | ||
9cc80b6d | 41 | uint64_t usec_sleep(struct thread_data *td, unsigned long usec) |
3c39a379 | 42 | { |
fd841467 | 43 | struct timespec req; |
8b6a404c | 44 | struct timespec tv; |
9cc80b6d | 45 | uint64_t t = 0; |
3c39a379 JA |
46 | |
47 | do { | |
fd841467 JA |
48 | unsigned long ts = usec; |
49 | ||
50 | if (usec < ns_granularity) { | |
9cc80b6d | 51 | t += usec_spin(usec); |
3c39a379 JA |
52 | break; |
53 | } | |
54 | ||
fd841467 | 55 | ts = usec - ns_granularity; |
3c39a379 | 56 | |
fd841467 JA |
57 | if (ts >= 1000000) { |
58 | req.tv_sec = ts / 1000000; | |
59 | ts -= 1000000 * req.tv_sec; | |
60 | } else | |
61 | req.tv_sec = 0; | |
62 | ||
63 | req.tv_nsec = ts * 1000; | |
64 | fio_gettime(&tv, NULL); | |
65 | ||
66 | if (nanosleep(&req, NULL) < 0) | |
3c39a379 JA |
67 | break; |
68 | ||
fd841467 | 69 | ts = utime_since_now(&tv); |
9cc80b6d | 70 | t += ts; |
fd841467 JA |
71 | if (ts >= usec) |
72 | break; | |
3c39a379 | 73 | |
fd841467 | 74 | usec -= ts; |
3c39a379 | 75 | } while (!td->terminate); |
9cc80b6d JA |
76 | |
77 | return t; | |
3c39a379 JA |
78 | } |
79 | ||
8b22c785 JA |
80 | uint64_t time_since_genesis(void) |
81 | { | |
82 | return time_since_now(&genesis); | |
83 | } | |
84 | ||
aa60bc58 | 85 | uint64_t mtime_since_genesis(void) |
263e529f JA |
86 | { |
87 | return mtime_since_now(&genesis); | |
88 | } | |
89 | ||
0de5b26f JA |
90 | uint64_t utime_since_genesis(void) |
91 | { | |
92 | return utime_since_now(&genesis); | |
93 | } | |
94 | ||
356014ff | 95 | bool in_ramp_time(struct thread_data *td) |
b29ee5b3 JA |
96 | { |
97 | return td->o.ramp_time && !td->ramp_time_over; | |
98 | } | |
99 | ||
3eeb1620 | 100 | static bool parent_update_ramp(struct thread_data *td) |
ada8f235 JA |
101 | { |
102 | struct thread_data *parent = td->parent; | |
103 | ||
104 | if (!parent || parent->ramp_time_over) | |
3eeb1620 | 105 | return false; |
ada8f235 JA |
106 | |
107 | reset_all_stats(parent); | |
49f3f914 | 108 | parent->ramp_time_over = true; |
ada8f235 | 109 | td_set_runstate(parent, TD_RAMP); |
3eeb1620 | 110 | return true; |
ada8f235 JA |
111 | } |
112 | ||
356014ff | 113 | bool ramp_time_over(struct thread_data *td) |
721938ae | 114 | { |
721938ae | 115 | if (!td->o.ramp_time || td->ramp_time_over) |
356014ff | 116 | return true; |
721938ae | 117 | |
8b6a404c | 118 | if (utime_since_now(&td->epoch) >= td->o.ramp_time) { |
49f3f914 | 119 | td->ramp_time_over = true; |
b29ee5b3 JA |
120 | reset_all_stats(td); |
121 | td_set_runstate(td, TD_RAMP); | |
3eeb1620 JA |
122 | |
123 | /* | |
124 | * If we have a parent, the parent isn't doing IO. Hence | |
125 | * the parent never enters do_io(), which will switch us | |
126 | * from RAMP -> RUNNING. Do this manually here. | |
127 | */ | |
128 | if (parent_update_ramp(td)) | |
129 | td_set_runstate(td, TD_RUNNING); | |
130 | ||
356014ff | 131 | return true; |
721938ae JA |
132 | } |
133 | ||
356014ff | 134 | return false; |
721938ae JA |
135 | } |
136 | ||
03e20d68 | 137 | void fio_time_init(void) |
263e529f | 138 | { |
fd841467 JA |
139 | int i; |
140 | ||
c223da83 JA |
141 | fio_clock_init(); |
142 | ||
fd841467 JA |
143 | /* |
144 | * Check the granularity of the nanosleep function | |
145 | */ | |
146 | for (i = 0; i < 10; i++) { | |
8b6a404c | 147 | struct timespec tv, ts; |
fd841467 JA |
148 | unsigned long elapsed; |
149 | ||
150 | fio_gettime(&tv, NULL); | |
151 | ts.tv_sec = 0; | |
152 | ts.tv_nsec = 1000; | |
153 | ||
154 | nanosleep(&ts, NULL); | |
155 | elapsed = utime_since_now(&tv); | |
156 | ||
157 | if (elapsed > ns_granularity) | |
158 | ns_granularity = elapsed; | |
159 | } | |
263e529f | 160 | } |
6043c579 | 161 | |
a2f77c9f JA |
162 | void set_genesis_time(void) |
163 | { | |
164 | fio_gettime(&genesis, NULL); | |
165 | } | |
166 | ||
3aea75b1 KC |
167 | void set_epoch_time(struct thread_data *td, int log_unix_epoch) |
168 | { | |
169 | fio_gettime(&td->epoch, NULL); | |
170 | if (log_unix_epoch) { | |
171 | struct timeval tv; | |
172 | gettimeofday(&tv, NULL); | |
173 | td->unix_epoch = (unsigned long long)(tv.tv_sec) * 1000 + | |
174 | (unsigned long long)(tv.tv_usec) / 1000; | |
175 | } | |
176 | } | |
177 | ||
8b6a404c | 178 | void fill_start_time(struct timespec *t) |
6043c579 JA |
179 | { |
180 | memcpy(t, &genesis, sizeof(genesis)); | |
181 | } |