15 static int last_majdev, last_mindev;
16 static struct disk_util *last_du;
18 static struct fio_mutex *disk_util_mutex;
20 FLIST_HEAD(disk_list);
22 static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
23 int majdev, int mindev, char *path);
25 static void disk_util_free(struct disk_util *du)
30 while (!flist_empty(&du->slaves)) {
31 struct disk_util *slave;
33 slave = flist_first_entry(&du->slaves, struct disk_util, slavelist);
34 flist_del(&slave->slavelist);
38 fio_mutex_remove(du->lock);
42 static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
45 unsigned long long sectors[2];
51 dprint(FD_DISKUTIL, "open stat file: %s\n", du->path);
53 f = fopen(du->path, "r");
57 p = fgets(line, sizeof(line), f);
63 dprint(FD_DISKUTIL, "%s: %s", du->path, p);
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],
69 (unsigned long long *) &dus->s.ticks[0],
70 (unsigned long long *) &dus->s.ios[1],
71 (unsigned long long *) &dus->s.merges[1],
73 (unsigned long long *) &dus->s.ticks[1],
75 (unsigned long long *) &dus->s.io_ticks,
76 (unsigned long long *) &dus->s.time_in_queue);
78 dprint(FD_DISKUTIL, "%s: stat read ok? %d\n", du->path, ret == 1);
79 dus->s.sectors[0] = sectors[0];
80 dus->s.sectors[1] = sectors[1];
84 static void update_io_tick_disk(struct disk_util *du)
86 struct disk_util_stat __dus, *dus, *ldus;
91 if (get_io_ticks(du, &__dus))
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);
108 fio_gettime(&t, NULL);
109 dus->s.msec += mtime_since(&du->time, &t);
110 memcpy(&du->time, &t, sizeof(t));
111 memcpy(&ldus->s, &__dus.s, sizeof(__dus.s));
114 int update_io_ticks(void)
116 struct flist_head *entry;
117 struct disk_util *du;
120 dprint(FD_DISKUTIL, "update io ticks\n");
122 fio_mutex_down(disk_util_mutex);
125 flist_for_each(entry, &disk_list) {
126 du = flist_entry(entry, struct disk_util, list);
127 update_io_tick_disk(du);
132 fio_mutex_up(disk_util_mutex);
136 static struct disk_util *disk_util_exists(int major, int minor)
138 struct flist_head *entry;
139 struct disk_util *du;
141 fio_mutex_down(disk_util_mutex);
143 flist_for_each(entry, &disk_list) {
144 du = flist_entry(entry, struct disk_util, list);
146 if (major == du->major && minor == du->minor) {
147 fio_mutex_up(disk_util_mutex);
152 fio_mutex_up(disk_util_mutex);
156 static int get_device_numbers(char *file_name, int *maj, int *min)
160 char tempname[PATH_MAX], *p;
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))
171 } else if (S_ISFIFO(st.st_mode))
174 majdev = major(st.st_dev);
175 mindev = minor(st.st_dev);
179 * must be a file, open "." in that path
181 strncpy(tempname, file_name, PATH_MAX - 1);
182 p = dirname(tempname);
184 perror("disk util stat");
188 majdev = major(st.st_dev);
189 mindev = minor(st.st_dev);
198 static int read_block_dev_entry(char *path, int *maj, int *min)
203 f = fopen(path, "r");
209 p = fgets(line, sizeof(line), f);
215 if (sscanf(p, "%u:%u", maj, min) != 2)
221 static void find_add_disk_slaves(struct thread_data *td, char *path,
222 struct disk_util *masterdu)
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;
231 sprintf(slavesdir, "%s/%s", path, "slaves");
232 dirhandle = opendir(slavesdir);
236 while ((dirent = readdir(dirhandle)) != NULL) {
237 if (!strcmp(dirent->d_name, ".") ||
238 !strcmp(dirent->d_name, ".."))
241 sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, dirent->d_name);
242 /* Can we always assume that the slaves device entries
243 * are links to the real directories for the slave
246 linklen = readlink(temppath, slavepath, PATH_MAX - 1);
248 perror("readlink() for slave device.");
252 slavepath[linklen] = '\0';
254 sprintf(temppath, "%s/%s/dev", slavesdir, slavepath);
255 if (read_block_dev_entry(temppath, &majdev, &mindev)) {
256 perror("Error getting slave device numbers.");
262 * See if this maj,min already exists
264 slavedu = disk_util_exists(majdev, mindev);
268 sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, slavepath);
269 __init_per_file_disk_util(td, majdev, mindev, temppath);
270 slavedu = disk_util_exists(majdev, mindev);
272 /* Should probably use an assert here. slavedu should
273 * always be present at this point. */
276 flist_add_tail(&slavedu->slavelist, &masterdu->slaves);
283 static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
284 int mindev, char *path)
286 struct disk_util *du, *__du;
287 struct flist_head *entry;
290 dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
292 du = smalloc(sizeof(*du));
294 log_err("fio: smalloc() pool exhausted\n");
298 memset(du, 0, sizeof(*du));
299 INIT_FLIST_HEAD(&du->list);
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);
307 strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ - 1);
308 du->sysfs_root = path;
311 INIT_FLIST_HEAD(&du->slavelist);
312 INIT_FLIST_HEAD(&du->slaves);
313 du->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
316 fio_mutex_down(disk_util_mutex);
318 flist_for_each(entry, &disk_list) {
319 __du = flist_entry(entry, struct disk_util, list);
321 dprint(FD_DISKUTIL, "found %s in list\n", __du->dus.name);
323 if (!strcmp((char *) du->dus.name, (char *) __du->dus.name)) {
325 fio_mutex_up(disk_util_mutex);
330 dprint(FD_DISKUTIL, "add %s to list\n", du->dus.name);
332 fio_gettime(&du->time, NULL);
333 get_io_ticks(du, &du->last_dus);
335 flist_add_tail(&du->list, &disk_list);
336 fio_mutex_up(disk_util_mutex);
338 find_add_disk_slaves(td, path, du);
342 static int check_dev_match(int majdev, int mindev, char *path)
346 if (read_block_dev_entry(path, &major, &minor))
349 if (majdev == major && mindev == minor)
355 static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
366 while ((dir = readdir(D)) != NULL) {
369 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
372 sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
374 if (!strcmp(dir->d_name, "dev")) {
375 if (!check_dev_match(majdev, mindev, full_path)) {
382 if (stat(full_path, &st) == -1) {
387 if (lstat(full_path, &st) == -1) {
393 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
396 found = find_block_dir(majdev, mindev, full_path, 0);
398 strcpy(path, full_path);
407 static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
408 int majdev, int mindev,
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.
420 sprintf(tmp, "%s/../queue", path);
421 if (!stat(tmp, &st)) {
423 sprintf(tmp, "%s/queue", p);
424 if (stat(tmp, &st)) {
425 log_err("unknown sysfs layout\n");
428 strncpy(tmp, p, PATH_MAX - 1);
429 sprintf(path, "%s", tmp);
432 if (td->o.ioscheduler && !td->sysfs_root)
433 td->sysfs_root = strdup(path);
435 return disk_util_add(td, majdev, mindev, path);
438 static struct disk_util *init_per_file_disk_util(struct thread_data *td,
443 struct disk_util *du;
446 if (get_device_numbers(filename, &majdev, &mindev))
449 dprint(FD_DISKUTIL, "%s belongs to maj/min %d/%d\n", filename, majdev,
452 du = disk_util_exists(majdev, mindev);
454 if (td->o.ioscheduler && !td->sysfs_root)
455 td->sysfs_root = strdup(du->sysfs_root);
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
466 if (mindev == last_mindev && majdev == last_majdev)
469 last_mindev = mindev;
470 last_majdev = majdev;
472 sprintf(foo, "/sys/block");
473 if (!find_block_dir(majdev, mindev, foo, 1))
476 return __init_per_file_disk_util(td, majdev, mindev, foo);
479 static struct disk_util *__init_disk_util(struct thread_data *td,
482 return init_per_file_disk_util(td, f->file_name);
485 void init_disk_util(struct thread_data *td)
490 if (!td->o.do_disk_util ||
491 (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
494 for_each_file(td, f, i)
495 f->du = __init_disk_util(td, f);
498 static void show_agg_stats(struct disk_util_agg *agg, int terse,
499 struct buf_output *out)
501 if (!agg->slavecount)
505 log_buf(out, ", aggrios=%llu/%llu, aggrmerge=%llu/%llu, "
506 "aggrticks=%llu/%llu, aggrin_queue=%llu, "
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,
517 log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
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,
529 static void aggregate_slaves_stats(struct disk_util *masterdu)
531 struct disk_util_agg *agg = &masterdu->agg;
532 struct disk_util_stat *dus;
533 struct flist_head *entry;
534 struct disk_util *slavedu;
537 flist_for_each(entry, &masterdu->slaves) {
538 slavedu = flist_entry(entry, struct disk_util, slavelist);
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;
551 util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
552 /* System utilization is the utilization of the
553 * component with the highest utilization.
555 if (util > agg->max_util.u.f)
556 agg->max_util.u.f = util;
560 if (agg->max_util.u.f > 100.0)
561 agg->max_util.u.f = 100.0;
564 void disk_util_prune_entries(void)
566 fio_mutex_down(disk_util_mutex);
568 while (!flist_empty(&disk_list)) {
569 struct disk_util *du;
571 du = flist_first_entry(&disk_list, struct disk_util, list);
572 flist_del(&du->list);
576 last_majdev = last_mindev = -1;
577 fio_mutex_up(disk_util_mutex);
578 fio_mutex_remove(disk_util_mutex);
581 void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
582 int terse, struct buf_output *out)
587 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
595 log_buf(out, " %s: ios=%llu/%llu, merge=%llu/%llu, "
596 "ticks=%llu/%llu, in_queue=%llu, util=%3.2f%%",
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,
607 log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
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,
620 * If the device has slaves, aggregate the stats for
621 * those slave devices also.
623 show_agg_stats(agg, terse, out);
629 void json_array_add_disk_util(struct disk_util_stat *dus,
630 struct disk_util_agg *agg, struct json_array *array)
632 struct json_object *obj;
636 util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
640 obj = json_create_object();
641 json_array_add_value_object(array, obj);
643 json_object_add_value_string(obj, "name", dus->name);
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);
651 json_object_add_value_float(obj, "util", util);
654 * If the device has slaves, aggregate the stats for
655 * those slave devices also.
657 if (!agg->slavecount)
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);
676 static void json_object_add_disk_utils(struct json_object *obj,
677 struct flist_head *head)
679 struct json_array *array = json_create_array();
680 struct flist_head *entry;
681 struct disk_util *du;
683 json_object_add_value_array(obj, "disk_util", array);
685 flist_for_each(entry, head) {
686 du = flist_entry(entry, struct disk_util, list);
688 aggregate_slaves_stats(du);
689 json_array_add_disk_util(&du->dus, &du->agg, array);
693 void show_disk_util(int terse, struct json_object *parent,
694 struct buf_output *out)
696 struct flist_head *entry;
697 struct disk_util *du;
699 if (!disk_util_mutex)
702 fio_mutex_down(disk_util_mutex);
704 if (flist_empty(&disk_list)) {
705 fio_mutex_up(disk_util_mutex);
709 if (output_format & FIO_OUTPUT_JSON)
712 if (!terse && !(output_format & FIO_OUTPUT_JSON))
713 log_buf(out, "\nDisk stats (read/write):\n");
715 if (output_format & FIO_OUTPUT_JSON)
716 json_object_add_disk_utils(parent, &disk_list);
717 if (output_format & ~(FIO_OUTPUT_JSON)) {
718 flist_for_each(entry, &disk_list) {
719 du = flist_entry(entry, struct disk_util, list);
721 aggregate_slaves_stats(du);
722 print_disk_util(&du->dus, &du->agg, terse, out);
726 fio_mutex_up(disk_util_mutex);
729 void setup_disk_util(void)
731 disk_util_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);