6 static struct timeval genesis;
8 unsigned long utime_since(struct timeval *s, struct timeval *e)
12 sec = e->tv_sec - s->tv_sec;
13 usec = e->tv_usec - s->tv_usec;
14 if (sec > 0 && usec < 0) {
19 sec *= (double) 1000000;
24 unsigned long utime_since_now(struct timeval *s)
28 fio_gettime(&t, NULL);
29 return utime_since(s, &t);
32 unsigned long mtime_since(struct timeval *s, struct timeval *e)
36 sec = e->tv_sec - s->tv_sec;
37 usec = e->tv_usec - s->tv_usec;
38 if (sec > 0 && usec < 0) {
44 usec /= (double) 1000;
49 unsigned long mtime_since_now(struct timeval *s)
52 void *p = __builtin_return_address(0);
55 return mtime_since(s, &t);
58 unsigned long time_since_now(struct timeval *s)
60 return mtime_since_now(s) / 1000;
64 * busy looping version for the last few usec
66 void __usec_sleep(unsigned int usec)
70 fio_gettime(&start, NULL);
71 while (utime_since_now(&start) < usec)
75 void usec_sleep(struct thread_data *td, unsigned long usec)
77 struct timespec req, rem;
79 req.tv_sec = usec / 1000000;
80 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
88 rem.tv_sec = rem.tv_nsec = 0;
89 if (nanosleep(&req, &rem) < 0)
92 if ((rem.tv_sec + rem.tv_nsec) == 0)
95 req.tv_nsec = rem.tv_nsec;
96 req.tv_sec = rem.tv_sec;
98 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
99 } while (!td->terminate);
102 void rate_throttle(struct thread_data *td, unsigned long time_spent,
103 unsigned int bytes, int ddir)
105 unsigned long usec_cycle;
110 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs[ddir]);
112 if (time_spent < usec_cycle) {
113 unsigned long s = usec_cycle - time_spent;
115 td->rate_pending_usleep += s;
116 if (td->rate_pending_usleep >= 100000) {
117 usec_sleep(td, td->rate_pending_usleep);
118 td->rate_pending_usleep = 0;
121 long overtime = time_spent - usec_cycle;
123 td->rate_pending_usleep -= overtime;
127 unsigned long mtime_since_genesis(void)
129 return mtime_since_now(&genesis);
132 static void fio_init time_init(void)
134 fio_gettime(&genesis, NULL);
137 void fill_start_time(struct timeval *t)
139 memcpy(t, &genesis, sizeof(genesis));