Commit | Line | Data |
---|---|---|
3c39a379 JA |
1 | #include <stdio.h> |
2 | #include <string.h> | |
3 | #include <sys/time.h> | |
4 | #include <sys/types.h> | |
5c4e1dbc | 5 | #include <sys/stat.h> |
3c39a379 JA |
6 | #include <dirent.h> |
7 | #include <libgen.h> | |
8 | #include <math.h> | |
9 | ||
10 | #include "fio.h" | |
3c39a379 JA |
11 | |
12 | static struct itimerval itimer; | |
5c4e1dbc | 13 | static struct list_head disk_list = LIST_HEAD_INIT(disk_list); |
c0a6b0d8 | 14 | static dev_t last_dev; |
3c39a379 | 15 | |
dbe1125e | 16 | /* |
34403fb1 | 17 | * Cheesy number->string conversion, complete with carry rounding error. |
dbe1125e JA |
18 | */ |
19 | static char *num2str(unsigned long num, int maxlen, int base) | |
20 | { | |
21 | /* | |
22 | * could be passed in for 10^3 base, but every caller expects | |
23 | * 2^10 base right now. | |
24 | */ | |
c8c51efd | 25 | const unsigned int thousand = 1024; |
f3502ba2 | 26 | char postfix[] = { 'K', 'M', 'G', 'P', 'E' }; |
dbe1125e JA |
27 | char *buf; |
28 | int i; | |
29 | ||
30 | buf = malloc(128); | |
31 | ||
32 | for (i = 0; base > 1; i++) | |
33 | base /= thousand; | |
34 | ||
35 | do { | |
36 | int len, carry = 0; | |
37 | ||
38 | len = sprintf(buf, "%'lu", num); | |
39 | if (len <= maxlen) { | |
40 | buf[len] = postfix[i]; | |
41 | buf[len + 1] = '\0'; | |
42 | return buf; | |
43 | } | |
44 | ||
45 | if ((num % thousand) >= (thousand / 2)) | |
46 | carry = 1; | |
47 | ||
48 | num /= thousand; | |
49 | num += carry; | |
50 | i++; | |
f3502ba2 | 51 | } while (i <= 5); |
dbe1125e JA |
52 | |
53 | return buf; | |
54 | } | |
55 | ||
3c39a379 JA |
56 | static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus) |
57 | { | |
58 | unsigned in_flight; | |
59 | char line[256]; | |
60 | FILE *f; | |
61 | char *p; | |
62 | ||
63 | f = fopen(du->path, "r"); | |
64 | if (!f) | |
65 | return 1; | |
66 | ||
67 | p = fgets(line, sizeof(line), f); | |
68 | if (!p) { | |
69 | fclose(f); | |
70 | return 1; | |
71 | } | |
72 | ||
73 | if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) { | |
74 | fclose(f); | |
75 | return 1; | |
76 | } | |
77 | ||
78 | fclose(f); | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static void update_io_tick_disk(struct disk_util *du) | |
83 | { | |
84 | struct disk_util_stat __dus, *dus, *ldus; | |
85 | struct timeval t; | |
86 | ||
87 | if (get_io_ticks(du, &__dus)) | |
88 | return; | |
89 | ||
90 | dus = &du->dus; | |
91 | ldus = &du->last_dus; | |
92 | ||
93 | dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]); | |
94 | dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]); | |
95 | dus->ios[0] += (__dus.ios[0] - ldus->ios[0]); | |
96 | dus->ios[1] += (__dus.ios[1] - ldus->ios[1]); | |
97 | dus->merges[0] += (__dus.merges[0] - ldus->merges[0]); | |
98 | dus->merges[1] += (__dus.merges[1] - ldus->merges[1]); | |
99 | dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]); | |
100 | dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]); | |
101 | dus->io_ticks += (__dus.io_ticks - ldus->io_ticks); | |
102 | dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue); | |
103 | ||
02bcaa8c | 104 | fio_gettime(&t, NULL); |
3c39a379 JA |
105 | du->msec += mtime_since(&du->time, &t); |
106 | memcpy(&du->time, &t, sizeof(t)); | |
107 | memcpy(ldus, &__dus, sizeof(__dus)); | |
108 | } | |
109 | ||
110 | void update_io_ticks(void) | |
111 | { | |
112 | struct list_head *entry; | |
113 | struct disk_util *du; | |
114 | ||
115 | list_for_each(entry, &disk_list) { | |
116 | du = list_entry(entry, struct disk_util, list); | |
117 | update_io_tick_disk(du); | |
118 | } | |
119 | } | |
120 | ||
121 | static int disk_util_exists(dev_t dev) | |
122 | { | |
123 | struct list_head *entry; | |
124 | struct disk_util *du; | |
125 | ||
126 | list_for_each(entry, &disk_list) { | |
127 | du = list_entry(entry, struct disk_util, list); | |
128 | ||
129 | if (du->dev == dev) | |
130 | return 1; | |
131 | } | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | static void disk_util_add(dev_t dev, char *path) | |
137 | { | |
1188caf8 JA |
138 | struct disk_util *du, *__du; |
139 | struct list_head *entry; | |
3c39a379 | 140 | |
1188caf8 | 141 | du = malloc(sizeof(*du)); |
3c39a379 JA |
142 | memset(du, 0, sizeof(*du)); |
143 | INIT_LIST_HEAD(&du->list); | |
144 | sprintf(du->path, "%s/stat", path); | |
145 | du->name = strdup(basename(path)); | |
146 | du->dev = dev; | |
147 | ||
1188caf8 JA |
148 | list_for_each(entry, &disk_list) { |
149 | __du = list_entry(entry, struct disk_util, list); | |
150 | ||
151 | if (!strcmp(du->name, __du->name)) { | |
152 | free(du->name); | |
153 | free(du); | |
154 | return; | |
155 | } | |
156 | } | |
157 | ||
02bcaa8c | 158 | fio_gettime(&du->time, NULL); |
3c39a379 JA |
159 | get_io_ticks(du, &du->last_dus); |
160 | ||
161 | list_add_tail(&du->list, &disk_list); | |
162 | } | |
163 | ||
164 | static int check_dev_match(dev_t dev, char *path) | |
165 | { | |
166 | unsigned int major, minor; | |
167 | char line[256], *p; | |
168 | FILE *f; | |
169 | ||
170 | f = fopen(path, "r"); | |
171 | if (!f) { | |
172 | perror("open path"); | |
173 | return 1; | |
174 | } | |
175 | ||
176 | p = fgets(line, sizeof(line), f); | |
177 | if (!p) { | |
178 | fclose(f); | |
179 | return 1; | |
180 | } | |
181 | ||
182 | if (sscanf(p, "%u:%u", &major, &minor) != 2) { | |
183 | fclose(f); | |
184 | return 1; | |
185 | } | |
186 | ||
187 | if (((major << 8) | minor) == dev) { | |
188 | fclose(f); | |
189 | return 0; | |
190 | } | |
191 | ||
192 | fclose(f); | |
193 | return 1; | |
194 | } | |
195 | ||
196 | static int find_block_dir(dev_t dev, char *path) | |
197 | { | |
198 | struct dirent *dir; | |
199 | struct stat st; | |
200 | int found = 0; | |
201 | DIR *D; | |
202 | ||
203 | D = opendir(path); | |
204 | if (!D) | |
205 | return 0; | |
206 | ||
207 | while ((dir = readdir(D)) != NULL) { | |
208 | char full_path[256]; | |
209 | ||
210 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) | |
211 | continue; | |
3c39a379 JA |
212 | |
213 | sprintf(full_path, "%s/%s", path, dir->d_name); | |
214 | ||
215 | if (!strcmp(dir->d_name, "dev")) { | |
216 | if (!check_dev_match(dev, full_path)) { | |
217 | found = 1; | |
218 | break; | |
219 | } | |
220 | } | |
221 | ||
3307d653 | 222 | if (lstat(full_path, &st) == -1) { |
3c39a379 JA |
223 | perror("stat"); |
224 | break; | |
225 | } | |
226 | ||
227 | if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) | |
228 | continue; | |
229 | ||
230 | found = find_block_dir(dev, full_path); | |
231 | if (found) { | |
232 | strcpy(path, full_path); | |
233 | break; | |
234 | } | |
235 | } | |
236 | ||
237 | closedir(D); | |
238 | return found; | |
239 | } | |
240 | ||
241 | void init_disk_util(struct thread_data *td) | |
242 | { | |
53cdc686 | 243 | struct fio_file *f; |
3c39a379 | 244 | struct stat st; |
0bbab0e7 | 245 | char foo[PATH_MAX], tmp[PATH_MAX]; |
3c39a379 JA |
246 | dev_t dev; |
247 | char *p; | |
248 | ||
138502a9 | 249 | if (!td->do_disk_util || (td->io_ops->flags & FIO_DISKLESSIO)) |
3c39a379 JA |
250 | return; |
251 | ||
53cdc686 JA |
252 | /* |
253 | * Just use the same file, they are on the same device. | |
254 | */ | |
255 | f = &td->files[0]; | |
256 | if (!stat(f->file_name, &st)) { | |
3c39a379 JA |
257 | if (S_ISBLK(st.st_mode)) |
258 | dev = st.st_rdev; | |
259 | else | |
260 | dev = st.st_dev; | |
261 | } else { | |
262 | /* | |
263 | * must be a file, open "." in that path | |
264 | */ | |
0bbab0e7 | 265 | strncpy(foo, f->file_name, PATH_MAX - 1); |
3c39a379 JA |
266 | p = dirname(foo); |
267 | if (stat(p, &st)) { | |
268 | perror("disk util stat"); | |
269 | return; | |
270 | } | |
271 | ||
272 | dev = st.st_dev; | |
273 | } | |
274 | ||
275 | if (disk_util_exists(dev)) | |
276 | return; | |
c0a6b0d8 JA |
277 | |
278 | /* | |
279 | * for an fs without a device, we will repeatedly stat through | |
280 | * sysfs which can take oodles of time for thousands of files. so | |
281 | * cache the last lookup and compare with that before going through | |
282 | * everything again. | |
283 | */ | |
284 | if (dev == last_dev) | |
285 | return; | |
286 | ||
287 | last_dev = dev; | |
3c39a379 JA |
288 | |
289 | sprintf(foo, "/sys/block"); | |
290 | if (!find_block_dir(dev, foo)) | |
291 | return; | |
292 | ||
293 | /* | |
294 | * If there's a ../queue/ directory there, we are inside a partition. | |
295 | * Check if that is the case and jump back. For loop/md/dm etc we | |
296 | * are already in the right spot. | |
297 | */ | |
298 | sprintf(tmp, "%s/../queue", foo); | |
299 | if (!stat(tmp, &st)) { | |
300 | p = dirname(foo); | |
301 | sprintf(tmp, "%s/queue", p); | |
302 | if (stat(tmp, &st)) { | |
3b70d7e5 | 303 | log_err("unknown sysfs layout\n"); |
3c39a379 JA |
304 | return; |
305 | } | |
0bbab0e7 | 306 | strncpy(tmp, p, PATH_MAX - 1); |
84585003 | 307 | sprintf(foo, "%s", tmp); |
3c39a379 JA |
308 | } |
309 | ||
eecf272f JA |
310 | if (td->ioscheduler) |
311 | td->sysfs_root = strdup(foo); | |
312 | ||
3c39a379 JA |
313 | disk_util_add(dev, foo); |
314 | } | |
315 | ||
316 | void disk_util_timer_arm(void) | |
317 | { | |
318 | itimer.it_value.tv_sec = 0; | |
319 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; | |
320 | setitimer(ITIMER_REAL, &itimer, NULL); | |
321 | } | |
322 | ||
323 | void update_rusage_stat(struct thread_data *td) | |
324 | { | |
079ad09b | 325 | struct thread_stat *ts = &td->ts; |
3c39a379 | 326 | |
079ad09b JA |
327 | getrusage(RUSAGE_SELF, &ts->ru_end); |
328 | ||
329 | ts->usr_time += mtime_since(&ts->ru_start.ru_utime, &ts->ru_end.ru_utime); | |
330 | ts->sys_time += mtime_since(&ts->ru_start.ru_stime, &ts->ru_end.ru_stime); | |
331 | ts->ctx += ts->ru_end.ru_nvcsw + ts->ru_end.ru_nivcsw - (ts->ru_start.ru_nvcsw + ts->ru_start.ru_nivcsw); | |
3c39a379 | 332 | |
079ad09b | 333 | memcpy(&ts->ru_start, &ts->ru_end, sizeof(ts->ru_end)); |
3c39a379 JA |
334 | } |
335 | ||
336 | static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max, | |
337 | double *mean, double *dev) | |
338 | { | |
68704084 | 339 | double n = is->samples; |
3c39a379 JA |
340 | |
341 | if (is->samples == 0) | |
342 | return 0; | |
343 | ||
344 | *min = is->min_val; | |
345 | *max = is->max_val; | |
346 | ||
347 | n = (double) is->samples; | |
68704084 | 348 | *mean = is->mean; |
e6d276f2 | 349 | |
68704084 JA |
350 | if (n > 1.0) |
351 | *dev = sqrt(is->S / (n - 1.0)); | |
ef9c5c40 | 352 | else |
68704084 | 353 | *dev = -1.0; |
ef9c5c40 | 354 | |
3c39a379 JA |
355 | return 1; |
356 | } | |
357 | ||
358 | static void show_group_stats(struct group_run_stats *rs, int id) | |
359 | { | |
dbe1125e JA |
360 | char *p1, *p2, *p3, *p4; |
361 | const char *ddir_str[] = { " READ", " WRITE" }; | |
362 | int i; | |
363 | ||
eb8bbf48 | 364 | fprintf(f_out, "\nRun status group %d (all jobs):\n", id); |
3c39a379 | 365 | |
dbe1125e JA |
366 | for (i = 0; i <= DDIR_WRITE; i++) { |
367 | if (!rs->max_run[i]) | |
368 | continue; | |
369 | ||
370 | p1 = num2str(rs->io_kb[i], 6, 1); | |
371 | p2 = num2str(rs->agg[i], 6, 1); | |
372 | p3 = num2str(rs->min_bw[i], 6, 1); | |
373 | p4 = num2str(rs->max_bw[i], 6, 1); | |
374 | ||
375 | fprintf(f_out, "%s: io=%siB, aggrb=%siB/s, minb=%siB/s, maxb=%siB/s, mint=%llumsec, maxt=%llumsec\n", ddir_str[i], p1, p2, p3, p4, rs->min_run[0], rs->max_run[0]); | |
376 | ||
377 | free(p1); | |
378 | free(p2); | |
379 | free(p3); | |
380 | free(p4); | |
381 | } | |
3c39a379 JA |
382 | } |
383 | ||
384 | static void show_disk_util(void) | |
385 | { | |
386 | struct disk_util_stat *dus; | |
b4a6a59a | 387 | struct list_head *entry, *next; |
3c39a379 JA |
388 | struct disk_util *du; |
389 | double util; | |
390 | ||
eb8bbf48 | 391 | fprintf(f_out, "\nDisk stats (read/write):\n"); |
3c39a379 JA |
392 | |
393 | list_for_each(entry, &disk_list) { | |
394 | du = list_entry(entry, struct disk_util, list); | |
395 | dus = &du->dus; | |
396 | ||
397 | util = (double) 100 * du->dus.io_ticks / (double) du->msec; | |
398 | if (util > 100.0) | |
399 | util = 100.0; | |
400 | ||
eb8bbf48 | 401 | fprintf(f_out, " %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util); |
3c39a379 | 402 | } |
b4a6a59a JA |
403 | |
404 | /* | |
405 | * now free the list | |
406 | */ | |
407 | list_for_each_safe(entry, next, &disk_list) { | |
408 | list_del(entry); | |
409 | du = list_entry(entry, struct disk_util, list); | |
410 | free(du->name); | |
411 | free(du); | |
412 | } | |
3c39a379 JA |
413 | } |
414 | ||
415 | static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs, | |
416 | int ddir) | |
417 | { | |
3c9b60c1 | 418 | const char *ddir_str[] = { "read ", "write" }; |
079ad09b | 419 | struct thread_stat *ts; |
3c39a379 JA |
420 | unsigned long min, max; |
421 | unsigned long long bw; | |
422 | double mean, dev; | |
dbe1125e | 423 | char *io_p, *bw_p; |
3c39a379 JA |
424 | |
425 | if (!td->runtime[ddir]) | |
426 | return; | |
427 | ||
428 | bw = td->io_bytes[ddir] / td->runtime[ddir]; | |
dbe1125e JA |
429 | io_p = num2str(td->io_bytes[ddir] >> 10, 6, 1); |
430 | bw_p = num2str(bw, 6, 1); | |
431 | ||
432 | fprintf(f_out, " %s: io=%siB, bw=%siB/s, runt=%6lumsec\n", ddir_str[ddir], io_p, bw_p, td->runtime[ddir]); | |
433 | ||
434 | free(io_p); | |
435 | free(bw_p); | |
3c39a379 | 436 | |
079ad09b JA |
437 | ts = &td->ts; |
438 | if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) | |
6104ddb6 | 439 | fprintf(f_out, " slat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev); |
3c39a379 | 440 | |
079ad09b | 441 | if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) |
6104ddb6 | 442 | fprintf(f_out, " clat (msec): min=%5lu, max=%5lu, avg=%5.02f, stdev=%5.02f\n", min, max, mean, dev); |
3c39a379 | 443 | |
079ad09b | 444 | if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) { |
3c39a379 JA |
445 | double p_of_agg; |
446 | ||
447 | p_of_agg = mean * 100 / (double) rs->agg[ddir]; | |
6104ddb6 | 448 | fprintf(f_out, " bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, stdev=%5.02f\n", min, max, p_of_agg, mean, dev); |
3c39a379 JA |
449 | } |
450 | } | |
451 | ||
452 | static void show_thread_status(struct thread_data *td, | |
453 | struct group_run_stats *rs) | |
454 | { | |
455 | double usr_cpu, sys_cpu; | |
69008999 | 456 | unsigned long runtime; |
71619dc2 | 457 | double io_u_dist[FIO_IO_U_MAP_NR]; |
ec118304 | 458 | double io_u_lat[FIO_IO_U_LAT_NR]; |
71619dc2 | 459 | int i; |
3c39a379 | 460 | |
6d663077 | 461 | if (!(td->io_bytes[0] + td->io_bytes[1])) |
3c39a379 JA |
462 | return; |
463 | ||
6d663077 JA |
464 | if (!td->error) |
465 | fprintf(f_out, "%s: (groupid=%d): err=%2d: pid=%d\n",td->name, td->groupid, td->error, td->pid); | |
466 | else | |
467 | fprintf(f_out, "%s: (groupid=%d): err=%2d (%s): pid=%d\n",td->name, td->groupid, td->error, td->verror, td->pid); | |
3c39a379 | 468 | |
413dd459 JA |
469 | if (td_read(td)) |
470 | show_ddir_status(td, rs, DDIR_READ); | |
471 | if (td_write(td)) | |
472 | show_ddir_status(td, rs, DDIR_WRITE); | |
3c39a379 | 473 | |
69008999 JA |
474 | runtime = mtime_since(&td->epoch, &td->end_time); |
475 | if (runtime) { | |
1e97cce9 | 476 | double runt = (double) runtime; |
3c39a379 | 477 | |
079ad09b JA |
478 | usr_cpu = (double) td->ts.usr_time * 100 / runt; |
479 | sys_cpu = (double) td->ts.sys_time * 100 / runt; | |
3c39a379 JA |
480 | } else { |
481 | usr_cpu = 0; | |
482 | sys_cpu = 0; | |
483 | } | |
484 | ||
079ad09b | 485 | fprintf(f_out, " cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ts.ctx); |
71619dc2 JA |
486 | |
487 | /* | |
488 | * Do depth distribution calculations | |
489 | */ | |
490 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { | |
491 | io_u_dist[i] = (double) td->io_u_map[i] / (double) td->total_io_u; | |
492 | io_u_dist[i] *= 100.0; | |
493 | } | |
494 | ||
5d458077 | 495 | fprintf(f_out, " IO depths : 1=%3.1f%%, 2=%3.1f%%, 4=%3.1f%%, 8=%3.1f%%, 16=%3.1f%%, 32=%3.1f%%, >=64=%3.1f%%\n", io_u_dist[0], io_u_dist[1], io_u_dist[2], io_u_dist[3], io_u_dist[4], io_u_dist[5], io_u_dist[6]); |
61697c37 | 496 | |
ec118304 JA |
497 | /* |
498 | * Do latency distribution calculations | |
499 | */ | |
500 | for (i = 0; i < FIO_IO_U_LAT_NR; i++) { | |
501 | io_u_lat[i] = (double) td->io_u_lat[i] / (double) td->total_io_u; | |
502 | io_u_lat[i] *= 100.0; | |
503 | } | |
504 | ||
8abdce66 JA |
505 | fprintf(f_out, " lat (msec): 2=%3.1f%%, 4=%3.1f%%, 10=%3.1f%%, 20=%3.1f%%, 50=%3.1f%%, 100=%3.1f%%\n", io_u_lat[0], io_u_lat[1], io_u_lat[2], io_u_lat[3], io_u_lat[4], io_u_lat[5]); |
506 | fprintf(f_out, " lat (msec): 250=%3.1f%%, 500=%3.1f%%, 750=%3.1f%%, 1000=%3.1f%%, >=2000=%3.1f%%\n", io_u_lat[6], io_u_lat[7], io_u_lat[8], io_u_lat[9], io_u_lat[10]); | |
ec118304 | 507 | |
61697c37 JA |
508 | if (td->description) |
509 | fprintf(f_out, "%s\n", td->description); | |
3c39a379 JA |
510 | } |
511 | ||
c6ae0a5b JA |
512 | static void show_ddir_status_terse(struct thread_data *td, |
513 | struct group_run_stats *rs, int ddir) | |
514 | { | |
079ad09b | 515 | struct thread_stat *ts = &td->ts; |
c6ae0a5b JA |
516 | unsigned long min, max; |
517 | unsigned long long bw; | |
518 | double mean, dev; | |
519 | ||
520 | bw = 0; | |
521 | if (td->runtime[ddir]) | |
522 | bw = td->io_bytes[ddir] / td->runtime[ddir]; | |
523 | ||
524 | fprintf(f_out, ",%llu,%llu,%lu", td->io_bytes[ddir] >> 10, bw, td->runtime[ddir]); | |
525 | ||
079ad09b | 526 | if (calc_lat(&ts->slat_stat[ddir], &min, &max, &mean, &dev)) |
c6ae0a5b JA |
527 | fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev); |
528 | else | |
529 | fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0); | |
530 | ||
079ad09b | 531 | if (calc_lat(&ts->clat_stat[ddir], &min, &max, &mean, &dev)) |
c6ae0a5b JA |
532 | fprintf(f_out, ",%lu,%lu,%f,%f", min, max, mean, dev); |
533 | else | |
534 | fprintf(f_out, ",%lu,%lu,%f,%f", 0UL, 0UL, 0.0, 0.0); | |
535 | ||
079ad09b | 536 | if (calc_lat(&ts->bw_stat[ddir], &min, &max, &mean, &dev)) { |
c6ae0a5b JA |
537 | double p_of_agg; |
538 | ||
539 | p_of_agg = mean * 100 / (double) rs->agg[ddir]; | |
540 | fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", min, max, p_of_agg, mean, dev); | |
541 | } else | |
542 | fprintf(f_out, ",%lu,%lu,%f%%,%f,%f", 0UL, 0UL, 0.0, 0.0, 0.0); | |
543 | ||
544 | } | |
545 | ||
546 | ||
547 | static void show_thread_status_terse(struct thread_data *td, | |
548 | struct group_run_stats *rs) | |
549 | { | |
550 | double usr_cpu, sys_cpu; | |
551 | ||
552 | fprintf(f_out, "%s,%d,%d",td->name, td->groupid, td->error); | |
553 | ||
554 | show_ddir_status_terse(td, rs, 0); | |
555 | show_ddir_status_terse(td, rs, 1); | |
556 | ||
557 | if (td->runtime[0] + td->runtime[1]) { | |
1e97cce9 | 558 | double runt = (double) (td->runtime[0] + td->runtime[1]); |
c6ae0a5b | 559 | |
079ad09b JA |
560 | usr_cpu = (double) td->ts.usr_time * 100 / runt; |
561 | sys_cpu = (double) td->ts.sys_time * 100 / runt; | |
c6ae0a5b JA |
562 | } else { |
563 | usr_cpu = 0; | |
564 | sys_cpu = 0; | |
565 | } | |
566 | ||
079ad09b | 567 | fprintf(f_out, ",%f%%,%f%%,%lu\n", usr_cpu, sys_cpu, td->ts.ctx); |
c6ae0a5b JA |
568 | } |
569 | ||
3c39a379 JA |
570 | void show_run_stats(void) |
571 | { | |
572 | struct group_run_stats *runstats, *rs; | |
573 | struct thread_data *td; | |
574 | int i; | |
575 | ||
576 | runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1)); | |
577 | ||
578 | for (i = 0; i < groupid + 1; i++) { | |
579 | rs = &runstats[i]; | |
580 | ||
581 | memset(rs, 0, sizeof(*rs)); | |
582 | rs->min_bw[0] = rs->min_run[0] = ~0UL; | |
583 | rs->min_bw[1] = rs->min_run[1] = ~0UL; | |
584 | } | |
585 | ||
34572e28 | 586 | for_each_td(td, i) { |
3c39a379 JA |
587 | unsigned long long rbw, wbw; |
588 | ||
3c39a379 JA |
589 | rs = &runstats[td->groupid]; |
590 | ||
591 | if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0]) | |
592 | rs->min_run[0] = td->runtime[0]; | |
593 | if (td->runtime[0] > rs->max_run[0]) | |
594 | rs->max_run[0] = td->runtime[0]; | |
595 | if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1]) | |
596 | rs->min_run[1] = td->runtime[1]; | |
597 | if (td->runtime[1] > rs->max_run[1]) | |
598 | rs->max_run[1] = td->runtime[1]; | |
599 | ||
600 | rbw = wbw = 0; | |
601 | if (td->runtime[0]) | |
602 | rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0]; | |
603 | if (td->runtime[1]) | |
604 | wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1]; | |
605 | ||
606 | if (rbw < rs->min_bw[0]) | |
607 | rs->min_bw[0] = rbw; | |
608 | if (wbw < rs->min_bw[1]) | |
609 | rs->min_bw[1] = wbw; | |
610 | if (rbw > rs->max_bw[0]) | |
611 | rs->max_bw[0] = rbw; | |
612 | if (wbw > rs->max_bw[1]) | |
613 | rs->max_bw[1] = wbw; | |
614 | ||
615 | rs->io_kb[0] += td->io_bytes[0] >> 10; | |
616 | rs->io_kb[1] += td->io_bytes[1] >> 10; | |
617 | } | |
618 | ||
619 | for (i = 0; i < groupid + 1; i++) { | |
620 | rs = &runstats[i]; | |
621 | ||
622 | if (rs->max_run[0]) | |
623 | rs->agg[0] = (rs->io_kb[0]*1024) / rs->max_run[0]; | |
624 | if (rs->max_run[1]) | |
625 | rs->agg[1] = (rs->io_kb[1]*1024) / rs->max_run[1]; | |
626 | } | |
627 | ||
628 | /* | |
629 | * don't overwrite last signal output | |
630 | */ | |
c6ae0a5b JA |
631 | if (!terse_output) |
632 | printf("\n"); | |
3c39a379 | 633 | |
34572e28 | 634 | for_each_td(td, i) { |
3c39a379 JA |
635 | rs = &runstats[td->groupid]; |
636 | ||
c6ae0a5b JA |
637 | if (terse_output) |
638 | show_thread_status_terse(td, rs); | |
639 | else | |
640 | show_thread_status(td, rs); | |
3c39a379 JA |
641 | } |
642 | ||
c6ae0a5b JA |
643 | if (!terse_output) { |
644 | for (i = 0; i < groupid + 1; i++) | |
645 | show_group_stats(&runstats[i], i); | |
3c39a379 | 646 | |
c6ae0a5b JA |
647 | show_disk_util(); |
648 | } | |
eecf272f JA |
649 | |
650 | free(runstats); | |
3c39a379 JA |
651 | } |
652 | ||
68704084 | 653 | static inline void add_stat_sample(struct io_stat *is, unsigned long data) |
3c39a379 | 654 | { |
68704084 JA |
655 | double val = data; |
656 | double delta, n; | |
657 | ||
658 | if (data > is->max_val) | |
659 | is->max_val = data; | |
660 | if (data < is->min_val) | |
661 | is->min_val = data; | |
662 | ||
663 | delta = val - is->mean; | |
664 | n = is->samples + 1.0; | |
665 | is->mean += delta / n; | |
666 | is->S += delta * (val - is->mean); | |
3c39a379 | 667 | |
3c39a379 JA |
668 | is->samples++; |
669 | } | |
670 | ||
bb3884d8 JA |
671 | static void __add_log_sample(struct io_log *iolog, unsigned long val, |
672 | enum fio_ddir ddir, unsigned long time) | |
3c39a379 JA |
673 | { |
674 | if (iolog->nr_samples == iolog->max_samples) { | |
675 | int new_size = sizeof(struct io_sample) * iolog->max_samples*2; | |
676 | ||
677 | iolog->log = realloc(iolog->log, new_size); | |
678 | iolog->max_samples <<= 1; | |
679 | } | |
680 | ||
681 | iolog->log[iolog->nr_samples].val = val; | |
bb3884d8 | 682 | iolog->log[iolog->nr_samples].time = time; |
3c39a379 JA |
683 | iolog->log[iolog->nr_samples].ddir = ddir; |
684 | iolog->nr_samples++; | |
685 | } | |
686 | ||
bb3884d8 JA |
687 | static void add_log_sample(struct thread_data *td, struct io_log *iolog, |
688 | unsigned long val, enum fio_ddir ddir) | |
689 | { | |
690 | __add_log_sample(iolog, val, ddir, mtime_since_now(&td->epoch)); | |
691 | } | |
692 | ||
693 | void add_agg_sample(unsigned long val, enum fio_ddir ddir) | |
694 | { | |
695 | struct io_log *iolog = agg_io_log[ddir]; | |
696 | ||
697 | __add_log_sample(iolog, val, ddir, mtime_since_genesis()); | |
698 | } | |
699 | ||
1e97cce9 JA |
700 | void add_clat_sample(struct thread_data *td, enum fio_ddir ddir, |
701 | unsigned long msec) | |
3c39a379 | 702 | { |
079ad09b JA |
703 | struct thread_stat *ts = &td->ts; |
704 | ||
705 | add_stat_sample(&ts->clat_stat[ddir], msec); | |
3c39a379 | 706 | |
079ad09b JA |
707 | if (ts->clat_log) |
708 | add_log_sample(td, ts->clat_log, msec, ddir); | |
3c39a379 JA |
709 | } |
710 | ||
1e97cce9 JA |
711 | void add_slat_sample(struct thread_data *td, enum fio_ddir ddir, |
712 | unsigned long msec) | |
3c39a379 | 713 | { |
079ad09b JA |
714 | struct thread_stat *ts = &td->ts; |
715 | ||
716 | add_stat_sample(&ts->slat_stat[ddir], msec); | |
3c39a379 | 717 | |
079ad09b JA |
718 | if (ts->slat_log) |
719 | add_log_sample(td, ts->slat_log, msec, ddir); | |
3c39a379 JA |
720 | } |
721 | ||
1e97cce9 JA |
722 | void add_bw_sample(struct thread_data *td, enum fio_ddir ddir, |
723 | struct timeval *t) | |
3c39a379 | 724 | { |
079ad09b JA |
725 | struct thread_stat *ts = &td->ts; |
726 | unsigned long spent = mtime_since(&ts->stat_sample_time[ddir], t); | |
3c39a379 JA |
727 | unsigned long rate; |
728 | ||
729 | if (spent < td->bw_avg_time) | |
730 | return; | |
731 | ||
079ad09b JA |
732 | rate = (td->this_io_bytes[ddir] - ts->stat_io_bytes[ddir]) / spent; |
733 | add_stat_sample(&ts->bw_stat[ddir], rate); | |
3c39a379 | 734 | |
079ad09b JA |
735 | if (ts->bw_log) |
736 | add_log_sample(td, ts->bw_log, rate, ddir); | |
3c39a379 | 737 | |
079ad09b JA |
738 | fio_gettime(&ts->stat_sample_time[ddir], NULL); |
739 | ts->stat_io_bytes[ddir] = td->this_io_bytes[ddir]; | |
3c39a379 | 740 | } |