configure: add gettid() test
[fio.git] / diskutil.c
index c25c5c9dccc965bb45e8eaf6cbbcd91afca4e99e..7be4c022431e1a0b823332f233d0159f07876ba4 100644 (file)
@@ -1,23 +1,25 @@
 #include <stdio.h>
 #include <string.h>
-#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>
+#ifdef CONFIG_VALGRIND_DEV
+#include <valgrind/drd.h>
+#else
+#define DRD_IGNORE_VAR(x) do { } while (0)
+#endif
 
 #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 fio_sem *disk_util_sem;
 
 static struct disk_util *__init_per_file_disk_util(struct thread_data *td,
                int majdev, int mindev, char *path);
@@ -35,7 +37,8 @@ static void disk_util_free(struct disk_util *du)
                slave->users--;
        }
 
-       fio_mutex_remove(du->lock);
+       fio_sem_remove(du->lock);
+       free(du->sysfs_root);
        sfree(du);
 }
 
@@ -84,7 +87,7 @@ static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
 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;
@@ -119,9 +122,9 @@ int update_io_ticks(void)
 
        dprint(FD_DISKUTIL, "update io ticks\n");
 
-       fio_mutex_down(disk_util_mutex);
+       fio_sem_down(disk_util_sem);
 
-       if (!helper_exit) {
+       if (!helper_should_exit()) {
                flist_for_each(entry, &disk_list) {
                        du = flist_entry(entry, struct disk_util, list);
                        update_io_tick_disk(du);
@@ -129,7 +132,7 @@ int update_io_ticks(void)
        } else
                ret = 1;
 
-       fio_mutex_up(disk_util_mutex);
+       fio_sem_up(disk_util_sem);
        return ret;
 }
 
@@ -138,18 +141,18 @@ static struct disk_util *disk_util_exists(int major, int minor)
        struct flist_head *entry;
        struct disk_util *du;
 
-       fio_mutex_down(disk_util_mutex);
+       fio_sem_down(disk_util_sem);
 
        flist_for_each(entry, &disk_list) {
                du = flist_entry(entry, struct disk_util, list);
 
                if (major == du->major && minor == du->minor) {
-                       fio_mutex_up(disk_util_mutex);
+                       fio_sem_up(disk_util_sem);
                        return du;
                }
        }
 
-       fio_mutex_up(disk_util_mutex);
+       fio_sem_up(disk_util_sem);
        return NULL;
 }
 
@@ -178,6 +181,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)) {
@@ -238,22 +242,28 @@ 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);
+               nowarn_snprintf(temppath, sizeof(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 - 1);
-               if (linklen  < 0) {
+               if (linklen < 0) {
                        perror("readlink() for slave device.");
                        closedir(dirhandle);
                        return;
                }
                slavepath[linklen] = '\0';
 
-               sprintf(temppath, "%s/%s/dev", slavesdir, slavepath);
+               nowarn_snprintf(temppath, sizeof(temppath), "%s/%s/dev",
+                               slavesdir, slavepath);
+               if (access(temppath, F_OK) != 0)
+                       nowarn_snprintf(temppath, sizeof(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;
                }
@@ -265,7 +275,8 @@ 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);
+               nowarn_snprintf(temppath, sizeof(temppath), "%s/%s", slavesdir,
+                               slavepath);
                __init_per_file_disk_util(td, majdev, mindev, temppath);
                slavedu = disk_util_exists(majdev, mindev);
 
@@ -290,11 +301,10 @@ 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;
-       }
 
+       DRD_IGNORE_VAR(du->users);
        memset(du, 0, sizeof(*du));
        INIT_FLIST_HEAD(&du->list);
        l = snprintf(du->path, sizeof(du->path), "%s/stat", path);
@@ -305,15 +315,15 @@ static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
                return NULL;
        }
        strncpy((char *) du->dus.name, basename(path), FIO_DU_NAME_SZ - 1);
-       du->sysfs_root = path;
+       du->sysfs_root = strdup(path);
        du->major = majdev;
        du->minor = mindev;
        INIT_FLIST_HEAD(&du->slavelist);
        INIT_FLIST_HEAD(&du->slaves);
-       du->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
+       du->lock = fio_sem_init(FIO_SEM_UNLOCKED);
        du->users = 0;
 
-       fio_mutex_down(disk_util_mutex);
+       fio_sem_down(disk_util_sem);
 
        flist_for_each(entry, &disk_list) {
                __du = flist_entry(entry, struct disk_util, list);
@@ -322,7 +332,7 @@ static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
 
                if (!strcmp((char *) du->dus.name, (char *) __du->dus.name)) {
                        disk_util_free(du);
-                       fio_mutex_up(disk_util_mutex);
+                       fio_sem_up(disk_util_sem);
                        return __du;
                }
        }
@@ -333,7 +343,7 @@ static struct disk_util *disk_util_add(struct thread_data *td, int majdev,
        get_io_ticks(du, &du->last_dus);
 
        flist_add_tail(&du->list, &disk_list);
-       fio_mutex_up(disk_util_mutex);
+       fio_sem_up(disk_util_sem);
 
        find_add_disk_slaves(td, path, du);
        return du;
@@ -364,12 +374,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)) {
@@ -425,13 +435,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);
 }
 
@@ -450,12 +458,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
@@ -488,7 +492,7 @@ 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)
@@ -563,7 +567,7 @@ static void aggregate_slaves_stats(struct disk_util *masterdu)
 
 void disk_util_prune_entries(void)
 {
-       fio_mutex_down(disk_util_mutex);
+       fio_sem_down(disk_util_sem);
 
        while (!flist_empty(&disk_list)) {
                struct disk_util *du;
@@ -574,8 +578,8 @@ void disk_util_prune_entries(void)
        }
 
        last_majdev = last_mindev = -1;
-       fio_mutex_up(disk_util_mutex);
-       fio_mutex_remove(disk_util_mutex);
+       fio_sem_up(disk_util_sem);
+       fio_sem_remove(disk_util_sem);
 }
 
 void print_disk_util(struct disk_util_stat *dus, struct disk_util_agg *agg,
@@ -697,13 +701,13 @@ void show_disk_util(int terse, struct json_object *parent,
        struct disk_util *du;
        bool do_json;
 
-       if (!disk_util_mutex)
+       if (!is_running_backend())
                return;
 
-       fio_mutex_down(disk_util_mutex);
+       fio_sem_down(disk_util_sem);
 
        if (flist_empty(&disk_list)) {
-               fio_mutex_up(disk_util_mutex);
+               fio_sem_up(disk_util_sem);
                return;
        }
 
@@ -726,10 +730,10 @@ void show_disk_util(int terse, struct json_object *parent,
                }
        }
 
-       fio_mutex_up(disk_util_mutex);
+       fio_sem_up(disk_util_sem);
 }
 
 void setup_disk_util(void)
 {
-       disk_util_mutex = fio_mutex_init(FIO_MUTEX_UNLOCKED);
+       disk_util_sem = fio_sem_init(FIO_SEM_UNLOCKED);
 }