json output fixups
[fio.git] / diskutil.c
CommitLineData
9f8f2064
JA
1#include <stdio.h>
2#include <string.h>
3#include <sys/time.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <dirent.h>
7#include <libgen.h>
8#include <math.h>
9edf7b3e 9#include <assert.h>
9f8f2064
JA
10
11#include "fio.h"
c97bd0fa 12#include "smalloc.h"
7c9b1bce 13#include "diskutil.h"
9f8f2064
JA
14
15static int last_majdev, last_mindev;
c97bd0fa 16static struct disk_util *last_du;
9f8f2064 17
9ec7779f 18static struct fio_mutex *disk_util_mutex;
9ec7779f 19
d09a64a0 20FLIST_HEAD(disk_list);
9f8f2064 21
2db4ecca
JA
22static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
23 int majdev, int mindev, char *path);
24
c97bd0fa
JA
25static void disk_util_free(struct disk_util *du)
26{
27 if (du == last_du)
28 last_du = NULL;
29
b8d541d5
JA
30 while (!flist_empty(&du->slaves)) {
31 struct disk_util *slave;
32
9342d5f8 33 slave = flist_first_entry(&du->slaves, struct disk_util, slavelist);
b8d541d5
JA
34 flist_del(&slave->slavelist);
35 slave->users--;
36 }
93bcfd20 37
c97bd0fa 38 fio_mutex_remove(du->lock);
c97bd0fa
JA
39 sfree(du);
40}
41
9f8f2064
JA
42static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
43{
44 unsigned in_flight;
d09a64a0 45 unsigned long long sectors[2];
9f8f2064
JA
46 char line[256];
47 FILE *f;
48 char *p;
5ec10eaa 49 int ret;
9f8f2064 50
cd991b9e
JA
51 dprint(FD_DISKUTIL, "open stat file: %s\n", du->path);
52
9f8f2064
JA
53 f = fopen(du->path, "r");
54 if (!f)
55 return 1;
56
57 p = fgets(line, sizeof(line), f);
58 if (!p) {
59 fclose(f);
60 return 1;
61 }
62
cd991b9e
JA
63 dprint(FD_DISKUTIL, "%s: %s", du->path, p);
64
504bc961
JA
65 ret = sscanf(p, "%llu %llu %llu %llu %llu %llu %llu %llu %u %llu %llu\n",
66 (unsigned long long *) &dus->s.ios[0],
67 (unsigned long long *) &dus->s.merges[0],
68 &sectors[0],
69 (unsigned long long *) &dus->s.ticks[0],
70 (unsigned long long *) &dus->s.ios[1],
71 (unsigned long long *) &dus->s.merges[1],
72 &sectors[1],
73 (unsigned long long *) &dus->s.ticks[1],
74 &in_flight,
75 (unsigned long long *) &dus->s.io_ticks,
76 (unsigned long long *) &dus->s.time_in_queue);
9f8f2064 77 fclose(f);
cd991b9e 78 dprint(FD_DISKUTIL, "%s: stat read ok? %d\n", du->path, ret == 1);
a3b4cf7d
JA
79 dus->s.sectors[0] = sectors[0];
80 dus->s.sectors[1] = sectors[1];
cd991b9e 81 return ret != 11;
9f8f2064
JA
82}
83
84static void update_io_tick_disk(struct disk_util *du)
85{
86 struct disk_util_stat __dus, *dus, *ldus;
87 struct timeval t;
88
c97bd0fa
JA
89 if (!du->users)
90 return;
90b97ac3
JA
91 if (get_io_ticks(du, &__dus))
92 return;
9f8f2064
JA
93
94 dus = &du->dus;
95 ldus = &du->last_dus;
96
a3b4cf7d
JA
97 dus->s.sectors[0] += (__dus.s.sectors[0] - ldus->s.sectors[0]);
98 dus->s.sectors[1] += (__dus.s.sectors[1] - ldus->s.sectors[1]);
99 dus->s.ios[0] += (__dus.s.ios[0] - ldus->s.ios[0]);
100 dus->s.ios[1] += (__dus.s.ios[1] - ldus->s.ios[1]);
101 dus->s.merges[0] += (__dus.s.merges[0] - ldus->s.merges[0]);
102 dus->s.merges[1] += (__dus.s.merges[1] - ldus->s.merges[1]);
103 dus->s.ticks[0] += (__dus.s.ticks[0] - ldus->s.ticks[0]);
104 dus->s.ticks[1] += (__dus.s.ticks[1] - ldus->s.ticks[1]);
105 dus->s.io_ticks += (__dus.s.io_ticks - ldus->s.io_ticks);
106 dus->s.time_in_queue += (__dus.s.time_in_queue - ldus->s.time_in_queue);
9f8f2064
JA
107
108 fio_gettime(&t, NULL);
a3b4cf7d 109 dus->s.msec += mtime_since(&du->time, &t);
9f8f2064 110 memcpy(&du->time, &t, sizeof(t));
a3b4cf7d 111 memcpy(&ldus->s, &__dus.s, sizeof(__dus.s));
9f8f2064
JA
112}
113
9ec7779f 114int update_io_ticks(void)
9f8f2064 115{
01743ee1 116 struct flist_head *entry;
9f8f2064 117 struct disk_util *du;
9ec7779f 118 int ret = 0;
9f8f2064 119
cd991b9e
JA
120 dprint(FD_DISKUTIL, "update io ticks\n");
121
9ec7779f
JA
122 fio_mutex_down(disk_util_mutex);
123
5ddc6707 124 if (!helper_exit) {
9ec7779f
JA
125 flist_for_each(entry, &disk_list) {
126 du = flist_entry(entry, struct disk_util, list);
127 update_io_tick_disk(du);
128 }
129 } else
130 ret = 1;
131
132 fio_mutex_up(disk_util_mutex);
133 return ret;
9f8f2064
JA
134}
135
e11c410c 136static struct disk_util *disk_util_exists(int major, int minor)
9f8f2064 137{
01743ee1 138 struct flist_head *entry;
9f8f2064
JA
139 struct disk_util *du;
140
9ec7779f
JA
141 fio_mutex_down(disk_util_mutex);
142
01743ee1
JA
143 flist_for_each(entry, &disk_list) {
144 du = flist_entry(entry, struct disk_util, list);
9f8f2064 145
9ec7779f
JA
146 if (major == du->major && minor == du->minor) {
147 fio_mutex_up(disk_util_mutex);
e11c410c 148 return du;
9ec7779f 149 }
9f8f2064
JA
150 }
151
9ec7779f 152 fio_mutex_up(disk_util_mutex);
e11c410c 153 return NULL;
9f8f2064
JA
154}
155
67423013
ST
156static int get_device_numbers(char *file_name, int *maj, int *min)
157{
158 struct stat st;
159 int majdev, mindev;
160 char tempname[PATH_MAX], *p;
161
162 if (!lstat(file_name, &st)) {
163 if (S_ISBLK(st.st_mode)) {
164 majdev = major(st.st_rdev);
165 mindev = minor(st.st_rdev);
166 } else if (S_ISCHR(st.st_mode)) {
167 majdev = major(st.st_rdev);
168 mindev = minor(st.st_rdev);
169 if (fio_lookup_raw(st.st_rdev, &majdev, &mindev))
170 return -1;
171 } else if (S_ISFIFO(st.st_mode))
172 return -1;
173 else {
174 majdev = major(st.st_dev);
175 mindev = minor(st.st_dev);
176 }
177 } else {
178 /*
179 * must be a file, open "." in that path
180 */
181 strncpy(tempname, file_name, PATH_MAX - 1);
182 p = dirname(tempname);
183 if (stat(p, &st)) {
184 perror("disk util stat");
185 return -1;
186 }
187
188 majdev = major(st.st_dev);
189 mindev = minor(st.st_dev);
190 }
191
192 *min = mindev;
193 *maj = majdev;
194
195 return 0;
196}
197
198static int read_block_dev_entry(char *path, int *maj, int *min)
199{
200 char line[256], *p;
201 FILE *f;
202
203 f = fopen(path, "r");
204 if (!f) {
205 perror("open path");
206 return 1;
207 }
208
209 p = fgets(line, sizeof(line), f);
210 fclose(f);
211
212 if (!p)
213 return 1;
214
215 if (sscanf(p, "%u:%u", maj, min) != 2)
216 return 1;
217
218 return 0;
219}
220
67423013 221static void find_add_disk_slaves(struct thread_data *td, char *path,
2db4ecca 222 struct disk_util *masterdu)
67423013
ST
223{
224 DIR *dirhandle = NULL;
225 struct dirent *dirent = NULL;
226 char slavesdir[PATH_MAX], temppath[PATH_MAX], slavepath[PATH_MAX];
227 struct disk_util *slavedu = NULL;
228 int majdev, mindev;
229 ssize_t linklen;
230
2db4ecca 231 sprintf(slavesdir, "%s/%s", path, "slaves");
67423013
ST
232 dirhandle = opendir(slavesdir);
233 if (!dirhandle)
234 return;
235
236 while ((dirent = readdir(dirhandle)) != NULL) {
2db4ecca
JA
237 if (!strcmp(dirent->d_name, ".") ||
238 !strcmp(dirent->d_name, ".."))
67423013
ST
239 continue;
240
b9fd788f 241 sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, dirent->d_name);
67423013
ST
242 /* Can we always assume that the slaves device entries
243 * are links to the real directories for the slave
244 * devices?
245 */
976406b8 246 linklen = readlink(temppath, slavepath, PATH_MAX - 1);
2db4ecca 247 if (linklen < 0) {
67423013 248 perror("readlink() for slave device.");
f0cc675f 249 closedir(dirhandle);
67423013
ST
250 return;
251 }
2db4ecca 252 slavepath[linklen] = '\0';
67423013
ST
253
254 sprintf(temppath, "%s/%s/dev", slavesdir, slavepath);
255 if (read_block_dev_entry(temppath, &majdev, &mindev)) {
256 perror("Error getting slave device numbers.");
f0cc675f 257 closedir(dirhandle);
67423013
ST
258 return;
259 }
260
b06de82e
AB
261 /*
262 * See if this maj,min already exists
263 */
264 slavedu = disk_util_exists(majdev, mindev);
265 if (slavedu)
266 continue;
267
b9fd788f 268 sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, slavepath);
67423013
ST
269 __init_per_file_disk_util(td, majdev, mindev, temppath);
270 slavedu = disk_util_exists(majdev, mindev);
271
272 /* Should probably use an assert here. slavedu should
273 * always be present at this point. */
b8d541d5
JA
274 if (slavedu) {
275 slavedu->users++;
67423013 276 flist_add_tail(&slavedu->slavelist, &masterdu->slaves);
b8d541d5 277 }
67423013
ST
278 }
279
280 closedir(dirhandle);
67423013
ST
281}
282
b8fefc2b 283static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
c97bd0fa 284 int mindev, char *path)
9f8f2064
JA
285{
286 struct disk_util *du, *__du;
01743ee1 287 struct flist_head *entry;
4b919f77 288 int l;
9f8f2064 289
cd991b9e
JA
290 dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
291
c97bd0fa 292 du = smalloc(sizeof(*du));
fba5c5ff
JA
293 if (!du) {
294 log_err("fio: smalloc() pool exhausted\n");
295 return NULL;
296 }
297
9f8f2064 298 memset(du, 0, sizeof(*du));
01743ee1 299 INIT_FLIST_HEAD(&du->list);
4b919f77
KR
300 l = snprintf(du->path, sizeof(du->path), "%s/stat", path);
301 if (l < 0 || l >= sizeof(du->path)) {
302 log_err("constructed path \"%.100s[...]/stat\" larger than buffer (%zu bytes)\n",
303 path, sizeof(du->path) - 1);
304 sfree(du);
305 return NULL;
306 }
a2ba41ed 307 strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ - 1);
e11c410c 308 du->sysfs_root = path;
9f8f2064
JA
309 du->major = majdev;
310 du->minor = mindev;
67423013
ST
311 INIT_FLIST_HEAD(&du->slavelist);
312 INIT_FLIST_HEAD(&du->slaves);
521da527 313 du->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
c97bd0fa 314 du->users = 0;
9f8f2064 315
9ec7779f
JA
316 fio_mutex_down(disk_util_mutex);
317
01743ee1
JA
318 flist_for_each(entry, &disk_list) {
319 __du = flist_entry(entry, struct disk_util, list);
9f8f2064 320
d09a64a0 321 dprint(FD_DISKUTIL, "found %s in list\n", __du->dus.name);
cd991b9e 322
d09a64a0 323 if (!strcmp((char *) du->dus.name, (char *) __du->dus.name)) {
c97bd0fa 324 disk_util_free(du);
9ec7779f 325 fio_mutex_up(disk_util_mutex);
c97bd0fa 326 return __du;
9f8f2064
JA
327 }
328 }
329
d09a64a0 330 dprint(FD_DISKUTIL, "add %s to list\n", du->dus.name);
cd991b9e 331
9f8f2064
JA
332 fio_gettime(&du->time, NULL);
333 get_io_ticks(du, &du->last_dus);
334
01743ee1 335 flist_add_tail(&du->list, &disk_list);
9ec7779f
JA
336 fio_mutex_up(disk_util_mutex);
337
67423013 338 find_add_disk_slaves(td, path, du);
c97bd0fa 339 return du;
9f8f2064
JA
340}
341
342static int check_dev_match(int majdev, int mindev, char *path)
343{
344 int major, minor;
9f8f2064 345
67423013 346 if (read_block_dev_entry(path, &major, &minor))
9f8f2064 347 return 1;
9f8f2064 348
c97bd0fa 349 if (majdev == major && mindev == minor)
9f8f2064 350 return 0;
9f8f2064 351
9f8f2064
JA
352 return 1;
353}
354
ac0be2ac 355static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
9f8f2064
JA
356{
357 struct dirent *dir;
358 struct stat st;
359 int found = 0;
360 DIR *D;
361
362 D = opendir(path);
363 if (!D)
364 return 0;
365
366 while ((dir = readdir(D)) != NULL) {
367 char full_path[256];
368
369 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
370 continue;
371
b9fd788f 372 sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
9f8f2064
JA
373
374 if (!strcmp(dir->d_name, "dev")) {
375 if (!check_dev_match(majdev, mindev, full_path)) {
376 found = 1;
377 break;
378 }
379 }
380
ac0be2ac
JA
381 if (link_ok) {
382 if (stat(full_path, &st) == -1) {
383 perror("stat");
384 break;
385 }
386 } else {
387 if (lstat(full_path, &st) == -1) {
388 perror("stat");
389 break;
390 }
9f8f2064
JA
391 }
392
393 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
394 continue;
395
ac0be2ac 396 found = find_block_dir(majdev, mindev, full_path, 0);
9f8f2064
JA
397 if (found) {
398 strcpy(path, full_path);
399 break;
400 }
401 }
402
403 closedir(D);
404 return found;
405}
406
c97bd0fa
JA
407static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
408 int majdev, int mindev,
409 char *path)
9f8f2064
JA
410{
411 struct stat st;
67423013 412 char tmp[PATH_MAX];
9f8f2064
JA
413 char *p;
414
67423013
ST
415 /*
416 * If there's a ../queue/ directory there, we are inside a partition.
417 * Check if that is the case and jump back. For loop/md/dm etc we
418 * are already in the right spot.
419 */
420 sprintf(tmp, "%s/../queue", path);
421 if (!stat(tmp, &st)) {
422 p = dirname(path);
423 sprintf(tmp, "%s/queue", p);
424 if (stat(tmp, &st)) {
425 log_err("unknown sysfs layout\n");
c97bd0fa 426 return NULL;
9f8f2064 427 }
67423013
ST
428 strncpy(tmp, p, PATH_MAX - 1);
429 sprintf(path, "%s", tmp);
9f8f2064
JA
430 }
431
67423013
ST
432 if (td->o.ioscheduler && !td->sysfs_root)
433 td->sysfs_root = strdup(path);
434
c97bd0fa 435 return disk_util_add(td, majdev, mindev, path);
67423013
ST
436}
437
c97bd0fa
JA
438static struct disk_util *init_per_file_disk_util(struct thread_data *td,
439 char *filename)
67423013
ST
440{
441
442 char foo[PATH_MAX];
443 struct disk_util *du;
444 int mindev, majdev;
445
c97bd0fa
JA
446 if (get_device_numbers(filename, &majdev, &mindev))
447 return NULL;
67423013
ST
448
449 dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", filename, majdev,
450 mindev);
cd991b9e 451
e11c410c
JA
452 du = disk_util_exists(majdev, mindev);
453 if (du) {
454 if (td->o.ioscheduler && !td->sysfs_root)
455 td->sysfs_root = strdup(du->sysfs_root);
456
c97bd0fa 457 return du;
e11c410c 458 }
9f8f2064
JA
459
460 /*
461 * for an fs without a device, we will repeatedly stat through
462 * sysfs which can take oodles of time for thousands of files. so
463 * cache the last lookup and compare with that before going through
464 * everything again.
465 */
466 if (mindev == last_mindev && majdev == last_majdev)
c97bd0fa 467 return last_du;
9f8f2064
JA
468
469 last_mindev = mindev;
470 last_majdev = majdev;
5ec10eaa 471
9f8f2064 472 sprintf(foo, "/sys/block");
ac0be2ac 473 if (!find_block_dir(majdev, mindev, foo, 1))
c97bd0fa 474 return NULL;
9f8f2064 475
c97bd0fa 476 return __init_per_file_disk_util(td, majdev, mindev, foo);
67423013 477}
9f8f2064 478
c97bd0fa
JA
479static struct disk_util *__init_disk_util(struct thread_data *td,
480 struct fio_file *f)
67423013
ST
481{
482 return init_per_file_disk_util(td, f->file_name);
9f8f2064
JA
483}
484
485void init_disk_util(struct thread_data *td)
486{
487 struct fio_file *f;
488 unsigned int i;
489
490 if (!td->o.do_disk_util ||
491 (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
492 return;
493
494 for_each_file(td, f, i)
c97bd0fa 495 f->du = __init_disk_util(td, f);
9f8f2064
JA
496}
497
a666cab8
JA
498static void show_agg_stats(struct disk_util_agg *agg, int terse,
499 struct buf_output *out)
d09a64a0
JA
500{
501 if (!agg->slavecount)
502 return;
503
f2f788dd 504 if (!terse) {
a666cab8 505 log_buf(out, ", aggrios=%llu/%llu, aggrmerge=%llu/%llu, "
504bc961
JA
506 "aggrticks=%llu/%llu, aggrin_queue=%llu, "
507 "aggrutil=%3.2f%%",
508 (unsigned long long) agg->ios[0] / agg->slavecount,
509 (unsigned long long) agg->ios[1] / agg->slavecount,
510 (unsigned long long) agg->merges[0] / agg->slavecount,
511 (unsigned long long) agg->merges[1] / agg->slavecount,
512 (unsigned long long) agg->ticks[0] / agg->slavecount,
513 (unsigned long long) agg->ticks[1] / agg->slavecount,
514 (unsigned long long) agg->time_in_queue / agg->slavecount,
515 agg->max_util.u.f);
f2f788dd 516 } else {
a666cab8 517 log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
504bc961
JA
518 (unsigned long long) agg->ios[0] / agg->slavecount,
519 (unsigned long long) agg->ios[1] / agg->slavecount,
520 (unsigned long long) agg->merges[0] / agg->slavecount,
521 (unsigned long long) agg->merges[1] / agg->slavecount,
522 (unsigned long long) agg->ticks[0] / agg->slavecount,
523 (unsigned long long) agg->ticks[1] / agg->slavecount,
524 (unsigned long long) agg->time_in_queue / agg->slavecount,
525 agg->max_util.u.f);
f2f788dd 526 }
d09a64a0
JA
527}
528
043b74b9
ST
529static void aggregate_slaves_stats(struct disk_util *masterdu)
530{
d09a64a0 531 struct disk_util_agg *agg = &masterdu->agg;
043b74b9
ST
532 struct disk_util_stat *dus;
533 struct flist_head *entry;
534 struct disk_util *slavedu;
d09a64a0 535 double util;
043b74b9
ST
536
537 flist_for_each(entry, &masterdu->slaves) {
538 slavedu = flist_entry(entry, struct disk_util, slavelist);
539 dus = &slavedu->dus;
a3b4cf7d
JA
540 agg->ios[0] += dus->s.ios[0];
541 agg->ios[1] += dus->s.ios[1];
542 agg->merges[0] += dus->s.merges[0];
543 agg->merges[1] += dus->s.merges[1];
544 agg->sectors[0] += dus->s.sectors[0];
545 agg->sectors[1] += dus->s.sectors[1];
546 agg->ticks[0] += dus->s.ticks[0];
547 agg->ticks[1] += dus->s.ticks[1];
548 agg->time_in_queue += dus->s.time_in_queue;
d09a64a0
JA
549 agg->slavecount++;
550
a3b4cf7d 551 util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
043b74b9
ST
552 /* System utilization is the utilization of the
553 * component with the highest utilization.
554 */
d09a64a0
JA
555 if (util > agg->max_util.u.f)
556 agg->max_util.u.f = util;
043b74b9
ST
557
558 }
559
d09a64a0
JA
560 if (agg->max_util.u.f > 100.0)
561 agg->max_util.u.f = 100.0;
562}
563
27357187 564void disk_util_prune_entries(void)
d09a64a0 565{
9ec7779f
JA
566 fio_mutex_down(disk_util_mutex);
567
d09a64a0 568 while (!flist_empty(&disk_list)) {
27357187
JA
569 struct disk_util *du;
570
9342d5f8 571 du = flist_first_entry(&disk_list, struct disk_util, list);
d09a64a0
JA
572 flist_del(&du->list);
573 disk_util_free(du);
574 }
043b74b9 575
d09a64a0 576 last_majdev = last_mindev = -1;
9ec7779f
JA
577 fio_mutex_up(disk_util_mutex);
578 fio_mutex_remove(disk_util_mutex);
d09a64a0
JA
579}
580
f2f788dd 581void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
a666cab8 582 int terse, struct buf_output *out)
d09a64a0 583{
6914f59e 584 double util = 0;
d09a64a0 585
a3b4cf7d
JA
586 if (dus->s.msec)
587 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
d09a64a0
JA
588 if (util > 100.0)
589 util = 100.0;
590
f2f788dd 591 if (!terse) {
f1d64553 592 if (agg->slavecount)
a666cab8 593 log_buf(out, " ");
f1d64553 594
a666cab8 595 log_buf(out, " %s: ios=%llu/%llu, merge=%llu/%llu, "
504bc961
JA
596 "ticks=%llu/%llu, in_queue=%llu, util=%3.2f%%",
597 dus->name,
598 (unsigned long long) dus->s.ios[0],
599 (unsigned long long) dus->s.ios[1],
600 (unsigned long long) dus->s.merges[0],
601 (unsigned long long) dus->s.merges[1],
602 (unsigned long long) dus->s.ticks[0],
603 (unsigned long long) dus->s.ticks[1],
604 (unsigned long long) dus->s.time_in_queue,
605 util);
f2f788dd 606 } else {
a666cab8 607 log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
504bc961
JA
608 dus->name,
609 (unsigned long long) dus->s.ios[0],
610 (unsigned long long) dus->s.ios[1],
611 (unsigned long long) dus->s.merges[0],
612 (unsigned long long) dus->s.merges[1],
613 (unsigned long long) dus->s.ticks[0],
614 (unsigned long long) dus->s.ticks[1],
615 (unsigned long long) dus->s.time_in_queue,
616 util);
f2f788dd 617 }
d09a64a0
JA
618
619 /*
620 * If the device has slaves, aggregate the stats for
621 * those slave devices also.
622 */
a666cab8 623 show_agg_stats(agg, terse, out);
d09a64a0 624
f2f788dd 625 if (!terse)
a666cab8 626 log_buf(out, "\n");
043b74b9
ST
627}
628
952b05e0
CF
629void json_array_add_disk_util(struct disk_util_stat *dus,
630 struct disk_util_agg *agg, struct json_array *array)
cc372b17 631{
cc372b17 632 struct json_object *obj;
952b05e0 633 double util = 0;
cc372b17 634
a3b4cf7d
JA
635 if (dus->s.msec)
636 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
cc372b17
SL
637 if (util > 100.0)
638 util = 100.0;
639
952b05e0
CF
640 obj = json_create_object();
641 json_array_add_value_object(array, obj);
cc372b17
SL
642
643 json_object_add_value_string(obj, "name", dus->name);
a3b4cf7d
JA
644 json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
645 json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
646 json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
647 json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
648 json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
649 json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
650 json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
cc372b17
SL
651 json_object_add_value_float(obj, "util", util);
652
653 /*
654 * If the device has slaves, aggregate the stats for
655 * those slave devices also.
656 */
657 if (!agg->slavecount)
658 return;
659 json_object_add_value_int(obj, "aggr_read_ios",
660 agg->ios[0] / agg->slavecount);
661 json_object_add_value_int(obj, "aggr_write_ios",
662 agg->ios[1] / agg->slavecount);
663 json_object_add_value_int(obj, "aggr_read_merges",
664 agg->merges[0] / agg->slavecount);
665 json_object_add_value_int(obj, "aggr_write_merge",
666 agg->merges[1] / agg->slavecount);
667 json_object_add_value_int(obj, "aggr_read_ticks",
668 agg->ticks[0] / agg->slavecount);
669 json_object_add_value_int(obj, "aggr_write_ticks",
670 agg->ticks[1] / agg->slavecount);
671 json_object_add_value_int(obj, "aggr_in_queue",
672 agg->time_in_queue / agg->slavecount);
673 json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
674}
675
10aa136b
JA
676static void json_object_add_disk_utils(struct json_object *obj,
677 struct flist_head *head)
952b05e0
CF
678{
679 struct json_array *array = json_create_array();
680 struct flist_head *entry;
681 struct disk_util *du;
682
683 json_object_add_value_array(obj, "disk_util", array);
684
685 flist_for_each(entry, head) {
686 du = flist_entry(entry, struct disk_util, list);
687
688 aggregate_slaves_stats(du);
689 json_array_add_disk_util(&du->dus, &du->agg, array);
690 }
691}
692
a666cab8
JA
693void show_disk_util(int terse, struct json_object *parent,
694 struct buf_output *out)
9f8f2064 695{
d09a64a0 696 struct flist_head *entry;
9f8f2064 697 struct disk_util *du;
9f8f2064 698
67a176fc
JA
699 if (!disk_util_mutex)
700 return;
701
9ec7779f
JA
702 fio_mutex_down(disk_util_mutex);
703
704 if (flist_empty(&disk_list)) {
705 fio_mutex_up(disk_util_mutex);
0a839f30 706 return;
9ec7779f 707 }
0a839f30 708
129fb2d4 709 if (output_format & FIO_OUTPUT_JSON)
9edf7b3e
JA
710 assert(parent);
711
129fb2d4 712 if (!terse && !(output_format & FIO_OUTPUT_JSON))
a666cab8 713 log_buf(out, "\nDisk stats (read/write):\n");
9f8f2064 714
129fb2d4 715 if (output_format & FIO_OUTPUT_JSON)
952b05e0 716 json_object_add_disk_utils(parent, &disk_list);
236d24df 717 if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
952b05e0
CF
718 flist_for_each(entry, &disk_list) {
719 du = flist_entry(entry, struct disk_util, list);
9f8f2064 720
952b05e0 721 aggregate_slaves_stats(du);
a666cab8 722 print_disk_util(&du->dus, &du->agg, terse, out);
952b05e0 723 }
129fb2d4 724 }
9ec7779f
JA
725
726 fio_mutex_up(disk_util_mutex);
727}
728
729void setup_disk_util(void)
730{
521da527 731 disk_util_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
9f8f2064 732}