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