blob: 643fcea8c04c4f481212443f0fd1e642562ef902 [file] [log] [blame]
Jens Axboe3c39a372006-06-06 20:56:12 +02001#include <time.h>
2#include <sys/time.h>
3
4#include "fio.h"
5
Jens Axboe263e5292006-10-18 15:37:01 +02006static struct timeval genesis;
Jens Axboefd841462007-01-13 12:20:30 +01007static unsigned long ns_granularity;
Jens Axboe263e5292006-10-18 15:37:01 +02008
Shawn Lewis10927312007-11-21 09:38:13 +01009unsigned long long utime_since(struct timeval *s, struct timeval *e)
Jens Axboe3c39a372006-06-06 20:56:12 +020010{
Shawn Lewis10927312007-11-21 09:38:13 +010011 long sec, usec;
12 unsigned long long ret;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010013
Jens Axboe3c39a372006-06-06 20:56:12 +020014 sec = e->tv_sec - s->tv_sec;
15 usec = e->tv_usec - s->tv_usec;
16 if (sec > 0 && usec < 0) {
17 sec--;
18 usec += 1000000;
19 }
20
Jens Axboe2bfe24b2007-05-21 14:27:32 +020021 /*
22 * time warp bug on some kernels?
23 */
Shawn Lewis10927312007-11-21 09:38:13 +010024 if (sec < 0 || (sec == 0 && usec < 0))
25 return 0;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010026
Shawn Lewis10927312007-11-21 09:38:13 +010027 ret = sec * 1000000ULL + usec;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010028
Jens Axboe2bfe24b2007-05-21 14:27:32 +020029 return ret;
Jens Axboe3c39a372006-06-06 20:56:12 +020030}
31
Shawn Lewis10927312007-11-21 09:38:13 +010032unsigned long long utime_since_now(struct timeval *s)
Jens Axboe3c39a372006-06-06 20:56:12 +020033{
34 struct timeval t;
35
Jens Axboe02bcaa82006-11-24 10:42:00 +010036 fio_gettime(&t, NULL);
Jens Axboe3c39a372006-06-06 20:56:12 +020037 return utime_since(s, &t);
38}
39
40unsigned long mtime_since(struct timeval *s, struct timeval *e)
41{
Jens Axboe2bfe24b2007-05-21 14:27:32 +020042 long sec, usec, ret;
Jens Axboe3c39a372006-06-06 20:56:12 +020043
44 sec = e->tv_sec - s->tv_sec;
45 usec = e->tv_usec - s->tv_usec;
46 if (sec > 0 && usec < 0) {
47 sec--;
48 usec += 1000000;
49 }
50
Jens Axboe64b18e12007-03-27 10:03:09 +020051 sec *= 1000UL;
52 usec /= 1000UL;
Jens Axboe2bfe24b2007-05-21 14:27:32 +020053 ret = sec + usec;
Jens Axboe3c39a372006-06-06 20:56:12 +020054
Jens Axboe2bfe24b2007-05-21 14:27:32 +020055 /*
56 * time warp bug on some kernels?
57 */
58 if (ret < 0)
59 ret = 0;
60
61 return ret;
Jens Axboe3c39a372006-06-06 20:56:12 +020062}
63
64unsigned long mtime_since_now(struct timeval *s)
65{
66 struct timeval t;
Jens Axboe02bcaa82006-11-24 10:42:00 +010067 void *p = __builtin_return_address(0);
Jens Axboe3c39a372006-06-06 20:56:12 +020068
Jens Axboe02bcaa82006-11-24 10:42:00 +010069 fio_gettime(&t, p);
Jens Axboe3c39a372006-06-06 20:56:12 +020070 return mtime_since(s, &t);
71}
72
73unsigned long time_since_now(struct timeval *s)
74{
75 return mtime_since_now(s) / 1000;
76}
77
78/*
79 * busy looping version for the last few usec
80 */
Jens Axboe6dd6f2c2008-12-10 13:24:12 +010081void usec_spin(unsigned int usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020082{
83 struct timeval start;
84
Jens Axboe02bcaa82006-11-24 10:42:00 +010085 fio_gettime(&start, NULL);
Jens Axboe3c39a372006-06-06 20:56:12 +020086 while (utime_since_now(&start) < usec)
87 nop;
88}
89
90void usec_sleep(struct thread_data *td, unsigned long usec)
91{
Jens Axboefd841462007-01-13 12:20:30 +010092 struct timespec req;
93 struct timeval tv;
Jens Axboe3c39a372006-06-06 20:56:12 +020094
95 do {
Jens Axboefd841462007-01-13 12:20:30 +010096 unsigned long ts = usec;
97
98 if (usec < ns_granularity) {
Jens Axboe6dd6f2c2008-12-10 13:24:12 +010099 usec_spin(usec);
Jens Axboe3c39a372006-06-06 20:56:12 +0200100 break;
101 }
102
Jens Axboefd841462007-01-13 12:20:30 +0100103 ts = usec - ns_granularity;
104
105 if (ts >= 1000000) {
106 req.tv_sec = ts / 1000000;
107 ts -= 1000000 * req.tv_sec;
108 } else
109 req.tv_sec = 0;
110
111 req.tv_nsec = ts * 1000;
112 fio_gettime(&tv, NULL);
113
114 if (nanosleep(&req, NULL) < 0)
Jens Axboe3c39a372006-06-06 20:56:12 +0200115 break;
116
Jens Axboefd841462007-01-13 12:20:30 +0100117 ts = utime_since_now(&tv);
118 if (ts >= usec)
Jens Axboe3c39a372006-06-06 20:56:12 +0200119 break;
120
Jens Axboefd841462007-01-13 12:20:30 +0100121 usec -= ts;
Jens Axboe3c39a372006-06-06 20:56:12 +0200122 } while (!td->terminate);
123}
124
125void rate_throttle(struct thread_data *td, unsigned long time_spent,
Jens Axboe413dd452007-02-23 09:26:09 +0100126 unsigned int bytes)
Jens Axboe3c39a372006-06-06 20:56:12 +0200127{
128 unsigned long usec_cycle;
Jens Axboec3852ae2007-02-23 10:47:46 +0100129 unsigned int bs;
Jens Axboe3c39a372006-06-06 20:56:12 +0200130
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100131 if (!td->o.rate && !td->o.rate_iops)
Jens Axboe3c39a372006-06-06 20:56:12 +0200132 return;
133
Jens Axboec3852ae2007-02-23 10:47:46 +0100134 if (td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100135 bs = td->o.rw_min_bs;
Jens Axboec3852ae2007-02-23 10:47:46 +0100136 else if (td_read(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100137 bs = td->o.min_bs[DDIR_READ];
Jens Axboec3852ae2007-02-23 10:47:46 +0100138 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100139 bs = td->o.min_bs[DDIR_WRITE];
Jens Axboec3852ae2007-02-23 10:47:46 +0100140
141 usec_cycle = td->rate_usec_cycle * (bytes / bs);
Jens Axboe3c39a372006-06-06 20:56:12 +0200142
143 if (time_spent < usec_cycle) {
144 unsigned long s = usec_cycle - time_spent;
145
146 td->rate_pending_usleep += s;
Jens Axboea998c862007-03-15 12:09:39 +0100147
Jens Axboe3c39a372006-06-06 20:56:12 +0200148 if (td->rate_pending_usleep >= 100000) {
Jens Axboea998c862007-03-15 12:09:39 +0100149 struct timeval t;
150
151 fio_gettime(&t, NULL);
Jens Axboe3c39a372006-06-06 20:56:12 +0200152 usec_sleep(td, td->rate_pending_usleep);
Jens Axboea998c862007-03-15 12:09:39 +0100153 td->rate_pending_usleep -= utime_since_now(&t);
Jens Axboe3c39a372006-06-06 20:56:12 +0200154 }
155 } else {
156 long overtime = time_spent - usec_cycle;
157
158 td->rate_pending_usleep -= overtime;
159 }
160}
Jens Axboe263e5292006-10-18 15:37:01 +0200161
162unsigned long mtime_since_genesis(void)
163{
164 return mtime_since_now(&genesis);
165}
166
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200167int in_ramp_time(struct thread_data *td)
168{
169 return td->o.ramp_time && !td->ramp_time_over;
170}
171
Jens Axboe721938a2008-09-10 09:46:16 +0200172int ramp_time_over(struct thread_data *td)
173{
174 struct timeval tv;
175
176 if (!td->o.ramp_time || td->ramp_time_over)
177 return 1;
178
179 fio_gettime(&tv, NULL);
180 if (mtime_since(&td->epoch, &tv) >= td->o.ramp_time * 1000) {
181 td->ramp_time_over = 1;
Jens Axboeb29ee5b2008-09-11 10:17:26 +0200182 reset_all_stats(td);
183 td_set_runstate(td, TD_RAMP);
Jens Axboe721938a2008-09-10 09:46:16 +0200184 return 1;
185 }
186
187 return 0;
188}
189
Jens Axboe02bcaa82006-11-24 10:42:00 +0100190static void fio_init time_init(void)
Jens Axboe263e5292006-10-18 15:37:01 +0200191{
Jens Axboefd841462007-01-13 12:20:30 +0100192 int i;
193
Jens Axboefd841462007-01-13 12:20:30 +0100194 /*
195 * Check the granularity of the nanosleep function
196 */
197 for (i = 0; i < 10; i++) {
198 struct timeval tv;
199 struct timespec ts;
200 unsigned long elapsed;
201
202 fio_gettime(&tv, NULL);
203 ts.tv_sec = 0;
204 ts.tv_nsec = 1000;
205
206 nanosleep(&ts, NULL);
207 elapsed = utime_since_now(&tv);
208
209 if (elapsed > ns_granularity)
210 ns_granularity = elapsed;
211 }
Jens Axboe263e5292006-10-18 15:37:01 +0200212}
Jens Axboe6043c572006-11-03 11:37:47 +0100213
Jens Axboea2f77c92007-02-22 11:53:00 +0100214void set_genesis_time(void)
215{
216 fio_gettime(&genesis, NULL);
217}
218
Jens Axboe6043c572006-11-03 11:37:47 +0100219void fill_start_time(struct timeval *t)
220{
221 memcpy(t, &genesis, sizeof(genesis));
222}