Commit | Line | Data |
---|---|---|
3c39a379 JA |
1 | #include <time.h> |
2 | #include <sys/time.h> | |
3 | ||
4 | #include "fio.h" | |
5 | ||
263e529f | 6 | static struct timeval genesis; |
fd841467 | 7 | static unsigned long ns_granularity; |
263e529f | 8 | |
10927316 | 9 | unsigned long long utime_since(struct timeval *s, struct timeval *e) |
3c39a379 | 10 | { |
10927316 SL |
11 | long sec, usec; |
12 | unsigned long long ret; | |
5ec10eaa | 13 | |
3c39a379 JA |
14 | sec = e->tv_sec - s->tv_sec; |
15 | usec = e->tv_usec - s->tv_usec; | |
16 | if (sec > 0 && usec < 0) { | |
17 | sec--; | |
18 | usec += 1000000; | |
19 | } | |
20 | ||
2bfe24bd JA |
21 | /* |
22 | * time warp bug on some kernels? | |
23 | */ | |
10927316 SL |
24 | if (sec < 0 || (sec == 0 && usec < 0)) |
25 | return 0; | |
5ec10eaa | 26 | |
10927316 | 27 | ret = sec * 1000000ULL + usec; |
5ec10eaa | 28 | |
2bfe24bd | 29 | return ret; |
3c39a379 JA |
30 | } |
31 | ||
10927316 | 32 | unsigned long 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 | ||
40 | unsigned 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 | ||
64 | unsigned 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 | ||
73 | unsigned 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 | */ | |
6dd6f2cd | 81 | void usec_spin(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 | ||
90 | void 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) { | |
6dd6f2cd | 99 | usec_spin(usec); |
3c39a379 JA |
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 | ||
263e529f JA |
125 | unsigned long mtime_since_genesis(void) |
126 | { | |
127 | return mtime_since_now(&genesis); | |
128 | } | |
129 | ||
b29ee5b3 JA |
130 | int in_ramp_time(struct thread_data *td) |
131 | { | |
132 | return td->o.ramp_time && !td->ramp_time_over; | |
133 | } | |
134 | ||
721938ae JA |
135 | int ramp_time_over(struct thread_data *td) |
136 | { | |
137 | struct timeval tv; | |
138 | ||
139 | if (!td->o.ramp_time || td->ramp_time_over) | |
140 | return 1; | |
141 | ||
142 | fio_gettime(&tv, NULL); | |
143 | if (mtime_since(&td->epoch, &tv) >= td->o.ramp_time * 1000) { | |
144 | td->ramp_time_over = 1; | |
b29ee5b3 JA |
145 | reset_all_stats(td); |
146 | td_set_runstate(td, TD_RAMP); | |
721938ae JA |
147 | return 1; |
148 | } | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
03e20d68 | 153 | void fio_time_init(void) |
263e529f | 154 | { |
fd841467 JA |
155 | int i; |
156 | ||
c223da83 JA |
157 | fio_clock_init(); |
158 | ||
fd841467 JA |
159 | /* |
160 | * Check the granularity of the nanosleep function | |
161 | */ | |
162 | for (i = 0; i < 10; i++) { | |
163 | struct timeval tv; | |
164 | struct timespec ts; | |
165 | unsigned long elapsed; | |
166 | ||
167 | fio_gettime(&tv, NULL); | |
168 | ts.tv_sec = 0; | |
169 | ts.tv_nsec = 1000; | |
170 | ||
171 | nanosleep(&ts, NULL); | |
172 | elapsed = utime_since_now(&tv); | |
173 | ||
174 | if (elapsed > ns_granularity) | |
175 | ns_granularity = elapsed; | |
176 | } | |
263e529f | 177 | } |
6043c579 | 178 | |
a2f77c9f JA |
179 | void set_genesis_time(void) |
180 | { | |
181 | fio_gettime(&genesis, NULL); | |
182 | } | |
183 | ||
6043c579 JA |
184 | void fill_start_time(struct timeval *t) |
185 | { | |
186 | memcpy(t, &genesis, sizeof(genesis)); | |
187 | } |