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 static unsigned long utime_since_now(struct timeval *s)
28 gettimeofday(&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)
53 gettimeofday(&t, NULL);
54 return mtime_since(s, &t);
57 unsigned long time_since_now(struct timeval *s)
59 return mtime_since_now(s) / 1000;
63 * busy looping version for the last few usec
65 void __usec_sleep(unsigned int usec)
69 gettimeofday(&start, NULL);
70 while (utime_since_now(&start) < usec)
74 void usec_sleep(struct thread_data *td, unsigned long usec)
76 struct timespec req, rem;
78 req.tv_sec = usec / 1000000;
79 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
87 rem.tv_sec = rem.tv_nsec = 0;
88 if (nanosleep(&req, &rem) < 0)
91 if ((rem.tv_sec + rem.tv_nsec) == 0)
94 req.tv_nsec = rem.tv_nsec;
95 req.tv_sec = rem.tv_sec;
97 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
98 } while (!td->terminate);
101 void rate_throttle(struct thread_data *td, unsigned long time_spent,
104 unsigned long usec_cycle;
109 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
111 if (time_spent < usec_cycle) {
112 unsigned long s = usec_cycle - time_spent;
114 td->rate_pending_usleep += s;
115 if (td->rate_pending_usleep >= 100000) {
116 usec_sleep(td, td->rate_pending_usleep);
117 td->rate_pending_usleep = 0;
120 long overtime = time_spent - usec_cycle;
122 td->rate_pending_usleep -= overtime;
126 unsigned long mtime_since_genesis(void)
128 return mtime_since_now(&genesis);
133 gettimeofday(&genesis, NULL);