Split mutex.c and .h each into three files
[fio.git] / diskutil.c
index bc12b02..789071d 100644 (file)
@@ -3,21 +3,22 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/sysmacros.h>
 #include <dirent.h>
 #include <libgen.h>
 #include <math.h>
+#include <assert.h>
 
 #include "fio.h"
 #include "smalloc.h"
 #include "diskutil.h"
+#include "helper_thread.h"
 
 static int last_majdev, last_mindev;
 static struct disk_util *last_du;
 
 static struct fio_mutex *disk_util_mutex;
 
-FLIST_HEAD(disk_list);
-
 static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
                int majdev, int mindev, char *path);
 
@@ -29,12 +30,13 @@ static void disk_util_free(struct disk_util *du)
        while (!flist_empty(&du->slaves)) {
                struct disk_util *slave;
 
-               slave = flist_entry(du->slaves.next, struct disk_util, slavelist);
+               slave = flist_first_entry(&du->slaves, struct disk_util, slavelist);
                flist_del(&slave->slavelist);
                slave->users--;
        }
 
        fio_mutex_remove(du->lock);
+       free(du->sysfs_root);
        sfree(du);
 }
 
@@ -61,23 +63,29 @@ static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
 
        dprint(FD_DISKUTIL, "%s: %s", du->path, p);
 
-       ret = sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0],
-                                       &dus->merges[0], &sectors[0],
-                                       &dus->ticks[0], &dus->ios[1],
-                                       &dus->merges[1], &sectors[1],
-                                       &dus->ticks[1], &in_flight,
-                                       &dus->io_ticks, &dus->time_in_queue);
+       ret = sscanf(p, "%llu %llu %llu %llu %llu %llu %llu %llu %u %llu %llu\n",
+                               (unsigned long long *) &dus->s.ios[0],
+                               (unsigned long long *) &dus->s.merges[0],
+                               &sectors[0],
+                               (unsigned long long *) &dus->s.ticks[0],
+                               (unsigned long long *) &dus->s.ios[1],
+                               (unsigned long long *) &dus->s.merges[1],
+                               &sectors[1],
+                               (unsigned long long *) &dus->s.ticks[1],
+                               &in_flight,
+                               (unsigned long long *) &dus->s.io_ticks,
+                               (unsigned long long *) &dus->s.time_in_queue);
        fclose(f);
        dprint(FD_DISKUTIL, "%s: stat read ok? %d\n", du->path, ret == 1);
-       dus->sectors[0] = sectors[0];
-       dus->sectors[1] = sectors[1];
+       dus->s.sectors[0] = sectors[0];
+       dus->s.sectors[1] = sectors[1];
        return ret != 11;
 }
 
 static void update_io_tick_disk(struct disk_util *du)
 {
        struct disk_util_stat __dus, *dus, *ldus;
-       struct timeval t;
+       struct timespec t;
 
        if (!du->users)
                return;
@@ -87,21 +95,21 @@ static void update_io_tick_disk(struct disk_util *du)
        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);
+       dus->s.sectors[0] += (__dus.s.sectors[0] - ldus->s.sectors[0]);
+       dus->s.sectors[1] += (__dus.s.sectors[1] - ldus->s.sectors[1]);
+       dus->s.ios[0] += (__dus.s.ios[0] - ldus->s.ios[0]);
+       dus->s.ios[1] += (__dus.s.ios[1] - ldus->s.ios[1]);
+       dus->s.merges[0] += (__dus.s.merges[0] - ldus->s.merges[0]);
+       dus->s.merges[1] += (__dus.s.merges[1] - ldus->s.merges[1]);
+       dus->s.ticks[0] += (__dus.s.ticks[0] - ldus->s.ticks[0]);
+       dus->s.ticks[1] += (__dus.s.ticks[1] - ldus->s.ticks[1]);
+       dus->s.io_ticks += (__dus.s.io_ticks - ldus->s.io_ticks);
+       dus->s.time_in_queue += (__dus.s.time_in_queue - ldus->s.time_in_queue);
 
        fio_gettime(&t, NULL);
-       dus->msec += mtime_since(&du->time, &t);
+       dus->s.msec += mtime_since(&du->time, &t);
        memcpy(&du->time, &t, sizeof(t));
