2 * Copyright (C) 2012 Fusion-io
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 * Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
19 #include <sys/types.h>
28 #include <asm/types.h>
45 LIST_HEAD(all_traces);
46 LIST_HEAD(fio_traces);
48 static char line[1024];
49 static int line_len = 1024;
50 static int found_mpstat = 0;
51 static int make_movie = 0;
52 static int keep_movie_svgs = 0;
53 static int opt_graph_width = 0;
54 static int opt_graph_height = 0;
56 static int columns = 1;
57 static int num_xticks = 9;
58 static int num_yticks = 4;
60 static double min_time = 0;
61 static double max_time = DBL_MAX;
62 static unsigned long long min_mb = 0;
63 static unsigned long long max_mb = ULLONG_MAX >> 20;
65 int plot_io_action = 0;
66 int io_per_process = 0;
67 unsigned int longest_proc_name = 0;
70 * this doesn't include the IO graph,
71 * but it counts the other graphs as they go out
73 static int total_graphs_written = 1;
85 QUEUE_DEPTH_GRAPH_INDEX,
99 static char *graphs_by_name[] = {
119 char *movie_styles[] = {
125 static int movie_style = 0;
127 static int lookup_movie_style(char *str)
131 for (i = 0; i < NUM_MOVIE_STYLES; i++) {
132 if (strcmp(str, movie_styles[i]) == 0)
138 static int active_graphs[TOTAL_GRAPHS];
139 static int last_active_graph = IOPS_GRAPH_INDEX;
141 static int label_index = 0;
142 static int num_traces = 0;
143 static int num_fio_traces = 0;
144 static int longest_label = 0;
146 static char *graph_title = "";
147 static char *output_filename = "trace.svg";
148 static char *blktrace_devices[MAX_DEVICES_PER_TRACE];
149 static int num_blktrace_devices = 0;
150 static char *blktrace_outfile = "trace";
151 static char *blktrace_dest_dir = ".";
152 static char **prog_argv = NULL;
153 static int prog_argc = 0;
154 static char *ffmpeg_codec = "libx264";
156 static void alloc_mpstat_gld(struct trace_file *tf)
158 struct graph_line_data **ptr;
160 if (tf->trace->mpstat_num_cpus == 0)
163 ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
164 sizeof(struct graph_line_data *));
166 perror("Unable to allocate mpstat arrays\n");
169 tf->mpstat_gld = ptr;
172 static void enable_all_graphs(void)
175 for (i = 0; i < TOTAL_GRAPHS; i++)
176 active_graphs[i] = 1;
179 static void disable_all_graphs(void)
182 for (i = 0; i < TOTAL_GRAPHS; i++)
183 active_graphs[i] = 0;
186 static int enable_one_graph(char *name)
189 for (i = 0; i < TOTAL_GRAPHS; i++) {
190 if (strcmp(name, graphs_by_name[i]) == 0) {
191 active_graphs[i] = 1;
198 static int disable_one_graph(char *name)
201 for (i = 0; i < TOTAL_GRAPHS; i++) {
202 if (strcmp(name, graphs_by_name[i]) == 0) {
203 active_graphs[i] = 0;
210 static int last_graph(void)
213 for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
214 if (active_graphs[i]) {
221 static int graphs_left(int cur)
225 for (i = cur; i < TOTAL_GRAPHS; i++) {
226 if (active_graphs[i])
232 static char * join_path(char *dest_dir, char *filename)
234 /* alloc 2 extra bytes for '/' and '\0' */
235 char *path = malloc(strlen(dest_dir) + strlen(filename) + 2);
236 sprintf(path, "%s%s%s", dest_dir, "/", filename);
241 static void add_trace_file(char *filename)
243 struct trace_file *tf;
245 tf = calloc(1, sizeof(*tf));
247 fprintf(stderr, "Unable to allocate memory\n");
251 tf->filename = strdup(filename);
252 list_add_tail(&tf->list, &all_traces);
253 tf->line_color = "black";
257 static void add_fio_trace_file(char *filename)
259 struct trace_file *tf;
261 tf = calloc(1, sizeof(*tf));
263 fprintf(stderr, "Unable to allocate memory\n");
267 tf->filename = strdup(filename);
268 list_add_tail(&tf->list, &fio_traces);
269 tf->line_color = pick_fio_color();
274 static void setup_trace_file_graphs(void)
276 struct trace_file *tf;
285 list_for_each_entry(tf, &all_traces, list) {
286 tf->tput_reads_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
287 tf->tput_writes_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
288 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
289 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
291 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
292 tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data *));
293 tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data *));
294 tf->io_plots_allocated = alloc_ptrs;
296 if (tf->trace->mpstat_num_cpus == 0)
299 alloc_mpstat_gld(tf);
300 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
302 alloc_line_data(tf->mpstat_min_seconds,
303 tf->mpstat_max_seconds,
304 tf->mpstat_max_seconds);
305 tf->mpstat_gld[i]->max = 100;
309 list_for_each_entry(tf, &fio_traces, list) {
310 if (tf->trace->fio_seconds > 0) {
311 tf->fio_gld = alloc_line_data(tf->min_seconds,
319 static void read_traces(void)
321 struct trace_file *tf;
330 list_for_each_entry(tf, &all_traces, list) {
331 path = join_path(blktrace_dest_dir, tf->filename);
333 trace = open_trace(path);
337 last_time = find_last_time(trace);
339 tf->max_seconds = SECONDS(last_time) + 1;
340 tf->stop_seconds = SECONDS(last_time) + 1;
342 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
343 &max_bank, &max_bank_offset);
344 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
345 tf->min_offset = ymin;
346 tf->max_offset = ymax;
348 read_mpstat(trace, path);
349 tf->mpstat_stop_seconds = trace->mpstat_seconds;
350 tf->mpstat_max_seconds = trace->mpstat_seconds;
351 if (tf->mpstat_max_seconds)
357 list_for_each_entry(tf, &fio_traces, list) {
358 trace = open_fio_trace(tf->filename);
362 tf->max_seconds = tf->trace->fio_seconds;
363 tf->stop_seconds = tf->trace->fio_seconds;
367 static void pick_line_graph_color(void)
369 struct trace_file *tf;
372 list_for_each_entry(tf, &all_traces, list) {
373 for (i = 0; i < tf->io_plots; i++) {
374 if (tf->gdd_reads[i]) {
375 tf->line_color = tf->gdd_reads[i]->color;
376 tf->reads_color = tf->gdd_reads[i]->color;
378 if (tf->gdd_writes[i]) {
379 tf->line_color = tf->gdd_writes[i]->color;
380 tf->writes_color = tf->gdd_writes[i]->color;
382 if (tf->writes_color && tf->reads_color)
385 if (!tf->reads_color)
386 tf->reads_color = tf->line_color;
387 if (!tf->writes_color)
388 tf->writes_color = tf->line_color;
392 static void read_fio_events(struct trace_file *tf)
399 first_fio(tf->trace);
401 ret = read_fio_event(tf->trace, &time, &bw, &dir);
406 add_fio_gld(time, bw, tf->fio_gld);
407 if (next_fio_line(tf->trace))
412 static void read_trace_events(void)
415 struct trace_file *tf;
420 double user, sys, iowait, irq, soft;
421 double max_user = 0, max_sys = 0, max_iowait = 0,
422 max_irq = 0, max_soft = 0;
424 list_for_each_entry(tf, &fio_traces, list)
427 list_for_each_entry(tf, &all_traces, list) {
432 if (SECONDS(get_record_time(trace)) > tf->max_seconds)
435 add_tput(trace, tf->tput_writes_gld, tf->tput_reads_gld);
436 add_iop(trace, tf->iop_gld);
438 add_pending_io(trace, tf->queue_depth_gld);
439 add_completed_io(trace, tf->latency_gld);
440 } while (!(ret = next_record(trace)));
442 list_for_each_entry(tf, &all_traces, list) {
445 if (trace->mpstat_num_cpus == 0)
450 for (time = 0; time < tf->mpstat_stop_seconds; time++) {
451 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
452 ret = read_mpstat_event(trace, &user, &sys,
453 &iowait, &irq, &soft);
456 if (next_mpstat_line(trace))
465 if (iowait > max_iowait)
468 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
469 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
470 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
471 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
472 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
474 if (next_mpstat(trace) == NULL)
480 list_for_each_entry(tf, &all_traces, list) {
483 if (trace->mpstat_num_cpus == 0)
486 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
487 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
488 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
489 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
490 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
495 static void set_trace_label(char *label)
498 struct trace_file *tf;
499 int len = strlen(label);
501 if (len > longest_label)
504 list_for_each_entry(tf, &all_traces, list) {
505 if (cur == label_index) {
506 tf->label = strdup(label);
513 list_for_each_entry(tf, &fio_traces, list) {
514 if (cur == label_index) {
515 tf->label = strdup(label);
523 static void set_blktrace_outfile(char *arg)
525 char *s = strdup(arg);
526 char *last_dot = strrchr(s, '.');
529 if (strcmp(last_dot, ".dump") == 0)
532 blktrace_outfile = s;
536 static void compare_minmax_tf(struct trace_file *tf, unsigned int *max_seconds,
537 u64 *min_offset, u64 *max_offset)
539 if (tf->max_seconds > *max_seconds)
540 *max_seconds = tf->max_seconds;
541 if (tf->max_offset > *max_offset)
542 *max_offset = tf->max_offset;
543 if (tf->min_offset < *min_offset)
544 *min_offset = tf->min_offset;
547 static void set_all_minmax_tf(unsigned int min_seconds,
548 unsigned int max_seconds,
549 u64 min_offset, u64 max_offset)
551 struct trace_file *tf;
552 struct list_head *traces = &all_traces;
554 list_for_each_entry(tf, traces, list) {
555 tf->min_seconds = min_seconds;
556 tf->max_seconds = max_seconds;
557 if (tf->stop_seconds > max_seconds)
558 tf->stop_seconds = max_seconds;
559 if (tf->mpstat_max_seconds) {
560 tf->mpstat_min_seconds = min_seconds;
561 tf->mpstat_max_seconds = max_seconds;
562 if (tf->mpstat_stop_seconds > max_seconds)
563 tf->mpstat_stop_seconds = max_seconds;
565 tf->min_offset = min_offset;
566 tf->max_offset = max_offset;
568 if (traces == &all_traces) {
569 traces = &fio_traces;
574 static struct pid_plot_history *alloc_pid_plot_history(char *color)
576 struct pid_plot_history *pph;
578 pph = calloc(1, sizeof(struct pid_plot_history));
580 perror("memory allocation failed");
583 pph->history = malloc(sizeof(double) * 4096);
585 perror("memory allocation failed");
588 pph->history_len = 4096;
594 static struct plot_history *alloc_plot_history(struct trace_file *tf)
596 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
600 perror("memory allocation failed");
603 ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
604 if (!ph->read_pid_history) {
605 perror("memory allocation failed");
608 ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
609 if (!ph->write_pid_history) {
610 perror("memory allocation failed");
613 ph->pid_history_count = tf->io_plots;
614 for (i = 0; i < tf->io_plots; i++) {
615 if (tf->gdd_reads[i])
616 ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
617 if (tf->gdd_writes[i])
618 ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
623 LIST_HEAD(movie_history);
624 int num_histories = 0;
626 static void free_plot_history(struct plot_history *ph)
630 for (pid = 0; pid < ph->pid_history_count; pid++) {
631 if (ph->read_pid_history[pid])
632 free(ph->read_pid_history[pid]);
633 if (ph->write_pid_history[pid])
634 free(ph->write_pid_history[pid]);
636 free(ph->read_pid_history);
637 free(ph->write_pid_history);
641 static void add_history(struct plot_history *ph, struct list_head *list)
643 struct plot_history *entry;
645 list_add_tail(&ph->list, list);
648 if (num_histories > 12) {
650 entry = list_entry(list->next, struct plot_history, list);
651 list_del(&entry->list);
652 free_plot_history(entry);
656 static void plot_movie_history(struct plot *plot, struct list_head *list)
658 struct plot_history *ph;
661 if (num_histories > 2)
662 rewind_spindle_steps(num_histories - 1);
664 list_for_each_entry(ph, list, list) {
665 for (pid = 0; pid < ph->pid_history_count; pid++) {
666 if (ph->read_pid_history[pid]) {
667 if (movie_style == MOVIE_SPINDLE) {
668 svg_io_graph_movie_array_spindle(plot,
669 ph->read_pid_history[pid]);
671 svg_io_graph_movie_array(plot,
672 ph->read_pid_history[pid]);
675 if (ph->write_pid_history[pid]) {
676 if (movie_style == MOVIE_SPINDLE) {
677 svg_io_graph_movie_array_spindle(plot,
678 ph->write_pid_history[pid]);
680 svg_io_graph_movie_array(plot,
681 ph->write_pid_history[pid]);
688 static void free_all_plot_history(struct list_head *head)
690 struct plot_history *entry;
692 while (!list_empty(head)) {
693 entry = list_entry(head->next, struct plot_history, list);
694 list_del(&entry->list);
695 free_plot_history(entry);
699 static int count_io_plot_types(void)
701 struct trace_file *tf;
703 int total_io_types = 0;
705 list_for_each_entry(tf, &all_traces, list) {
706 for (i = 0; i < tf->io_plots; i++) {
707 if (tf->gdd_reads[i])
709 if (tf->gdd_writes[i])
713 return total_io_types;
716 static void plot_io_legend(struct plot *plot, struct graph_dot_data *gdd, char *prefix, char *rw)
721 ret = asprintf(&label, "%s %s", prefix, gdd->label);
723 ret = asprintf(&label, "%s", prefix);
725 perror("Failed to process labels");
728 svg_add_legend(plot, label, rw, gdd->color);
732 static void plot_io(struct plot *plot, unsigned int min_seconds,
733 unsigned int max_seconds, u64 min_offset, u64 max_offset)
735 struct trace_file *tf;
738 if (active_graphs[IO_GRAPH_INDEX] == 0)
743 svg_alloc_legend(plot, count_io_plot_types() * 2);
745 set_plot_label(plot, "Device IO");
746 set_ylabel(plot, "Offset (MB)");
747 set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
748 max_offset / (1024 * 1024), "");
749 set_xticks(plot, num_xticks, min_seconds, max_seconds);
751 list_for_each_entry(tf, &all_traces, list) {
752 char *prefix = tf->label ? tf->label : "";
754 for (i = 0; i < tf->io_plots; i++) {
755 if (tf->gdd_writes[i]) {
756 svg_io_graph(plot, tf->gdd_writes[i]);
757 plot_io_legend(plot, tf->gdd_writes[i], prefix, " Writes");
759 if (tf->gdd_reads[i]) {
760 svg_io_graph(plot, tf->gdd_reads[i]);
761 plot_io_legend(plot, tf->gdd_reads[i], prefix, " Reads");
765 if (plot->add_xlabel)
766 set_xlabel(plot, "Time (seconds)");
767 svg_write_legend(plot);
771 static void plot_tput(struct plot *plot, unsigned int min_seconds,
772 unsigned int max_seconds, int with_legend)
774 struct trace_file *tf;
779 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
783 svg_alloc_legend(plot, num_traces * 2);
785 list_for_each_entry(tf, &all_traces, list) {
786 if (tf->tput_writes_gld->max > max)
787 max = tf->tput_writes_gld->max;
788 if (tf->tput_reads_gld->max > max)
789 max = tf->tput_reads_gld->max;
791 list_for_each_entry(tf, &all_traces, list) {
792 if (tf->tput_writes_gld->max > 0)
793 tf->tput_writes_gld->max = max;
794 if (tf->tput_reads_gld->max > 0)
795 tf->tput_reads_gld->max = max;
799 set_plot_label(plot, "Throughput");
801 tf = list_entry(all_traces.next, struct trace_file, list);
803 scale_line_graph_bytes(&max, &units, 1024);
804 sprintf(line, "%sB/s", units);
805 set_ylabel(plot, line);
806 set_yticks(plot, num_yticks, 0, max, "");
807 set_xticks(plot, num_xticks, min_seconds, max_seconds);
809 list_for_each_entry(tf, &all_traces, list) {
810 if (tf->tput_writes_gld->max > 0) {
811 svg_line_graph(plot, tf->tput_writes_gld, tf->writes_color, 0, 0);
813 svg_add_legend(plot, tf->label, "Writes", tf->writes_color);
815 if (tf->tput_reads_gld->max > 0) {
816 svg_line_graph(plot, tf->tput_reads_gld, tf->reads_color, 0, 0);
818 svg_add_legend(plot, tf->label, "Reads", tf->reads_color);
822 if (plot->add_xlabel)
823 set_xlabel(plot, "Time (seconds)");
826 svg_write_legend(plot);
829 total_graphs_written++;
832 static void plot_fio_tput(struct plot *plot,
833 unsigned int min_seconds, unsigned int max_seconds)
835 struct trace_file *tf;
840 if (num_fio_traces == 0 || active_graphs[FIO_GRAPH_INDEX] == 0)
843 if (num_fio_traces > 1)
844 svg_alloc_legend(plot, num_fio_traces);
846 list_for_each_entry(tf, &fio_traces, list) {
847 if (tf->fio_gld->max > max)
848 max = tf->fio_gld->max;
851 list_for_each_entry(tf, &fio_traces, list) {
852 if (tf->fio_gld->max > 0)
853 tf->fio_gld->max = max;
857 set_plot_label(plot, "Fio Throughput");
859 tf = list_entry(all_traces.next, struct trace_file, list);
861 scale_line_graph_bytes(&max, &units, 1024);
862 sprintf(line, "%sB/s", units);
863 set_ylabel(plot, line);
864 set_yticks(plot, num_yticks, 0, max, "");
866 set_xticks(plot, num_xticks, min_seconds, max_seconds);
867 list_for_each_entry(tf, &fio_traces, list) {
868 if (tf->fio_gld->max > 0) {
869 svg_line_graph(plot, tf->fio_gld, tf->line_color, 0, 0);
870 if (num_fio_traces > 1)
871 svg_add_legend(plot, tf->label, "", tf->line_color);
875 if (plot->add_xlabel)
876 set_xlabel(plot, "Time (seconds)");
878 if (num_fio_traces > 1)
879 svg_write_legend(plot);
881 total_graphs_written++;
884 static void plot_cpu(struct plot *plot, unsigned int max_seconds, char *label,
885 int active_index, int gld_index)
887 struct trace_file *tf;
896 if (active_graphs[active_index] == 0)
899 list_for_each_entry(tf, &all_traces, list) {
900 if (tf->trace->mpstat_num_cpus > max)
901 max = tf->trace->mpstat_num_cpus;
906 tf = list_entry(all_traces.next, struct trace_file, list);
908 ymax = tf->mpstat_gld[gld_index]->max;
912 svg_alloc_legend(plot, num_traces * max);
915 set_plot_label(plot, label);
917 max_seconds = tf->mpstat_max_seconds;
919 set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
920 set_ylabel(plot, "Percent");
921 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
924 list_for_each_entry(tf, &all_traces, list) {
925 if (tf->mpstat_gld == 0)
927 for (gld_i = tf->mpstat_gld[0]->min_seconds;
928 gld_i < tf->mpstat_gld[0]->stop_seconds; gld_i++) {
929 if (tf->mpstat_gld[gld_index]->data[gld_i].count) {
930 avg += (tf->mpstat_gld[gld_index]->data[gld_i].sum /
931 tf->mpstat_gld[gld_index]->data[gld_i].count);
934 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
935 tf->mpstat_gld[gld_index]->min_seconds;
936 color = pick_cpu_color();
937 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
938 svg_add_legend(plot, tf->label, " avg", color);
940 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
941 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
944 for (gld_i = gld->min_seconds;
945 gld_i < gld->stop_seconds; gld_i++) {
946 if (gld->data[i].count) {
947 this_avg += gld->data[i].sum /
952 this_avg /= gld->stop_seconds - gld->min_seconds;
954 for (gld_i = gld->min_seconds;
955 gld_i < gld->stop_seconds; gld_i++) {
958 if (gld->data[gld_i].count == 0)
960 val = (double)gld->data[gld_i].sum /
961 gld->data[gld_i].count;
963 if (this_avg > avg + 30 || val > 95) {
964 color = pick_cpu_color();
965 svg_line_graph(plot, gld, color, avg + 30, 95);
966 snprintf(line, line_len, " CPU %d\n", i - 1);
967 svg_add_legend(plot, tf->label, line, color);
976 if (plot->add_xlabel)
977 set_xlabel(plot, "Time (seconds)");
979 if (!plot->no_legend) {
980 svg_write_legend(plot);
981 svg_free_legend(plot);
984 total_graphs_written++;
987 static void plot_queue_depth(struct plot *plot, unsigned int min_seconds,
988 unsigned int max_seconds)
990 struct trace_file *tf;
992 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
996 set_plot_label(plot, "Queue Depth");
998 svg_alloc_legend(plot, num_traces);
1000 tf = list_entry(all_traces.next, struct trace_file, list);
1001 set_ylabel(plot, "Pending IO");
1002 set_yticks(plot, num_yticks, 0, tf->queue_depth_gld->max, "");
1003 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1005 list_for_each_entry(tf, &all_traces, list) {
1006 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
1008 svg_add_legend(plot, tf->label, "", tf->line_color);
1011 if (plot->add_xlabel)
1012 set_xlabel(plot, "Time (seconds)");
1014 svg_write_legend(plot);
1016 total_graphs_written++;
1019 static void system_check(const char *cmd)
1021 if (system(cmd) < 0) {
1024 fprintf(stderr, "system exec failed (%d): %s\n", err, cmd);
1029 static void convert_movie_files(char *movie_dir)
1031 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
1032 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
1037 static void mencode_movie(char *movie_dir)
1039 fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
1040 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
1041 "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
1046 static void tencode_movie(char *movie_dir)
1048 fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
1049 snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
1050 output_filename, movie_dir, output_filename);
1054 static void encode_movie(char *movie_dir)
1056 char *last_dot = strrchr(output_filename, '.');
1059 (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
1060 tencode_movie(movie_dir);
1062 mencode_movie(movie_dir);
1065 static void cleanup_movie(char *movie_dir)
1067 if (keep_movie_svgs) {
1068 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
1071 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
1072 snprintf(line, line_len, "rm %s/*", movie_dir);
1075 snprintf(line, line_len, "rmdir %s", movie_dir);
1079 static void plot_io_movie(struct plot *plot)
1081 struct trace_file *tf;
1083 struct plot_history *history;
1086 int movie_frames_per_sec = 20;
1087 int total_frames = movie_len * movie_frames_per_sec;
1090 int graph_width_factor = 5;
1092 char movie_dir[] = "io-movie-XXXXXX";
1094 if (mkdtemp(movie_dir) == NULL) {
1095 perror("Unable to create temp directory for movie files");
1099 get_graph_size(&cols, &rows);
1100 batch_count = cols / total_frames;
1102 if (batch_count == 0)
1105 list_for_each_entry(tf, &all_traces, list) {
1106 char *prefix = tf->label ? tf->label : "";
1110 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
1111 set_plot_output(plot, line);
1112 set_plot_title(plot, graph_title);
1113 orig_y_offset = plot->start_y_offset;
1115 plot->no_legend = 1;
1117 set_graph_size(cols / graph_width_factor, rows / 8);
1118 plot->timeline = i / graph_width_factor;
1120 plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
1122 plot_cpu(plot, tf->max_seconds,
1123 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1125 plot->direction = PLOT_ACROSS;
1126 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
1128 /* movie graph starts here */
1129 plot->start_y_offset = orig_y_offset;
1130 set_graph_size(cols - cols / graph_width_factor, rows);
1131 plot->no_legend = 0;
1133 plot->direction = PLOT_DOWN;;
1135 if (movie_style == MOVIE_SPINDLE)
1136 setup_axis_spindle(plot);
1140 svg_alloc_legend(plot, count_io_plot_types() * 2);
1142 history = alloc_plot_history(tf);
1145 for (pid = 0; pid < tf->io_plots; pid++) {
1146 if (tf->gdd_reads[pid])
1147 plot_io_legend(plot, tf->gdd_reads[pid], prefix, " Reads");
1148 if (tf->gdd_writes[pid])
1149 plot_io_legend(plot, tf->gdd_writes[pid], prefix, " Writes");
1153 while (i < cols && batch_i < batch_count) {
1154 for (pid = 0; pid < tf->io_plots; pid++) {
1155 if (tf->gdd_reads[pid]) {
1156 svg_io_graph_movie(tf->gdd_reads[pid],
1157 history->read_pid_history[pid],
1160 if (tf->gdd_writes[pid]) {
1161 svg_io_graph_movie(tf->gdd_writes[pid],
1162 history->write_pid_history[pid],
1170 add_history(history, &movie_history);
1172 plot_movie_history(plot, &movie_history);
1174 svg_write_legend(plot);
1178 close_plot_file(plot);
1180 free_all_plot_history(&movie_history);
1182 convert_movie_files(movie_dir);
1183 encode_movie(movie_dir);
1184 cleanup_movie(movie_dir);
1187 static void plot_latency(struct plot *plot, unsigned int min_seconds,
1188 unsigned int max_seconds)
1190 struct trace_file *tf;
1195 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1199 svg_alloc_legend(plot, num_traces);
1201 list_for_each_entry(tf, &all_traces, list) {
1202 if (tf->latency_gld->max > max)
1203 max = tf->latency_gld->max;
1206 list_for_each_entry(tf, &all_traces, list)
1207 tf->latency_gld->max = max;
1210 set_plot_label(plot, "IO Latency");
1212 tf = list_entry(all_traces.next, struct trace_file, list);
1214 scale_line_graph_time(&max, &units);
1215 sprintf(line, "latency (%ss)", units);
1216 set_ylabel(plot, line);
1217 set_yticks(plot, num_yticks, 0, max, "");
1218 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1220 list_for_each_entry(tf, &all_traces, list) {
1221 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1223 svg_add_legend(plot, tf->label, "", tf->line_color);
1226 if (plot->add_xlabel)
1227 set_xlabel(plot, "Time (seconds)");
1229 svg_write_legend(plot);
1231 total_graphs_written++;
1234 static void plot_iops(struct plot *plot, unsigned int min_seconds,
1235 unsigned int max_seconds)
1237 struct trace_file *tf;
1241 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1244 list_for_each_entry(tf, &all_traces, list) {
1245 if (tf->iop_gld->max > max)
1246 max = tf->iop_gld->max;
1249 list_for_each_entry(tf, &all_traces, list)
1250 tf->iop_gld->max = max;
1253 set_plot_label(plot, "IOPs");
1255 svg_alloc_legend(plot, num_traces);
1257 tf = list_entry(all_traces.next, struct trace_file, list);
1259 scale_line_graph_bytes(&max, &units, 1000);
1260 set_ylabel(plot, "IO/s");
1262 set_yticks(plot, num_yticks, 0, max, units);
1263 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1265 list_for_each_entry(tf, &all_traces, list) {
1266 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1268 svg_add_legend(plot, tf->label, "", tf->line_color);
1271 if (plot->add_xlabel)
1272 set_xlabel(plot, "Time (seconds)");
1274 svg_write_legend(plot);
1277 total_graphs_written++;
1280 static void check_plot_columns(struct plot *plot, int index)
1284 if (columns > 1 && (total_graphs_written == 0 ||
1285 total_graphs_written % columns != 0)) {
1286 count = graphs_left(index);
1287 if (plot->direction == PLOT_DOWN) {
1288 plot->start_x_offset = 0;
1289 if (count <= columns)
1290 plot->add_xlabel = 1;
1292 plot->direction = PLOT_ACROSS;
1295 plot->direction = PLOT_DOWN;
1296 if (index == last_active_graph)
1297 plot->add_xlabel = 1;
1306 char *option_string = "+F:T:t:o:l:r:O:N:d:D:pm::h:w:c:x:y:a:C:PK";
1307 static struct option long_options[] = {
1308 {"columns", required_argument, 0, 'c'},
1309 {"fio-trace", required_argument, 0, 'F'},
1310 {"title", required_argument, 0, 'T'},
1311 {"trace", required_argument, 0, 't'},
1312 {"output", required_argument, 0, 'o'},
1313 {"label", required_argument, 0, 'l'},
1314 {"rolling", required_argument, 0, 'r'},
1315 {"no-graph", required_argument, 0, 'N'},
1316 {"only-graph", required_argument, 0, 'O'},
1317 {"device", required_argument, 0, 'd'},
1318 {"blktrace-destination", required_argument, 0, 'D'},
1319 {"prog", no_argument, 0, 'p'},
1320 {"movie", optional_argument, 0, 'm'},
1321 {"codec", optional_argument, 0, 'C'},
1322 {"keep-movie-svgs", no_argument, 0, 'K'},
1323 {"width", required_argument, 0, 'w'},
1324 {"height", required_argument, 0, 'h'},
1325 {"xzoom", required_argument, 0, 'x'},
1326 {"yzoom", required_argument, 0, 'y'},
1327 {"io-plot-action", required_argument, 0, 'a'},
1328 {"per-process-io", no_argument, 0, 'P'},
1329 {"help", no_argument, 0, HELP_LONG_OPT},
1333 static void print_usage(void)
1335 fprintf(stderr, "iowatcher usage:\n"
1336 "\t-d (--device): device for blktrace to trace\n"
1337 "\t-D (--blktrace-destination): destination for blktrace\n"
1338 "\t-t (--trace): trace file name (more than one allowed)\n"
1339 "\t-F (--fio-trace): fio bandwidth trace (more than one allowed)\n"
1340 "\t-l (--label): trace label in the graph\n"
1341 "\t-o (--output): output file name for the SVG image or video\n"
1342 "\t-p (--prog): run a program while blktrace is run\n"
1343 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
1344 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1345 "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
1346 "\t-r (--rolling): number of seconds in the rolling averge\n"
1347 "\t-T (--title): graph title\n"
1348 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1349 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1350 "\t-O (--only-graph): add a single graph to the output\n"
1351 "\t-h (--height): set the height of each graph\n"
1352 "\t-w (--width): set the width of each graph\n"
1353 "\t-c (--columns): numbers of columns in graph output\n"
1354 "\t-x (--xzoom): limit processed time to min:max\n"
1355 "\t-y (--yzoom): limit processed sectors to min:max\n"
1356 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1357 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1362 static int parse_double_range(char *str, double *min, double *max)
1366 /* Empty lower bound - leave original value */
1367 if (str[0] != ':') {
1368 *min = strtod(str, &end);
1369 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1375 /* Empty upper bound - leave original value */
1377 *max = strtod(end+1, &end);
1378 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1388 static int parse_ull_range(char *str, unsigned long long *min,
1389 unsigned long long *max)
1393 /* Empty lower bound - leave original value */
1394 if (str[0] != ':') {
1395 *min = strtoull(str, &end, 10);
1396 if (*min == ULLONG_MAX && errno == ERANGE)
1402 /* Empty upper bound - leave original value */
1404 *max = strtoull(end+1, &end, 10);
1405 if (*max == ULLONG_MAX && errno == ERANGE)
1415 static int parse_options(int ac, char **av)
1422 int option_index = 0;
1424 c = getopt_long(ac, av, option_string,
1425 long_options, &option_index);
1432 graph_title = strdup(optarg);
1435 add_trace_file(optarg);
1436 set_blktrace_outfile(optarg);
1439 add_fio_trace_file(optarg);
1442 output_filename = strdup(optarg);
1445 set_trace_label(optarg);
1448 set_rolling_avg(atoi(optarg));
1452 disable_all_graphs();
1455 enable_one_graph(optarg);
1458 disable_one_graph(optarg);
1461 if (num_blktrace_devices == MAX_DEVICES_PER_TRACE - 1) {
1462 fprintf(stderr, "Too many blktrace devices provided\n");
1465 blktrace_devices[num_blktrace_devices++] = strdup(optarg);
1468 blktrace_dest_dir = strdup(optarg);
1469 if (!strcmp(blktrace_dest_dir, "")) {
1470 fprintf(stderr, "Need a directory\n");
1478 keep_movie_svgs = 1;
1483 movie_style = lookup_movie_style(optarg);
1484 if (movie_style < 0) {
1485 fprintf(stderr, "Unknown movie style %s\n", optarg);
1489 fprintf(stderr, "Using movie style: %s\n",
1490 movie_styles[movie_style]);
1493 ffmpeg_codec = strdup(optarg);
1496 opt_graph_height = atoi(optarg);
1499 opt_graph_width = atoi(optarg);
1502 columns = atoi(optarg);
1505 if (parse_double_range(optarg, &min_time, &max_time)
1507 fprintf(stderr, "Cannot parse time range %s\n",
1513 if (parse_ull_range(optarg, &min_mb, &max_mb)
1516 "Cannot parse offset range %s\n",
1520 if (max_mb > ULLONG_MAX >> 20) {
1522 "Upper range limit too big."
1523 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1528 if (strlen(optarg) != 1) {
1530 fprintf(stderr, "Action must be one of Q, D, C.");
1533 plot_io_action = action_char_to_num(optarg[0]);
1534 if (plot_io_action < 0)
1549 if (optind < ac && p_flagged) {
1550 prog_argv = &av[optind];
1551 prog_argc = ac - optind;
1552 } else if (p_flagged) {
1553 fprintf(stderr, "--prog or -p given but no program specified\n");
1555 } else if (optind < ac) {
1556 fprintf(stderr, "Extra arguments '%s'... (and --prog not specified)\n", av[optind]);
1563 static void dest_mkdir(char *dir)
1567 ret = mkdir(dir, 0777);
1568 if (ret && errno != EEXIST) {
1569 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1574 int main(int ac, char **av)
1577 unsigned int min_seconds = 0;
1578 unsigned int max_seconds = 0;
1580 u64 min_offset = ~(u64)0;
1581 struct trace_file *tf;
1585 init_io_hash_table();
1586 init_process_hash_table();
1588 enable_all_graphs();
1590 parse_options(ac, av);
1592 last_active_graph = last_graph();
1594 set_io_graph_scale(256);
1595 if (movie_style == MOVIE_SPINDLE)
1596 set_graph_size(750, 550);
1598 set_graph_size(700, 400);
1601 * the plots in the movie don't have a seconds
1602 * line yet, this makes us skip it
1604 last_active_graph = TOTAL_GRAPHS + 1;
1606 if (opt_graph_height)
1607 set_graph_height(opt_graph_height);
1609 if (opt_graph_width)
1610 set_graph_width(opt_graph_width);
1612 if (list_empty(&all_traces) && list_empty(&fio_traces)) {
1613 fprintf(stderr, "No traces found, exiting\n");
1617 if (num_blktrace_devices) {
1618 char *path = join_path(blktrace_dest_dir, blktrace_outfile);
1619 dest_mkdir(blktrace_dest_dir);
1622 snprintf(line, line_len, "%s.dump", path);
1625 ret = start_blktrace(blktrace_devices, num_blktrace_devices,
1626 blktrace_outfile, blktrace_dest_dir);
1628 perror("Exiting due to blktrace failure");
1632 snprintf(line, line_len, "%s.mpstat", path);
1633 ret = start_mpstat(line);
1635 perror("Exiting due to mpstat failure");
1639 if (prog_argv && prog_argc) {
1640 run_program(prog_argc, prog_argv, 1, NULL, NULL);
1641 wait_for_tracers(SIGINT);
1643 printf("Tracing until interrupted...\n");
1644 wait_for_tracers(0);
1649 /* step one, read all the traces */
1652 /* step two, find the maxes for time and offset */
1653 list_for_each_entry(tf, &all_traces, list)
1654 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1655 list_for_each_entry(tf, &fio_traces, list)
1656 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1657 min_seconds = min_time;
1658 if (max_seconds > max_time)
1659 max_seconds = ceil(max_time);
1660 if (min_offset < min_mb << 20)
1661 min_offset = min_mb << 20;
1662 if (max_offset > max_mb << 20)
1663 max_offset = max_mb << 20;
1665 /* push the max we found into all the tfs */
1666 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1668 /* alloc graphing structs for all the traces */
1669 setup_trace_file_graphs();
1671 /* run through all the traces and read their events */
1672 read_trace_events();
1674 pick_line_graph_color();
1676 plot = alloc_plot();
1679 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1680 plot_io_movie(plot);
1684 set_plot_output(plot, output_filename);
1686 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1687 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1688 else if (num_traces >= 1 || num_fio_traces >= 1)
1689 set_legend_width(longest_label);
1691 set_legend_width(0);
1693 get_graph_size(&cols, &rows);
1695 plot->add_xlabel = 1;
1696 set_plot_title(plot, graph_title);
1698 check_plot_columns(plot, IO_GRAPH_INDEX);
1699 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1700 plot->add_xlabel = 0;
1703 set_graph_size(cols / columns, rows);
1704 num_xticks /= columns;
1711 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1712 plot_tput(plot, min_seconds, max_seconds, 1);
1714 check_plot_columns(plot, FIO_GRAPH_INDEX);
1715 plot_fio_tput(plot, min_seconds, max_seconds);
1717 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1718 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1719 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1721 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1722 plot_cpu(plot, max_seconds, "CPU System Time",
1723 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1725 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1726 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1727 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1729 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1730 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1731 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1733 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1734 plot_cpu(plot, max_seconds, "CPU User Time",
1735 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1737 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1738 plot_latency(plot, min_seconds, max_seconds);
1740 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1741 plot_queue_depth(plot, min_seconds, max_seconds);
1743 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1744 plot_iops(plot, min_seconds, max_seconds);
1748 close_plot_file(plot);