Fix for a race when fio prints I/O statistics periodically
[fio.git] / t / btrace2fio.c
index e4e05ca4394f938f9b704bb57c51825746262c5a..32dda92e858be105a4fc641c961600901f8c499c 100644 (file)
@@ -450,7 +450,7 @@ static struct btrace_pid *pid_hash_get(pid_t pid)
  * Load a blktrace file by reading all the blk_io_trace entries, and storing
  * them as io_pieces like the fio text version would do.
  */
-static int load_blktrace(const char *filename, int need_swap)
+static int load_blktrace(const char *fname, int need_swap)
 {
        struct btrace_pid *p;
        unsigned long traces;
@@ -458,7 +458,7 @@ static int load_blktrace(const char *filename, int need_swap)
        struct fifo *fifo;
        int fd, ret = 0;
 
-       fd = open(filename, O_RDONLY);
+       fd = open(fname, O_RDONLY);
        if (fd < 0) {
                perror("open trace file\n");
                return 1;
@@ -468,8 +468,7 @@ static int load_blktrace(const char *filename, int need_swap)
 
        traces = 0;
        do {
-               int ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
-
+               ret = trace_fifo_get(fifo, fd, &t, sizeof(t));
                if (ret < 0)
                        goto err;
                else if (!ret)
@@ -589,7 +588,7 @@ static void __output_p_ascii(struct btrace_pid *p, unsigned long *ios)
                perc = ((float) o->merges[i] * 100.0) / (float) total;
                printf("\tmerges: %lu (perc=%3.2f%%)\n", o->merges[i], perc);
                perc = ((float) o->seq[i] * 100.0) / (float) o->ios[i];
-               printf("\tseq:    %lu (perc=%3.2f%%)\n", o->seq[i], perc);
+               printf("\tseq:    %lu (perc=%3.2f%%)\n", (unsigned long) o->seq[i], perc);
                printf("\trate:   %lu KB/sec\n", o_to_kb_rate(o, i));
 
                for (j = 0; j < o->nr_bs[i]; j++) {
@@ -870,9 +869,11 @@ static int trace_needs_swap(const char *trace_file, int *swap)
 
        ret = read(fd, &t, sizeof(t));
        if (ret < 0) {
+               close(fd);
                perror("read");
                return 1;
        } else if (ret != sizeof(t)) {
+               close(fd);
                log_err("fio: short read on trace file\n");
                return 1;
        }