Commit | Line | Data |
---|---|---|
02bcaa8c | 1 | /* |
f5cc024a | 2 | * Clock functions |
02bcaa8c | 3 | */ |
f5cc024a | 4 | |
02bcaa8c | 5 | #include <unistd.h> |
c223da83 | 6 | #include <math.h> |
02bcaa8c | 7 | #include <sys/time.h> |
03e20d68 | 8 | #include <time.h> |
02bcaa8c JA |
9 | |
10 | #include "fio.h" | |
be4ecfdf | 11 | #include "smalloc.h" |
02bcaa8c JA |
12 | |
13 | #include "hash.h" | |
14 | ||
09a32402 | 15 | #ifdef ARCH_HAVE_CPU_CLOCK |
c223da83 | 16 | static unsigned long cycles_per_usec; |
c223da83 | 17 | static unsigned long last_cycles; |
09a32402 JA |
18 | #endif |
19 | static struct timeval last_tv; | |
3e488920 | 20 | static int last_tv_valid; |
02bcaa8c | 21 | |
16de1bf9 | 22 | enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE; |
c223da83 | 23 | |
02bcaa8c JA |
24 | #ifdef FIO_DEBUG_TIME |
25 | ||
26 | #define HASH_BITS 8 | |
27 | #define HASH_SIZE (1 << HASH_BITS) | |
28 | ||
01743ee1 | 29 | static struct flist_head hash[HASH_SIZE]; |
02bcaa8c JA |
30 | static int gtod_inited; |
31 | ||
32 | struct gtod_log { | |
01743ee1 | 33 | struct flist_head list; |
02bcaa8c JA |
34 | void *caller; |
35 | unsigned long calls; | |
36 | }; | |
37 | ||
38 | static struct gtod_log *find_hash(void *caller) | |
39 | { | |
40 | unsigned long h = hash_ptr(caller, HASH_BITS); | |
01743ee1 | 41 | struct flist_head *entry; |
02bcaa8c | 42 | |
01743ee1 JA |
43 | flist_for_each(entry, &hash[h]) { |
44 | struct gtod_log *log = flist_entry(entry, struct gtod_log, | |
45 | list); | |
02bcaa8c JA |
46 | |
47 | if (log->caller == caller) | |
48 | return log; | |
49 | } | |
50 | ||
51 | return NULL; | |
52 | } | |
53 | ||
54 | static struct gtod_log *find_log(void *caller) | |
55 | { | |
56 | struct gtod_log *log = find_hash(caller); | |
57 | ||
58 | if (!log) { | |
59 | unsigned long h; | |
60 | ||
61 | log = malloc(sizeof(*log)); | |
01743ee1 | 62 | INIT_FLIST_HEAD(&log->list); |
02bcaa8c JA |
63 | log->caller = caller; |
64 | log->calls = 0; | |
65 | ||
66 | h = hash_ptr(caller, HASH_BITS); | |
01743ee1 | 67 | flist_add_tail(&log->list, &hash[h]); |
02bcaa8c JA |
68 | } |
69 | ||
70 | return log; | |
71 | } | |
72 | ||
73 | static void gtod_log_caller(void *caller) | |
74 | { | |
75 | if (gtod_inited) { | |
76 | struct gtod_log *log = find_log(caller); | |
77 | ||
78 | log->calls++; | |
79 | } | |
80 | } | |
81 | ||
82 | static void fio_exit fio_dump_gtod(void) | |
83 | { | |
84 | unsigned long total_calls = 0; | |
85 | int i; | |
86 | ||
87 | for (i = 0; i < HASH_SIZE; i++) { | |
01743ee1 | 88 | struct flist_head *entry; |
02bcaa8c JA |
89 | struct gtod_log *log; |
90 | ||
01743ee1 JA |
91 | flist_for_each(entry, &hash[i]) { |
92 | log = flist_entry(entry, struct gtod_log, list); | |
02bcaa8c | 93 | |
5ec10eaa JA |
94 | printf("function %p, calls %lu\n", log->caller, |
95 | log->calls); | |
02bcaa8c JA |
96 | total_calls += log->calls; |
97 | } | |
98 | } | |
99 | ||
100 | printf("Total %lu gettimeofday\n", total_calls); | |
101 | } | |
102 | ||
103 | static void fio_init gtod_init(void) | |
104 | { | |
105 | int i; | |
106 | ||
107 | for (i = 0; i < HASH_SIZE; i++) | |
01743ee1 | 108 | INIT_FLIST_HEAD(&hash[i]); |
02bcaa8c JA |
109 | |
110 | gtod_inited = 1; | |
111 | } | |
112 | ||
113 | #endif /* FIO_DEBUG_TIME */ | |
114 | ||
1e97cce9 | 115 | #ifdef FIO_DEBUG_TIME |
02bcaa8c | 116 | void fio_gettime(struct timeval *tp, void *caller) |
1e97cce9 JA |
117 | #else |
118 | void fio_gettime(struct timeval *tp, void fio_unused *caller) | |
119 | #endif | |
02bcaa8c JA |
120 | { |
121 | #ifdef FIO_DEBUG_TIME | |
122 | if (!caller) | |
123 | caller = __builtin_return_address(0); | |
124 | ||
125 | gtod_log_caller(caller); | |
02bcaa8c | 126 | #endif |
be4ecfdf JA |
127 | if (fio_tv) { |
128 | memcpy(tp, fio_tv, sizeof(*tp)); | |
129 | return; | |
c223da83 JA |
130 | } |
131 | ||
132 | switch (fio_clock_source) { | |
133 | case CS_GTOD: | |
02bcaa8c | 134 | gettimeofday(tp, NULL); |
c223da83 JA |
135 | break; |
136 | case CS_CGETTIME: { | |
02bcaa8c JA |
137 | struct timespec ts; |
138 | ||
5bd9c78c JA |
139 | #ifdef FIO_HAVE_CLOCK_MONOTONIC |
140 | if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) { | |
141 | #else | |
d481e006 | 142 | if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { |
5bd9c78c | 143 | #endif |
c223da83 JA |
144 | log_err("fio: clock_gettime fails\n"); |
145 | assert(0); | |
02bcaa8c JA |
146 | } |
147 | ||
148 | tp->tv_sec = ts.tv_sec; | |
149 | tp->tv_usec = ts.tv_nsec / 1000; | |
c223da83 JA |
150 | break; |
151 | } | |
152 | #ifdef ARCH_HAVE_CPU_CLOCK | |
153 | case CS_CPUCLOCK: { | |
154 | unsigned long long usecs, t; | |
155 | ||
156 | t = get_cpu_clock(); | |
157 | if (t < last_cycles) { | |
158 | dprint(FD_TIME, "CPU clock going back in time\n"); | |
159 | t = last_cycles; | |
160 | } | |
161 | ||
162 | usecs = t / cycles_per_usec; | |
163 | tp->tv_sec = usecs / 1000000; | |
164 | tp->tv_usec = usecs % 1000000; | |
165 | last_cycles = t; | |
166 | break; | |
167 | } | |
168 | #endif | |
169 | default: | |
170 | log_err("fio: invalid clock source %d\n", fio_clock_source); | |
171 | break; | |
02bcaa8c | 172 | } |
3e488920 JA |
173 | |
174 | /* | |
175 | * If Linux is using the tsc clock on non-synced processors, | |
176 | * sometimes time can appear to drift backwards. Fix that up. | |
177 | */ | |
178 | if (last_tv_valid) { | |
179 | if (tp->tv_sec < last_tv.tv_sec) | |
180 | tp->tv_sec = last_tv.tv_sec; | |
181 | else if (last_tv.tv_sec == tp->tv_sec && | |
182 | tp->tv_usec < last_tv.tv_usec) | |
183 | tp->tv_usec = last_tv.tv_usec; | |
184 | } | |
185 | last_tv_valid = 1; | |
186 | memcpy(&last_tv, tp, sizeof(*tp)); | |
02bcaa8c | 187 | } |
be4ecfdf | 188 | |
09a32402 | 189 | #ifdef ARCH_HAVE_CPU_CLOCK |
c223da83 JA |
190 | static unsigned long get_cycles_per_usec(void) |
191 | { | |
192 | struct timeval s, e; | |
193 | unsigned long long c_s, c_e; | |
194 | ||
195 | gettimeofday(&s, NULL); | |
196 | c_s = get_cpu_clock(); | |
197 | do { | |
198 | unsigned long long elapsed; | |
199 | ||
200 | gettimeofday(&e, NULL); | |
201 | elapsed = utime_since(&s, &e); | |
202 | if (elapsed >= 10) { | |
203 | c_e = get_cpu_clock(); | |
204 | break; | |
205 | } | |
206 | } while (1); | |
207 | ||
208 | return c_e - c_s; | |
209 | } | |
210 | ||
09a32402 | 211 | static void calibrate_cpu_clock(void) |
c223da83 JA |
212 | { |
213 | double delta, mean, S; | |
214 | unsigned long avg, cycles[10]; | |
215 | int i, samples; | |
216 | ||
c223da83 JA |
217 | cycles[0] = get_cycles_per_usec(); |
218 | S = delta = mean = 0.0; | |
219 | for (i = 0; i < 10; i++) { | |
220 | cycles[i] = get_cycles_per_usec(); | |
221 | delta = cycles[i] - mean; | |
222 | if (delta) { | |
223 | mean += delta / (i + 1.0); | |
224 | S += delta * (cycles[i] - mean); | |
225 | } | |
226 | } | |
227 | ||
228 | S = sqrt(S / (10 - 1.0)); | |
229 | ||
230 | samples = avg = 0; | |
231 | for (i = 0; i < 10; i++) { | |
232 | double this = cycles[i]; | |
233 | ||
03e20d68 | 234 | if ((fmax(this, mean) - fmin(this, mean)) > S) |
c223da83 JA |
235 | continue; |
236 | samples++; | |
237 | avg += this; | |
238 | } | |
239 | ||
240 | S /= 10.0; | |
241 | mean /= 10.0; | |
242 | ||
243 | for (i = 0; i < 10; i++) | |
244 | dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10); | |
245 | ||
246 | avg /= (samples * 10); | |
247 | dprint(FD_TIME, "avg: %lu\n", avg); | |
248 | dprint(FD_TIME, "mean=%f, S=%f\n", mean, S); | |
249 | ||
250 | cycles_per_usec = avg; | |
09a32402 JA |
251 | |
252 | } | |
253 | #else | |
254 | static void calibrate_cpu_clock(void) | |
255 | { | |
256 | } | |
257 | #endif | |
258 | ||
259 | void fio_clock_init(void) | |
260 | { | |
261 | last_tv_valid = 0; | |
262 | calibrate_cpu_clock(); | |
c223da83 JA |
263 | } |
264 | ||
39ab7da2 | 265 | unsigned long long utime_since(struct timeval *s, struct timeval *e) |
be4ecfdf | 266 | { |
39ab7da2 JA |
267 | long sec, usec; |
268 | unsigned long long ret; | |
269 | ||
270 | sec = e->tv_sec - s->tv_sec; | |
271 | usec = e->tv_usec - s->tv_usec; | |
272 | if (sec > 0 && usec < 0) { | |
273 | sec--; | |
274 | usec += 1000000; | |
275 | } | |
276 | ||
277 | /* | |
278 | * time warp bug on some kernels? | |
279 | */ | |
280 | if (sec < 0 || (sec == 0 && usec < 0)) | |
281 | return 0; | |
282 | ||
283 | ret = sec * 1000000ULL + usec; | |
284 | ||
285 | return ret; | |
be4ecfdf JA |
286 | } |
287 | ||
39ab7da2 | 288 | unsigned long long utime_since_now(struct timeval *s) |
be4ecfdf | 289 | { |
39ab7da2 JA |
290 | struct timeval t; |
291 | ||
292 | fio_gettime(&t, NULL); | |
293 | return utime_since(s, &t); | |
be4ecfdf | 294 | } |
783a3eb1 | 295 | |
39ab7da2 | 296 | unsigned long mtime_since(struct timeval *s, struct timeval *e) |
783a3eb1 | 297 | { |
39ab7da2 | 298 | long sec, usec, ret; |
783a3eb1 | 299 | |
39ab7da2 JA |
300 | sec = e->tv_sec - s->tv_sec; |
301 | usec = e->tv_usec - s->tv_usec; | |
302 | if (sec > 0 && usec < 0) { | |
303 | sec--; | |
304 | usec += 1000000; | |
783a3eb1 JA |
305 | } |
306 | ||
39ab7da2 JA |
307 | if (sec < 0 || (sec == 0 && usec < 0)) |
308 | return 0; | |
309 | ||
310 | sec *= 1000UL; | |
311 | usec /= 1000UL; | |
312 | ret = sec + usec; | |
313 | ||
314 | return ret; | |
783a3eb1 JA |
315 | } |
316 | ||
39ab7da2 | 317 | unsigned long mtime_since_now(struct timeval *s) |
783a3eb1 | 318 | { |
39ab7da2 JA |
319 | struct timeval t; |
320 | void *p = __builtin_return_address(0); | |
783a3eb1 | 321 | |
39ab7da2 JA |
322 | fio_gettime(&t, p); |
323 | return mtime_since(s, &t); | |
324 | } | |
783a3eb1 | 325 | |
39ab7da2 JA |
326 | unsigned long time_since_now(struct timeval *s) |
327 | { | |
328 | return mtime_since_now(s) / 1000; | |
783a3eb1 | 329 | } |