6 static struct timeval genesis;
7 static unsigned long ns_granularity;
9 unsigned long utime_since(struct timeval *s, struct timeval *e)
13 sec = e->tv_sec - s->tv_sec;
14 usec = e->tv_usec - s->tv_usec;
15 if (sec > 0 && usec < 0) {
25 unsigned long utime_since_now(struct timeval *s)
29 fio_gettime(&t, NULL);
30 return utime_since(s, &t);
33 unsigned long mtime_since(struct timeval *s, struct timeval *e)
37 sec = e->tv_sec - s->tv_sec;
38 usec = e->tv_usec - s->tv_usec;
39 if (sec > 0 && usec < 0) {
50 unsigned long mtime_since_now(struct timeval *s)
53 void *p = __builtin_return_address(0);
56 return mtime_since(s, &t);
59 unsigned long time_since_now(struct timeval *s)
61 return mtime_since_now(s) / 1000;
65 * busy looping version for the last few usec
67 void __usec_sleep(unsigned int usec)
71 fio_gettime(&start, NULL);
72 while (utime_since_now(&start) < usec)
76 void usec_sleep(struct thread_data *td, unsigned long usec)
82 unsigned long ts = usec;
84 if (usec < ns_granularity) {
89 ts = usec - ns_granularity;
92 req.tv_sec = ts / 1000000;
93 ts -= 1000000 * req.tv_sec;
97 req.tv_nsec = ts * 1000;
98 fio_gettime(&tv, NULL);
100 if (nanosleep(&req, NULL) < 0)
103 ts = utime_since_now(&tv);
108 } while (!td->terminate);
111 void rate_throttle(struct thread_data *td, unsigned long time_spent,
114 unsigned long usec_cycle;
117 if (!td->o.rate && !td->o.rate_iops)
121 bs = td->o.rw_min_bs;
122 else if (td_read(td))
123 bs = td->o.min_bs[DDIR_READ];
125 bs = td->o.min_bs[DDIR_WRITE];
127 usec_cycle = td->rate_usec_cycle * (bytes / bs);
129 if (time_spent < usec_cycle) {
130 unsigned long s = usec_cycle - time_spent;
132 td->rate_pending_usleep += s;
134 if (td->rate_pending_usleep >= 100000) {
137 fio_gettime(&t, NULL);
138 usec_sleep(td, td->rate_pending_usleep);
139 td->rate_pending_usleep -= utime_since_now(&t);
142 long overtime = time_spent - usec_cycle;
144 td->rate_pending_usleep -= overtime;
148 unsigned long mtime_since_genesis(void)
150 return mtime_since_now(&genesis);
153 static void fio_init time_init(void)
158 * Check the granularity of the nanosleep function
160 for (i = 0; i < 10; i++) {
163 unsigned long elapsed;
165 fio_gettime(&tv, NULL);
169 nanosleep(&ts, NULL);
170 elapsed = utime_since_now(&tv);
172 if (elapsed > ns_granularity)
173 ns_granularity = elapsed;
177 void set_genesis_time(void)
179 fio_gettime(&genesis, NULL);
182 void fill_start_time(struct timeval *t)
184 memcpy(t, &genesis, sizeof(genesis));