iowatcher: Make seconds unsigned
[blktrace.git] / iowatcher / main.c
index bc6132ad0e65da23fcd0820dde348ebad04fa5c2..5cbfa53ddf4a2e218e830c1e5d6c3b6976b07f4d 100644 (file)
@@ -12,7 +12,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  *  Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
  */
 #include <time.h>
 #include <math.h>
 #include <getopt.h>
+#include <limits.h>
+#include <float.h>
 
 #include "plot.h"
 #include "blkparse.h"
 #include "list.h"
 #include "tracers.h"
 #include "mpstat.h"
+#include "fio.h"
 
 LIST_HEAD(all_traces);
+LIST_HEAD(fio_traces);
 
 static char line[1024];
 static int line_len = 1024;
 static int found_mpstat = 0;
-static int cpu_color_index = 0;
-static int color_index = 0;
 static int make_movie = 0;
+static int keep_movie_svgs = 0;
 static int opt_graph_width = 0;
 static int opt_graph_height = 0;
 
-char *colors[] = {
-       "blue", "darkgreen",
-       "red", "aqua",
-       "orange", "darkviolet",
-       "brown", "#00FF00",
-       "yellow", "coral",
-       "black", "darkred",
-       "fuchsia", "crimson",
-       NULL };
-
-char *pick_color(void) {
-       char *ret = colors[color_index];
-       if (!ret) {
-               color_index = 0;
-               ret = colors[color_index];
-       }
-       color_index++;
-       return ret;
-}
+static int columns = 1;
+static int num_xticks = 9;
+static int num_yticks = 4;
 
-char *pick_cpu_color(void) {
-       char *ret = colors[cpu_color_index];
-       if (!ret) {
-               color_index = 0;
-               ret = colors[cpu_color_index];
-       }
-       cpu_color_index++;
-       return ret;
-}
+static double min_time = 0;
+static double max_time = DBL_MAX;
+static unsigned long long min_mb = 0;
+static unsigned long long max_mb = ULLONG_MAX >> 20;
+
+int plot_io_action = 0;
+int io_per_process = 0;
+unsigned int longest_proc_name = 0;
+
+/*
+ * this doesn't include the IO graph,
+ * but it counts the other graphs as they go out
+ */
+static int total_graphs_written = 1;
 
 enum {
        IO_GRAPH_INDEX = 0,
        TPUT_GRAPH_INDEX,
+       FIO_GRAPH_INDEX,
        CPU_SYS_GRAPH_INDEX,
        CPU_IO_GRAPH_INDEX,
        CPU_IRQ_GRAPH_INDEX,
@@ -105,6 +98,7 @@ enum {
 static char *graphs_by_name[] = {
        "io",
        "tput",
+       "fio",
        "cpu-sys",
        "cpu-io",
        "cpu-irq",
@@ -115,36 +109,47 @@ static char *graphs_by_name[] = {
        "iops",
 };
 
+enum {
+       MOVIE_SPINDLE,
+       MOVIE_RECT,
+       NUM_MOVIE_STYLES,
+};
+
+char *movie_styles[] = {
+       "spindle",
+       "rect",
+       NULL
+};
+
+static int movie_style = 0;
+
+static int lookup_movie_style(char *str)
+{
+       int i;
+
+       for (i = 0; i < NUM_MOVIE_STYLES; i++) {
+               if (strcmp(str, movie_styles[i]) == 0)
+                       return i;
+       }
+       return -1;
+}
+
 static int active_graphs[TOTAL_GRAPHS];
 static int last_active_graph = IOPS_GRAPH_INDEX;
 
 static int label_index = 0;
 static int num_traces = 0;
+static int num_fio_traces = 0;
 static int longest_label = 0;
 
-struct trace_file {
-       struct list_head list;
-       char *filename;
-       char *label;
-       struct trace *trace;
-       int seconds;
-       int stop_seconds;
-       u64 max_offset;
-
-       char *read_color;
-       char *write_color;
-
-       struct graph_line_data *tput_gld;
-       struct graph_line_data *iop_gld;
-       struct graph_line_data *latency_gld;
-       struct graph_line_data *queue_depth_gld;
-       struct graph_dot_data *gdd_writes;
-       struct graph_dot_data *gdd_reads;
-
-       int mpstat_seconds;
-       int mpstat_stop_seconds;
-       struct graph_line_data **mpstat_gld;
-};
+static char *graph_title = "";
+static char *output_filename = "trace.svg";
+static char *blktrace_devices[MAX_DEVICES_PER_TRACE];
+static int num_blktrace_devices = 0;
+static char *blktrace_outfile = "trace";
+static char *blktrace_dest_dir = ".";
+static char *program_to_run = NULL;
+static char *ffmpeg_codec = "libx264";
 
 static void alloc_mpstat_gld(struct trace_file *tf)
 {
@@ -210,6 +215,27 @@ static int last_graph(void)
        }
        return -ENOENT;
 }
+
+static int graphs_left(int cur)
+{
+       int i;
+       int left = 0;
+       for (i = cur; i < TOTAL_GRAPHS; i++) {
+               if (active_graphs[i])
+                       left++;
+       }
+       return left;
+}
+
+static char * join_path(char *dest_dir, char *filename)
+{
+       /* alloc 2 extra bytes for '/' and '\0' */
+       char *path = malloc(strlen(dest_dir) + strlen(filename) + 2);
+       sprintf(path, "%s%s%s", dest_dir, "/", filename);
+
+       return path;
+}
+
 static void add_trace_file(char *filename)
 {
        struct trace_file *tf;
@@ -222,23 +248,48 @@ static void add_trace_file(char *filename)
        tf->label = "";
        tf->filename = strdup(filename);
        list_add_tail(&tf->list, &all_traces);
-       tf->read_color = pick_color();
-       tf->write_color = pick_color();
+       tf->line_color = "black";
        num_traces++;
 }
 
+static void add_fio_trace_file(char *filename)
+{
+       struct trace_file *tf;
+
+       tf = calloc(1, sizeof(*tf));
+       if (!tf) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
+       tf->label = "";
+       tf->filename = strdup(filename);
+       list_add_tail(&tf->list, &fio_traces);
+       tf->line_color = pick_fio_color();
+       tf->fio_trace = 1;
+       num_fio_traces++;
+}
+
 static void setup_trace_file_graphs(void)
 {
        struct trace_file *tf;
        int i;
+       int alloc_ptrs;
+
+       if (io_per_process)
+               alloc_ptrs = 16;
+       else
+               alloc_ptrs = 1;
 
        list_for_each_entry(tf, &all_traces, list) {
-               tf->tput_gld = alloc_line_data(tf->seconds, tf->stop_seconds);
-               tf->latency_gld = alloc_line_data(tf->seconds, tf->stop_seconds);
-               tf->queue_depth_gld = alloc_line_data(tf->seconds, tf->stop_seconds);
-               tf->iop_gld = alloc_line_data(tf->seconds, tf->stop_seconds);
-               tf->gdd_writes = alloc_dot_data(tf->seconds, tf->max_offset, tf->stop_seconds);
-               tf->gdd_reads = alloc_dot_data(tf->seconds, tf->max_offset, tf->stop_seconds);
+               tf->tput_reads_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
+               tf->tput_writes_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
+               tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
+               tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
+
+               tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
+               tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
+               tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
+               tf->io_plots_allocated = alloc_ptrs;
 
                if (tf->trace->mpstat_num_cpus == 0)
                        continue;
@@ -246,11 +297,21 @@ static void setup_trace_file_graphs(void)
                alloc_mpstat_gld(tf);
                for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
                        tf->mpstat_gld[i] =
-                               alloc_line_data(tf->mpstat_seconds,
-                                               tf->mpstat_seconds);
+                               alloc_line_data(tf->mpstat_min_seconds,
+                                               tf->mpstat_max_seconds,
+                                               tf->mpstat_max_seconds);
                        tf->mpstat_gld[i]->max = 100;
                }
        }
+
+       list_for_each_entry(tf, &fio_traces, list) {
+               if (tf->trace->fio_seconds > 0) {
+                       tf->fio_gld = alloc_line_data(tf->min_seconds,
+                                                     tf->max_seconds,
+                                                     tf->stop_seconds);
+               }
+       }
+
 }
 
 static void read_traces(void)
@@ -260,26 +321,89 @@ static void read_traces(void)
        u64 last_time;
        u64 ymin;
        u64 ymax;
+       u64 max_bank;
+       u64 max_bank_offset;
+       char *path = NULL;
 
        list_for_each_entry(tf, &all_traces, list) {
-               trace = open_trace(tf->filename);
+               path = join_path(blktrace_dest_dir, tf->filename);
+
+               trace = open_trace(path);
                if (!trace)
                        exit(1);
 
                last_time = find_last_time(trace);
                tf->trace = trace;
-               tf->seconds = SECONDS(last_time);
-               tf->stop_seconds = SECONDS(last_time);
-               tf->max_offset = find_highest_offset(trace);
+               tf->max_seconds = SECONDS(last_time) + 1;
+               tf->stop_seconds = SECONDS(last_time) + 1;
 
-               filter_outliers(trace, tf->max_offset, &ymin, &ymax);
+               find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
+                                   &max_bank, &max_bank_offset);
+               filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
+               tf->min_offset = ymin;
                tf->max_offset = ymax;
 
-               read_mpstat(trace, tf->filename);
+               read_mpstat(trace, path);
                tf->mpstat_stop_seconds = trace->mpstat_seconds;
-               tf->mpstat_seconds = trace->mpstat_seconds;
-               if (tf->mpstat_seconds)
+               tf->mpstat_max_seconds = trace->mpstat_seconds;
+               if (tf->mpstat_max_seconds)
                        found_mpstat = 1;
+
+               free(path);
+       }
+
+       list_for_each_entry(tf, &fio_traces, list) {
+               trace = open_fio_trace(tf->filename);
+               if (!trace)
+                       exit(1);
+               tf->trace = trace;
+               tf->max_seconds = tf->trace->fio_seconds;
+               tf->stop_seconds = tf->trace->fio_seconds;
+       }
+}
+
+static void pick_line_graph_color(void)
+{
+       struct trace_file *tf;
+       int i;
+
+       list_for_each_entry(tf, &all_traces, list) {
+               for (i = 0; i < tf->io_plots; i++) {
+                       if (tf->gdd_reads[i]) {
+                               tf->line_color = tf->gdd_reads[i]->color;
+                               tf->reads_color = tf->gdd_reads[i]->color;
+                       }
+                       if (tf->gdd_writes[i]) {
+                               tf->line_color = tf->gdd_writes[i]->color;
+                               tf->writes_color = tf->gdd_writes[i]->color;
+                       }
+                       if (tf->writes_color && tf->reads_color)
+                               break;
+               }
+               if (!tf->reads_color)
+                       tf->reads_color = tf->line_color;
+               if (!tf->writes_color)
+                       tf->writes_color = tf->line_color;
+       }
+}
+
+static void read_fio_events(struct trace_file *tf)
+{
+       u64 bw = 0;
+       int time = 0;
+       int dir = 0;
+       int ret;
+
+       first_fio(tf->trace);
+       while (1) {
+               ret = read_fio_event(tf->trace, &time, &bw, &dir);
+               if (ret)
+                       break;
+
+               if (dir <= 1)
+                       add_fio_gld(time, bw, tf->fio_gld);
+               if (next_fio_line(tf->trace))
+                       break;
        }
 }
 
