iowatcher: wrap system() in a checker function
[blktrace.git] / iowatcher / main.c
index 8adeaccd172ab17a22df0c712019da73df64194c..2797afb918665367d0d1cd295e92eef34979ef85 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 <signal.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;
 
 static int columns = 1;
 static int num_xticks = 9;
+static int num_yticks = 4;
+
+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,
@@ -58,39 +72,10 @@ static int num_xticks = 9;
  */
 static int total_graphs_written = 1;
 
-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;
-}
-
-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;
-}
-
 enum {
        IO_GRAPH_INDEX = 0,
        TPUT_GRAPH_INDEX,
+       FIO_GRAPH_INDEX,
        CPU_SYS_GRAPH_INDEX,
        CPU_IO_GRAPH_INDEX,
        CPU_IRQ_GRAPH_INDEX,
@@ -114,6 +99,7 @@ enum {
 static char *graphs_by_name[] = {
        "io",
        "tput",
+       "fio",
        "cpu-sys",
        "cpu-io",
        "cpu-irq",
@@ -154,31 +140,18 @@ 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 **prog_argv = NULL;
+static int prog_argc = 0;
+static char *ffmpeg_codec = "libx264";
 
 static void alloc_mpstat_gld(struct trace_file *tf)
 {
@@ -256,6 +229,15 @@ static int graphs_left(int cur)
        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;
@@ -268,23 +250,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;
@@ -292,11 +299,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)
@@ -308,26 +325,87 @@ static void read_traces(void)
        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);
-               find_highest_offset(trace, &tf->max_offset, &max_bank,
-                                   &max_bank_offset);
-               filter_outliers(trace, tf->max_offset, &ymin, &ymax);
+               tf->max_seconds = SECONDS(last_time) + 1;
+               tf->stop_seconds = SECONDS(last_time) + 1;
+
+               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;
        }
 }
 
@@ -338,25 +416,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;
@@ -366,7 +447,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);
@@ -421,6 +502,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++;
@@ -430,13 +520,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);
@@ -450,59 +533,111 @@ 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)
+static struct pid_plot_history *alloc_pid_plot_history(char *color)
 {
-       char *ret;
-       char *pattern = strdup("io-movie-XXXXXX");;
+       struct pid_plot_history *pph;
 
-       ret = mkdtemp(pattern);
-       if (!ret) {
-               perror("Unable to create temp directory for movie files");
+       pph = calloc(1, sizeof(struct pid_plot_history));
+       if (!pph) {
+               perror("memory allocation failed");
                exit(1);
        }
-       return ret;
+       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(char *color)
+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->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
+       if (!ph->write_pid_history) {
                perror("memory allocation failed");
                exit(1);
        }
-       ph->history_len = 4096;
-       ph->color = color;
+       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;
@@ -514,65 +649,117 @@ 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)
 {
        struct plot_history *ph;
+       int pid;
 
        if (num_histories > 2)
                rewind_spindle_steps(num_histories - 1);
 
        list_for_each_entry(ph, list, list) {
-               if (movie_style == MOVIE_SPINDLE)
-                       svg_io_graph_movie_array_spindle(plot, ph);
-               else
-                       svg_io_graph_movie_array(plot, ph);
+               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_legend(struct plot *plot, struct graph_dot_data *gdd, char *prefix, char *rw)
+{
+       int ret = 0;
+       char *label = NULL;
+       if (io_per_process)
+               ret = asprintf(&label, "%s %s", prefix, gdd->label);
+       else
+               ret = asprintf(&label, "%s", prefix);
+       if (ret < 0) {
+               perror("Failed to process labels");
+               exit(1);
        }
+       svg_add_legend(plot, label, rw, gdd->color);
+       free(label);
 }
 
-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, num_xticks, 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 *prefix = tf->label ? tf->label : "";
 
-               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);
+               for (i = 0; i < tf->io_plots; i++) {
+                       if (tf->gdd_writes[i]) {
+                               svg_io_graph(plot, tf->gdd_writes[i]);
+                               plot_io_legend(plot, tf->gdd_writes[i], prefix, " Writes");
+                       }
+                       if (tf->gdd_reads[i]) {
+                               svg_io_graph(plot, tf->gdd_reads[i]);
+                               plot_io_legend(plot, tf->gdd_reads[i], prefix, " Reads");
+                       }
                }
        }
        if (plot->add_xlabel)
@@ -581,7 +768,8 @@ static void plot_io(struct plot *plot, int seconds, u64 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;
@@ -591,14 +779,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");
@@ -608,30 +803,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, num_xticks, 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, 0, 0);
-               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);
+
+       close_plot(plot);
+       total_graphs_written++;
+}
+
+static void plot_fio_tput(struct plot *plot,
+                         unsigned int min_seconds, unsigned int max_seconds)
+{
+       struct trace_file *tf;
+       char *units;
+       char line[128];
+       u64 max = 0;
+
+       if (num_fio_traces == 0 || active_graphs[FIO_GRAPH_INDEX] == 0)
+               return;
+
+       if (num_fio_traces > 1)
+               svg_alloc_legend(plot, num_fio_traces);
+
+       list_for_each_entry(tf, &fio_traces, list) {
+               if (tf->fio_gld->max > max)
+                       max = tf->fio_gld->max;
+       }
+
+       list_for_each_entry(tf, &fio_traces, list) {
+               if (tf->fio_gld->max > 0)
+                       tf->fio_gld->max = max;
+       }
+
+       setup_axis(plot);
+       set_plot_label(plot, "Fio Throughput");
+
+       tf = list_entry(all_traces.next, struct trace_file, list);
+
+       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);
+               }
+       }
+
+       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;
        int max = 0;
        int i;
