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>
44 LIST_HEAD(all_traces);
45 LIST_HEAD(fio_traces);
47 static char line[1024];
48 static int line_len = 1024;
49 static int found_mpstat = 0;
50 static int make_movie = 0;
51 static int keep_movie_svgs = 0;
52 static int opt_graph_width = 0;
53 static int opt_graph_height = 0;
55 static int columns = 1;
56 static int num_xticks = 9;
57 static int num_yticks = 4;
59 static double min_time = 0;
60 static double max_time = DBL_MAX;
61 static unsigned long long min_mb = 0;
62 static unsigned long long max_mb = ULLONG_MAX >> 20;
64 int plot_io_action = 0;
65 int io_per_process = 0;
66 unsigned int longest_proc_name = 0;
69 * this doesn't include the IO graph,
70 * but it counts the other graphs as they go out
72 static int total_graphs_written = 1;
84 QUEUE_DEPTH_GRAPH_INDEX,
98 static char *graphs_by_name[] = {
118 char *movie_styles[] = {
124 static int movie_style = 0;
126 static int lookup_movie_style(char *str)
130 for (i = 0; i < NUM_MOVIE_STYLES; i++) {
131 if (strcmp(str, movie_styles[i]) == 0)
137 static int active_graphs[TOTAL_GRAPHS];
138 static int last_active_graph = IOPS_GRAPH_INDEX;
140 static int label_index = 0;
141 static int num_traces = 0;
142 static int num_fio_traces = 0;
143 static int longest_label = 0;
145 static char *graph_title = "";
146 static char *output_filename = "trace.svg";
147 static char *blktrace_devices[MAX_DEVICES_PER_TRACE];
148 static int num_blktrace_devices = 0;
149 static char *blktrace_outfile = "trace";
150 static char *blktrace_dest_dir = ".";
151 static char *program_to_run = NULL;
152 static char *ffmpeg_codec = "libx264";
154 static void alloc_mpstat_gld(struct trace_file *tf)
156 struct graph_line_data **ptr;
158 if (tf->trace->mpstat_num_cpus == 0)
161 ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
162 sizeof(struct graph_line_data *));
164 perror("Unable to allocate mpstat arrays\n");
167 tf->mpstat_gld = ptr;
170 static void enable_all_graphs(void)
173 for (i = 0; i < TOTAL_GRAPHS; i++)
174 active_graphs[i] = 1;
177 static void disable_all_graphs(void)
180 for (i = 0; i < TOTAL_GRAPHS; i++)
181 active_graphs[i] = 0;
184 static int enable_one_graph(char *name)
187 for (i = 0; i < TOTAL_GRAPHS; i++) {
188 if (strcmp(name, graphs_by_name[i]) == 0) {
189 active_graphs[i] = 1;
196 static int disable_one_graph(char *name)
199 for (i = 0; i < TOTAL_GRAPHS; i++) {
200 if (strcmp(name, graphs_by_name[i]) == 0) {
201 active_graphs[i] = 0;
208 static int last_graph(void)
211 for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
212 if (active_graphs[i]) {
219 static int graphs_left(int cur)
223 for (i = cur; i < TOTAL_GRAPHS; i++) {
224 if (active_graphs[i])
230 static char * join_path(char *dest_dir, char *filename)
232 /* alloc 2 extra bytes for '/' and '\0' */
233 char *path = malloc(strlen(dest_dir) + strlen(filename) + 2);
234 sprintf(path, "%s%s%s", dest_dir, "/", filename);
239 static void add_trace_file(char *filename)
241 struct trace_file *tf;
243 tf = calloc(1, sizeof(*tf));
245 fprintf(stderr, "Unable to allocate memory\n");
249 tf->filename = strdup(filename);
250 list_add_tail(&tf->list, &all_traces);
251 tf->line_color = "black";
255 static void add_fio_trace_file(char *filename)
257 struct trace_file *tf;
259 tf = calloc(1, sizeof(*tf));
261 fprintf(stderr, "Unable to allocate memory\n");
265 tf->filename = strdup(filename);
266 list_add_tail(&tf->list, &fio_traces);
267 tf->line_color = pick_fio_color();
272 static void setup_trace_file_graphs(void)
274 struct trace_file *tf;
283 list_for_each_entry(tf, &all_traces, list) {
284 tf->tput_reads_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
285 tf->tput_writes_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
286 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
287 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
289 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
290 tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
291 tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
292 tf->io_plots_allocated = alloc_ptrs;
294 if (tf->trace->mpstat_num_cpus == 0)
297 alloc_mpstat_gld(tf);
298 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
300 alloc_line_data(tf->mpstat_min_seconds,
301 tf->mpstat_max_seconds,
302 tf->mpstat_max_seconds);
303 tf->mpstat_gld[i]->max = 100;
307 list_for_each_entry(tf, &fio_traces, list) {
308 if (tf->trace->fio_seconds > 0) {
309 tf->fio_gld = alloc_line_data(tf->min_seconds,
317 static void read_traces(void)
319 struct trace_file *tf;
328 list_for_each_entry(tf, &all_traces, list) {
329 path = join_path(blktrace_dest_dir, tf->filename);
331 trace = open_trace(path);
335 last_time = find_last_time(trace);
337 tf->max_seconds = SECONDS(last_time) + 1;
338 tf->stop_seconds = SECONDS(last_time) + 1;
340 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
341 &max_bank, &max_bank_offset);
342 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
343 tf->min_offset = ymin;
344 tf->max_offset = ymax;
346 read_mpstat(trace, path);
347 tf->mpstat_stop_seconds = trace->mpstat_seconds;
348 tf->mpstat_max_seconds = trace->mpstat_seconds;
349 if (tf->mpstat_max_seconds)
355 list_for_each_entry(tf, &fio_traces, list) {
356 trace = open_fio_trace(tf->filename);
360 tf->max_seconds = tf->trace->fio_seconds;
361 tf->stop_seconds = tf->trace->fio_seconds;
365 static void pick_line_graph_color(void)
367 struct trace_file *tf;
370 list_for_each_entry(tf, &all_traces, list) {
371 for (i = 0; i < tf->io_plots; i++) {
372 if (tf->gdd_reads[i]) {
373 tf->line_color = tf->gdd_reads[i]->color;
374 tf->reads_color = tf->gdd_reads[i]->color;
376 if (tf->gdd_writes[i]) {
377 tf->line_color = tf->gdd_writes[i]->color;
378 tf->writes_color = tf->gdd_writes[i]->color;
380 if (tf->writes_color && tf->reads_color)
383 if (!tf->reads_color)
384 tf->reads_color = tf->line_color;
385 if (!tf->writes_color)
386 tf->writes_color = tf->line_color;
390 static void read_fio_events(struct trace_file *tf)
397 first_fio(tf->trace);
399 ret = read_fio_event(tf->trace, &time, &bw, &dir);
404 add_fio_gld(time, bw, tf->fio_gld);
405 if (next_fio_line(tf->trace))
410 static void read_trace_events(void)
413 struct trace_file *tf;
418 double user, sys, iowait, irq, soft;
419 double max_user = 0, max_sys = 0, max_iowait = 0,
420 max_irq = 0, max_soft = 0;
422 list_for_each_entry(tf, &fio_traces, list)
425 list_for_each_entry(tf, &all_traces, list) {
430 if (SECONDS(get_record_time(trace)) > tf->max_seconds)
433 add_tput(trace, tf->tput_writes_gld, tf->tput_reads_gld);
434 add_iop(trace, tf->iop_gld);
436 add_pending_io(trace, tf->queue_depth_gld);
437 add_completed_io(trace, tf->latency_gld);
438 } while (!(ret = next_record(trace)));
440 list_for_each_entry(tf, &all_traces, list) {
443 if (trace->mpstat_num_cpus == 0)
448 for (time = 0; time < tf->mpstat_stop_seconds; time++) {
449 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
450 ret = read_mpstat_event(trace, &user, &sys,
451 &iowait, &irq, &soft);
454 if (next_mpstat_line(trace))
463 if (iowait > max_iowait)
466 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
467 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
468 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
469 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
470 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
472 if (next_mpstat(trace) == NULL)
478 list_for_each_entry(tf, &all_traces, list) {
481 if (trace->mpstat_num_cpus == 0)
484 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
485 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
486 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
487 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
488 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
493 static void set_trace_label(char *label)
496 struct trace_file *tf;
497 int len = strlen(label);
499 if (len > longest_label)
502 list_for_each_entry(tf, &all_traces, list) {
503 if (cur == label_index) {
504 tf->label = strdup(label);
511 list_for_each_entry(tf, &fio_traces, list) {
512 if (cur == label_index) {
513 tf->label = strdup(label);
521 static void set_blktrace_outfile(char *arg)
523 char *s = strdup(arg);
524 char *last_dot = strrchr(s, '.');
527 if (strcmp(last_dot, ".dump") == 0)
530 blktrace_outfile = s;
534 static void compare_minmax_tf(struct trace_file *tf, unsigned int *max_seconds,
535 u64 *min_offset, u64 *max_offset)
537 if (tf->max_seconds > *max_seconds)
538 *max_seconds = tf->max_seconds;
539 if (tf->max_offset > *max_offset)
540 *max_offset = tf->max_offset;
541 if (tf->min_offset < *min_offset)
542 *min_offset = tf->min_offset;
545 static void set_all_minmax_tf(unsigned int min_seconds,
546 unsigned int max_seconds,
547 u64 min_offset, u64 max_offset)
549 struct trace_file *tf;
550 struct list_head *traces = &all_traces;
552 list_for_each_entry(tf, traces, list) {
553 tf->min_seconds = min_seconds;
554 tf->max_seconds = max_seconds;
555 if (tf->stop_seconds > max_seconds)
556 tf->stop_seconds = max_seconds;
557 if (tf->mpstat_max_seconds) {
558 tf->mpstat_min_seconds = min_seconds;
559 tf->mpstat_max_seconds = max_seconds;
560 if (tf->mpstat_stop_seconds > max_seconds)
561 tf->mpstat_stop_seconds = max_seconds;
563 tf->min_offset = min_offset;
564 tf->max_offset = max_offset;
566 if (traces == &all_traces) {
567 traces = &fio_traces;
572 static char *create_movie_temp_dir(void)
575 char *pattern = strdup("io-movie-XXXXXX");;
577 ret = mkdtemp(pattern);
579 perror("Unable to create temp directory for movie files");
585 static struct pid_plot_history *alloc_pid_plot_history(char *color)
587 struct pid_plot_history *pph;
589 pph = calloc(1, sizeof(struct pid_plot_history));
591 perror("memory allocation failed");
594 pph->history = malloc(sizeof(double) * 4096);
596 perror("memory allocation failed");
599 pph->history_len = 4096;
605 static struct plot_history *alloc_plot_history(struct trace_file *tf)
607 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
611 perror("memory allocation failed");
614 ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
615 if (!ph->read_pid_history) {
616 perror("memory allocation failed");
619 ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
620 if (!ph->write_pid_history) {
621 perror("memory allocation failed");
624 ph->pid_history_count = tf->io_plots;
625 for (i = 0; i < tf->io_plots; i++) {
626 if (tf->gdd_reads[i])
627 ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
628 if (tf->gdd_writes[i])
629 ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
634 LIST_HEAD(movie_history);
635 int num_histories = 0;
637 static void free_plot_history(struct plot_history *ph)
641 for (pid = 0; pid < ph->pid_history_count; pid++) {
642 if (ph->read_pid_history[pid])
643 free(ph->read_pid_history[pid]);
644 if (ph->write_pid_history[pid])
645 free(ph->write_pid_history[pid]);
647 free(ph->read_pid_history);
648 free(ph->write_pid_history);
652 static void add_history(struct plot_history *ph, struct list_head *list)
654 struct plot_history *entry;
656 list_add_tail(&ph->list, list);
659 if (num_histories > 12) {
661 entry = list_entry(list->next, struct plot_history, list);
662 list_del(&entry->list);
663 free_plot_history(entry);
667 static void plot_movie_history(struct plot *plot, struct list_head *list)
669 struct plot_history *ph;
672 if (num_histories > 2)
673 rewind_spindle_steps(num_histories - 1);
675 list_for_each_entry(ph, list, list) {
676 for (pid = 0; pid < ph->pid_history_count; pid++) {
677 if (ph->read_pid_history[pid]) {
678 if (movie_style == MOVIE_SPINDLE) {
679 svg_io_graph_movie_array_spindle(plot,
680 ph->read_pid_history[pid]);
682 svg_io_graph_movie_array(plot,
683 ph->read_pid_history[pid]);
686 if (ph->write_pid_history[pid]) {
687 if (movie_style == MOVIE_SPINDLE) {
688 svg_io_graph_movie_array_spindle(plot,
689 ph->write_pid_history[pid]);
691 svg_io_graph_movie_array(plot,
692 ph->write_pid_history[pid]);
699 static void free_all_plot_history(struct list_head *head)
701 struct plot_history *entry;
703 while (!list_empty(head)) {
704 entry = list_entry(head->next, struct plot_history, list);
705 list_del(&entry->list);
706 free_plot_history(entry);
710 static int count_io_plot_types(void)
712 struct trace_file *tf;
714 int total_io_types = 0;
716 list_for_each_entry(tf, &all_traces, list) {
717 for (i = 0; i < tf->io_plots; i++) {
718 if (tf->gdd_reads[i])
720 if (tf->gdd_writes[i])
724 return total_io_types;
727 static void plot_io(struct plot *plot, unsigned int min_seconds,
728 unsigned int max_seconds, u64 min_offset, u64 max_offset)
730 struct trace_file *tf;
733 if (active_graphs[IO_GRAPH_INDEX] == 0)
738 svg_alloc_legend(plot, count_io_plot_types() * 2);
740 set_plot_label(plot, "Device IO");
741 set_ylabel(plot, "Offset (MB)");
742 set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
743 max_offset / (1024 * 1024), "");
744 set_xticks(plot, num_xticks, min_seconds, max_seconds);
746 list_for_each_entry(tf, &all_traces, list) {
753 strncpy(label, tf->label, 255);
758 pos = label + strlen(label);
760 for (i = 0; i < tf->io_plots; i++) {
761 if (tf->gdd_writes[i]) {
762 svg_io_graph(plot, tf->gdd_writes[i]);
764 strcpy(pos, tf->gdd_writes[i]->label);
765 svg_add_legend(plot, label, " Writes", tf->gdd_writes[i]->color);
767 if (tf->gdd_reads[i]) {
768 svg_io_graph(plot, tf->gdd_reads[i]);
770 strcpy(pos, tf->gdd_reads[i]->label);
771 svg_add_legend(plot, label, " Reads", tf->gdd_reads[i]->color);
776 if (plot->add_xlabel)
777 set_xlabel(plot, "Time (seconds)");
778 svg_write_legend(plot);
782 static void plot_tput(struct plot *plot, unsigned int min_seconds,
783 unsigned int max_seconds, int with_legend)
785 struct trace_file *tf;
790 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
794 svg_alloc_legend(plot, num_traces * 2);
796 list_for_each_entry(tf, &all_traces, list) {
797 if (tf->tput_writes_gld->max > max)
798 max = tf->tput_writes_gld->max;
799 if (tf->tput_reads_gld->max > max)
800 max = tf->tput_reads_gld->max;
802 list_for_each_entry(tf, &all_traces, list) {
803 if (tf->tput_writes_gld->max > 0)
804 tf->tput_writes_gld->max = max;
805 if (tf->tput_reads_gld->max > 0)
806 tf->tput_reads_gld->max = max;
810 set_plot_label(plot, "Throughput");
812 tf = list_entry(all_traces.next, struct trace_file, list);
814 scale_line_graph_bytes(&max, &units, 1024);
815 sprintf(line, "%sB/s", units);
816 set_ylabel(plot, line);
817 set_yticks(plot, num_yticks, 0, max, "");
818 set_xticks(plot, num_xticks, min_seconds, max_seconds);
820 list_for_each_entry(tf, &all_traces, list) {
821 if (tf->tput_writes_gld->max > 0) {
822 svg_line_graph(plot, tf->tput_writes_gld, tf->writes_color, 0, 0);
824 svg_add_legend(plot, tf->label, "Writes", tf->writes_color);
826 if (tf->tput_reads_gld->max > 0) {
827 svg_line_graph(plot, tf->tput_reads_gld, tf->reads_color, 0, 0);
829 svg_add_legend(plot, tf->label, "Reads", tf->reads_color);
833 if (plot->add_xlabel)
834 set_xlabel(plot, "Time (seconds)");
837 svg_write_legend(plot);
840 total_graphs_written++;
843 static void plot_fio_tput(struct plot *plot,
844 unsigned int min_seconds, unsigned int max_seconds)
846 struct trace_file *tf;
851 if (num_fio_traces == 0 || active_graphs[FIO_GRAPH_INDEX] == 0)
854 if (num_fio_traces > 1)
855 svg_alloc_legend(plot, num_fio_traces);
857 list_for_each_entry(tf, &fio_traces, list) {
858 if (tf->fio_gld->max > max)
859 max = tf->fio_gld->max;
862 list_for_each_entry(tf, &fio_traces, list) {
863 if (tf->fio_gld->max > 0)
864 tf->fio_gld->max = max;
868 set_plot_label(plot, "Fio Throughput");
870 tf = list_entry(all_traces.next, struct trace_file, list);
872 scale_line_graph_bytes(&max, &units, 1024);
873 sprintf(line, "%sB/s", units);
874 set_ylabel(plot, line);
875 set_yticks(plot, num_yticks, 0, max, "");
877 set_xticks(plot, num_xticks, min_seconds, max_seconds);
878 list_for_each_entry(tf, &fio_traces, list) {
879 if (tf->fio_gld->max > 0) {
880 svg_line_graph(plot, tf->fio_gld, tf->line_color, 0, 0);
881 if (num_fio_traces > 1)
882 svg_add_legend(plot, tf->label, "", tf->line_color);
886 if (plot->add_xlabel)
887 set_xlabel(plot, "Time (seconds)");
889 if (num_fio_traces > 1)
890 svg_write_legend(plot);
892 total_graphs_written++;
895 static void plot_cpu(struct plot *plot, unsigned int max_seconds, char *label,
896 int active_index, int gld_index)
898 struct trace_file *tf;
907 if (active_graphs[active_index] == 0)
910 list_for_each_entry(tf, &all_traces, list) {
911 if (tf->trace->mpstat_num_cpus > max)
912 max = tf->trace->mpstat_num_cpus;
917 tf = list_entry(all_traces.next, struct trace_file, list);
919 ymax = tf->mpstat_gld[gld_index]->max;
923 svg_alloc_legend(plot, num_traces * max);
926 set_plot_label(plot, label);
928 max_seconds = tf->mpstat_max_seconds;
930 set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
931 set_ylabel(plot, "Percent");
932 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
935 list_for_each_entry(tf, &all_traces, list) {
936 if (tf->mpstat_gld == 0)
938 for (gld_i = tf->mpstat_gld[0]->min_seconds;
939 gld_i < tf->mpstat_gld[0]->stop_seconds; gld_i++) {
940 if (tf->mpstat_gld[gld_index]->data[gld_i].count) {
941 avg += (tf->mpstat_gld[gld_index]->data[gld_i].sum /
942 tf->mpstat_gld[gld_index]->data[gld_i].count);
945 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
946 tf->mpstat_gld[gld_index]->min_seconds;
947 color = pick_cpu_color();
948 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
949 svg_add_legend(plot, tf->label, " avg", color);
951 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
952 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
955 for (gld_i = gld->min_seconds;
956 gld_i < gld->stop_seconds; gld_i++) {
957 if (gld->data[i].count) {
958 this_avg += gld->data[i].sum /
963 this_avg /= gld->stop_seconds - gld->min_seconds;
965 for (gld_i = gld->min_seconds;
966 gld_i < gld->stop_seconds; gld_i++) {
969 if (gld->data[gld_i].count == 0)
971 val = (double)gld->data[gld_i].sum /
972 gld->data[gld_i].count;
974 if (this_avg > avg + 30 || val > 95) {
975 color = pick_cpu_color();
976 svg_line_graph(plot, gld, color, avg + 30, 95);
977 snprintf(line, line_len, " CPU %d\n", i - 1);
978 svg_add_legend(plot, tf->label, line, color);
987 if (plot->add_xlabel)
988 set_xlabel(plot, "Time (seconds)");
990 if (!plot->no_legend) {
991 svg_write_legend(plot);
992 svg_free_legend(plot);
995 total_graphs_written++;
998 static void plot_queue_depth(struct plot *plot, unsigned int min_seconds,
999 unsigned int max_seconds)
1001 struct trace_file *tf;
1003 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
1007 set_plot_label(plot, "Queue Depth");
1009 svg_alloc_legend(plot, num_traces);
1011 tf = list_entry(all_traces.next, struct trace_file, list);
1012 set_ylabel(plot, "Pending IO");
1013 set_yticks(plot, num_yticks, 0, tf->queue_depth_gld->max, "");
1014 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1016 list_for_each_entry(tf, &all_traces, list) {
1017 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
1019 svg_add_legend(plot, tf->label, "", tf->line_color);
1022 if (plot->add_xlabel)
1023 set_xlabel(plot, "Time (seconds)");
1025 svg_write_legend(plot);
1027 total_graphs_written++;
1030 static void convert_movie_files(char *movie_dir)
1032 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
1033 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
1038 static void mencode_movie(char *movie_dir)
1040 fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
1041 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
1042 "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
1047 static void tencode_movie(char *movie_dir)
1049 fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
1050 snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
1051 output_filename, movie_dir, output_filename);
1055 static void encode_movie(char *movie_dir)
1057 char *last_dot = strrchr(output_filename, '.');
1060 (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
1061 tencode_movie(movie_dir);
1063 mencode_movie(movie_dir);
1066 static void cleanup_movie(char *movie_dir)
1068 if (keep_movie_svgs) {
1069 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
1072 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
1073 snprintf(line, line_len, "rm %s/*", movie_dir);
1076 snprintf(line, line_len, "rmdir %s", movie_dir);
1080 static void plot_io_movie(struct plot *plot)
1082 struct trace_file *tf;
1083 char *movie_dir = create_movie_temp_dir();
1085 struct plot_history *history;
1088 int movie_frames_per_sec = 20;
1089 int total_frames = movie_len * movie_frames_per_sec;
1092 int graph_width_factor = 5;
1095 get_graph_size(&cols, &rows);
1096 batch_count = cols / total_frames;
1098 if (batch_count == 0)
1101 list_for_each_entry(tf, &all_traces, list) {
1108 strcpy(label, tf->label);
1112 pos = label + strlen(label);
1116 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
1117 set_plot_output(plot, line);
1118 set_plot_title(plot, graph_title);
1119 orig_y_offset = plot->start_y_offset;
1121 plot->no_legend = 1;
1123 set_graph_size(cols / graph_width_factor, rows / 8);
1124 plot->timeline = i / graph_width_factor;
1126 plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
1128 plot_cpu(plot, tf->max_seconds,
1129 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1131 plot->direction = PLOT_ACROSS;
1132 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
1134 /* movie graph starts here */
1135 plot->start_y_offset = orig_y_offset;
1136 set_graph_size(cols - cols / graph_width_factor, rows);
1137 plot->no_legend = 0;
1139 plot->direction = PLOT_DOWN;;
1141 if (movie_style == MOVIE_SPINDLE)
1142 setup_axis_spindle(plot);
1146 svg_alloc_legend(plot, count_io_plot_types() * 2);
1148 history = alloc_plot_history(tf);
1151 for (pid = 0; pid < tf->io_plots; pid++) {
1152 if (tf->gdd_reads[pid]) {
1154 strcpy(pos, tf->gdd_reads[pid]->label);
1155 svg_add_legend(plot, label, " Reads", tf->gdd_reads[pid]->color);
1157 if (tf->gdd_writes[pid]) {
1159 strcpy(pos, tf->gdd_writes[pid]->label);
1160 svg_add_legend(plot, label, " Writes", tf->gdd_writes[pid]->color);
1165 while (i < cols && batch_i < batch_count) {
1166 for (pid = 0; pid < tf->io_plots; pid++) {
1167 if (tf->gdd_reads[pid]) {
1168 svg_io_graph_movie(tf->gdd_reads[pid],
1169 history->read_pid_history[pid],
1172 if (tf->gdd_writes[pid]) {
1173 svg_io_graph_movie(tf->gdd_writes[pid],
1174 history->write_pid_history[pid],
1182 add_history(history, &movie_history);
1184 plot_movie_history(plot, &movie_history);
1186 svg_write_legend(plot);
1190 close_plot_file(plot);
1192 free_all_plot_history(&movie_history);
1194 convert_movie_files(movie_dir);
1195 encode_movie(movie_dir);
1196 cleanup_movie(movie_dir);
1200 static void plot_latency(struct plot *plot, unsigned int min_seconds,
1201 unsigned int max_seconds)
1203 struct trace_file *tf;
1208 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1212 svg_alloc_legend(plot, num_traces);
1214 list_for_each_entry(tf, &all_traces, list) {
1215 if (tf->latency_gld->max > max)
1216 max = tf->latency_gld->max;
1219 list_for_each_entry(tf, &all_traces, list)
1220 tf->latency_gld->max = max;
1223 set_plot_label(plot, "IO Latency");
1225 tf = list_entry(all_traces.next, struct trace_file, list);
1227 scale_line_graph_time(&max, &units);
1228 sprintf(line, "latency (%ss)", units);
1229 set_ylabel(plot, line);
1230 set_yticks(plot, num_yticks, 0, max, "");
1231 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1233 list_for_each_entry(tf, &all_traces, list) {
1234 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1236 svg_add_legend(plot, tf->label, "", tf->line_color);
1239 if (plot->add_xlabel)
1240 set_xlabel(plot, "Time (seconds)");
1242 svg_write_legend(plot);
1244 total_graphs_written++;
1247 static void plot_iops(struct plot *plot, unsigned int min_seconds,
1248 unsigned int max_seconds)
1250 struct trace_file *tf;
1254 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1257 list_for_each_entry(tf, &all_traces, list) {
1258 if (tf->iop_gld->max > max)
1259 max = tf->iop_gld->max;
1262 list_for_each_entry(tf, &all_traces, list)
1263 tf->iop_gld->max = max;
1266 set_plot_label(plot, "IOPs");
1268 svg_alloc_legend(plot, num_traces);
1270 tf = list_entry(all_traces.next, struct trace_file, list);
1272 scale_line_graph_bytes(&max, &units, 1000);
1273 set_ylabel(plot, "IO/s");
1275 set_yticks(plot, num_yticks, 0, max, units);
1276 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1278 list_for_each_entry(tf, &all_traces, list) {
1279 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1281 svg_add_legend(plot, tf->label, "", tf->line_color);
1284 if (plot->add_xlabel)
1285 set_xlabel(plot, "Time (seconds)");
1287 svg_write_legend(plot);
1290 total_graphs_written++;
1293 static void check_plot_columns(struct plot *plot, int index)
1297 if (columns > 1 && (total_graphs_written == 0 ||
1298 total_graphs_written % columns != 0)) {
1299 count = graphs_left(index);
1300 if (plot->direction == PLOT_DOWN) {
1301 plot->start_x_offset = 0;
1302 if (count <= columns)
1303 plot->add_xlabel = 1;
1305 plot->direction = PLOT_ACROSS;
1308 plot->direction = PLOT_DOWN;
1309 if (index == last_active_graph)
1310 plot->add_xlabel = 1;
1319 char *option_string = "F:T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:C:PK";
1320 static struct option long_options[] = {
1321 {"columns", required_argument, 0, 'c'},
1322 {"fio-trace", required_argument, 0, 'F'},
1323 {"title", required_argument, 0, 'T'},
1324 {"trace", required_argument, 0, 't'},
1325 {"output", required_argument, 0, 'o'},
1326 {"label", required_argument, 0, 'l'},
1327 {"rolling", required_argument, 0, 'r'},
1328 {"no-graph", required_argument, 0, 'N'},
1329 {"only-graph", required_argument, 0, 'O'},
1330 {"device", required_argument, 0, 'd'},
1331 {"blktrace-destination", required_argument, 0, 'D'},
1332 {"prog", required_argument, 0, 'p'},
1333 {"movie", optional_argument, 0, 'm'},
1334 {"codec", optional_argument, 0, 'C'},
1335 {"keep-movie-svgs", no_argument, 0, 'K'},
1336 {"width", required_argument, 0, 'w'},
1337 {"height", required_argument, 0, 'h'},
1338 {"xzoom", required_argument, 0, 'x'},
1339 {"yzoom", required_argument, 0, 'y'},
1340 {"io-plot-action", required_argument, 0, 'a'},
1341 {"per-process-io", no_argument, 0, 'P'},
1342 {"help", no_argument, 0, HELP_LONG_OPT},
1346 static void print_usage(void)
1348 fprintf(stderr, "iowatcher usage:\n"
1349 "\t-d (--device): device for blktrace to trace\n"
1350 "\t-D (--blktrace-destination): destination for blktrace\n"
1351 "\t-t (--trace): trace file name (more than one allowed)\n"
1352 "\t-F (--fio-trace): fio bandwidth trace (more than one allowed)\n"
1353 "\t-l (--label): trace label in the graph\n"
1354 "\t-o (--output): output file name (SVG only)\n"
1355 "\t-p (--prog): program to run while blktrace is run\n"
1356 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
1357 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1358 "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
1359 "\t-r (--rolling): number of seconds in the rolling averge\n"
1360 "\t-T (--title): graph title\n"
1361 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1362 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1363 "\t-O (--only-graph): add a single graph to the output\n"
1364 "\t-h (--height): set the height of each graph\n"
1365 "\t-w (--width): set the width of each graph\n"
1366 "\t-c (--columns): numbers of columns in graph output\n"
1367 "\t-x (--xzoom): limit processed time to min:max\n"
1368 "\t-y (--yzoom): limit processed sectors to min:max\n"
1369 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1370 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1375 static int parse_double_range(char *str, double *min, double *max)
1379 /* Empty lower bound - leave original value */
1380 if (str[0] != ':') {
1381 *min = strtod(str, &end);
1382 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1388 /* Empty upper bound - leave original value */
1390 *max = strtod(end+1, &end);
1391 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1401 static int parse_ull_range(char *str, unsigned long long *min,
1402 unsigned long long *max)
1406 /* Empty lower bound - leave original value */
1407 if (str[0] != ':') {
1408 *min = strtoull(str, &end, 10);
1409 if (*min == ULLONG_MAX && errno == ERANGE)
1415 /* Empty upper bound - leave original value */
1417 *max = strtoull(end+1, &end, 10);
1418 if (*max == ULLONG_MAX && errno == ERANGE)
1428 static int parse_options(int ac, char **av)
1434 // int this_option_optind = optind ? optind : 1;
1435 int option_index = 0;
1437 c = getopt_long(ac, av, option_string,
1438 long_options, &option_index);
1445 graph_title = strdup(optarg);
1448 add_trace_file(optarg);
1449 set_blktrace_outfile(optarg);
1452 add_fio_trace_file(optarg);
1455 output_filename = strdup(optarg);
1458 set_trace_label(optarg);
1461 set_rolling_avg(atoi(optarg));
1465 disable_all_graphs();
1468 enable_one_graph(optarg);
1471 disable_one_graph(optarg);
1474 if (num_blktrace_devices == MAX_DEVICES_PER_TRACE - 1) {
1475 fprintf(stderr, "Too many blktrace devices provided\n");
1478 blktrace_devices[num_blktrace_devices++] = strdup(optarg);
1481 blktrace_dest_dir = strdup(optarg);
1482 if (!strcmp(blktrace_dest_dir, "")) {
1483 fprintf(stderr, "Need a directory\n");
1488 program_to_run = strdup(optarg);
1491 keep_movie_svgs = 1;
1496 movie_style = lookup_movie_style(optarg);
1497 if (movie_style < 0) {
1498 fprintf(stderr, "Unknown movie style %s\n", optarg);
1502 fprintf(stderr, "Using movie style: %s\n",
1503 movie_styles[movie_style]);
1506 ffmpeg_codec = strdup(optarg);
1509 opt_graph_height = atoi(optarg);
1512 opt_graph_width = atoi(optarg);
1515 columns = atoi(optarg);
1518 if (parse_double_range(optarg, &min_time, &max_time)
1520 fprintf(stderr, "Cannot parse time range %s\n",
1526 if (parse_ull_range(optarg, &min_mb, &max_mb)
1529 "Cannot parse offset range %s\n",
1533 if (max_mb > ULLONG_MAX >> 20) {
1535 "Upper range limit too big."
1536 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1541 if (strlen(optarg) != 1) {
1543 fprintf(stderr, "Action must be one of Q, D, C.");
1546 plot_io_action = action_char_to_num(optarg[0]);
1547 if (plot_io_action < 0)
1564 static void dest_mkdir(char *dir)
1568 ret = mkdir(dir, 0777);
1569 if (ret && errno != EEXIST) {
1570 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1575 int main(int ac, char **av)
1578 unsigned int min_seconds = 0;
1579 unsigned int max_seconds = 0;
1581 u64 min_offset = ~(u64)0;
1582 struct trace_file *tf;
1586 init_io_hash_table();
1587 init_process_hash_table();
1589 enable_all_graphs();
1591 parse_options(ac, av);
1593 last_active_graph = last_graph();
1595 set_io_graph_scale(256);
1596 if (movie_style == MOVIE_SPINDLE)
1597 set_graph_size(750, 550);
1599 set_graph_size(700, 400);
1602 * the plots in the movie don't have a seconds
1603 * line yet, this makes us skip it
1605 last_active_graph = TOTAL_GRAPHS + 1;
1607 if (opt_graph_height)
1608 set_graph_height(opt_graph_height);
1610 if (opt_graph_width)
1611 set_graph_width(opt_graph_width);
1613 if (list_empty(&all_traces) && list_empty(&fio_traces)) {
1614 fprintf(stderr, "No traces found, exiting\n");
1618 if (num_blktrace_devices) {
1621 dest_mkdir(blktrace_dest_dir);
1622 if (num_blktrace_devices > 1) {
1623 snprintf(line, line_len, "%s/%s", blktrace_dest_dir,
1628 path = join_path(blktrace_dest_dir, blktrace_outfile);
1630 snprintf(line, line_len, "%s.dump", path);
1633 ret = start_blktrace(blktrace_devices, num_blktrace_devices,
1634 blktrace_outfile, blktrace_dest_dir);
1636 fprintf(stderr, "exiting due to blktrace failure\n");
1641 if (program_to_run) {
1642 ret = run_program(program_to_run);
1644 fprintf(stderr, "failed to run %s\n",
1650 /* no program specified, just wait for
1658 /* step one, read all the traces */
1661 /* step two, find the maxes for time and offset */
1662 list_for_each_entry(tf, &all_traces, list)
1663 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1664 list_for_each_entry(tf, &fio_traces, list)
1665 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1666 min_seconds = min_time;
1667 if (max_seconds > max_time)
1668 max_seconds = ceil(max_time);
1669 if (min_offset < min_mb << 20)
1670 min_offset = min_mb << 20;
1671 if (max_offset > max_mb << 20)
1672 max_offset = max_mb << 20;
1674 /* push the max we found into all the tfs */
1675 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1677 /* alloc graphing structs for all the traces */
1678 setup_trace_file_graphs();
1680 /* run through all the traces and read their events */
1681 read_trace_events();
1683 pick_line_graph_color();
1685 plot = alloc_plot();
1688 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1689 plot_io_movie(plot);
1693 set_plot_output(plot, output_filename);
1695 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1696 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1697 else if (num_traces >= 1 || num_fio_traces >= 1)
1698 set_legend_width(longest_label);
1700 set_legend_width(0);
1702 get_graph_size(&cols, &rows);
1704 plot->add_xlabel = 1;
1705 set_plot_title(plot, graph_title);
1707 check_plot_columns(plot, IO_GRAPH_INDEX);
1708 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1709 plot->add_xlabel = 0;
1712 set_graph_size(cols / columns, rows);
1713 num_xticks /= columns;
1720 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1721 plot_tput(plot, min_seconds, max_seconds, 1);
1723 check_plot_columns(plot, FIO_GRAPH_INDEX);
1724 plot_fio_tput(plot, min_seconds, max_seconds);
1726 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1727 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1728 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1730 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1731 plot_cpu(plot, max_seconds, "CPU System Time",
1732 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1734 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1735 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1736 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1738 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1739 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1740 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1742 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1743 plot_cpu(plot, max_seconds, "CPU User Time",
1744 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1746 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1747 plot_latency(plot, min_seconds, max_seconds);
1749 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1750 plot_queue_depth(plot, min_seconds, max_seconds);
1752 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1753 plot_iops(plot, min_seconds, max_seconds);
1757 close_plot_file(plot);