6 static struct timeval genesis;
7 static unsigned long ns_granularity;
10 * busy looping version for the last few usec
12 uint64_t usec_spin(unsigned int usec)
17 fio_gettime(&start, NULL);
18 while ((t = utime_since_now(&start)) < usec)
24 uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
31 unsigned long ts = usec;
33 if (usec < ns_granularity) {
38 ts = usec - ns_granularity;
41 req.tv_sec = ts / 1000000;
42 ts -= 1000000 * req.tv_sec;
46 req.tv_nsec = ts * 1000;
47 fio_gettime(&tv, NULL);
49 if (nanosleep(&req, NULL) < 0)
52 ts = utime_since_now(&tv);
58 } while (!td->terminate);
63 uint64_t time_since_genesis(void)
65 return time_since_now(&genesis);
68 uint64_t mtime_since_genesis(void)
70 return mtime_since_now(&genesis);
73 uint64_t utime_since_genesis(void)
75 return utime_since_now(&genesis);
78 int in_ramp_time(struct thread_data *td)
80 return td->o.ramp_time && !td->ramp_time_over;
83 static void parent_update_ramp(struct thread_data *td)
85 struct thread_data *parent = td->parent;
87 if (!parent || parent->ramp_time_over)
90 reset_all_stats(parent);
91 parent->ramp_time_over = 1;
92 td_set_runstate(parent, TD_RAMP);
95 int ramp_time_over(struct thread_data *td)
99 if (!td->o.ramp_time || td->ramp_time_over)
102 fio_gettime(&tv, NULL);
103 if (utime_since(&td->epoch, &tv) >= td->o.ramp_time) {
104 td->ramp_time_over = 1;
106 td_set_runstate(td, TD_RAMP);
107 parent_update_ramp(td);
114 void fio_time_init(void)
121 * Check the granularity of the nanosleep function
123 for (i = 0; i < 10; i++) {
126 unsigned long elapsed;
128 fio_gettime(&tv, NULL);
132 nanosleep(&ts, NULL);
133 elapsed = utime_since_now(&tv);
135 if (elapsed > ns_granularity)
136 ns_granularity = elapsed;
140 void set_genesis_time(void)
142 fio_gettime(&genesis, NULL);
145 void fill_start_time(struct timeval *t)
147 memcpy(t, &genesis, sizeof(genesis));