-       int gld_i;
+       unsigned int gld_i;
        char *color;
        double avg = 0;
        int ymax;
@@ -658,21 +914,25 @@ static void plot_cpu(struct plot *plot, int seconds, char *label,
        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, num_xticks, 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++) {
-                       if (tf->mpstat_gld[gld_index]->data[i].count) {
-                               avg += (tf->mpstat_gld[gld_index]->data[i].sum /
-                                       tf->mpstat_gld[gld_index]->data[i].count);
+               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, 0, 0);
                svg_add_legend(plot, tf->label, " avg", color);
@@ -681,16 +941,18 @@ static void plot_cpu(struct plot *plot, int seconds, char *label,
                        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++) {
+                       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;
+                       this_avg /= gld->stop_seconds - gld->min_seconds;
 
-                       for (gld_i = 0; gld_i < gld->stop_seconds; gld_i++) {
+                       for (gld_i = gld->min_seconds;
+                            gld_i < gld->stop_seconds; gld_i++) {
                                double val;
 
                                if (gld->data[gld_i].count == 0)
@@ -722,7 +984,8 @@ static void plot_cpu(struct plot *plot, int seconds, char *label,
        total_graphs_written++;
 }
 
-static void plot_queue_depth(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;
 
@@ -736,13 +999,13 @@ static void plot_queue_depth(struct plot *plot, int seconds)
 
        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, num_xticks, 0, seconds);
+       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->queue_depth_gld, tf->read_color, 0, 0);
+               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)
@@ -753,39 +1016,71 @@ static void plot_queue_depth(struct plot *plot, int seconds)
        total_graphs_written++;
 }
 
+static void system_check(const char *cmd)
+{
+       if (system(cmd) < 0) {
+               int err = errno;
+
+               fprintf(stderr, "system exec failed (%d): %s\n", err, cmd);
+               exit(1);
+       }
+}
+
 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);
