blob: d0ecbe5c831af59f857164af9fa433d96eaceda0 [file] [log] [blame]
Jens Axboe3c39a372006-06-06 20:56:12 +02001#include <time.h>
2#include <sys/time.h>
3
4#include "fio.h"
5
Jens Axboe263e5292006-10-18 15:37:01 +02006static struct timeval genesis;
7
Jens Axboe3c39a372006-06-06 20:56:12 +02008unsigned long utime_since(struct timeval *s, struct timeval *e)
9{
Jens Axboe1e97cce2006-12-05 11:44:16 +010010 long sec, usec;
Jens Axboe3c39a372006-06-06 20:56:12 +020011
12 sec = e->tv_sec - s->tv_sec;
13 usec = e->tv_usec - s->tv_usec;
14 if (sec > 0 && usec < 0) {
15 sec--;
16 usec += 1000000;
17 }
18
19 sec *= (double) 1000000;
20
21 return sec + usec;
22}
23
Jens Axboe69008992006-11-24 13:12:56 +010024unsigned long utime_since_now(struct timeval *s)
Jens Axboe3c39a372006-06-06 20:56:12 +020025{
26 struct timeval t;
27
Jens Axboe02bcaa82006-11-24 10:42:00 +010028 fio_gettime(&t, NULL);
Jens Axboe3c39a372006-06-06 20:56:12 +020029 return utime_since(s, &t);
30}
31
32unsigned long mtime_since(struct timeval *s, struct timeval *e)
33{
Jens Axboe1e97cce2006-12-05 11:44:16 +010034 long sec, usec;
Jens Axboe3c39a372006-06-06 20:56:12 +020035
36 sec = e->tv_sec - s->tv_sec;
37 usec = e->tv_usec - s->tv_usec;
38 if (sec > 0 && usec < 0) {
39 sec--;
40 usec += 1000000;
41 }
42
43 sec *= (double) 1000;
44 usec /= (double) 1000;
45
46 return sec + usec;
47}
48
49unsigned long mtime_since_now(struct timeval *s)
50{
51 struct timeval t;
Jens Axboe02bcaa82006-11-24 10:42:00 +010052 void *p = __builtin_return_address(0);
Jens Axboe3c39a372006-06-06 20:56:12 +020053
Jens Axboe02bcaa82006-11-24 10:42:00 +010054 fio_gettime(&t, p);
Jens Axboe3c39a372006-06-06 20:56:12 +020055 return mtime_since(s, &t);
56}
57
58unsigned long time_since_now(struct timeval *s)
59{
60 return mtime_since_now(s) / 1000;
61}
62
63/*
64 * busy looping version for the last few usec
65 */
Jens Axboeb990b5c2006-09-14 09:48:22 +020066void __usec_sleep(unsigned int usec)
Jens Axboe3c39a372006-06-06 20:56:12 +020067{
68 struct timeval start;
69
Jens Axboe02bcaa82006-11-24 10:42:00 +010070 fio_gettime(&start, NULL);
Jens Axboe3c39a372006-06-06 20:56:12 +020071 while (utime_since_now(&start) < usec)
72 nop;
73}
74
75void usec_sleep(struct thread_data *td, unsigned long usec)
76{
77 struct timespec req, rem;
78
79 req.tv_sec = usec / 1000000;
80 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
81
82 do {
83 if (usec < 5000) {
84 __usec_sleep(usec);
85 break;
86 }
87
88 rem.tv_sec = rem.tv_nsec = 0;
89 if (nanosleep(&req, &rem) < 0)
90 break;
91
92 if ((rem.tv_sec + rem.tv_nsec) == 0)
93 break;
94
95 req.tv_nsec = rem.tv_nsec;
96 req.tv_sec = rem.tv_sec;
97
98 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
99 } while (!td->terminate);
100}
101
102void rate_throttle(struct thread_data *td, unsigned long time_spent,
Jens Axboea00735e2006-11-03 08:58:08 +0100103 unsigned int bytes, int ddir)
Jens Axboe3c39a372006-06-06 20:56:12 +0200104{
105 unsigned long usec_cycle;
106
107 if (!td->rate)
108 return;
109
Jens Axboea00735e2006-11-03 08:58:08 +0100110 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs[ddir]);
Jens Axboe3c39a372006-06-06 20:56:12 +0200111
112 if (time_spent < usec_cycle) {
113 unsigned long s = usec_cycle - time_spent;
114
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;
119 }
120 } else {
121 long overtime = time_spent - usec_cycle;
122
123 td->rate_pending_usleep -= overtime;
124 }
125}
Jens Axboe263e5292006-10-18 15:37:01 +0200126
127unsigned long mtime_since_genesis(void)
128{
129 return mtime_since_now(&genesis);
130}
131
Jens Axboe02bcaa82006-11-24 10:42:00 +0100132static void fio_init time_init(void)
Jens Axboe263e5292006-10-18 15:37:01 +0200133{
Jens Axboe02bcaa82006-11-24 10:42:00 +0100134 fio_gettime(&genesis, NULL);
Jens Axboe263e5292006-10-18 15:37:01 +0200135}
Jens Axboe6043c572006-11-03 11:37:47 +0100136
137void fill_start_time(struct timeval *t)
138{
139 memcpy(t, &genesis, sizeof(genesis));
140}