-       memcpy(ldus, &__dus, sizeof(__dus));
+       memcpy(&ldus->s, &__dus.s, sizeof(__dus.s));
 }
 
 int update_io_ticks(void)
@@ -114,7 +122,7 @@ int update_io_ticks(void)
 
        fio_mutex_down(disk_util_mutex);
 
-       if (!disk_util_exit) {
+       if (!helper_should_exit()) {
                flist_for_each(entry, &disk_list) {
                        du = flist_entry(entry, struct disk_util, list);
                        update_io_tick_disk(du);
@@ -171,6 +179,7 @@ static int get_device_numbers(char *file_name, int *maj, int *min)
                /*
                 * must be a file, open "." in that path
                 */
+               tempname[PATH_MAX - 1] = '\0';
                strncpy(tempname, file_name, PATH_MAX - 1);
                p = dirname(tempname);
                if (stat(p, &st)) {
@@ -231,21 +240,25 @@ static void find_add_disk_slaves(struct thread_data *td, char *path,
                    !strcmp(dirent->d_name, ".."))
                        continue;
 
-               sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, dirent->d_name);
+               sprintf(temppath, "%s/%s", slavesdir, dirent->d_name);
                /* Can we always assume that the slaves device entries
                 * are links to the real directories for the slave
                 * devices?
                 */
-               linklen = readlink(temppath, slavepath, PATH_MAX - 0);
-               if (linklen  < 0) {
+               linklen = readlink(temppath, slavepath, PATH_MAX - 1);
+               if (linklen < 0) {
                        perror("readlink() for slave device.");
+                       closedir(dirhandle);
                        return;
                }
                slavepath[linklen] = '\0';
 
                sprintf(temppath, "%s/%s/dev", slavesdir, slavepath);
+               if (access(temppath, F_OK) != 0)
+                       sprintf(temppath, "%s/%s/device/dev", slavesdir, slavepath);
                if (read_block_dev_entry(temppath, &majdev, &mindev)) {
-                       perror("Error getting slave device numbers.");
+                       perror("Error getting slave device numbers");
+                       closedir(dirhandle);
                        return;
                }
 
@@ -256,7 +269,7 @@ static void find_add_disk_slaves(struct thread_data *td, char *path,
                if (slavedu)
                        continue;
 
-               sprintf(temppath, "%s%s%s", slavesdir, FIO_OS_PATH_SEPARATOR, slavepath);
+               sprintf(temppath, "%s/%s", slavesdir, slavepath);
                __init_per_file_disk_util(td, majdev, mindev, temppath);
                slavedu = disk_util_exists(majdev, mindev);
 
@@ -281,10 +294,8 @@ static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
        dprint(FD_DISKUTIL, "add maj/min %d/%d: %s\n", majdev, mindev, path);
 
        du = smalloc(sizeof(*du));
-       if (!du) {
-               log_err("fio: smalloc() pool exhausted\n");
+       if (!du)
                return NULL;
-       }
 
        memset(du, 0, sizeof(*du));
        INIT_FLIST_HEAD(&du->list);
@@ -295,8 +306,8 @@ static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
                sfree(du);
                return NULL;
        }
-       strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ);
-       du->sysfs_root = path;
+       strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ - 1);
+       du->sysfs_root = strdup(path);
        du->major = majdev;
        du->minor = mindev;
        INIT_FLIST_HEAD(&du->slavelist);
@@ -355,12 +366,12 @@ static int find_block_dir(int majdev, int mindev, char *path, int link_ok)
                return 0;
 
        while ((dir = readdir(D)) != NULL) {
-               char full_path[256];
+               char full_path[257];
 
                if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
                        continue;
 
-               sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
+               sprintf(full_path, "%s/%s", path, dir->d_name);
 
                if (!strcmp(dir->d_name, "dev")) {
                        if (!check_dev_match(majdev, mindev, full_path)) {
@@ -416,13 +427,11 @@ static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
                        log_err("unknown sysfs layout\n");
                        return NULL;
                }
+               tmp[PATH_MAX - 1] = '\0';
                strncpy(tmp, p, PATH_MAX - 1);
                sprintf(path, "%s", tmp);
        }
 
-       if (td->o.ioscheduler && !td->sysfs_root)
-               td->sysfs_root = strdup(path);
-
        return disk_util_add(td, majdev, mindev, path);
 }
 
@@ -441,12 +450,8 @@ static struct disk_util *init_per_file_disk_util(struct thread_data *td,
                        mindev);
 
        du = disk_util_exists(majdev, mindev);
-       if (du) {
-               if (td->o.ioscheduler && !td->sysfs_root)
-                       td->sysfs_root = strdup(du->sysfs_root);
-
+       if (du)
                return du;
-       }
 
        /*
         * for an fs without a device, we will repeatedly stat through
@@ -479,39 +484,41 @@ void init_disk_util(struct thread_data *td)
        unsigned int i;
 
        if (!td->o.do_disk_util ||
-           (td->io_ops->flags & (FIO_DISKLESSIO | FIO_NODISKUTIL)))
+           td_ioengine_flagged(td, FIO_DISKLESSIO | FIO_NODISKUTIL))
                return;
 
        for_each_file(td, f, i)
                f->du = __init_disk_util(td, f);
 }
 
-static void show_agg_stats(struct disk_util_agg *agg, int terse)
+static void show_agg_stats(struct disk_util_agg *agg, int terse,
+                          struct buf_output *out)
 {
        if (!agg->slavecount)
                return;
 
        if (!terse) {
-               log_info(", aggrios=%u/%u, aggrmerge=%u/%u, aggrticks=%u/%u,"
-                               " aggrin_queue=%u, aggrutil=%3.2f%%",
-                               agg->ios[0] / agg->slavecount,
-                               agg->ios[1] / agg->slavecount,
-                               agg->merges[0] / agg->slavecount,
-                               agg->merges[1] / agg->slavecount,
-                               agg->ticks[0] / agg->slavecount,
-                               agg->ticks[1] / agg->slavecount,
-                               agg->time_in_queue / agg->slavecount,
-                               agg->max_util.u.f);
+               log_buf(out, ", aggrios=%llu/%llu, aggrmerge=%llu/%llu, "
+                        "aggrticks=%llu/%llu, aggrin_queue=%llu, "
+                        "aggrutil=%3.2f%%",
+                       (unsigned long long) agg->ios[0] / agg->slavecount,
+                       (unsigned long long) agg->ios[1] / agg->slavecount,
+                       (unsigned long long) agg->merges[0] / agg->slavecount,
+                       (unsigned long long) agg->merges[1] / agg->slavecount,
+                       (unsigned long long) agg->ticks[0] / agg->slavecount,
+                       (unsigned long long) agg->ticks[1] / agg->slavecount,
+                       (unsigned long long) agg->time_in_queue / agg->slavecount,
+                       agg->max_util.u.f);
        } else {
-               log_info(";slaves;%u;%u;%u;%u;%u;%u;%u;%3.2f%%",
-                               agg->ios[0] / agg->slavecount,
-                               agg->ios[1] / agg->slavecount,
-                               agg->merges[0] / agg->slavecount,
-                               agg->merges[1] / agg->slavecount,
-                               agg->ticks[0] / agg->slavecount,
-                               agg->ticks[1] / agg->slavecount,
-                               agg->time_in_queue / agg->slavecount,
-                               agg->max_util.u.f);
+               log_buf(out, ";slaves;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
+                       (unsigned long long) agg->ios[0] / agg->slavecount,
+                       (unsigned long long) agg->ios[1] / agg->slavecount,
+                       (unsigned long long) agg->merges[0] / agg->slavecount,
+                       (unsigned long long) agg->merges[1] / agg->slavecount,
+                       (unsigned long long) agg->ticks[0] / agg->slavecount,
+                       (unsigned long long) agg->ticks[1] / agg->slavecount,
+                       (unsigned long long) agg->time_in_queue / agg->slavecount,
+                       agg->max_util.u.f);
        }
 }
 
@@ -526,18 +533,18 @@ static void aggregate_slaves_stats(struct disk_util *masterdu)
        flist_for_each(entry, &masterdu->slaves) {
                slavedu = flist_entry(entry, struct disk_util, slavelist);
                dus = &slavedu->dus;
-               agg->ios[0] += dus->ios[0];
-               agg->ios[1] += dus->ios[1];
-               agg->merges[0] += dus->merges[0];
-               agg->merges[1] += dus->merges[1];
-               agg->sectors[0] += dus->sectors[0];
-               agg->sectors[1] += dus->sectors[1];
-               agg->ticks[0] += dus->ticks[0];
-               agg->ticks[1] += dus->ticks[1];
-               agg->time_in_queue += dus->time_in_queue;
+               agg->ios[0] += dus->s.ios[0];
+               agg->ios[1] += dus->s.ios[1];
+               agg->merges[0] += dus->s.merges[0];
+               agg->merges[1] += dus->s.merges[1];
+               agg->sectors[0] += dus->s.sectors[0];
+               agg->sectors[1] += dus->s.sectors[1];
+               agg->ticks[0] += dus->s.ticks[0];
+               agg->ticks[1] += dus->s.ticks[1];
+               agg->time_in_queue += dus->s.time_in_queue;
                agg->slavecount++;
 
-               util = (double) (100 * dus->io_ticks / (double) slavedu->dus.msec);
+               util = (double) (100 * dus->s.io_ticks / (double) slavedu->dus.s.msec);
                /* System utilization is the utilization of the
                 * component with the highest utilization.
                 */
@@ -557,7 +564,7 @@ void disk_util_prune_entries(void)
        while (!flist_empty(&disk_list)) {
                struct disk_util *du;
 
-               du = flist_entry(disk_list.next, struct disk_util, list);
+               du = flist_first_entry(&disk_list, struct disk_util, list);
                flist_del(&du->list);
                disk_util_free(du);
        }
@@ -568,41 +575,51 @@ void disk_util_prune_entries(void)
 }
 
 void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
-                    int terse)
+                    int terse, struct buf_output *out)
 {
        double util = 0;
 
-       if (dus->msec)
-               util = (double) 100 * dus->io_ticks / (double) dus->msec;
+       if (dus->s.msec)
+               util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
        if (util > 100.0)
                util = 100.0;
 
        if (!terse) {
                if (agg->slavecount)
-                       log_info("  ");
-
-               log_info("  %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, "
-                        "in_queue=%u, util=%3.2f%%", dus->name,
-                                       dus->ios[0], dus->ios[1],
-                                       dus->merges[0], dus->merges[1],
-                                       dus->ticks[0], dus->ticks[1],
-                                       dus->time_in_queue, util);
+                       log_buf(out, "  ");
+
+               log_buf(out, "  %s: ios=%llu/%llu, merge=%llu/%llu, "
+                        "ticks=%llu/%llu, in_queue=%llu, util=%3.2f%%",
+                               dus->name,
+                               (unsigned long long) dus->s.ios[0],
+                               (unsigned long long) dus->s.ios[1],
+                               (unsigned long long) dus->s.merges[0],
+                               (unsigned long long) dus->s.merges[1],
+                               (unsigned long long) dus->s.ticks[0],
+                               (unsigned long long) dus->s.ticks[1],
+                               (unsigned long long) dus->s.time_in_queue,
+                               util);
        } else {
-               log_info(";%s;%u;%u;%u;%u;%u;%u;%u;%3.2f%%",
-                                       dus->name, dus->ios[0], dus->ios[1],
-                                       dus->merges[0], dus->merges[1],
-                                       dus->ticks[0], dus->ticks[1],
-                                       dus->time_in_queue, util);
+               log_buf(out, ";%s;%llu;%llu;%llu;%llu;%llu;%llu;%llu;%3.2f%%",
+                               dus->name,
+                               (unsigned long long) dus->s.ios[0],
+                               (unsigned long long) dus->s.ios[1],
+                               (unsigned long long) dus->s.merges[0],
+                               (unsigned long long) dus->s.merges[1],
+                               (unsigned long long) dus->s.ticks[0],
+                               (unsigned long long) dus->s.ticks[1],
+                               (unsigned long long) dus->s.time_in_queue,
+                               util);
        }
 
        /*
         * If the device has slaves, aggregate the stats for
         * those slave devices also.
         */
-       show_agg_stats(agg, terse);
+       show_agg_stats(agg, terse, out);
 
        if (!terse)
-               log_info("\n");
+               log_buf(out, "\n");
 }
 
 void json_array_add_disk_util(struct disk_util_stat *dus,
@@ -611,8 +628,8 @@ void json_array_add_disk_util(struct disk_util_stat *dus,
        struct json_object *obj;
        double util = 0;
 
-       if (dus->msec)
-               util = (double) 100 * dus->io_ticks / (double) dus->msec;
+       if (dus->s.msec)
+               util = (double) 100 * dus->s.io_ticks / (double) dus->s.msec;
        if (util > 100.0)
                util = 100.0;
 
@@ -620,13 +637,13 @@ void json_array_add_disk_util(struct disk_util_stat *dus,
        json_array_add_value_object(array, obj);
 
        json_object_add_value_string(obj, "name", dus->name);
-       json_object_add_value_int(obj, "read_ios", dus->ios[0]);
-       json_object_add_value_int(obj, "write_ios", dus->ios[1]);
-       json_object_add_value_int(obj, "read_merges", dus->merges[0]);
-       json_object_add_value_int(obj, "write_merges", dus->merges[1]);
-       json_object_add_value_int(obj, "read_ticks", dus->ticks[0]);
-       json_object_add_value_int(obj, "write_ticks", dus->ticks[1]);
-       json_object_add_value_int(obj, "in_queue", dus->time_in_queue);
+       json_object_add_value_int(obj, "read_ios", dus->s.ios[0]);
+       json_object_add_value_int(obj, "write_ios", dus->s.ios[1]);
+       json_object_add_value_int(obj, "read_merges", dus->s.merges[0]);
+       json_object_add_value_int(obj, "write_merges", dus->s.merges[1]);
+       json_object_add_value_int(obj, "read_ticks", dus->s.ticks[0]);
+       json_object_add_value_int(obj, "write_ticks", dus->s.ticks[1]);
+       json_object_add_value_int(obj, "in_queue", dus->s.time_in_queue);
        json_object_add_value_float(obj, "util", util);
 
        /*
@@ -652,8 +669,8 @@ void json_array_add_disk_util(struct disk_util_stat *dus,
        json_object_add_value_float(obj, "aggr_util", agg->max_util.u.f);
 }
 
-void json_object_add_disk_utils(struct json_object *obj,
-               struct flist_head *head)
+static void json_object_add_disk_utils(struct json_object *obj,
+                                      struct flist_head *head)
 {
        struct json_array *array = json_create_array();
        struct flist_head *entry;
@@ -669,10 +686,15 @@ void json_object_add_disk_utils(struct json_object *obj,
        }
 }
 
-void show_disk_util(int terse, struct json_object *parent)
+void show_disk_util(int terse, struct json_object *parent,
+                   struct buf_output *out)
 {
        struct flist_head *entry;
        struct disk_util *du;
+       bool do_json;
+
+       if (!disk_util_mutex)
+               return;
 
        fio_mutex_down(disk_util_mutex);
 
@@ -681,18 +703,24 @@ void show_disk_util(int terse, struct json_object *parent)
                return;
        }
 
-       if (!terse && !parent)
-               log_info("\nDisk stats (read/write):\n");
+       if ((output_format & FIO_OUTPUT_JSON) && parent)
+               do_json = true;
+       else
+               do_json = false;
+
+       if (!terse && !do_json)
+               log_buf(out, "\nDisk stats (read/write):\n");
 
-       if (output_format == FIO_OUTPUT_JSON) {
+       if (do_json)
                json_object_add_disk_utils(parent, &disk_list);
-       } else
+       else if (output_format & ~(FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS)) {
                flist_for_each(entry, &disk_list) {
                        du = flist_entry(entry, struct disk_util, list);
 
                        aggregate_slaves_stats(du);
-                       print_disk_util(&du->dus, &du->agg, terse);
+                       print_disk_util(&du->dus, &du->agg, terse, out);
                }
+       }
 
        fio_mutex_up(disk_util_mutex);
 }