+       system_check(line);
 }
 
 static void mencode_movie(char *movie_dir)
 {
-       fprintf(stderr, "Creating movie %s\n", 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 libx264 %s", movie_dir, output_filename, output_filename);
-       system(line);
+                "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
+                output_filename);
+       system_check(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_check(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);
+       system_check(line);
 
        snprintf(line, line_len, "rmdir %s", movie_dir);
-       system(line);
+       system_check(line);
 }
 
 static void plot_io_movie(struct plot *plot)
 {
        struct trace_file *tf;
-       char *movie_dir = create_movie_temp_dir();
-       int i;
-       struct plot_history *read_history;
-       struct plot_history *write_history;
+       int i, pid;
+       struct plot_history *history;
        int batch_i;
        int movie_len = 30;
        int movie_frames_per_sec = 20;
@@ -794,6 +1089,12 @@ static void plot_io_movie(struct plot *plot)
        int batch_count;
        int graph_width_factor = 5;
        int orig_y_offset;
+       char movie_dir[] = "io-movie-XXXXXX";
+
+       if (mkdtemp(movie_dir) == NULL) {
+               perror("Unable to create temp directory for movie files");
+               exit(1);
+       }
 
        get_graph_size(&cols, &rows);
        batch_count = cols / total_frames;
@@ -802,9 +1103,7 @@ static void plot_io_movie(struct plot *plot)
                batch_count = 1;
 
        list_for_each_entry(tf, &all_traces, list) {
-               char *label = tf->label;
-               if (!label)
-                       label = "";
+               char *prefix = tf->label ? tf->label : "";
 
                i = 0;
                while (i < cols) {
@@ -818,13 +1117,13 @@ static void plot_io_movie(struct plot *plot)
                        set_graph_size(cols / graph_width_factor, rows / 8);
                        plot->timeline = i / graph_width_factor;
 
-                       plot_tput(plot, tf->gdd_reads->seconds);
+                       plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
 
-                       plot_cpu(plot, tf->gdd_reads->seconds,
+                       plot_cpu(plot, tf->max_seconds,
                                   "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
 
                        plot->direction = PLOT_ACROSS;
-                       plot_queue_depth(plot, tf->gdd_reads->seconds);
+                       plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
 
                        /* movie graph starts here */
                        plot->start_y_offset = orig_y_offset;
@@ -838,31 +1137,39 @@ static void plot_io_movie(struct plot *plot)
                        else
                                setup_axis(plot);
 
-                       svg_alloc_legend(plot, num_traces * 2);
+                       svg_alloc_legend(plot, count_io_plot_types() * 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;
+                       history = alloc_plot_history(tf);
+                       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);
+                       for (pid = 0; pid < tf->io_plots; pid++) {
+                               if (tf->gdd_reads[pid])
+                                       plot_io_legend(plot, tf->gdd_reads[pid], prefix, " Reads");
+                               if (tf->gdd_writes[pid])
+                                       plot_io_legend(plot, tf->gdd_writes[pid], prefix, " Writes");
+                       }
 
                        batch_i = 0;
                        while (i < cols && batch_i < batch_count) {
-                               svg_io_graph_movie(tf->gdd_reads, read_history, i);
-                               svg_io_graph_movie(tf->gdd_writes, write_history, i);
+                               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(read_history, &movie_history_reads);
-                       add_history(write_history, &movie_history_writes);
+                       add_history(history, &movie_history);
 
-                       plot_movie_history(plot, &movie_history_reads);
-                       plot_movie_history(plot, &movie_history_writes);
+                       plot_movie_history(plot, &movie_history);
 
                        svg_write_legend(plot);
                        close_plot(plot);
@@ -870,16 +1177,15 @@ static void plot_io_movie(struct plot *plot)
 
                        close_plot_file(plot);
                }
-               free_all_plot_history(&movie_history_reads);
-               free_all_plot_history(&movie_history_writes);
+               free_all_plot_history(&movie_history);
        }
        convert_movie_files(movie_dir);
-       mencode_movie(movie_dir);
+       encode_movie(movie_dir);
        cleanup_movie(movie_dir);
-       free(movie_dir);
 }
 
-static void plot_latency(struct plot *plot, int seconds)
+static void plot_latency(struct plot *plot, unsigned int min_seconds,
+                        unsigned int max_seconds)
 {
        struct trace_file *tf;
        char *units;
@@ -891,10 +1197,12 @@ static void plot_latency(struct plot *plot, int seconds)
 
        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;
 
@@ -906,13 +1214,13 @@ static void plot_latency(struct plot *plot, int seconds)
        scale_line_graph_time(&max, &units);
        sprintf(line, "latency (%ss)", units);
        set_ylabel(plot, line);
-       set_yticks(plot, 4, 0, max, "");
-       set_xticks(plot, num_xticks, 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->latency_gld, tf->read_color, 0, 0);
+               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)
@@ -923,7 +1231,8 @@ static void plot_latency(struct plot *plot, int seconds)
        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;
@@ -950,13 +1259,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, num_xticks, 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, 0, 0);
+               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)
@@ -994,9 +1303,10 @@ enum {
        HELP_LONG_OPT = 1,
 };
 
-char *option_string = "T:t:o:l:r:O:N:d:p:m::h:w:c:";
+char *option_string = "+F:T:t:o:l:r:O:N:d:D:pm::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'},
@@ -1005,10 +1315,17 @@ static struct option long_options[] = {
        {"no-graph", required_argument, 0, 'N'},
        {"only-graph", required_argument, 0, 'O'},
        {"device", required_argument, 0, 'd'},
-       {"prog", required_argument, 0, 'p'},
+       {"blktrace-destination", required_argument, 0, 'D'},
+       {"prog", no_argument, 0, 'p'},
        {"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}
 };
@@ -1017,11 +1334,15 @@ 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-o (--output): output file name for the SVG image or video\n"
+               "\t-p (--prog): run a program while blktrace is run\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, \n"
@@ -1030,17 +1351,74 @@ static void print_usage(void)
                "\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;
        int disabled = 0;
+       int p_flagged = 0;
 
        while (1) {
-               // int this_option_optind = optind ? optind : 1;
                int option_index = 0;
 
                c = getopt_long(ac, av, option_string,
@@ -1057,6 +1435,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;
@@ -1077,10 +1458,24 @@ 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);
+                       p_flagged = 1;
+                       break;
+               case 'K':
+                       keep_movie_svgs = 1;
                        break;
                case 'm':
                        make_movie = 1;
@@ -1094,6 +1489,9 @@ static int parse_options(int ac, char **av)
                        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);
                        break;
@@ -1103,6 +1501,42 @@ static int parse_options(int ac, char **av)
                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();
@@ -1111,20 +1545,45 @@ static int parse_options(int ac, char **av)
                        break;
                }
        }
+
+       if (optind < ac && p_flagged) {
+               prog_argv = &av[optind];
+               prog_argc = ac - optind;
+       } else if (p_flagged) {
+               fprintf(stderr, "--prog or -p given but no program specified\n");
+               exit(1);
+       } else if (optind < ac) {
+               fprintf(stderr, "Extra arguments '%s'... (and --prog not specified)\n", av[optind]);
+               exit(1);
+       }
+
        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();
 
@@ -1150,34 +1609,41 @@ int main(int ac, char **av)
        if (opt_graph_width)
                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 = join_path(blktrace_dest_dir, blktrace_outfile);
+               dest_mkdir(blktrace_dest_dir);
+               dest_mkdir(path);
+
+               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);
+                       perror("Exiting due to blktrace failure");
+                       exit(ret);
                }
