6 static struct timeval genesis;
7 static unsigned long ns_granularity;
9 unsigned long long utime_since(struct timeval *s, struct timeval *e)
12 unsigned long long ret;
14 sec = e->tv_sec - s->tv_sec;
15 usec = e->tv_usec - s->tv_usec;
16 if (sec > 0 && usec < 0) {
22 * time warp bug on some kernels?
24 if (sec < 0 || (sec == 0 && usec < 0))
27 ret = sec * 1000000ULL + usec;
32 unsigned long long utime_since_now(struct timeval *s)
36 fio_gettime(&t, NULL);
37 return utime_since(s, &t);
40 unsigned long mtime_since(struct timeval *s, struct timeval *e)
44 sec = e->tv_sec - s->tv_sec;
45 usec = e->tv_usec - s->tv_usec;
46 if (sec > 0 && usec < 0) {
56 * time warp bug on some kernels?
64 unsigned long mtime_since_now(struct timeval *s)
67 void *p = __builtin_return_address(0);
70 return mtime_since(s, &t);
73 unsigned long time_since_now(struct timeval *s)
75 return mtime_since_now(s) / 1000;
79 * busy looping version for the last few usec
81 void usec_spin(unsigned int usec)
85 fio_gettime(&start, NULL);
86 while (utime_since_now(&start) < usec)
90 void usec_sleep(struct thread_data *td, unsigned long usec)
96 unsigned long ts = usec;
98 if (usec < ns_granularity) {
103 ts = usec - ns_granularity;
106 req.tv_sec = ts / 1000000;
107 ts -= 1000000 * req.tv_sec;
111 req.tv_nsec = ts * 1000;
112 fio_gettime(&tv, NULL);
114 if (nanosleep(&req, NULL) < 0)
117 ts = utime_since_now(&tv);
122 } while (!td->terminate);
125 unsigned long mtime_since_genesis(void)
127 return mtime_since_now(&genesis);
130 int in_ramp_time(struct thread_data *td)
132 return td->o.ramp_time && !td->ramp_time_over;
135 int ramp_time_over(struct thread_data *td)
139 if (!td->o.ramp_time || td->ramp_time_over)
142 fio_gettime(&tv, NULL);
143 if (mtime_since(&td->epoch, &tv) >= td->o.ramp_time * 1000) {
144 td->ramp_time_over = 1;
146 td_set_runstate(td, TD_RAMP);
153 void fio_init fio_time_init(void)
160 * Check the granularity of the nanosleep function
162 for (i = 0; i < 10; i++) {
165 unsigned long elapsed;
167 fio_gettime(&tv, NULL);
171 nanosleep(&ts, NULL);
172 elapsed = utime_since_now(&tv);
174 if (elapsed > ns_granularity)
175 ns_granularity = elapsed;
179 void set_genesis_time(void)
181 fio_gettime(&genesis, NULL);
184 void fill_start_time(struct timeval *t)
186 memcpy(t, &genesis, sizeof(genesis));