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