-               start_mpstat(blktrace_outfile);
-               if (program_to_run) {
-                       ret = run_program(program_to_run);
-                       if (ret) {
-                               fprintf(stderr, "failed to run %s\n",
-                                       program_to_run);
-                               exit(1);
-                       }
-                       wait_for_tracers();
-                       blktrace_to_dump(blktrace_outfile);
+
+               snprintf(line, line_len, "%s.mpstat", path);
+               ret = start_mpstat(line);
+               if (ret) {
+                       perror("Exiting due to mpstat failure");
+                       exit(ret);
+               }
+
+               if (prog_argv && prog_argc) {
+                       run_program(prog_argc, prog_argv, 1, NULL, NULL);
+                       wait_for_tracers(SIGINT);
                } else {
-                       /* no program specified, just wait for
-                        * blktrace to exit
-                        */
-                       wait_for_tracers();
+                       printf("Tracing until interrupted...\n");
+                       wait_for_tracers(0);
                }
+               free(path);
        }
 
        /* step one, read all the traces */
@@ -1185,10 +1651,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();
@@ -1196,9 +1671,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);
        }
@@ -1206,8 +1684,8 @@ 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);
@@ -1217,7 +1695,8 @@ int main(int ac, char **av)
                plot->add_xlabel = 1;
        set_plot_title(plot, graph_title);
 
-       plot_io(plot, seconds, max_offset);
+       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) {
@@ -1226,38 +1705,43 @@ int main(int ac, char **av)
                if (num_xticks < 2)
                        num_xticks = 2;
        }
+       if (rows <= 50)
+               num_yticks--;
 
        check_plot_columns(plot, TPUT_GRAPH_INDEX);
-       plot_tput(plot, seconds);
+       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, seconds, "CPU IO Wait Time",
+       plot_cpu(plot, max_seconds, "CPU IO Wait Time",
                 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
 
        check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
-       plot_cpu(plot, seconds, "CPU System Time",
+       plot_cpu(plot, max_seconds, "CPU System Time",
                 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
 
        check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
-       plot_cpu(plot, seconds, "CPU IRQ Time",
+       plot_cpu(plot, max_seconds, "CPU IRQ Time",
                 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
 
        check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
-       plot_cpu(plot, seconds, "CPU SoftIRQ Time",
+       plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
                 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
 
        check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
-       plot_cpu(plot, seconds, "CPU User Time",
+       plot_cpu(plot, max_seconds, "CPU User Time",
                 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
 
        check_plot_columns(plot, LATENCY_GRAPH_INDEX);
-       plot_latency(plot, seconds);
+       plot_latency(plot, min_seconds, max_seconds);
 
        check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
-       plot_queue_depth(plot, seconds);
+       plot_queue_depth(plot, min_seconds, max_seconds);
 
        check_plot_columns(plot, IOPS_GRAPH_INDEX);
-       plot_iops(plot, seconds);
+       plot_iops(plot, min_seconds, max_seconds);
 
        /* once for all */
        close_plot(plot);