6 static struct timespec genesis;
7 static unsigned long ns_granularity;
9 void timespec_add_msec(struct timespec *ts, unsigned int msec)
11 uint64_t adj_nsec = 1000000ULL * msec;
13 ts->tv_nsec += adj_nsec;
14 if (adj_nsec >= 1000000000) {
15 uint64_t adj_sec = adj_nsec / 1000000000;
17 ts->tv_nsec -= adj_sec * 1000000000;
18 ts->tv_sec += adj_sec;
20 if (ts->tv_nsec >= 1000000000){
21 ts->tv_nsec -= 1000000000;
27 * busy looping version for the last few usec
29 uint64_t usec_spin(unsigned int usec)
31 struct timespec start;
34 fio_gettime(&start, NULL);
35 while ((t = utime_since_now(&start)) < usec)
41 uint64_t usec_sleep(struct thread_data *td, unsigned long usec)
48 unsigned long ts = usec;
50 if (usec < ns_granularity) {
55 ts = usec - ns_granularity;
58 req.tv_sec = ts / 1000000;
59 ts -= 1000000 * req.tv_sec;
61 * Limit sleep to ~1 second at most, otherwise we
62 * don't notice then someone signaled the job to
70 req.tv_nsec = ts * 1000;
71 fio_gettime(&tv, NULL);
73 if (nanosleep(&req, NULL) < 0)
76 ts = utime_since_now(&tv);
82 } while (!td->terminate);
87 uint64_t time_since_genesis(void)
89 return time_since_now(&genesis);
92 uint64_t mtime_since_genesis(void)
94 return mtime_since_now(&genesis);
97 uint64_t utime_since_genesis(void)
99 return utime_since_now(&genesis);
102 bool in_ramp_time(struct thread_data *td)
104 return td->o.ramp_time && !td->ramp_time_over;
107 static bool parent_update_ramp(struct thread_data *td)
109 struct thread_data *parent = td->parent;
111 if (!parent || parent->ramp_time_over)
114 reset_all_stats(parent);
115 parent->ramp_time_over = true;
116 td_set_runstate(parent, TD_RAMP);
120 bool ramp_time_over(struct thread_data *td)
122 if (!td->o.ramp_time || td->ramp_time_over)
125 if (utime_since_now(&td->epoch) >= td->o.ramp_time) {
126 td->ramp_time_over = true;
129 td_set_runstate(td, TD_RAMP);
132 * If we have a parent, the parent isn't doing IO. Hence
133 * the parent never enters do_io(), which will switch us
134 * from RAMP -> RUNNING. Do this manually here.
136 if (parent_update_ramp(td))
137 td_set_runstate(td, TD_RUNNING);
145 void fio_time_init(void)
152 * Check the granularity of the nanosleep function
154 for (i = 0; i < 10; i++) {
155 struct timespec tv, ts;
156 unsigned long elapsed;
158 fio_gettime(&tv, NULL);
162 nanosleep(&ts, NULL);
163 elapsed = utime_since_now(&tv);
165 if (elapsed > ns_granularity)
166 ns_granularity = elapsed;
170 void set_genesis_time(void)
172 fio_gettime(&genesis, NULL);
175 void set_epoch_time(struct thread_data *td, int log_alternate_epoch, clockid_t clock_id)
177 fio_gettime(&td->epoch, NULL);
178 if (log_alternate_epoch) {
180 clock_gettime(clock_id, &ts);
181 td->alternate_epoch = (unsigned long long)(ts.tv_sec) * 1000 +
182 (unsigned long long)(ts.tv_nsec) / 1000000;
186 void fill_start_time(struct timespec *t)
188 memcpy(t, &genesis, sizeof(genesis));