6 static struct timeval genesis;
7 static unsigned long ns_granularity;
8 unsigned long long genesis_cycles;
10 unsigned long long utime_since(struct timeval *s, struct timeval *e)
13 unsigned long long ret;
15 sec = e->tv_sec - s->tv_sec;
16 usec = e->tv_usec - s->tv_usec;
17 if (sec > 0 && usec < 0) {
23 * time warp bug on some kernels?
25 if (sec < 0 || (sec == 0 && usec < 0))
28 ret = sec * 1000000ULL + usec;
33 unsigned long long utime_since_now(struct timeval *s)
37 fio_gettime(&t, NULL);
38 return utime_since(s, &t);
41 unsigned long mtime_since(struct timeval *s, struct timeval *e)
45 sec = e->tv_sec - s->tv_sec;
46 usec = e->tv_usec - s->tv_usec;
47 if (sec > 0 && usec < 0) {
57 * time warp bug on some kernels?
65 unsigned long mtime_since_now(struct timeval *s)
68 void *p = __builtin_return_address(0);
71 return mtime_since(s, &t);
74 unsigned long time_since_now(struct timeval *s)
76 return mtime_since_now(s) / 1000;
80 * busy looping version for the last few usec
82 void usec_spin(unsigned int usec)
86 fio_gettime(&start, NULL);
87 while (utime_since_now(&start) < usec)
91 void usec_sleep(struct thread_data *td, unsigned long usec)
97 unsigned long ts = usec;
99 if (usec < ns_granularity) {
104 ts = usec - ns_granularity;
107 req.tv_sec = ts / 1000000;
108 ts -= 1000000 * req.tv_sec;
112 req.tv_nsec = ts * 1000;
113 fio_gettime(&tv, NULL);
115 if (nanosleep(&req, NULL) < 0)
118 ts = utime_since_now(&tv);
123 } while (!td->terminate);
126 unsigned long mtime_since_genesis(void)
128 return mtime_since_now(&genesis);
131 int in_ramp_time(struct thread_data *td)
133 return td->o.ramp_time && !td->ramp_time_over;
136 int ramp_time_over(struct thread_data *td)
140 if (!td->o.ramp_time || td->ramp_time_over)
143 fio_gettime(&tv, NULL);
144 if (mtime_since(&td->epoch, &tv) >= td->o.ramp_time * 1000) {
145 td->ramp_time_over = 1;
147 td_set_runstate(td, TD_RAMP);
154 void fio_init fio_time_init(void)
161 * Check the granularity of the nanosleep function
163 for (i = 0; i < 10; i++) {
166 unsigned long elapsed;
168 fio_gettime(&tv, NULL);
172 nanosleep(&ts, NULL);
173 elapsed = utime_since_now(&tv);
175 if (elapsed > ns_granularity)
176 ns_granularity = elapsed;
180 void set_genesis_time(void)
182 fio_gettime(&genesis, NULL);
183 genesis_cycles = get_cpu_clock();
186 void fill_start_time(struct timeval *t)
188 memcpy(t, &genesis, sizeof(genesis));