6 unsigned long utime_since(struct timeval *s, struct timeval *e)
10 sec = e->tv_sec - s->tv_sec;
11 usec = e->tv_usec - s->tv_usec;
12 if (sec > 0 && usec < 0) {
17 sec *= (double) 1000000;
22 static unsigned long utime_since_now(struct timeval *s)
26 gettimeofday(&t, NULL);
27 return utime_since(s, &t);
30 unsigned long mtime_since(struct timeval *s, struct timeval *e)
34 sec = e->tv_sec - s->tv_sec;
35 usec = e->tv_usec - s->tv_usec;
36 if (sec > 0 && usec < 0) {
42 usec /= (double) 1000;
47 unsigned long mtime_since_now(struct timeval *s)
51 gettimeofday(&t, NULL);
52 return mtime_since(s, &t);
55 unsigned long time_since_now(struct timeval *s)
57 return mtime_since_now(s) / 1000;
61 * busy looping version for the last few usec
63 void __usec_sleep(unsigned int usec)
67 gettimeofday(&start, NULL);
68 while (utime_since_now(&start) < usec)
72 void usec_sleep(struct thread_data *td, unsigned long usec)
74 struct timespec req, rem;
76 req.tv_sec = usec / 1000000;
77 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
85 rem.tv_sec = rem.tv_nsec = 0;
86 if (nanosleep(&req, &rem) < 0)
89 if ((rem.tv_sec + rem.tv_nsec) == 0)
92 req.tv_nsec = rem.tv_nsec;
93 req.tv_sec = rem.tv_sec;
95 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
96 } while (!td->terminate);
99 void rate_throttle(struct thread_data *td, unsigned long time_spent,
102 unsigned long usec_cycle;
107 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
109 if (time_spent < usec_cycle) {
110 unsigned long s = usec_cycle - time_spent;
112 td->rate_pending_usleep += s;
113 if (td->rate_pending_usleep >= 100000) {
114 usec_sleep(td, td->rate_pending_usleep);
115 td->rate_pending_usleep = 0;
118 long overtime = time_spent - usec_cycle;
120 td->rate_pending_usleep -= overtime;