iowatcher: Add bounds checking in find_step
[blktrace.git] / iowatcher / main.c
index 6e392f279ad82adc9f5a522ffb5af5b2a95b719f..d23e3b25f03d73d5045b10c767a4bb93c79a5e94 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 "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;
@@ -72,6 +74,7 @@ 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,
@@ -95,6 +98,7 @@ enum {
 static char *graphs_by_name[] = {
        "io",
        "tput",
+       "fio",
        "cpu-sys",
        "cpu-io",
        "cpu-irq",
@@ -135,14 +139,17 @@ 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;
 
 static char *graph_title = "";
 static char *output_filename = "trace.svg";
-static char *blktrace_device = NULL;
+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)
 {
@@ -245,6 +252,23 @@ static void add_trace_file(char *filename)
        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;
@@ -255,10 +279,13 @@ static void setup_trace_file_graphs(void)
                alloc_ptrs = 16;
        else
                alloc_ptrs = 1;
+
        list_for_each_entry(tf, &all_traces, list) {
-               tf->tput_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, 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));
@@ -276,6 +303,15 @@ static void setup_trace_file_graphs(void)
                        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)
@@ -291,6 +327,7 @@ static void read_traces(void)
 
        list_for_each_entry(tf, &all_traces, list) {
                path = join_path(blktrace_dest_dir, tf->filename);
+
                trace = open_trace(path);
                if (!trace)
                        exit(1);
@@ -299,6 +336,7 @@ static void read_traces(void)
                tf->trace = trace;
                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);
@@ -310,8 +348,18 @@ static void read_traces(void)
                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)
@@ -323,13 +371,39 @@ static void pick_line_graph_color(void)
                for (i = 0; i < tf->io_plots; i++) {
                        if (tf->gdd_reads[i]) {
                                tf->line_color = tf->gdd_reads[i]->color;
-                               break;
+                               tf->reads_color = tf->gdd_reads[i]->color;
                        }
                        if (tf->gdd_writes[i]) {
                                tf->line_color = tf->gdd_writes[i]->color;
-                               break;
+                               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;
        }
 }
 
@@ -345,12 +419,16 @@ static void read_trace_events(void)
        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) {
                        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);
                        add_pending_io(trace, tf->queue_depth_gld);
@@ -423,6 +501,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++;
@@ -458,8 +545,9 @@ static void compare_minmax_tf(struct trace_file *tf, int *max_seconds, u64 *min_
 static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
 {
        struct trace_file *tf;
-
-       list_for_each_entry(tf, &all_traces, list) {
+       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)
@@ -473,6 +561,10 @@ static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset,
                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)
@@ -655,13 +747,20 @@ static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min
                if (!tf->label)
                        label[0] = 0;
                else {
-                       strcpy(label, tf->label);
+                       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)
@@ -669,12 +768,6 @@ static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min
                                svg_add_legend(plot, label, " Reads", tf->gdd_reads[i]->color);
                        }
 
-                       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 (plot->add_xlabel)
@@ -683,7 +776,8 @@ static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min
        close_plot(plot);
 }
 
-static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
+static void plot_tput(struct plot *plot, int min_seconds, int max_seconds,
+                     int with_legend)
 {
        struct trace_file *tf;
        char *units;
@@ -693,14 +787,21 @@ static void plot_tput(struct plot *plot, int min_seconds, int max_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");
@@ -714,14 +815,74 @@ static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
        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->line_color, 0, 0);
-               if (num_traces > 1)
-                       svg_add_legend(plot, tf->label, "", tf->line_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, int min_seconds, 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++;
@@ -873,7 +1034,8 @@ 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 libx264 %s", movie_dir, output_filename, output_filename);
+                "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
+                output_filename);
        system(line);
 }
 
@@ -956,8 +1118,7 @@ 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->min_seconds,
-                                 tf->max_seconds);
+                       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);
@@ -1148,9 +1309,10 @@ enum {
        HELP_LONG_OPT = 1,
 };
 
-char *option_string = "T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:PK";
+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'},
@@ -1162,6 +1324,7 @@ static struct option long_options[] = {
        {"blktrace-destination", required_argument, 0, 'D'},
        {"prog", required_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'},
@@ -1179,11 +1342,13 @@ static void print_usage(void)
                "\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-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"
@@ -1276,6 +1441,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;
@@ -1296,7 +1464,11 @@ 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);
@@ -1323,6 +1495,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;
@@ -1379,11 +1554,11 @@ action_err:
        return 0;
 }
 
-static void dest_mkdir()
+static void dest_mkdir(char *dir)
 {
        int ret;
 
-       ret = mkdir(blktrace_dest_dir, 0777);
+       ret = mkdir(dir, 0777);
        if (ret && errno != EEXIST) {
                fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
                exit(errno);
@@ -1428,19 +1603,28 @@ 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) {
+       if (num_blktrace_devices) {
                char *path;
 
-               dest_mkdir();
+               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);
 
-               ret = start_blktrace(blktrace_device, blktrace_outfile,
-                                    blktrace_dest_dir);
+               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);
@@ -1455,7 +1639,6 @@ int main(int ac, char **av)
                                exit(1);
                        }
                        wait_for_tracers();
-                       blktrace_to_dump(path);
                } else {
                        /* no program specified, just wait for
                         * blktrace to exit
@@ -1471,6 +1654,8 @@ int main(int ac, char **av)
        /* step two, find the maxes for time and offset */
        list_for_each_entry(tf, &all_traces, list)
                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);
@@ -1502,7 +1687,7 @@ int main(int ac, char **av)
 
        if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
                set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
-       else if (num_traces > 1)
+       else if (num_traces >= 1 || num_fio_traces >= 1)
                set_legend_width(longest_label);
        else
                set_legend_width(0);
@@ -1526,7 +1711,10 @@ int main(int ac, char **av)
                num_yticks--;
 
        check_plot_columns(plot, TPUT_GRAPH_INDEX);
-       plot_tput(plot, min_seconds, max_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, max_seconds, "CPU IO Wait Time",