6 static struct timespec genesis;
7 static unsigned long ns_granularity;
9 void timespec_add_msec(struct timespec *ts, unsigned int msec)
11 unsigned long adj_nsec = 1000000 * msec;
13 ts->tv_nsec += adj_nsec;
14 if (adj_nsec >= 1000000000) {
15 unsigned long adj_sec = adj_nsec / 1000000000UL;
17 ts->tv_nsec -= adj_sec * 1000000000UL;
18 ts->tv_sec += adj_sec;
20 if (ts->tv_nsec >= 1000000000UL){
21 ts->tv_nsec -= 1000000000UL;
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;
63 req.tv_nsec = ts * 1000;
64 fio_gettime(&tv, NULL);
66 if (nanosleep(&req, NULL) < 0)
69 ts = utime_since_now(&tv);
75 } while (!td->terminate);
80 uint64_t time_since_genesis(void)
82 return time_since_now(&genesis);
85 uint64_t mtime_since_genesis(void)
87 return mtime_since_now(&genesis);
90 uint64_t utime_since_genesis(void)
92 return utime_since_now(&genesis);
95 bool in_ramp_time(struct thread_data *td)
97 return td->o.ramp_time && !td->ramp_time_over;
100 static void parent_update_ramp(struct thread_data *td)
102 struct thread_data *parent = td->parent;
104 if (!parent || parent->ramp_time_over)
107 reset_all_stats(parent);
108 parent->ramp_time_over = 1;
109 td_set_runstate(parent, TD_RAMP);
112 bool ramp_time_over(struct thread_data *td)
114 if (!td->o.ramp_time || td->ramp_time_over)
117 if (utime_since_now(&td->epoch) >= td->o.ramp_time) {
118 td->ramp_time_over = 1;
120 td_set_runstate(td, TD_RAMP);
121 parent_update_ramp(td);
128 void fio_time_init(void)
135 * Check the granularity of the nanosleep function
137 for (i = 0; i < 10; i++) {
138 struct timespec tv, ts;
139 unsigned long elapsed;
141 fio_gettime(&tv, NULL);
145 nanosleep(&ts, NULL);
146 elapsed = utime_since_now(&tv);
148 if (elapsed > ns_granularity)
149 ns_granularity = elapsed;
153 void set_genesis_time(void)
155 fio_gettime(&genesis, NULL);
158 void set_epoch_time(struct thread_data *td, int log_unix_epoch)
160 fio_gettime(&td->epoch, NULL);
161 if (log_unix_epoch) {
163 gettimeofday(&tv, NULL);
164 td->unix_epoch = (unsigned long long)(tv.tv_sec) * 1000 +
165 (unsigned long long)(tv.tv_usec) / 1000;
169 void fill_start_time(struct timespec *t)
171 memcpy(t, &genesis, sizeof(genesis));