Seperate disk util code out of fio.c
authorJens Axboe <jens.axboe@oracle.com>
Mon, 2 Apr 2007 13:08:01 +0000 (15:08 +0200)
committerJens Axboe <jens.axboe@oracle.com>
Mon, 2 Apr 2007 13:08:01 +0000 (15:08 +0200)
It's only used if FIO_HAVE_DISK_UTIL is set.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
Makefile
diskutil.c [new file with mode: 0644]
fio.h
stat.c

index 10b4ffc..f172ed4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ PROGS   = fio
 SCRIPTS = fio_generate_plots
 OBJS = gettime.o fio.o ioengines.o init.o stat.o log.o time.o md5.o crc32.o \
        filesetup.o eta.o verify.o memory.o io_u.o parse.o mutex.o options.o \
-       rbtree.o
+       rbtree.o diskutil.o
 
 OBJS += engines/cpu.o
 OBJS += engines/libaio.o
diff --git a/diskutil.c b/diskutil.c
new file mode 100644 (file)
index 0000000..399aced
--- /dev/null
@@ -0,0 +1,328 @@
+#include <stdio.h>
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <libgen.h>
+#include <math.h>
+
+#include "fio.h"
+
+static int last_majdev, last_mindev;
+static struct itimerval itimer;
+
+static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
+
+static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
+{
+       unsigned in_flight;
+       char line[256];
+       FILE *f;
+       char *p;
+
+       f = fopen(du->path, "r");
+       if (!f)
+               return 1;
+
+       p = fgets(line, sizeof(line), f);
+       if (!p) {
+               fclose(f);
+               return 1;
+       }
+
+       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) {
+               fclose(f);
+               return 1;
+       }
+
+       fclose(f);
+       return 0;
+}
+
+static void update_io_tick_disk(struct disk_util *du)
+{
+       struct disk_util_stat __dus, *dus, *ldus;
+       struct timeval t;
+
+       if (get_io_ticks(du, &__dus))
+               return;
+
+       dus = &du->dus;
+       ldus = &du->last_dus;
+
+       dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
+       dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
+       dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
+       dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
+       dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
+       dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
+       dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
+       dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
+       dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
+       dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
+
+       fio_gettime(&t, NULL);
+       du->msec += mtime_since(&du->time, &t);
+       memcpy(&du->time, &t, sizeof(t));
+       memcpy(ldus, &__dus, sizeof(__dus));
+}
+
+void update_io_ticks(void)
+{
+       struct list_head *entry;
+       struct disk_util *du;
+
+       list_for_each(entry, &disk_list) {
+               du = list_entry(entry, struct disk_util, list);
+               update_io_tick_disk(du);
+       }
+}
+
+static int disk_util_exists(int major, int minor)
+{
+       struct list_head *entry;
+       struct disk_util *du;
+
+       list_for_each(entry, &disk_list) {
+               du = list_entry(entry, struct disk_util, list);
+
+               if (major == du->major && minor == du->minor)
+                       return 1;
+       }
+
+       return 0;
+}
+
+static void disk_util_add(int majdev, int mindev, char *path)
+{
+       struct disk_util *du, *__du;
+       struct list_head *entry;
+
+       du = malloc(sizeof(*du));
+       memset(du, 0, sizeof(*du));
+       INIT_LIST_HEAD(&du->list);
+       sprintf(du->path, "%s/stat", path);
+       du->name = strdup(basename(path));
+       du->major = majdev;
+       du->minor = mindev;
+
+       list_for_each(entry, &disk_list) {
+               __du = list_entry(entry, struct disk_util, list);
+
+               if (!strcmp(du->name, __du->name)) {
+                       free(du->name);
+                       free(du);
+                       return;
+               }
+       }
+
+       fio_gettime(&du->time, NULL);
+       get_io_ticks(du, &du->last_dus);
+
+       list_add_tail(&du->list, &disk_list);
+}
+
+static int check_dev_match(int majdev, int mindev, char *path)
+{
+       int major, minor;
+       char line[256], *p;
+       FILE *f;
+
+       f = fopen(path, "r");
+       if (!f) {
+               perror("open path");
+               return 1;
+       }
+
+       p = fgets(line, sizeof(line), f);
+       if (!p) {
+               fclose(f);
+               return 1;
+       }
+
+       if (sscanf(p, "%u:%u", &major, &minor) != 2) {
+               fclose(f);
+               return 1;
+       }
+
+       if (majdev == major && mindev == minor) {
+               fclose(f);
+               return 0;
+       }
+
+       fclose(f);
+       return 1;
+}
+
+static int find_block_dir(int majdev, int mindev, char *path)
+{
+       struct dirent *dir;
+       struct stat st;
+       int found = 0;
+       DIR *D;
+
+       D = opendir(path);
+       if (!D)
+               return 0;
+
+       while ((dir = readdir(D)) != NULL) {
+               char full_path[256];
+
+               if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
+                       continue;
+
+               sprintf(full_path, "%s/%s", path, dir->d_name);
+
+               if (!strcmp(dir->d_name, "dev")) {
+                       if (!check_dev_match(majdev, mindev, full_path)) {
+                               found = 1;
+                               break;
+                       }
+               }
+
+               if (lstat(full_path, &st) == -1) {
+                       perror("stat");
+                       break;
+               }
+
+               if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
+                       continue;
+
+               found = find_block_dir(majdev, mindev, full_path);
+               if (found) {
+                       strcpy(path, full_path);
+                       break;
+               }
+       }
+
+       closedir(D);
+       return found;
+}
+
+static void __init_disk_util(struct thread_data *td, struct fio_file *f)
+{
+       struct stat st;
+       char foo[PATH_MAX], tmp[PATH_MAX];
+       int mindev, majdev;
+       char *p;
+
+       if (!stat(f->file_name, &st)) {
+               if (S_ISBLK(st.st_mode)) {
+                       majdev = major(st.st_rdev);
+                       mindev = minor(st.st_rdev);
+               } else if (S_ISCHR(st.st_mode)) {
+                       majdev = major(st.st_rdev);
+                       mindev = minor(st.st_rdev);
+                       fio_lookup_raw(st.st_rdev, &majdev, &mindev);
+               } else {
+                       majdev = major(st.st_dev);
+                       mindev = minor(st.st_dev);
+               }
+       } else {
+               /*
+                * must be a file, open "." in that path
+                */
+               strncpy(foo, f->file_name, PATH_MAX - 1);
+               p = dirname(foo);
+               if (stat(p, &st)) {
+                       perror("disk util stat");
+                       return;
+               }
+
+               majdev = major(st.st_dev);
+               mindev = minor(st.st_dev);
+       }
+
+       if (disk_util_exists(majdev, mindev))
+               return;
+
+       /*
+        * for an fs without a device, we will repeatedly stat through
+        * sysfs which can take oodles of time for thousands of files. so
+        * cache the last lookup and compare with that before going through
+        * everything again.
+        */
+       if (mindev == last_mindev && majdev == last_majdev)
+               return;
+
+       last_mindev = mindev;
+       last_majdev = majdev;
+               
+       sprintf(foo, "/sys/block");
+       if (!find_block_dir(majdev, mindev, foo))
+               return;
+
+       /*
+        * If there's a ../queue/ directory there, we are inside a partition.
+        * Check if that is the case and jump back. For loop/md/dm etc we
+        * are already in the right spot.
+        */
+       sprintf(tmp, "%s/../queue", foo);
+       if (!stat(tmp, &st)) {
+               p = dirname(foo);
+               sprintf(tmp, "%s/queue", p);
+               if (stat(tmp, &st)) {
+                       log_err("unknown sysfs layout\n");
+                       return;
+               }
+               strncpy(tmp, p, PATH_MAX - 1);
+               sprintf(foo, "%s", tmp);
+       }
+
+       if (td->o.ioscheduler && !td->sysfs_root)
+               td->sysfs_root = strdup(foo);
+
+       disk_util_add(majdev, mindev, foo);
+}
+
+void init_disk_util(struct thread_data *td)
+{
+       struct fio_file *f;
+       unsigned int i;
+
+       if (!td->o.do_disk_util ||
+           (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
+               return;
+
+       for_each_file(td, f, i)
+               __init_disk_util(td, f);
+}
+
+void disk_util_timer_arm(void)
+{
+       itimer.it_value.tv_sec = 0;
+       itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
+       setitimer(ITIMER_REAL, &itimer, NULL);
+}
+
+void show_disk_util(void)
+{
+       struct disk_util_stat *dus;
+       struct list_head *entry, *next;
+       struct disk_util *du;
+       double util;
+
+       log_info("\nDisk stats (read/write):\n");
+
+       list_for_each(entry, &disk_list) {
+               du = list_entry(entry, struct disk_util, list);
+               dus = &du->dus;
+
+               util = (double) 100 * du->dus.io_ticks / (double) du->msec;
+               if (util > 100.0)
+                       util = 100.0;
+
+               log_info("  %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);
+       }
+
+       /*
+        * now free the list
+        */
+       list_for_each_safe(entry, next, &disk_list) {
+               list_del(entry);
+               du = list_entry(entry, struct disk_util, list);
+               free(du->name);
+               free(du);
+       }
+}
diff --git a/fio.h b/fio.h
index a86a582..a62971d 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -746,6 +746,21 @@ extern void dup_files(struct thread_data *, struct thread_data *);
 extern void print_thread_status(void);
 extern void print_status_init(int);
 
+/*
+ * disk util stuff
+ */
+#ifdef FIO_HAVE_DISK_UTIL
+extern void show_disk_util(void);
+extern void disk_util_timer_arm(void);
+extern void init_disk_util(struct thread_data *);
+extern void update_io_ticks(void);
+#else
+#define show_disk_util()
+#define disk_util_timer_arm()
+#define init_disk_util(td)
+#define update_io_ticks()
+#endif
+
 /*
  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
  * will never back again. It may cycle between running/verififying/fsyncing.
diff --git a/stat.c b/stat.c
index 29a5f91..3a785ee 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -9,10 +9,6 @@
 
 #include "fio.h"
 
-static struct itimerval itimer;
-static struct list_head disk_list = LIST_HEAD_INIT(disk_list);
-static int last_majdev, last_mindev;
-
 /*
  * Cheesy number->string conversion, complete with carry rounding error.
  */
@@ -56,288 +52,6 @@ static char *num2str(unsigned long num, int maxlen, int base, int pow2)
        return buf;
 }
 
-static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
-{
-       unsigned in_flight;
-       char line[256];
-       FILE *f;
-       char *p;
-
-       f = fopen(du->path, "r");
-       if (!f)
-               return 1;
-
-       p = fgets(line, sizeof(line), f);
-       if (!p) {
-               fclose(f);
-               return 1;
-       }
-
-       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) {
-               fclose(f);
-               return 1;
-       }
-
-       fclose(f);
-       return 0;
-}
-
-static void update_io_tick_disk(struct disk_util *du)
-{
-       struct disk_util_stat __dus, *dus, *ldus;
-       struct timeval t;
-
-       if (get_io_ticks(du, &__dus))
-               return;
-
-       dus = &du->dus;
-       ldus = &du->last_dus;
-
-       dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
-       dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
-       dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
-       dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
-       dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
-       dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
-       dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
-       dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
-       dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
-       dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
-
-       fio_gettime(&t, NULL);
-       du->msec += mtime_since(&du->time, &t);
-       memcpy(&du->time, &t, sizeof(t));
-       memcpy(ldus, &__dus, sizeof(__dus));
-}
-
-void update_io_ticks(void)
-{
-       struct list_head *entry;
-       struct disk_util *du;
-
-       list_for_each(entry, &disk_list) {
-               du = list_entry(entry, struct disk_util, list);
-               update_io_tick_disk(du);
-       }
-}
-
-static int disk_util_exists(int major, int minor)
-{
-       struct list_head *entry;
-       struct disk_util *du;
-
-       list_for_each(entry, &disk_list) {
-               du = list_entry(entry, struct disk_util, list);
-
-               if (major == du->major && minor == du->minor)
-                       return 1;
-       }
-
-       return 0;
-}
-
-static void disk_util_add(int majdev, int mindev, char *path)
-{
-       struct disk_util *du, *__du;
-       struct list_head *entry;
-
-       du = malloc(sizeof(*du));
-       memset(du, 0, sizeof(*du));
-       INIT_LIST_HEAD(&du->list);
-       sprintf(du->path, "%s/stat", path);
-       du->name = strdup(basename(path));
-       du->major = majdev;
-       du->minor = mindev;
-
-       list_for_each(entry, &disk_list) {
-               __du = list_entry(entry, struct disk_util, list);
-
-               if (!strcmp(du->name, __du->name)) {
-                       free(du->name);
-                       free(du);
-                       return;
-               }
-       }
-
-       fio_gettime(&du->time, NULL);
-       get_io_ticks(du, &du->last_dus);
-
-       list_add_tail(&du->list, &disk_list);
-}
-
-static int check_dev_match(int majdev, int mindev, char *path)
-{
-       int major, minor;
-       char line[256], *p;
-       FILE *f;
-
-       f = fopen(path, "r");
-       if (!f) {
-               perror("open path");
-               return 1;
-       }
-
-       p = fgets(line, sizeof(line), f);
-       if (!p) {
-               fclose(f);
-               return 1;
-       }
-
-       if (sscanf(p, "%u:%u", &major, &minor) != 2) {
-               fclose(f);
-               return 1;
-       }
-
-       if (majdev == major && mindev == minor) {
-               fclose(f);
-               return 0;
-       }
-
-       fclose(f);
-       return 1;
-}
-
-static int find_block_dir(int majdev, int mindev, char *path)
-{
-       struct dirent *dir;
-       struct stat st;
-       int found = 0;
-       DIR *D;
-
-       D = opendir(path);
-       if (!D)
-               return 0;
-
-       while ((dir = readdir(D)) != NULL) {
-               char full_path[256];
-
-               if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
-                       continue;
-
-               sprintf(full_path, "%s/%s", path, dir->d_name);
-
-               if (!strcmp(dir->d_name, "dev")) {
-                       if (!check_dev_match(majdev, mindev, full_path)) {
-                               found = 1;
-                               break;
-                       }
-               }
-
-               if (lstat(full_path, &st) == -1) {
-                       perror("stat");
-                       break;
-               }
-
-               if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
-                       continue;
-
-               found = find_block_dir(majdev, mindev, full_path);
-               if (found) {
-                       strcpy(path, full_path);
-                       break;
-               }
-       }
-
-       closedir(D);
-       return found;
-}
-
-static void __init_disk_util(struct thread_data *td, struct fio_file *f)
-{
-       struct stat st;
-       char foo[PATH_MAX], tmp[PATH_MAX];
-       int mindev, majdev;
-       char *p;
-
-       if (!stat(f->file_name, &st)) {
-               if (S_ISBLK(st.st_mode)) {
-                       majdev = major(st.st_rdev);
-                       mindev = minor(st.st_rdev);
-               } else if (S_ISCHR(st.st_mode)) {
-                       majdev = major(st.st_rdev);
-                       mindev = minor(st.st_rdev);
-                       fio_lookup_raw(st.st_rdev, &majdev, &mindev);
-               } else {
-                       majdev = major(st.st_dev);
-                       mindev = minor(st.st_dev);
-               }
-       } else {
-               /*
-                * must be a file, open "." in that path
-                */
-               strncpy(foo, f->file_name, PATH_MAX - 1);
-               p = dirname(foo);
-               if (stat(p, &st)) {
-                       perror("disk util stat");
-                       return;
-               }
-
-               majdev = major(st.st_dev);
-               mindev = minor(st.st_dev);
-       }
-
-       if (disk_util_exists(majdev, mindev))
-               return;
-
-       /*
-        * for an fs without a device, we will repeatedly stat through
-        * sysfs which can take oodles of time for thousands of files. so
-        * cache the last lookup and compare with that before going through
-        * everything again.
-        */
-       if (mindev == last_mindev && majdev == last_majdev)
-               return;
-
-       last_mindev = mindev;
-       last_majdev = majdev;
-               
-       sprintf(foo, "/sys/block");
-       if (!find_block_dir(majdev, mindev, foo))
-               return;
-
-       /*
-        * If there's a ../queue/ directory there, we are inside a partition.
-        * Check if that is the case and jump back. For loop/md/dm etc we
-        * are already in the right spot.
-        */
-       sprintf(tmp, "%s/../queue", foo);
-       if (!stat(tmp, &st)) {
-               p = dirname(foo);
-               sprintf(tmp, "%s/queue", p);
-               if (stat(tmp, &st)) {
-                       log_err("unknown sysfs layout\n");
-                       return;
-               }
-               strncpy(tmp, p, PATH_MAX - 1);
-               sprintf(foo, "%s", tmp);
-       }
-
-       if (td->o.ioscheduler && !td->sysfs_root)
-               td->sysfs_root = strdup(foo);
-
-       disk_util_add(majdev, mindev, foo);
-}
-
-void init_disk_util(struct thread_data *td)
-{
-       struct fio_file *f;
-       unsigned int i;
-
-       if (!td->o.do_disk_util ||
-           (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
-               return;
-
-       for_each_file(td, f, i)
-               __init_disk_util(td, f);
-}
-
-void disk_util_timer_arm(void)
-{
-       itimer.it_value.tv_sec = 0;
-       itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
-       setitimer(ITIMER_REAL, &itimer, NULL);
-}
-
 void update_rusage_stat(struct thread_data *td)
 {
        struct thread_stat *ts = &td->ts;
@@ -399,37 +113,6 @@ static void show_group_stats(struct group_run_stats *rs, int id)
        }
 }
 
-static void show_disk_util(void)
-{
-       struct disk_util_stat *dus;
-       struct list_head *entry, *next;
-       struct disk_util *du;
-       double util;
-
-       log_info("\nDisk stats (read/write):\n");
-
-       list_for_each(entry, &disk_list) {
-               du = list_entry(entry, struct disk_util, list);
-               dus = &du->dus;
-
-               util = (double) 100 * du->dus.io_ticks / (double) du->msec;
-               if (util > 100.0)
-                       util = 100.0;
-
-               log_info("  %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);
-       }
-
-       /*
-        * now free the list
-        */
-       list_for_each_safe(entry, next, &disk_list) {
-               list_del(entry);
-               du = list_entry(entry, struct disk_util, list);
-               free(du->name);
-               free(du);
-       }
-}
-
 #define ts_total_io_u(ts)      \
        ((ts)->total_io_u[0] + (ts)->total_io_u[1])