@@ -290,25 +414,28 @@ static void read_trace_events(void)
        struct trace *trace;
        int ret;
        int i;
-       int time;
+       unsigned int time;
        double user, sys, iowait, irq, soft;
        double max_user = 0, max_sys = 0, max_iowait = 0,
               max_irq = 0, max_soft = 0;
 
+       list_for_each_entry(tf, &fio_traces, list)
+               read_fio_events(tf);
+
        list_for_each_entry(tf, &all_traces, list) {
                trace = tf->trace;
+
                first_record(trace);
-               while (1) {
+               do {
+                       if (SECONDS(get_record_time(trace)) > tf->max_seconds)
+                               continue;
                        check_record(trace);
-                       add_tput(trace, tf->tput_gld);
+                       add_tput(trace, tf->tput_writes_gld, tf->tput_reads_gld);
                        add_iop(trace, tf->iop_gld);
-                       add_io(trace, tf->gdd_writes, tf->gdd_reads);
+                       add_io(trace, tf);
                        add_pending_io(trace, tf->queue_depth_gld);
                        add_completed_io(trace, tf->latency_gld);
-                       ret = next_record(trace);
-                       if (ret)
-                               break;
-               }
+               } while (!(ret = next_record(trace)));
        }
        list_for_each_entry(tf, &all_traces, list) {
                trace = tf->trace;
@@ -318,7 +445,7 @@ static void read_trace_events(void)
 
                first_mpstat(trace);
 
-               for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
+               for (time = 0; time < tf->mpstat_stop_seconds; time++) {
                        for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
                                ret = read_mpstat_event(trace, &user, &sys,
                                                        &iowait, &irq, &soft);
@@ -373,6 +500,15 @@ static void set_trace_label(char *label)
                longest_label = len;
 
        list_for_each_entry(tf, &all_traces, list) {
+               if (cur == label_index) {
+                       tf->label = strdup(label);
+                       label_index++;
+                       return;
+                       break;
+               }
+               cur++;
+       }
+       list_for_each_entry(tf, &fio_traces, list) {
                if (cur == label_index) {
                        tf->label = strdup(label);
                        label_index++;
@@ -382,13 +518,6 @@ static void set_trace_label(char *label)
        }
 }
 
-static char *graph_title = "";
-static char *output_filename = "trace.svg";
-static char *blktrace_device = NULL;
-static char *blktrace_outfile = "trace";
-static char *blktrace_dest_dir = ".";
-static char *program_to_run = NULL;
-
 static void set_blktrace_outfile(char *arg)
 {
        char *s = strdup(arg);
@@ -402,28 +531,48 @@ static void set_blktrace_outfile(char *arg)
 }
 
 
-static void compare_max_tf(struct trace_file *tf, int *seconds, u64 *max_offset)
+static void compare_minmax_tf(struct trace_file *tf, unsigned int *max_seconds,
+                             u64 *min_offset, u64 *max_offset)
 {
-       if (tf->seconds > *seconds)
-               *seconds = tf->seconds;
+       if (tf->max_seconds > *max_seconds)
+               *max_seconds = tf->max_seconds;
        if (tf->max_offset > *max_offset)
                *max_offset = tf->max_offset;
+       if (tf->min_offset < *min_offset)
+               *min_offset = tf->min_offset;
 }
 
-static void set_all_max_tf(int seconds, u64 max_offset)
+static void set_all_minmax_tf(unsigned int min_seconds,
+                             unsigned int max_seconds,
+                             u64 min_offset, u64 max_offset)
 {
        struct trace_file *tf;
-
-       list_for_each_entry(tf, &all_traces, list) {
-               tf->seconds = seconds;
+       struct list_head *traces = &all_traces;
+again:
+       list_for_each_entry(tf, traces, list) {
+               tf->min_seconds = min_seconds;
+               tf->max_seconds = max_seconds;
+               if (tf->stop_seconds > max_seconds)
+                       tf->stop_seconds = max_seconds;
+               if (tf->mpstat_max_seconds) {
+                       tf->mpstat_min_seconds = min_seconds;
+                       tf->mpstat_max_seconds = max_seconds;
+                       if (tf->mpstat_stop_seconds > max_seconds)
+                               tf->mpstat_stop_seconds = max_seconds;
+               }
+               tf->min_offset = min_offset;
                tf->max_offset = max_offset;
        }
+       if (traces == &all_traces) {
+               traces = &fio_traces;
+               goto again;
+       }
 }
 
 static char *create_movie_temp_dir(void)
 {
        char *ret;
-       char *pattern = strdup("btrfs-movie-XXXXXX");;
+       char *pattern = strdup("io-movie-XXXXXX");;
 
        ret = mkdtemp(pattern);
        if (!ret) {
@@ -433,28 +582,73 @@ static char *create_movie_temp_dir(void)
        return ret;
 }
 
-static struct plot_history *alloc_plot_history(char *color)
+static struct pid_plot_history *alloc_pid_plot_history(char *color)
+{
+       struct pid_plot_history *pph;
+
+       pph = calloc(1, sizeof(struct pid_plot_history));
+       if (!pph) {
+               perror("memory allocation failed");
+               exit(1);
+       }
+       pph->history = malloc(sizeof(double) * 4096);
+       if (!pph->history) {
+               perror("memory allocation failed");
+               exit(1);
+       }
+       pph->history_len = 4096;
+       pph->color = color;
+
+       return pph;
+}
+
+static struct plot_history *alloc_plot_history(struct trace_file *tf)
 {
        struct plot_history *ph = calloc(1, sizeof(struct plot_history));
+       int i;
 
        if (!ph) {
                perror("memory allocation failed");
                exit(1);
        }
-       ph->history = calloc(4096, sizeof(double));
-       if (!ph->history) {
+       ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
+       if (!ph->read_pid_history) {
                perror("memory allocation failed");
                exit(1);
        }
-       ph->history_len = 4096;
-       ph->color = color;
+       ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
+       if (!ph->write_pid_history) {
+               perror("memory allocation failed");
+               exit(1);
+       }
+       ph->pid_history_count = tf->io_plots;
+       for (i = 0; i < tf->io_plots; i++) {
+               if (tf->gdd_reads[i])
+                       ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
+               if (tf->gdd_writes[i])
+                       ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
+       }
        return ph;
 }
 
-LIST_HEAD(movie_history_writes);
-LIST_HEAD(movie_history_reads);
+LIST_HEAD(movie_history);
 int num_histories = 0;
 
+static void free_plot_history(struct plot_history *ph)
+{
+       int pid;
+
+       for (pid = 0; pid < ph->pid_history_count; pid++) {
+               if (ph->read_pid_history[pid])
+                       free(ph->read_pid_history[pid]);
+               if (ph->write_pid_history[pid])
+                       free(ph->write_pid_history[pid]);
+       }
+       free(ph->read_pid_history);
+       free(ph->write_pid_history);
+       free(ph);
+}
+
 static void add_history(struct plot_history *ph, struct list_head *list)
 {
        struct plot_history *entry;
@@ -466,81 +660,127 @@ static void add_history(struct plot_history *ph, struct list_head *list)
                num_histories--;
                entry = list_entry(list->next, struct plot_history, list);
                list_del(&entry->list);
-               free(entry->history);
-               free(entry);
+               free_plot_history(entry);
        }
 }
 
 static void plot_movie_history(struct plot *plot, struct list_head *list)
 {
-       float alpha = 0.1;
        struct plot_history *ph;
+       int pid;
+
+       if (num_histories > 2)
+               rewind_spindle_steps(num_histories - 1);
 
        list_for_each_entry(ph, list, list) {
-               if (ph->list.next == list)
-                       alpha = 1;
-               svg_io_graph_movie_array(plot, ph, 1);
-               alpha += 0.2;
-               if (alpha > 1)
-                       alpha = 0.8;
+               for (pid = 0; pid < ph->pid_history_count; pid++) {
+                       if (ph->read_pid_history[pid]) {
+                               if (movie_style == MOVIE_SPINDLE) {
+                                       svg_io_graph_movie_array_spindle(plot,
+                                               ph->read_pid_history[pid]);
+                               } else {
+                                       svg_io_graph_movie_array(plot,
+                                               ph->read_pid_history[pid]);
+                               }
+                       }
+                       if (ph->write_pid_history[pid]) {
+                               if (movie_style == MOVIE_SPINDLE) {
+                                       svg_io_graph_movie_array_spindle(plot,
+                                               ph->write_pid_history[pid]);
+                               } else {
+                                       svg_io_graph_movie_array(plot,
+                                               ph->write_pid_history[pid]);
+                               }
+                       }
+               }
         }
 }
 
 static void free_all_plot_history(struct list_head *head)
 {
        struct plot_history *entry;
+
        while (!list_empty(head)) {
                entry = list_entry(head->next, struct plot_history, list);
                list_del(&entry->list);
-               free(entry->history);
-               free(entry);
+               free_plot_history(entry);
+       }
+}
+
+static int count_io_plot_types(void)
+{
+       struct trace_file *tf;
+       int i;
+       int total_io_types = 0;
+
+       list_for_each_entry(tf, &all_traces, list) {
+               for (i = 0; i < tf->io_plots; i++) {
+                       if (tf->gdd_reads[i])
+                               total_io_types++;
+                       if (tf->gdd_writes[i])
+                               total_io_types++;
+               }
        }
+       return total_io_types;
 }
 
-static void __plot_io(struct plot *plot, int seconds, u64 max_offset)
+static void plot_io(struct plot *plot, unsigned int min_seconds,
+                   unsigned int max_seconds, u64 min_offset, u64 max_offset)
 {
        struct trace_file *tf;
+       int i;
 
        if (active_graphs[IO_GRAPH_INDEX] == 0)
                return;
 
        setup_axis(plot);
 
-       svg_alloc_legend(plot, num_traces * 2);
+       svg_alloc_legend(plot, count_io_plot_types() * 2);
 
        set_plot_label(plot, "Device IO");
        set_ylabel(plot, "Offset (MB)");
-       set_yticks(plot, 4, 0, max_offset / (1024 * 1024), "");
-       set_xticks(plot, 9, 0, seconds);
+       set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
+                  max_offset / (1024 * 1024), "");
+       set_xticks(plot, num_xticks, min_seconds, max_seconds);
 
        list_for_each_entry(tf, &all_traces, list) {
-               char *label = tf->label;
-
-               if (!label)
-                       label = "";
-               svg_io_graph(plot, tf->gdd_reads, tf->read_color);
-               if (tf->gdd_reads->total_ios)
-                       svg_add_legend(plot, label, " Reads", tf->read_color);
+               char label[256];
+               char *pos;
+
+               if (!tf->label)
+                       label[0] = 0;
+               else {
+                       strncpy(label, tf->label, 255);
+                       label[255] = 0;
+                       if (io_per_process)
+                               strcat(label, " ");
+               }
+               pos = label + strlen(label);
+
+               for (i = 0; i < tf->io_plots; i++) {
+                       if (tf->gdd_writes[i]) {
+                               svg_io_graph(plot, tf->gdd_writes[i]);
+                               if (io_per_process)
+                                       strcpy(pos, tf->gdd_writes[i]->label);
+                               svg_add_legend(plot, label, " Writes", tf->gdd_writes[i]->color);
+                       }
+                       if (tf->gdd_reads[i]) {
+                               svg_io_graph(plot, tf->gdd_reads[i]);
+                               if (io_per_process)
+                                       strcpy(pos, tf->gdd_reads[i]->label);
+                               svg_add_legend(plot, label, " Reads", tf->gdd_reads[i]->color);
+                       }
 
-               svg_io_graph(plot, tf->gdd_writes, tf->write_color);
-               if (tf->gdd_writes->total_ios) {
-                       svg_add_legend(plot, label, " Writes", tf->write_color);
                }
        }
        if (plot->add_xlabel)
                set_xlabel(plot, "Time (seconds)");
        svg_write_legend(plot);
-}
-
-static void plot_io(struct plot *plot, int seconds, u64 max_offset)
-{
-       plot->add_xlabel = last_active_graph == IO_GRAPH_INDEX;
-
-       __plot_io(plot, seconds, max_offset);
        close_plot(plot);
 }
 
-static void __plot_tput(struct plot *plot, int seconds)
+static void plot_tput(struct plot *plot, unsigned int min_seconds,
+                     unsigned int max_seconds, int with_legend)
 {
        struct trace_file *tf;
        char *units;
@@ -550,14 +790,21 @@ static void __plot_tput(struct plot *plot, int seconds)
        if (active_graphs[TPUT_GRAPH_INDEX] == 0)
                return;
 
-       if (num_traces > 1)
-               svg_alloc_legend(plot, num_traces);
+       if (with_legend)
+               svg_alloc_legend(plot, num_traces * 2);
+
        list_for_each_entry(tf, &all_traces, list) {
-               if (tf->tput_gld->max > max)
-                       max = tf->tput_gld->max;
+               if (tf->tput_writes_gld->max > max)
+                       max = tf->tput_writes_gld->max;
+               if (tf->tput_reads_gld->max > max)
+                       max = tf->tput_reads_gld->max;
+       }
+       list_for_each_entry(tf, &all_traces, list) {
+               if (tf->tput_writes_gld->max > 0)
+                       tf->tput_writes_gld->max = max;
+               if (tf->tput_reads_gld->max > 0)
+                       tf->tput_reads_gld->max = max;
        }
-       list_for_each_entry(tf, &all_traces, list)
-               tf->tput_gld->max = max;
 
        setup_axis(plot);
        set_plot_label(plot, "Throughput");
@@ -567,143 +814,91 @@ static void __plot_tput(struct plot *plot, int seconds)
        scale_line_graph_bytes(&max, &units, 1024);
        sprintf(line, "%sB/s", units);
        set_ylabel(plot, line);
-       set_yticks(plot, 4, 0, max, "");
-       set_xticks(plot, 9, 0, seconds);
+       set_yticks(plot, num_yticks, 0, max, "");
+       set_xticks(plot, num_xticks, min_seconds, max_seconds);
 
        list_for_each_entry(tf, &all_traces, list) {
-               svg_line_graph(plot, tf->tput_gld, tf->read_color);
-               if (num_traces > 1)
-                       svg_add_legend(plot, tf->label, "", tf->read_color);
+               if (tf->tput_writes_gld->max > 0) {
+                       svg_line_graph(plot, tf->tput_writes_gld, tf->writes_color, 0, 0);
+                       if (with_legend)
+                               svg_add_legend(plot, tf->label, "Writes", tf->writes_color);
+               }
+               if (tf->tput_reads_gld->max > 0) {
+                       svg_line_graph(plot, tf->tput_reads_gld, tf->reads_color, 0, 0);
+                       if (with_legend)
+                               svg_add_legend(plot, tf->label, "Reads", tf->reads_color);
+               }
        }
 
        if (plot->add_xlabel)
                set_xlabel(plot, "Time (seconds)");
-       if (num_traces > 1)
+
+       if (with_legend)
                svg_write_legend(plot);
-}
 
-static void plot_tput(struct plot *plot, int seconds)
-{
-       plot->add_xlabel = last_active_graph == TPUT_GRAPH_INDEX;
-       __plot_tput(plot, seconds);
        close_plot(plot);
+       total_graphs_written++;
 }
 
-static void convert_movie_files(char *movie_dir)
-{
-       fprintf(stderr, "Converting svg files in %s\n", movie_dir);
-       snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
-                movie_dir);
-       system(line);
-}
-
-static void mencode_movie(char *movie_dir)
-{
-       fprintf(stderr, "Creating movie %s\n", movie_dir);
-       snprintf(line, line_len, "ffmpeg -r 25 -y -i %s/%%10d-%s.svg.png -b:v 250k "
-                "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
-       system(line);
-}
-
-static void cleanup_movie(char *movie_dir)
-{
-       fprintf(stderr, "Removing movie dir %s\n", movie_dir);
-       snprintf(line, line_len, "rm %s/*", movie_dir);
-       system(line);
-
-       snprintf(line, line_len, "rmdir %s", movie_dir);
-       system(line);
-}
-
-static void plot_io_movie(struct plot *plot)
+static void plot_fio_tput(struct plot *plot,
+                         unsigned int min_seconds, unsigned int max_seconds)
 {
        struct trace_file *tf;
-       char *movie_dir = create_movie_temp_dir();
-       int i;
-       struct plot_history *read_history;
-       struct plot_history *write_history;
-       int batch_i;
-       int movie_len = 30;
-       int movie_frames_per_sec = 16;
-       int total_frames = movie_len * movie_frames_per_sec;
-       int rows, cols;
-       int batch_count;
-
-       get_graph_size(&cols, &rows);
-       batch_count = cols / total_frames;
-
-       if (batch_count == 0)
-               batch_count = 1;
-
-       list_for_each_entry(tf, &all_traces, list) {
-               char *label = tf->label;
-               if (!label)
-                       label = "";
-
-               i = 0;
-               while (i < cols) {
-                       snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
-                       set_plot_output(plot, line);
-
-                       set_plot_title(plot, graph_title);
-                       setup_axis(plot);
-                       svg_alloc_legend(plot, num_traces * 2);
-
-                       read_history = alloc_plot_history(tf->read_color);
-                       write_history = alloc_plot_history(tf->write_color);
-                       read_history->col = i;
-                       write_history->col = i;
-
-                       if (tf->gdd_reads->total_ios)
-                               svg_add_legend(plot, label, " Reads", tf->read_color);
-                       if (tf->gdd_writes->total_ios)
-                               svg_add_legend(plot, label, " Writes", tf->write_color);
+       char *units;
+       char line[128];
+       u64 max = 0;
 
-                       batch_i = 0;
-                       while (i < cols && batch_i < batch_count) {
-                               /* print just this column */
-                               svg_io_graph_movie(tf->gdd_reads, read_history, i);
+       if (num_fio_traces == 0 || active_graphs[FIO_GRAPH_INDEX] == 0)
+               return;
 
-                               svg_io_graph_movie(tf->gdd_writes, write_history, i);
-                               i++;
-                               batch_i++;
-                       }
+       if (num_fio_traces > 1)
+               svg_alloc_legend(plot, num_fio_traces);
 
-                       add_history(read_history, &movie_history_reads);
-                       add_history(write_history, &movie_history_writes);
+       list_for_each_entry(tf, &fio_traces, list) {
+               if (tf->fio_gld->max > max)
+                       max = tf->fio_gld->max;
+       }
 
-                       plot_movie_history(plot, &movie_history_reads);
-                       plot_movie_history(plot, &movie_history_writes);
+       list_for_each_entry(tf, &fio_traces, list) {
+               if (tf->fio_gld->max > 0)
+                       tf->fio_gld->max = max;
+       }
 
-                       svg_write_legend(plot);
-                       close_plot(plot);
+       setup_axis(plot);
+       set_plot_label(plot, "Fio Throughput");
 
-                       set_graph_size(cols, rows / 3);
-                       plot->add_xlabel = 1;
-                       __plot_tput(plot, tf->gdd_reads->seconds);
-                       svg_write_time_line(plot, i);
-                       close_plot(plot);
-                       set_graph_size(cols, rows);
+       tf = list_entry(all_traces.next, struct trace_file, list);
 
-                       close_plot(plot);
+       scale_line_graph_bytes(&max, &units, 1024);
+       sprintf(line, "%sB/s", units);
+       set_ylabel(plot, line);
+       set_yticks(plot, num_yticks, 0, max, "");
+
+       set_xticks(plot, num_xticks, min_seconds, max_seconds);
+       list_for_each_entry(tf, &fio_traces, list) {
+               if (tf->fio_gld->max > 0) {
+                       svg_line_graph(plot, tf->fio_gld, tf->line_color, 0, 0);
+                       if (num_fio_traces > 1)
+                               svg_add_legend(plot, tf->label, "", tf->line_color);
                }
-               free_all_plot_history(&movie_history_reads);
-               free_all_plot_history(&movie_history_writes);
        }
-       convert_movie_files(movie_dir);
-       mencode_movie(movie_dir);
-       cleanup_movie(movie_dir);
-       free(movie_dir);
+
+       if (plot->add_xlabel)
+               set_xlabel(plot, "Time (seconds)");
+
+       if (num_fio_traces > 1)
+               svg_write_legend(plot);
+       close_plot(plot);
+       total_graphs_written++;
 }
 
-static void plot_cpu(struct plot *plot, int seconds, char *label,
+static void plot_cpu(struct plot *plot, unsigned int max_seconds, char *label,
                     int active_index, int gld_index)
 {
        struct trace_file *tf;
-       char line[128];
        int max = 0;
        int i;
-       int gld_i;
+       unsigned int gld_i;
        char *color;
        double avg = 0;
        int ymax;
@@ -727,41 +922,59 @@ static void plot_cpu(struct plot *plot, int seconds, char *label,
 
        svg_alloc_legend(plot, num_traces * max);
 
-       plot->add_xlabel = last_active_graph == active_index;
        setup_axis(plot);
        set_plot_label(plot, label);
 
-       seconds = tf->mpstat_seconds;
+       max_seconds = tf->mpstat_max_seconds;
 
-       set_yticks(plot, 4, 0, tf->mpstat_gld[gld_index]->max, "");
+       set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
        set_ylabel(plot, "Percent");
-       set_xticks(plot, 9, 0, seconds);
+       set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
 
-       cpu_color_index = 0;
+       reset_cpu_color();
        list_for_each_entry(tf, &all_traces, list) {
-               for (i = 0; i < tf->mpstat_gld[0]->stop_seconds; i++) {
-                       avg += tf->mpstat_gld[gld_index]->data[i].sum;
+               if (tf->mpstat_gld == 0)
+                       break;
+               for (gld_i = tf->mpstat_gld[0]->min_seconds;
+                    gld_i < tf->mpstat_gld[0]->stop_seconds; gld_i++) {
+                       if (tf->mpstat_gld[gld_index]->data[gld_i].count) {
+                               avg += (tf->mpstat_gld[gld_index]->data[gld_i].sum /
+                                       tf->mpstat_gld[gld_index]->data[gld_i].count);
+                       }
                }
-               avg /= tf->mpstat_gld[gld_index]->stop_seconds;
+               avg /= tf->mpstat_gld[gld_index]->stop_seconds -
+                      tf->mpstat_gld[gld_index]->min_seconds;
                color = pick_cpu_color();
-               svg_line_graph(plot, tf->mpstat_gld[0], color);
+               svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
                svg_add_legend(plot, tf->label, " avg", color);
 
                for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
                        struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
                        double this_avg = 0;
 
-                       for (gld_i = 0; gld_i < gld->stop_seconds; gld_i++)
-                               this_avg += gld->data[i].sum;
+                       for (gld_i = gld->min_seconds;
+                            gld_i < gld->stop_seconds; gld_i++) {
+                               if (gld->data[i].count) {
+                                       this_avg += gld->data[i].sum /
+                                               gld->data[i].count;
+                               }
+                       }
+
+                       this_avg /= gld->stop_seconds - gld->min_seconds;
 
-                       this_avg /= gld->stop_seconds;
+                       for (gld_i = gld->min_seconds;
+                            gld_i < gld->stop_seconds; gld_i++) {
+                               double val;
 
-                       for (gld_i = 0; gld_i < gld->stop_seconds; gld_i++) {
-                               if (this_avg > avg + 30 ||
-                                   gld->data[gld_i].sum > 95) {
+                               if (gld->data[gld_i].count == 0)
+                                       continue;
+                               val = (double)gld->data[gld_i].sum /
+                                       gld->data[gld_i].count;
+
+                               if (this_avg > avg + 30 || val > 95) {
                                        color = pick_cpu_color();
-                                       svg_line_graph(plot, gld, color);
-                                       snprintf(line, 128, " CPU %d\n", i - 1);
+                                       svg_line_graph(plot, gld, color, avg + 30, 95);
+                                       snprintf(line, line_len, " CPU %d\n", i - 1);
                                        svg_add_legend(plot, tf->label, line, color);
                                        plotted++;
                                        break;
@@ -774,48 +987,36 @@ static void plot_cpu(struct plot *plot, int seconds, char *label,
        if (plot->add_xlabel)
                set_xlabel(plot, "Time (seconds)");
 
-       if (plot->legend_index <= 8)
+       if (!plot->no_legend) {
                svg_write_legend(plot);
-       else
                svg_free_legend(plot);
+       }
        close_plot(plot);
+       total_graphs_written++;
 }
 
-static void plot_latency(struct plot *plot, int seconds)
+static void plot_queue_depth(struct plot *plot, unsigned int min_seconds,
+                            unsigned int max_seconds)
 {
        struct trace_file *tf;
-       char *units;
-       char line[128];
-       u64 max = 0;
 
-       if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
+       if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
                return;
 
+       setup_axis(plot);
+       set_plot_label(plot, "Queue Depth");
        if (num_traces > 1)
                svg_alloc_legend(plot, num_traces);
-       list_for_each_entry(tf, &all_traces, list) {
-               if (tf->latency_gld->max > max)
-                       max = tf->latency_gld->max;
-       }
-       list_for_each_entry(tf, &all_traces, list)
-               tf->latency_gld->max = max;
-
-       plot->add_xlabel = last_active_graph == TPUT_GRAPH_INDEX;
-       setup_axis(plot);
-       set_plot_label(plot, "IO Latency");
 
        tf = list_entry(all_traces.next, struct trace_file, list);
-
-       scale_line_graph_time(&max, &units);
-       sprintf(line, "latency (%ss)", units);
-       set_ylabel(plot, line);
-       set_yticks(plot, 4, 0, max, "");
-       set_xticks(plot, 9, 0, seconds);
+       set_ylabel(plot, "Pending IO");
+       set_yticks(plot, num_yticks, 0, tf->queue_depth_gld->max, "");
+       set_xticks(plot, num_xticks, min_seconds, max_seconds);
 
        list_for_each_entry(tf, &all_traces, list) {
-               svg_line_graph(plot, tf->latency_gld, tf->read_color);
+               svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
                if (num_traces > 1)
-                       svg_add_legend(plot, tf->label, "", tf->read_color);
+                       svg_add_legend(plot, tf->label, "", tf->line_color);
        }
 
        if (plot->add_xlabel)
@@ -823,31 +1024,216 @@ static void plot_latency(struct plot *plot, int seconds)
        if (num_traces > 1)
                svg_write_legend(plot);
        close_plot(plot);
+       total_graphs_written++;
 }
 
-static void plot_queue_depth(struct plot *plot, int seconds)
+static void convert_movie_files(char *movie_dir)
 {
-       struct trace_file *tf;
+       fprintf(stderr, "Converting svg files in %s\n", movie_dir);
+       snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
+                movie_dir);
+       system(line);
+}
 
-       if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
+static void mencode_movie(char *movie_dir)
+{
+       fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
+       snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
+                "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
+                output_filename);
+       system(line);
+}
+
+static void tencode_movie(char *movie_dir)
+{
+       fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
+       snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
+                       output_filename, movie_dir, output_filename);
+       system(line);
+}
+
+static void encode_movie(char *movie_dir)
+{
+       char *last_dot = strrchr(output_filename, '.');
+
+       if (last_dot &&
+           (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
+               tencode_movie(movie_dir);
+       } else
+               mencode_movie(movie_dir);
+}
+
+static void cleanup_movie(char *movie_dir)
+{
+       if (keep_movie_svgs) {
+               fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
                return;
+       }
+       fprintf(stderr, "Removing movie dir %s\n", movie_dir);
+       snprintf(line, line_len, "rm %s/*", movie_dir);
+       system(line);
 
-       plot->add_xlabel = last_active_graph == QUEUE_DEPTH_GRAPH_INDEX;
+       snprintf(line, line_len, "rmdir %s", movie_dir);
+       system(line);
+}
+
+static void plot_io_movie(struct plot *plot)
+{
+       struct trace_file *tf;
+       char *movie_dir = create_movie_temp_dir();
+       int i, pid;
+       struct plot_history *history;
+       int batch_i;
+       int movie_len = 30;
+       int movie_frames_per_sec = 20;
+       int total_frames = movie_len * movie_frames_per_sec;
+       int rows, cols;
+       int batch_count;
+       int graph_width_factor = 5;
+       int orig_y_offset;
+
+       get_graph_size(&cols, &rows);
+       batch_count = cols / total_frames;
+
+       if (batch_count == 0)
+               batch_count = 1;
+
+       list_for_each_entry(tf, &all_traces, list) {
+               char label[256];
+               char *pos;
+
+               if (!tf->label)
+                       label[0] = 0;
+               else {
+                       strcpy(label, tf->label);
+                       if (io_per_process)
+                               strcat(label, " ");
+               }
+               pos = label + strlen(label);
+
+               i = 0;
+               while (i < cols) {
+                       snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
+                       set_plot_output(plot, line);
+                       set_plot_title(plot, graph_title);
+                       orig_y_offset = plot->start_y_offset;
+
+                       plot->no_legend = 1;
+
+                       set_graph_size(cols / graph_width_factor, rows / 8);
+                       plot->timeline = i / graph_width_factor;
+
+                       plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
+
+                       plot_cpu(plot, tf->max_seconds,
+                                  "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
+
+                       plot->direction = PLOT_ACROSS;
+                       plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
+
+                       /* movie graph starts here */
+                       plot->start_y_offset = orig_y_offset;
+                       set_graph_size(cols - cols / graph_width_factor, rows);
+                       plot->no_legend = 0;
+                       plot->timeline = 0;
+                       plot->direction = PLOT_DOWN;;
+
+                       if (movie_style == MOVIE_SPINDLE)
+                               setup_axis_spindle(plot);
+                       else
+                               setup_axis(plot);
+
+                       svg_alloc_legend(plot, count_io_plot_types() * 2);
+
+                       history = alloc_plot_history(tf);
+                       history->col = i;
+
+                       for (pid = 0; pid < tf->io_plots; pid++) {
+                               if (tf->gdd_reads[pid]) {
+                                       if (io_per_process)
+                                               strcpy(pos, tf->gdd_reads[pid]->label);
+                                       svg_add_legend(plot, label, " Reads", tf->gdd_reads[pid]->color);
+                               }
+                               if (tf->gdd_writes[pid]) {
+                                       if (io_per_process)
+                                               strcpy(pos, tf->gdd_writes[pid]->label);
+                                       svg_add_legend(plot, label, " Writes", tf->gdd_writes[pid]->color);
+                               }
+                       }
+
+                       batch_i = 0;
+                       while (i < cols && batch_i < batch_count) {
+                               for (pid = 0; pid < tf->io_plots; pid++) {
+                                       if (tf->gdd_reads[pid]) {
+                                               svg_io_graph_movie(tf->gdd_reads[pid],
+                                                                  history->read_pid_history[pid],
+                                                                  i);
+                                       }
+                                       if (tf->gdd_writes[pid]) {
+                                               svg_io_graph_movie(tf->gdd_writes[pid],
+                                                                  history->write_pid_history[pid],
+                                                                  i);
+                                       }
+                               }
+                               i++;
+                               batch_i++;
+                       }
+
+                       add_history(history, &movie_history);
+
+                       plot_movie_history(plot, &movie_history);
+
+                       svg_write_legend(plot);
+                       close_plot(plot);
+                       close_plot(plot);
+
+                       close_plot_file(plot);
+               }
+               free_all_plot_history(&movie_history);
+       }
+       convert_movie_files(movie_dir);
+       encode_movie(movie_dir);
+       cleanup_movie(movie_dir);
+       free(movie_dir);
+}
+
+static void plot_latency(struct plot *plot, unsigned int min_seconds,
+                        unsigned int max_seconds)
+{
+       struct trace_file *tf;
+       char *units;
+       char line[128];
+       u64 max = 0;
+
+       if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
+               return;
 
-       setup_axis(plot);
-       set_plot_label(plot, "Queue Depth");
        if (num_traces > 1)
                svg_alloc_legend(plot, num_traces);
 
+       list_for_each_entry(tf, &all_traces, list) {
+               if (tf->latency_gld->max > max)
+                       max = tf->latency_gld->max;
+       }
+
+       list_for_each_entry(tf, &all_traces, list)
+               tf->latency_gld->max = max;
+
+       setup_axis(plot);
+       set_plot_label(plot, "IO Latency");
+
        tf = list_entry(all_traces.next, struct trace_file, list);
-       set_ylabel(plot, "Pending IO");
-       set_yticks(plot, 4, 0, tf->queue_depth_gld->max, "");
-       set_xticks(plot, 9, 0, seconds);
+
+       scale_line_graph_time(&max, &units);
+       sprintf(line, "latency (%ss)", units);
+       set_ylabel(plot, line);
+       set_yticks(plot, num_yticks, 0, max, "");
+       set_xticks(plot, num_xticks, min_seconds, max_seconds);
 
        list_for_each_entry(tf, &all_traces, list) {
-               svg_line_graph(plot, tf->queue_depth_gld, tf->read_color);
+               svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
                if (num_traces > 1)
-                       svg_add_legend(plot, tf->label, "", tf->read_color);
+                       svg_add_legend(plot, tf->label, "", tf->line_color);
        }
 
        if (plot->add_xlabel)
@@ -855,9 +1241,11 @@ static void plot_queue_depth(struct plot *plot, int seconds)
        if (num_traces > 1)
                svg_write_legend(plot);
        close_plot(plot);
+       total_graphs_written++;
 }
 
-static void plot_iops(struct plot *plot, int seconds)
+static void plot_iops(struct plot *plot, unsigned int min_seconds,
+                     unsigned int max_seconds)
 {
        struct trace_file *tf;
        char *units;
@@ -874,8 +1262,6 @@ static void plot_iops(struct plot *plot, int seconds)
        list_for_each_entry(tf, &all_traces, list)
                tf->iop_gld->max = max;
 
-
-       plot->add_xlabel = last_active_graph == IOPS_GRAPH_INDEX;
        setup_axis(plot);
        set_plot_label(plot, "IOPs");
        if (num_traces > 1)
@@ -886,13 +1272,13 @@ static void plot_iops(struct plot *plot, int seconds)
        scale_line_graph_bytes(&max, &units, 1000);
        set_ylabel(plot, "IO/s");
 
-       set_yticks(plot, 4, 0, max, units);
-       set_xticks(plot, 9, 0, seconds);
+       set_yticks(plot, num_yticks, 0, max, units);
+       set_xticks(plot, num_xticks, min_seconds, max_seconds);
 
        list_for_each_entry(tf, &all_traces, list) {
-               svg_line_graph(plot, tf->iop_gld, tf->read_color);
+               svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
                if (num_traces > 1)
-                       svg_add_legend(plot, tf->label, "", tf->read_color);
+                       svg_add_legend(plot, tf->label, "", tf->line_color);
        }
 
        if (plot->add_xlabel)
@@ -901,14 +1287,39 @@ static void plot_iops(struct plot *plot, int seconds)
                svg_write_legend(plot);
 
        close_plot(plot);
+       total_graphs_written++;
+}
+
+static void check_plot_columns(struct plot *plot, int index)
+{
+       int count;
+
+       if (columns > 1 && (total_graphs_written == 0 ||
+           total_graphs_written % columns != 0)) {
+               count = graphs_left(index);
+               if (plot->direction == PLOT_DOWN) {
+                       plot->start_x_offset = 0;
+                       if (count <= columns)
+                               plot->add_xlabel = 1;
+               }
+               plot->direction = PLOT_ACROSS;
+
+       } else {
+               plot->direction = PLOT_DOWN;
+               if (index == last_active_graph)
+                       plot->add_xlabel = 1;
+       }
+
 }
 
 enum {
        HELP_LONG_OPT = 1,
 };
 
-char *option_string = "T:t:o:l:r:O:N:d:p:mh:w:";
+char *option_string = "F:T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:C:PK";
 static struct option long_options[] = {
+       {"columns", required_argument, 0, 'c'},
+       {"fio-trace", required_argument, 0, 'F'},
        {"title", required_argument, 0, 'T'},
        {"trace", required_argument, 0, 't'},
        {"output", required_argument, 0, 'o'},
@@ -917,10 +1328,17 @@ static struct option long_options[] = {
        {"no-graph", required_argument, 0, 'N'},
        {"only-graph", required_argument, 0, 'O'},
        {"device", required_argument, 0, 'd'},
+       {"blktrace-destination", required_argument, 0, 'D'},
        {"prog", required_argument, 0, 'p'},
-       {"movie", no_argument, 0, 'm'},
+       {"movie", optional_argument, 0, 'm'},
+       {"codec", optional_argument, 0, 'C'},
+       {"keep-movie-svgs", no_argument, 0, 'K'},
        {"width", required_argument, 0, 'w'},
        {"height", required_argument, 0, 'h'},
+       {"xzoom", required_argument, 0, 'x'},
+       {"yzoom", required_argument, 0, 'y'},
+       {"io-plot-action", required_argument, 0, 'a'},
+       {"per-process-io", no_argument, 0, 'P'},
        {"help", no_argument, 0, HELP_LONG_OPT},
        {0, 0, 0, 0}
 };
@@ -929,20 +1347,84 @@ static void print_usage(void)
 {
        fprintf(stderr, "iowatcher usage:\n"
                "\t-d (--device): device for blktrace to trace\n"
+               "\t-D (--blktrace-destination): destination for blktrace\n"
                "\t-t (--trace): trace file name (more than one allowed)\n"
+               "\t-F (--fio-trace): fio bandwidth trace (more than one allowed)\n"
                "\t-l (--label): trace label in the graph\n"
                "\t-o (--output): output file name (SVG only)\n"
                "\t-p (--prog): program to run while blktrace is run\n"
-               "\t-p (--movie): create IO animations\n"
+               "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
+               "\t-m (--movie [=spindle|rect]): create IO animations\n"
+               "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
                "\t-r (--rolling): number of seconds in the rolling averge\n"
                "\t-T (--title): graph title\n"
-               "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, iops)\n"
+               "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
+               "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
+               "\t-O (--only-graph): add a single graph to the output\n"
                "\t-h (--height): set the height of each graph\n"
                "\t-w (--width): set the width of each graph\n"
+               "\t-c (--columns): numbers of columns in graph output\n"
+               "\t-x (--xzoom): limit processed time to min:max\n"
+               "\t-y (--yzoom): limit processed sectors to min:max\n"
+               "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
+               "\t-P (--per-process-io): distinguish between processes in IO graph\n"
               );
        exit(1);
 }
 
+static int parse_double_range(char *str, double *min, double *max)
+{
+       char *end;
+
+       /* Empty lower bound - leave original value */
+       if (str[0] != ':') {
+               *min = strtod(str, &end);
+               if (*min == HUGE_VAL || *min == -HUGE_VAL)
+                       return -ERANGE;
+               if (*end != ':')
+                       return -EINVAL;
+       } else
+               end = str;
+       /* Empty upper bound - leave original value */
+       if (end[1]) {
+               *max = strtod(end+1, &end);
+               if (*max == HUGE_VAL || *max == -HUGE_VAL)
+                       return -ERANGE;
+               if (*end != 0)
+                       return -EINVAL;
+       }
+       if (*min > *max)
+               return -EINVAL;
+       return 0;
+}
+
+static int parse_ull_range(char *str, unsigned long long *min,
+                          unsigned long long *max)
+{
+       char *end;
+
+       /* Empty lower bound - leave original value */
+       if (str[0] != ':') {
+               *min = strtoull(str, &end, 10);
+               if (*min == ULLONG_MAX && errno == ERANGE)
+                       return -ERANGE;
+               if (*end != ':')
+                       return -EINVAL;
+       } else
+               end = str;
+       /* Empty upper bound - leave original value */
+       if (end[1]) {
+               *max = strtoull(end+1, &end, 10);
+               if (*max == ULLONG_MAX && errno == ERANGE)
+                       return -ERANGE;
+               if (*end != 0)
+                       return -EINVAL;
+       }
+       if (*min > *max)
+               return -EINVAL;
+       return 0;
+}
+
 static int parse_options(int ac, char **av)
 {
        int c;
@@ -966,6 +1448,9 @@ static int parse_options(int ac, char **av)
                        add_trace_file(optarg);
                        set_blktrace_outfile(optarg);
                        break;
+               case 'F':
+                       add_fio_trace_file(optarg);
+                       break;
                case 'o':
                        output_filename = strdup(optarg);
                        break;
@@ -986,13 +1471,39 @@ static int parse_options(int ac, char **av)
                        disable_one_graph(optarg);
                        break;
                case 'd':
-                       blktrace_device = strdup(optarg);
+                       if (num_blktrace_devices == MAX_DEVICES_PER_TRACE - 1) {
+                               fprintf(stderr, "Too many blktrace devices provided\n");
+                               exit(1);
+                       }
+                       blktrace_devices[num_blktrace_devices++] = strdup(optarg);
+                       break;
+               case 'D':
+                       blktrace_dest_dir = strdup(optarg);
+                       if (!strcmp(blktrace_dest_dir, "")) {
+                               fprintf(stderr, "Need a directory\n");
+                               print_usage();
+                       }
                        break;
                case 'p':
                        program_to_run = strdup(optarg);
                        break;
+               case 'K':
+                       keep_movie_svgs = 1;
+                       break;
                case 'm':
                        make_movie = 1;
+                       if (optarg) {
+                               movie_style = lookup_movie_style(optarg);
+                               if (movie_style < 0) {
+                                       fprintf(stderr, "Unknown movie style %s\n", optarg);
+                                       print_usage();
+                               }
+                       }
+                       fprintf(stderr, "Using movie style: %s\n",
+                               movie_styles[movie_style]);
+                       break;
+               case 'C':
+                       ffmpeg_codec = strdup(optarg);
                        break;
                case 'h':
                        opt_graph_height = atoi(optarg);
@@ -1000,6 +1511,45 @@ static int parse_options(int ac, char **av)
                case 'w':
                        opt_graph_width = atoi(optarg);
                        break;
+               case 'c':
+                       columns = atoi(optarg);
+                       break;
+               case 'x':
+                       if (parse_double_range(optarg, &min_time, &max_time)
+                           < 0) {
+                               fprintf(stderr, "Cannot parse time range %s\n",
+                                       optarg);
+                               exit(1);
+                       }
+                       break;
+               case 'y':
+                       if (parse_ull_range(optarg, &min_mb, &max_mb)
+                           < 0) {
+                               fprintf(stderr,
+                                       "Cannot parse offset range %s\n",
+                                       optarg);
+                               exit(1);
+                       }
+                       if (max_mb > ULLONG_MAX >> 20) {
+                               fprintf(stderr,
+                                       "Upper range limit too big."
+                                       " Maximum is %llu.\n", ULLONG_MAX >> 20);
+                               exit(1);
+                       }
+                       break;
+               case 'a':
+                       if (strlen(optarg) != 1) {
+action_err:
+                               fprintf(stderr, "Action must be one of Q, D, C.");
+                               exit(1);
+                       }
+                       plot_io_action = action_char_to_num(optarg[0]);
+                       if (plot_io_action < 0)
+                               goto action_err;
+                       break;
+               case 'P':
+                       io_per_process = 1;
+                       break;
                case '?':
                case HELP_LONG_OPT:
                        print_usage();
@@ -1011,16 +1561,30 @@ static int parse_options(int ac, char **av)
        return 0;
 }
 
+static void dest_mkdir(char *dir)
+{
+       int ret;
+
+       ret = mkdir(dir, 0777);
+       if (ret && errno != EEXIST) {
+               fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
+               exit(errno);
+       }
+}
 
 int main(int ac, char **av)
 {
        struct plot *plot;
-       int seconds = 0;
+       unsigned int min_seconds = 0;
+       unsigned int max_seconds = 0;
        u64 max_offset = 0;
+       u64 min_offset = ~(u64)0;
        struct trace_file *tf;
        int ret;
+       int rows, cols;
 
        init_io_hash_table();
+       init_process_hash_table();
 
        enable_all_graphs();
 
@@ -1029,27 +1593,51 @@ int main(int ac, char **av)
        last_active_graph = last_graph();
        if (make_movie) {
                set_io_graph_scale(256);
-               set_graph_size(700, 250);
+               if (movie_style == MOVIE_SPINDLE)
+                       set_graph_size(750, 550);
+               else
+                       set_graph_size(700, 400);
+
+               /*
+                * the plots in the movie don't have a seconds
+                * line yet, this makes us skip it
+                */
+               last_active_graph = TOTAL_GRAPHS + 1;
        }
        if (opt_graph_height)
                set_graph_height(opt_graph_height);
 
        if (opt_graph_width)
-               set_graph_width(opt_graph_height);
+               set_graph_width(opt_graph_width);
 
-       if (list_empty(&all_traces)) {
+       if (list_empty(&all_traces) && list_empty(&fio_traces)) {
                fprintf(stderr, "No traces found, exiting\n");
                exit(1);
        }
 
-       if (blktrace_device) {
-               ret = start_blktrace(blktrace_device, blktrace_outfile,
-                                    blktrace_dest_dir);
+       if (num_blktrace_devices) {
+               char *path;
+
+               dest_mkdir(blktrace_dest_dir);
+               if (num_blktrace_devices > 1) {
+                       snprintf(line, line_len, "%s/%s", blktrace_dest_dir,
+                                blktrace_outfile);
+                       dest_mkdir(line);
+               }
+
+               path = join_path(blktrace_dest_dir, blktrace_outfile);
+
+               snprintf(line, line_len, "%s.dump", path);
+               unlink(line);
+
+               ret = start_blktrace(blktrace_devices, num_blktrace_devices,
+                                    blktrace_outfile, blktrace_dest_dir);
                if (ret) {
                        fprintf(stderr, "exiting due to blktrace failure\n");
                        exit(1);
                }
-               start_mpstat(blktrace_outfile);
+
+               start_mpstat(path);
                if (program_to_run) {
                        ret = run_program(program_to_run);
                        if (ret) {
@@ -1058,13 +1646,13 @@ int main(int ac, char **av)
                                exit(1);
                        }
                        wait_for_tracers();
-                       blktrace_to_dump(blktrace_outfile);
                } else {
                        /* no program specified, just wait for
                         * blktrace to exit
                         */
                        wait_for_tracers();
                }
+               free(path);
        }
 
        /* step one, read all the traces */
@@ -1072,10 +1660,19 @@ int main(int ac, char **av)
 
        /* step two, find the maxes for time and offset */
        list_for_each_entry(tf, &all_traces, list)
-               compare_max_tf(tf, &seconds, &max_offset);
+               compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
+       list_for_each_entry(tf, &fio_traces, list)
+               compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
+       min_seconds = min_time;
+       if (max_seconds > max_time)
+               max_seconds = ceil(max_time);
+       if (min_offset < min_mb << 20)
+               min_offset = min_mb << 20;
+       if (max_offset > max_mb << 20)
+               max_offset = max_mb << 20;
 
        /* push the max we found into all the tfs */
-       set_all_max_tf(seconds, max_offset);
+       set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
 
        /* alloc graphing structs for all the traces */
        setup_trace_file_graphs();
@@ -1083,9 +1680,12 @@ int main(int ac, char **av)
        /* run through all the traces and read their events */
        read_trace_events();
 
+       pick_line_graph_color();
+
        plot = alloc_plot();
 
        if (make_movie) {
+               set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
                plot_io_movie(plot);
                exit(0);
        }
@@ -1093,31 +1693,67 @@ int main(int ac, char **av)
        set_plot_output(plot, output_filename);
 
        if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
-               set_legend_width(longest_label + strlen("writes"));
-       else if (num_traces > 1)
+               set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
+       else if (num_traces >= 1 || num_fio_traces >= 1)
                set_legend_width(longest_label);
        else
                set_legend_width(0);
 
+       get_graph_size(&cols, &rows);
+       if (columns > 1)
+               plot->add_xlabel = 1;
        set_plot_title(plot, graph_title);
-       plot_io(plot, seconds, max_offset);
-       plot_tput(plot, seconds);
-       plot_cpu(plot, seconds, "CPU IO Wait Time",
+
+       check_plot_columns(plot, IO_GRAPH_INDEX);
+       plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
+       plot->add_xlabel = 0;
+
+       if (columns > 1) {
+               set_graph_size(cols / columns, rows);
+               num_xticks /= columns;
+               if (num_xticks < 2)
+                       num_xticks = 2;
+       }
+       if (rows <= 50)
+               num_yticks--;
+
+       check_plot_columns(plot, TPUT_GRAPH_INDEX);
+       plot_tput(plot, min_seconds, max_seconds, 1);
+
+       check_plot_columns(plot, FIO_GRAPH_INDEX);
+       plot_fio_tput(plot, min_seconds, max_seconds);
+
+       check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
+       plot_cpu(plot, max_seconds, "CPU IO Wait Time",
                 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
-       plot_cpu(plot, seconds, "CPU System Time",
+
+       check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
+       plot_cpu(plot, max_seconds, "CPU System Time",
                 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
-       plot_cpu(plot, seconds, "CPU IRQ Time",
+
+       check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
+       plot_cpu(plot, max_seconds, "CPU IRQ Time",
                 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
-       plot_cpu(plot, seconds, "CPU SoftIRQ Time",
+
+       check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
+       plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
                 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
-       plot_cpu(plot, seconds, "CPU User Time",
+
+       check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
+       plot_cpu(plot, max_seconds, "CPU User Time",
                 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
 
-       plot_latency(plot, seconds);
-       plot_queue_depth(plot, seconds);
-       plot_iops(plot, seconds);
+       check_plot_columns(plot, LATENCY_GRAPH_INDEX);
+       plot_latency(plot, min_seconds, max_seconds);
+
+       check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
+       plot_queue_depth(plot, min_seconds, max_seconds);
+
+       check_plot_columns(plot, IOPS_GRAPH_INDEX);
+       plot_iops(plot, min_seconds, max_seconds);
 
        /* once for all */
        close_plot(plot);
+       close_plot_file(plot);
        return 0;
 }