netsplice: fix a few typos
[fio.git] / time.c
CommitLineData
3c39a379
JA
1#include <time.h>
2#include <sys/time.h>
3
4#include "fio.h"
5
263e529f 6static struct timeval genesis;
fd841467 7static unsigned long ns_granularity;
263e529f 8
3c39a379
JA
9unsigned long utime_since(struct timeval *s, struct timeval *e)
10{
2bfe24bd 11 long sec, usec, ret;
3c39a379
JA
12
13 sec = e->tv_sec - s->tv_sec;
14 usec = e->tv_usec - s->tv_usec;
15 if (sec > 0 && usec < 0) {
16 sec--;
17 usec += 1000000;
18 }
19
64b18e12 20 sec *= 1000000UL;
2bfe24bd 21 ret = sec + usec;
3c39a379 22
2bfe24bd
JA
23 /*
24 * time warp bug on some kernels?
25 */
26 if (ret < 0)
27 ret = 0;
28
29 return ret;
3c39a379
JA
30}
31
69008999 32unsigned long utime_since_now(struct timeval *s)
3c39a379
JA
33{
34 struct timeval t;
35
02bcaa8c 36 fio_gettime(&t, NULL);
3c39a379
JA
37 return utime_since(s, &t);
38}
39
40unsigned long mtime_since(struct timeval *s, struct timeval *e)
41{
2bfe24bd 42 long sec, usec, ret;
3c39a379
JA
43
44 sec = e->tv_sec - s->tv_sec;
45 usec = e->tv_usec - s->tv_usec;
46 if (sec > 0 && usec < 0) {
47 sec--;
48 usec += 1000000;
49 }
50
64b18e12
JA
51 sec *= 1000UL;
52 usec /= 1000UL;
2bfe24bd
JA
53 ret = sec + usec;
54
55 /*
56 * time warp bug on some kernels?
57 */
58 if (ret < 0)
59 ret = 0;
3c39a379 60
2bfe24bd 61 return ret;
3c39a379
JA
62}
63
64unsigned long mtime_since_now(struct timeval *s)
65{
66 struct timeval t;
02bcaa8c 67 void *p = __builtin_return_address(0);
3c39a379 68
02bcaa8c 69 fio_gettime(&t, p);
3c39a379
JA
70 return mtime_since(s, &t);
71}
72
73unsigned long time_since_now(struct timeval *s)
74{
75 return mtime_since_now(s) / 1000;
76}
77
78/*
79 * busy looping version for the last few usec
80 */
b990b5c0 81void __usec_sleep(unsigned int usec)
3c39a379
JA
82{
83 struct timeval start;
84
02bcaa8c 85 fio_gettime(&start, NULL);
3c39a379
JA
86 while (utime_since_now(&start) < usec)
87 nop;
88}
89
90void usec_sleep(struct thread_data *td, unsigned long usec)
91{
fd841467
JA
92 struct timespec req;
93 struct timeval tv;
3c39a379
JA
94
95 do {
fd841467
JA
96 unsigned long ts = usec;
97
98 if (usec < ns_granularity) {
3c39a379
JA
99 __usec_sleep(usec);
100 break;
101 }
102
fd841467 103 ts = usec - ns_granularity;
3c39a379 104
fd841467
JA
105 if (ts >= 1000000) {
106 req.tv_sec = ts / 1000000;
107 ts -= 1000000 * req.tv_sec;
108 } else
109 req.tv_sec = 0;
110
111 req.tv_nsec = ts * 1000;
112 fio_gettime(&tv, NULL);
113
114 if (nanosleep(&req, NULL) < 0)
3c39a379
JA
115 break;
116
fd841467
JA
117 ts = utime_since_now(&tv);
118 if (ts >= usec)
119 break;
3c39a379 120
fd841467 121 usec -= ts;
3c39a379
JA
122 } while (!td->terminate);
123}
124
125void rate_throttle(struct thread_data *td, unsigned long time_spent,
413dd459 126 unsigned int bytes)
3c39a379
JA
127{
128 unsigned long usec_cycle;
c3852ae2 129 unsigned int bs;
3c39a379 130
2dc1bbeb 131 if (!td->o.rate && !td->o.rate_iops)
3c39a379
JA
132 return;
133
c3852ae2 134 if (td_rw(td))
2dc1bbeb 135 bs = td->o.rw_min_bs;
c3852ae2 136 else if (td_read(td))
2dc1bbeb 137 bs = td->o.min_bs[DDIR_READ];
c3852ae2 138 else
2dc1bbeb 139 bs = td->o.min_bs[DDIR_WRITE];
c3852ae2
JA
140
141 usec_cycle = td->rate_usec_cycle * (bytes / bs);
3c39a379
JA
142
143 if (time_spent < usec_cycle) {
144 unsigned long s = usec_cycle - time_spent;
145
146 td->rate_pending_usleep += s;
a998c869 147
3c39a379 148 if (td->rate_pending_usleep >= 100000) {
a998c869
JA
149 struct timeval t;
150
151 fio_gettime(&t, NULL);
3c39a379 152 usec_sleep(td, td->rate_pending_usleep);
a998c869 153 td->rate_pending_usleep -= utime_since_now(&t);
3c39a379
JA
154 }
155 } else {
156 long overtime = time_spent - usec_cycle;
157
158 td->rate_pending_usleep -= overtime;
159 }
160}
263e529f
JA
161
162unsigned long mtime_since_genesis(void)
163{
164 return mtime_since_now(&genesis);
165}
166
02bcaa8c 167static void fio_init time_init(void)
263e529f 168{
fd841467
JA
169 int i;
170
fd841467
JA
171 /*
172 * Check the granularity of the nanosleep function
173 */
174 for (i = 0; i < 10; i++) {
175 struct timeval tv;
176 struct timespec ts;
177 unsigned long elapsed;
178
179 fio_gettime(&tv, NULL);
180 ts.tv_sec = 0;
181 ts.tv_nsec = 1000;
182
183 nanosleep(&ts, NULL);
184 elapsed = utime_since_now(&tv);
185
186 if (elapsed > ns_granularity)
187 ns_granularity = elapsed;
188 }
263e529f 189}
6043c579 190
a2f77c9f
JA
191void set_genesis_time(void)
192{
193 fio_gettime(&genesis, NULL);
194}
195
6043c579
JA
196void fill_start_time(struct timeval *t)
197{
198 memcpy(t, &genesis, sizeof(genesis));
199}