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 if (num_blktrace_devices)
332 path = join_path(blktrace_dest_dir, tf->filename);
334 path = strdup(tf->filename);
336 trace = open_trace(path);
340 last_time = find_last_time(trace);
342 tf->max_seconds = SECONDS(last_time) + 1;
343 tf->stop_seconds = SECONDS(last_time) + 1;
345 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
346 &max_bank, &max_bank_offset);
347 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
348 tf->min_offset = ymin;
349 tf->max_offset = ymax;
351 read_mpstat(trace, path);
352 tf->mpstat_stop_seconds = trace->mpstat_seconds;
353 tf->mpstat_max_seconds = trace->mpstat_seconds;
354 if (tf->mpstat_max_seconds)
360 list_for_each_entry(tf, &fio_traces, list) {
361 trace = open_fio_trace(tf->filename);
365 tf->max_seconds = tf->trace->fio_seconds;
366 tf->stop_seconds = tf->trace->fio_seconds;
370 static void pick_line_graph_color(void)
372 struct trace_file *tf;
375 list_for_each_entry(tf, &all_traces, list) {
376 for (i = 0; i < tf->io_plots; i++) {
377 if (tf->gdd_reads[i]) {
378 tf->line_color = tf->gdd_reads[i]->color;
379 tf->reads_color = tf->gdd_reads[i]->color;
381 if (tf->gdd_writes[i]) {
382 tf->line_color = tf->gdd_writes[i]->color;
383 tf->writes_color = tf->gdd_writes[i]->color;
385 if (tf->writes_color && tf->reads_color)
388 if (!tf->reads_color)
389 tf->reads_color = tf->line_color;
390 if (!tf->writes_color)
391 tf->writes_color = tf->line_color;
395 static void read_fio_events(struct trace_file *tf)
402 first_fio(tf->trace);
404 ret = read_fio_event(tf->trace, &time, &bw, &dir);
409 add_fio_gld(time, bw, tf->fio_gld);
410 if (next_fio_line(tf->trace))
415 static void read_trace_events(void)
418 struct trace_file *tf;
423 double user, sys, iowait, irq, soft;
424 double max_user = 0, max_sys = 0, max_iowait = 0,
425 max_irq = 0, max_soft = 0;
427 list_for_each_entry(tf, &fio_traces, list)
430 list_for_each_entry(tf, &all_traces, list) {
436 if (SECONDS(get_record_time(trace)) > tf->max_seconds)
438 add_tput(trace, tf->tput_writes_gld, tf->tput_reads_gld);
439 add_iop(trace, tf->iop_gld);
441 add_pending_io(trace, tf->queue_depth_gld);
442 add_completed_io(trace, tf->latency_gld);
443 } while (!(ret = next_record(trace)));
445 list_for_each_entry(tf, &all_traces, list) {
448 if (trace->mpstat_num_cpus == 0)
453 for (time = 0; time < tf->mpstat_stop_seconds; time++) {
454 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
455 ret = read_mpstat_event(trace, &user, &sys,
456 &iowait, &irq, &soft);
459 if (next_mpstat_line(trace))
468 if (iowait > max_iowait)
471 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
472 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
473 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
474 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
475 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
477 if (next_mpstat(trace) == NULL)
483 list_for_each_entry(tf, &all_traces, list) {
486 if (trace->mpstat_num_cpus == 0)
489 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
490 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
491 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
492 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
493 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
498 static void set_trace_label(char *label)
501 struct trace_file *tf;
502 int len = strlen(label);
504 if (len > longest_label)
507 list_for_each_entry(tf, &all_traces, list) {
508 if (cur == label_index) {
509 tf->label = strdup(label);
516 list_for_each_entry(tf, &fio_traces, list) {
517 if (cur == label_index) {
518 tf->label = strdup(label);
526 static void set_blktrace_outfile(char *arg)
528 char *s = strdup(arg);
529 char *last_dot = strrchr(s, '.');
532 if (strcmp(last_dot, ".dump") == 0)
535 blktrace_outfile = s;
539 static void compare_minmax_tf(struct trace_file *tf, unsigned int *max_seconds,
540 u64 *min_offset, u64 *max_offset)
542 if (tf->max_seconds > *max_seconds)
543 *max_seconds = tf->max_seconds;
544 if (tf->max_offset > *max_offset)
545 *max_offset = tf->max_offset;
546 if (tf->min_offset < *min_offset)
547 *min_offset = tf->min_offset;
550 static void set_all_minmax_tf(unsigned int min_seconds,
551 unsigned int max_seconds,
552 u64 min_offset, u64 max_offset)
554 struct trace_file *tf;
555 struct list_head *traces = &all_traces;
557 list_for_each_entry(tf, traces, list) {
558 tf->min_seconds = min_seconds;
559 tf->max_seconds = max_seconds;
560 if (tf->stop_seconds > max_seconds)
561 tf->stop_seconds = max_seconds;
562 if (tf->mpstat_max_seconds) {
563 tf->mpstat_min_seconds = min_seconds;
564 tf->mpstat_max_seconds = max_seconds;
565 if (tf->mpstat_stop_seconds > max_seconds)
566 tf->mpstat_stop_seconds = max_seconds;
568 tf->min_offset = min_offset;
569 tf->max_offset = max_offset;
571 if (traces == &all_traces) {
572 traces = &fio_traces;
577 static struct pid_plot_history *alloc_pid_plot_history(char *color)
579 struct pid_plot_history *pph;
581 pph = calloc(1, sizeof(struct pid_plot_history));
583 perror("memory allocation failed");
586 pph->history = malloc(sizeof(double) * 4096);
588 perror("memory allocation failed");
591 pph->history_len = 4096;
597 static struct plot_history *alloc_plot_history(struct trace_file *tf)
599 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
603 perror("memory allocation failed");
606 ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
607 if (!ph->read_pid_history) {
608 perror("memory allocation failed");
611 ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
612 if (!ph->write_pid_history) {
613 perror("memory allocation failed");
616 ph->pid_history_count = tf->io_plots;
617 for (i = 0; i < tf->io_plots; i++) {
618 if (tf->gdd_reads[i])
619 ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
620 if (tf->gdd_writes[i])
621 ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
626 LIST_HEAD(movie_history);
627 int num_histories = 0;
629 static void free_plot_history(struct plot_history *ph)
633 for (pid = 0; pid < ph->pid_history_count; pid++) {
634 if (ph->read_pid_history[pid])
635 free(ph->read_pid_history[pid]);
636 if (ph->write_pid_history[pid])
637 free(ph->write_pid_history[pid]);
639 free(ph->read_pid_history);
640 free(ph->write_pid_history);
644 static void add_history(struct plot_history *ph, struct list_head *list)
646 struct plot_history *entry;
648 list_add_tail(&ph->list, list);
651 if (num_histories > 12) {
653 entry = list_entry(list->next, struct plot_history, list);
654 list_del(&entry->list);
655 free_plot_history(entry);
659 static void plot_movie_history(struct plot *plot, struct list_head *list)
661 struct plot_history *ph;
664 if (num_histories > 2)
665 rewind_spindle_steps(num_histories - 1);
667 list_for_each_entry(ph, list, list) {
668 for (pid = 0; pid < ph->pid_history_count; pid++) {
669 if (ph->read_pid_history[pid]) {
670 if (movie_style == MOVIE_SPINDLE) {
671 svg_io_graph_movie_array_spindle(plot,
672 ph->read_pid_history[pid]);
674 svg_io_graph_movie_array(plot,
675 ph->read_pid_history[pid]);
678 if (ph->write_pid_history[pid]) {
679 if (movie_style == MOVIE_SPINDLE) {
680 svg_io_graph_movie_array_spindle(plot,
681 ph->write_pid_history[pid]);
683 svg_io_graph_movie_array(plot,
684 ph->write_pid_history[pid]);
691 static void free_all_plot_history(struct list_head *head)
693 struct plot_history *entry;
695 while (!list_empty(head)) {
696 entry = list_entry(head->next, struct plot_history, list);
697 list_del(&entry->list);
698 free_plot_history(entry);
702 static int count_io_plot_types(void)
704 struct trace_file *tf;
706 int total_io_types = 0;
708 list_for_each_entry(tf, &all_traces, list) {
709 for (i = 0; i < tf->io_plots; i++) {
710 if (tf->gdd_reads[i])
712 if (tf->gdd_writes[i])
716 return total_io_types;
719 static void plot_io_legend(struct plot *plot, struct graph_dot_data *gdd, char *prefix, char *rw)
724 ret = asprintf(&label, "%s %s", prefix, gdd->label);
726 ret = asprintf(&label, "%s", prefix);
728 perror("Failed to process labels");
731 svg_add_legend(plot, label, rw, gdd->color);
735 static void plot_io(struct plot *plot, unsigned int min_seconds,
736 unsigned int max_seconds, u64 min_offset, u64 max_offset)
738 struct trace_file *tf;
741 if (active_graphs[IO_GRAPH_INDEX] == 0)
746 svg_alloc_legend(plot, count_io_plot_types() * 2);
748 set_plot_label(plot, "Device IO");
749 set_ylabel(plot, "Offset (MB)");
750 set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
751 max_offset / (1024 * 1024), "");
752 set_xticks(plot, num_xticks, min_seconds, max_seconds);
754 list_for_each_entry(tf, &all_traces, list) {
755 char *prefix = tf->label ? tf->label : "";
757 for (i = 0; i < tf->io_plots; i++) {
758 if (tf->gdd_writes[i]) {
759 svg_io_graph(plot, tf->gdd_writes[i]);
760 plot_io_legend(plot, tf->gdd_writes[i], prefix, " Writes");
762 if (tf->gdd_reads[i]) {
763 svg_io_graph(plot, tf->gdd_reads[i]);
764 plot_io_legend(plot, tf->gdd_reads[i], prefix, " Reads");
768 if (plot->add_xlabel)
769 set_xlabel(plot, "Time (seconds)");
770 svg_write_legend(plot);
774 static void plot_tput(struct plot *plot, unsigned int min_seconds,
775 unsigned int max_seconds, int with_legend)
777 struct trace_file *tf;
782 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
786 svg_alloc_legend(plot, num_traces * 2);
788 list_for_each_entry(tf, &all_traces, list) {
789 val = line_graph_roll_avg_max(tf->tput_writes_gld);
792 val = line_graph_roll_avg_max(tf->tput_reads_gld);
796 list_for_each_entry(tf, &all_traces, list) {
797 if (tf->tput_writes_gld->max > 0)
798 tf->tput_writes_gld->max = max;
799 if (tf->tput_reads_gld->max > 0)
800 tf->tput_reads_gld->max = max;
804 set_plot_label(plot, "Throughput");
806 tf = list_entry(all_traces.next, struct trace_file, list);
808 scale_line_graph_bytes(&max, &units, 1024);
809 sprintf(line, "%sB/s", units);
810 set_ylabel(plot, line);
811 set_yticks(plot, num_yticks, 0, max, "");
812 set_xticks(plot, num_xticks, min_seconds, max_seconds);
814 list_for_each_entry(tf, &all_traces, list) {
815 if (tf->tput_writes_gld->max > 0) {
816 svg_line_graph(plot, tf->tput_writes_gld, tf->writes_color, 0, 0);
818 svg_add_legend(plot, tf->label, " Writes", tf->writes_color);
820 if (tf->tput_reads_gld->max > 0) {
821 svg_line_graph(plot, tf->tput_reads_gld, tf->reads_color, 0, 0);
823 svg_add_legend(plot, tf->label, " Reads", tf->reads_color);
827 if (plot->add_xlabel)
828 set_xlabel(plot, "Time (seconds)");
831 svg_write_legend(plot);
834 total_graphs_written++;
837 static void plot_fio_tput(struct plot *plot,
838 unsigned int min_seconds, unsigned int max_seconds)
840 struct trace_file *tf;
845 if (num_fio_traces == 0 || active_graphs[FIO_GRAPH_INDEX] == 0)
848 if (num_fio_traces > 1)
849 svg_alloc_legend(plot, num_fio_traces);
851 list_for_each_entry(tf, &fio_traces, list) {
852 val = line_graph_roll_avg_max(tf->fio_gld);
857 list_for_each_entry(tf, &fio_traces, list) {
858 if (tf->fio_gld->max > 0)
859 tf->fio_gld->max = max;
863 set_plot_label(plot, "Fio Throughput");
865 tf = list_entry(all_traces.next, struct trace_file, list);
867 scale_line_graph_bytes(&max, &units, 1024);
868 sprintf(line, "%sB/s", units);
869 set_ylabel(plot, line);
870 set_yticks(plot, num_yticks, 0, max, "");
872 set_xticks(plot, num_xticks, min_seconds, max_seconds);
873 list_for_each_entry(tf, &fio_traces, list) {
874 if (tf->fio_gld->max > 0) {
875 svg_line_graph(plot, tf->fio_gld, tf->line_color, 0, 0);
876 if (num_fio_traces > 1)
877 svg_add_legend(plot, tf->label, "", tf->line_color);
881 if (plot->add_xlabel)
882 set_xlabel(plot, "Time (seconds)");
884 if (num_fio_traces > 1)
885 svg_write_legend(plot);
887 total_graphs_written++;
890 static void plot_cpu(struct plot *plot, unsigned int max_seconds, char *label,
891 int active_index, int gld_index)
893 struct trace_file *tf;
902 if (active_graphs[active_index] == 0)
905 list_for_each_entry(tf, &all_traces, list) {
906 if (tf->trace->mpstat_num_cpus > max)
907 max = tf->trace->mpstat_num_cpus;
912 tf = list_entry(all_traces.next, struct trace_file, list);
914 ymax = tf->mpstat_gld[gld_index]->max;
918 svg_alloc_legend(plot, num_traces * max);
921 set_plot_label(plot, label);
923 max_seconds = tf->mpstat_max_seconds;
925 set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
926 set_ylabel(plot, "Percent");
927 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
930 list_for_each_entry(tf, &all_traces, list) {
931 if (tf->mpstat_gld == 0)
933 for (gld_i = tf->mpstat_gld[0]->min_seconds;
934 gld_i < tf->mpstat_gld[0]->stop_seconds; gld_i++) {
935 if (tf->mpstat_gld[gld_index]->data[gld_i].count) {
936 avg += (tf->mpstat_gld[gld_index]->data[gld_i].sum /
937 tf->mpstat_gld[gld_index]->data[gld_i].count);
940 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
941 tf->mpstat_gld[gld_index]->min_seconds;
942 color = pick_cpu_color();
943 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
944 svg_add_legend(plot, tf->label, " avg", color);
946 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
947 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
950 for (gld_i = gld->min_seconds;
951 gld_i < gld->stop_seconds; gld_i++) {
952 if (gld->data[i].count) {
953 this_avg += gld->data[i].sum /
958 this_avg /= gld->stop_seconds - gld->min_seconds;
960 for (gld_i = gld->min_seconds;
961 gld_i < gld->stop_seconds; gld_i++) {
964 if (gld->data[gld_i].count == 0)
966 val = (double)gld->data[gld_i].sum /
967 gld->data[gld_i].count;
969 if (this_avg > avg + 30 || val > 95) {
970 color = pick_cpu_color();
971 svg_line_graph(plot, gld, color, avg + 30, 95);
972 snprintf(line, line_len, " CPU %d\n", i - 1);
973 svg_add_legend(plot, tf->label, line, color);
982 if (plot->add_xlabel)
983 set_xlabel(plot, "Time (seconds)");
985 if (!plot->no_legend) {
986 svg_write_legend(plot);
987 svg_free_legend(plot);
990 total_graphs_written++;
993 static void plot_queue_depth(struct plot *plot, unsigned int min_seconds,
994 unsigned int max_seconds)
996 struct trace_file *tf;
999 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
1003 set_plot_label(plot, "Queue Depth");
1005 svg_alloc_legend(plot, num_traces);
1007 list_for_each_entry(tf, &all_traces, list) {
1008 val = line_graph_roll_avg_max(tf->queue_depth_gld);
1013 list_for_each_entry(tf, &all_traces, list)
1014 tf->queue_depth_gld->max = max;
1016 set_ylabel(plot, "Pending IO");
1017 set_yticks(plot, num_yticks, 0, max, "");
1018 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1020 list_for_each_entry(tf, &all_traces, list) {
1021 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
1023 svg_add_legend(plot, tf->label, "", tf->line_color);
1026 if (plot->add_xlabel)
1027 set_xlabel(plot, "Time (seconds)");
1029 svg_write_legend(plot);
1031 total_graphs_written++;
1034 static void system_check(const char *cmd)
1036 if (system(cmd) < 0) {
1039 fprintf(stderr, "system exec failed (%d): %s\n", err, cmd);
1044 static void convert_movie_files(char *movie_dir)
1046 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
1047 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
1052 static void mencode_movie(char *movie_dir)
1054 fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
1055 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
1056 "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
1061 static void tencode_movie(char *movie_dir)
1063 fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
1064 snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
1065 output_filename, movie_dir, output_filename);
1069 static void encode_movie(char *movie_dir)
1071 char *last_dot = strrchr(output_filename, '.');
1074 (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
1075 tencode_movie(movie_dir);
1077 mencode_movie(movie_dir);
1080 static void cleanup_movie(char *movie_dir)
1082 if (keep_movie_svgs) {
1083 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
1086 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
1087 snprintf(line, line_len, "rm %s/*", movie_dir);
1090 snprintf(line, line_len, "rmdir %s", movie_dir);
1094 static void plot_io_movie(struct plot *plot)
1096 struct trace_file *tf;
1098 struct plot_history *history;
1101 int movie_frames_per_sec = 20;
1102 int total_frames = movie_len * movie_frames_per_sec;
1105 int graph_width_factor = 5;
1107 char movie_dir[] = "io-movie-XXXXXX";
1109 if (mkdtemp(movie_dir) == NULL) {
1110 perror("Unable to create temp directory for movie files");
1114 get_graph_size(&cols, &rows);
1115 batch_count = cols / total_frames;
1117 if (batch_count == 0)
1120 list_for_each_entry(tf, &all_traces, list) {
1121 char *prefix = tf->label ? tf->label : "";
1125 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
1126 set_plot_output(plot, line);
1127 set_plot_title(plot, graph_title);
1128 orig_y_offset = plot->start_y_offset;
1130 plot->no_legend = 1;
1132 set_graph_size(cols / graph_width_factor, rows / 8);
1133 plot->timeline = i / graph_width_factor;
1135 plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
1137 plot_cpu(plot, tf->max_seconds,
1138 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1140 plot->direction = PLOT_ACROSS;
1141 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
1143 /* movie graph starts here */
1144 plot->start_y_offset = orig_y_offset;
1145 set_graph_size(cols - cols / graph_width_factor, rows);
1146 plot->no_legend = 0;
1148 plot->direction = PLOT_DOWN;;
1150 if (movie_style == MOVIE_SPINDLE)
1151 setup_axis_spindle(plot);
1155 svg_alloc_legend(plot, count_io_plot_types() * 2);
1157 history = alloc_plot_history(tf);
1160 for (pid = 0; pid < tf->io_plots; pid++) {
1161 if (tf->gdd_reads[pid])
1162 plot_io_legend(plot, tf->gdd_reads[pid], prefix, " Reads");
1163 if (tf->gdd_writes[pid])
1164 plot_io_legend(plot, tf->gdd_writes[pid], prefix, " Writes");
1168 while (i < cols && batch_i < batch_count) {
1169 for (pid = 0; pid < tf->io_plots; pid++) {
1170 if (tf->gdd_reads[pid]) {
1171 svg_io_graph_movie(tf->gdd_reads[pid],
1172 history->read_pid_history[pid],
1175 if (tf->gdd_writes[pid]) {
1176 svg_io_graph_movie(tf->gdd_writes[pid],
1177 history->write_pid_history[pid],
1185 add_history(history, &movie_history);
1187 plot_movie_history(plot, &movie_history);
1189 svg_write_legend(plot);
1193 close_plot_file(plot);
1195 free_all_plot_history(&movie_history);
1197 convert_movie_files(movie_dir);
1198 encode_movie(movie_dir);
1199 cleanup_movie(movie_dir);
1202 static void plot_latency(struct plot *plot, unsigned int min_seconds,
1203 unsigned int max_seconds)
1205 struct trace_file *tf;
1210 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1214 svg_alloc_legend(plot, num_traces);
1216 list_for_each_entry(tf, &all_traces, list) {
1217 val = line_graph_roll_avg_max(tf->latency_gld);
1222 list_for_each_entry(tf, &all_traces, list)
1223 tf->latency_gld->max = max;
1226 set_plot_label(plot, "IO Latency");
1228 tf = list_entry(all_traces.next, struct trace_file, list);
1230 scale_line_graph_time(&max, &units);
1231 sprintf(line, "latency (%ss)", units);
1232 set_ylabel(plot, line);
1233 set_yticks(plot, num_yticks, 0, max, "");
1234 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1236 list_for_each_entry(tf, &all_traces, list) {
1237 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1239 svg_add_legend(plot, tf->label, "", tf->line_color);
1242 if (plot->add_xlabel)
1243 set_xlabel(plot, "Time (seconds)");
1245 svg_write_legend(plot);
1247 total_graphs_written++;
1250 static void plot_iops(struct plot *plot, unsigned int min_seconds,
1251 unsigned int max_seconds)
1253 struct trace_file *tf;
1257 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1260 list_for_each_entry(tf, &all_traces, list) {
1261 val = line_graph_roll_avg_max(tf->iop_gld);
1266 list_for_each_entry(tf, &all_traces, list)
1267 tf->iop_gld->max = max;
1270 set_plot_label(plot, "IOPs");
1272 svg_alloc_legend(plot, num_traces);
1274 tf = list_entry(all_traces.next, struct trace_file, list);
1276 scale_line_graph_bytes(&max, &units, 1000);
1277 set_ylabel(plot, "IO/s");
1279 set_yticks(plot, num_yticks, 0, max, units);
1280 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1282 list_for_each_entry(tf, &all_traces, list) {
1283 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1285 svg_add_legend(plot, tf->label, "", tf->line_color);
1288 if (plot->add_xlabel)
1289 set_xlabel(plot, "Time (seconds)");
1291 svg_write_legend(plot);
1294 total_graphs_written++;
1297 static void check_plot_columns(struct plot *plot, int index)
1301 if (columns > 1 && (total_graphs_written == 0 ||
1302 total_graphs_written % columns != 0)) {
1303 count = graphs_left(index);
1304 if (plot->direction == PLOT_DOWN) {
1305 plot->start_x_offset = 0;
1306 if (count <= columns)
1307 plot->add_xlabel = 1;
1309 plot->direction = PLOT_ACROSS;
1312 plot->direction = PLOT_DOWN;
1313 if (index == last_active_graph)
1314 plot->add_xlabel = 1;
1323 char *option_string = "+F:T:t:o:l:r:O:N:d:D:pm::h:w:c:x:y:a:C:PK";
1324 static struct option long_options[] = {
1325 {"columns", required_argument, 0, 'c'},
1326 {"fio-trace", required_argument, 0, 'F'},
1327 {"title", required_argument, 0, 'T'},
1328 {"trace", required_argument, 0, 't'},
1329 {"output", required_argument, 0, 'o'},
1330 {"label", required_argument, 0, 'l'},
1331 {"rolling", required_argument, 0, 'r'},
1332 {"no-graph", required_argument, 0, 'N'},
1333 {"only-graph", required_argument, 0, 'O'},
1334 {"device", required_argument, 0, 'd'},
1335 {"blktrace-destination", required_argument, 0, 'D'},
1336 {"prog", no_argument, 0, 'p'},
1337 {"movie", optional_argument, 0, 'm'},
1338 {"codec", optional_argument, 0, 'C'},
1339 {"keep-movie-svgs", no_argument, 0, 'K'},
1340 {"width", required_argument, 0, 'w'},
1341 {"height", required_argument, 0, 'h'},
1342 {"xzoom", required_argument, 0, 'x'},
1343 {"yzoom", required_argument, 0, 'y'},
1344 {"io-plot-action", required_argument, 0, 'a'},
1345 {"per-process-io", no_argument, 0, 'P'},
1346 {"help", no_argument, 0, HELP_LONG_OPT},
1350 static void print_usage(void)
1352 fprintf(stderr, "iowatcher usage:\n"
1353 "\t-d (--device): device for blktrace to trace\n"
1354 "\t-D (--blktrace-destination): destination for blktrace\n"
1355 "\t-t (--trace): trace file name (more than one allowed)\n"
1356 "\t-F (--fio-trace): fio bandwidth trace (more than one allowed)\n"
1357 "\t-l (--label): trace label in the graph\n"
1358 "\t-o (--output): output file name for the SVG image or video\n"
1359 "\t-p (--prog): run a program while blktrace is run\n"
1360 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
1361 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1362 "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
1363 "\t-r (--rolling): number of seconds in the rolling averge\n"
1364 "\t-T (--title): graph title\n"
1365 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue-depth, \n"
1366 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1367 "\t-O (--only-graph): add a single graph to the output\n"
1368 "\t-h (--height): set the height of each graph\n"
1369 "\t-w (--width): set the width of each graph\n"
1370 "\t-c (--columns): numbers of columns in graph output\n"
1371 "\t-x (--xzoom): limit processed time to min:max\n"
1372 "\t-y (--yzoom): limit processed sectors to min:max\n"
1373 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1374 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1379 static int parse_double_range(char *str, double *min, double *max)
1383 /* Empty lower bound - leave original value */
1384 if (str[0] != ':') {
1385 *min = strtod(str, &end);
1386 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1392 /* Empty upper bound - leave original value */
1394 *max = strtod(end+1, &end);
1395 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1405 static int parse_ull_range(char *str, unsigned long long *min,
1406 unsigned long long *max)
1410 /* Empty lower bound - leave original value */
1411 if (str[0] != ':') {
1412 *min = strtoull(str, &end, 10);
1413 if (*min == ULLONG_MAX && errno == ERANGE)
1419 /* Empty upper bound - leave original value */
1421 *max = strtoull(end+1, &end, 10);
1422 if (*max == ULLONG_MAX && errno == ERANGE)
1432 static int parse_options(int ac, char **av)
1439 int option_index = 0;
1441 c = getopt_long(ac, av, option_string,
1442 long_options, &option_index);
1449 graph_title = strdup(optarg);
1452 add_trace_file(optarg);
1453 set_blktrace_outfile(optarg);
1456 add_fio_trace_file(optarg);
1459 output_filename = strdup(optarg);
1462 set_trace_label(optarg);
1465 set_rolling_avg(atoi(optarg));
1469 disable_all_graphs();
1472 enable_one_graph(optarg);
1475 disable_one_graph(optarg);
1478 if (num_blktrace_devices == MAX_DEVICES_PER_TRACE - 1) {
1479 fprintf(stderr, "Too many blktrace devices provided\n");
1482 blktrace_devices[num_blktrace_devices++] = strdup(optarg);
1485 blktrace_dest_dir = strdup(optarg);
1486 if (!strcmp(blktrace_dest_dir, "")) {
1487 fprintf(stderr, "Need a directory\n");
1495 keep_movie_svgs = 1;
1500 movie_style = lookup_movie_style(optarg);
1501 if (movie_style < 0) {
1502 fprintf(stderr, "Unknown movie style %s\n", optarg);
1506 fprintf(stderr, "Using movie style: %s\n",
1507 movie_styles[movie_style]);
1510 ffmpeg_codec = strdup(optarg);
1513 opt_graph_height = atoi(optarg);
1516 opt_graph_width = atoi(optarg);
1519 columns = atoi(optarg);
1522 if (parse_double_range(optarg, &min_time, &max_time)
1524 fprintf(stderr, "Cannot parse time range %s\n",
1530 if (parse_ull_range(optarg, &min_mb, &max_mb)
1533 "Cannot parse offset range %s\n",
1537 if (max_mb > ULLONG_MAX >> 20) {
1539 "Upper range limit too big."
1540 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1545 if (strlen(optarg) != 1) {
1547 fprintf(stderr, "Action must be one of Q, D, C.");
1550 plot_io_action = action_char_to_num(optarg[0]);
1551 if (plot_io_action < 0)
1566 if (optind < ac && p_flagged) {
1567 prog_argv = &av[optind];
1568 prog_argc = ac - optind;
1569 } else if (p_flagged) {
1570 fprintf(stderr, "--prog or -p given but no program specified\n");
1572 } else if (optind < ac) {
1573 fprintf(stderr, "Extra arguments '%s'... (and --prog not specified)\n", av[optind]);
1580 static void dest_mkdir(char *dir)
1584 ret = mkdir(dir, 0777);
1585 if (ret && errno != EEXIST) {
1586 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1591 int main(int ac, char **av)
1594 unsigned int min_seconds = 0;
1595 unsigned int max_seconds = 0;
1597 u64 min_offset = ~(u64)0;
1598 struct trace_file *tf;
1602 init_io_hash_table();
1603 init_process_hash_table();
1605 enable_all_graphs();
1607 parse_options(ac, av);
1609 last_active_graph = last_graph();
1611 set_io_graph_scale(256);
1612 if (movie_style == MOVIE_SPINDLE)
1613 set_graph_size(750, 550);
1615 set_graph_size(700, 400);
1618 * the plots in the movie don't have a seconds
1619 * line yet, this makes us skip it
1621 last_active_graph = TOTAL_GRAPHS + 1;
1623 if (opt_graph_height)
1624 set_graph_height(opt_graph_height);
1626 if (opt_graph_width)
1627 set_graph_width(opt_graph_width);
1629 if (list_empty(&all_traces) && list_empty(&fio_traces)) {
1630 fprintf(stderr, "No traces found, exiting\n");
1634 if (num_blktrace_devices) {
1635 char *path = join_path(blktrace_dest_dir, blktrace_outfile);
1636 dest_mkdir(blktrace_dest_dir);
1639 snprintf(line, line_len, "%s.dump", path);
1642 ret = start_blktrace(blktrace_devices, num_blktrace_devices,
1643 blktrace_outfile, blktrace_dest_dir);
1645 perror("Exiting due to blktrace failure");
1649 snprintf(line, line_len, "%s.mpstat", path);
1650 ret = start_mpstat(line);
1652 perror("Exiting due to mpstat failure");
1656 if (prog_argv && prog_argc) {
1657 run_program(prog_argc, prog_argv, 1, NULL, NULL);
1658 wait_for_tracers(SIGINT);
1660 printf("Tracing until interrupted...\n");
1661 wait_for_tracers(0);
1666 /* step one, read all the traces */
1669 /* step two, find the maxes for time and offset */
1670 list_for_each_entry(tf, &all_traces, list)
1671 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1672 list_for_each_entry(tf, &fio_traces, list)
1673 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1674 min_seconds = min_time;
1675 if (max_seconds > max_time)
1676 max_seconds = ceil(max_time);
1677 if (min_offset < min_mb << 20)
1678 min_offset = min_mb << 20;
1679 if (max_offset > max_mb << 20)
1680 max_offset = max_mb << 20;
1682 /* push the max we found into all the tfs */
1683 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1685 /* alloc graphing structs for all the traces */
1686 setup_trace_file_graphs();
1688 /* run through all the traces and read their events */
1689 read_trace_events();
1691 pick_line_graph_color();
1693 plot = alloc_plot();
1696 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1697 plot_io_movie(plot);
1701 set_plot_output(plot, output_filename);
1703 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1704 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1705 else if (num_traces >= 1 || num_fio_traces >= 1)
1706 set_legend_width(longest_label);
1708 set_legend_width(0);
1710 get_graph_size(&cols, &rows);
1712 plot->add_xlabel = 1;
1713 set_plot_title(plot, graph_title);
1715 check_plot_columns(plot, IO_GRAPH_INDEX);
1716 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1717 plot->add_xlabel = 0;
1720 set_graph_size(cols / columns, rows);
1721 num_xticks /= columns;
1728 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1729 plot_tput(plot, min_seconds, max_seconds, 1);
1731 check_plot_columns(plot, FIO_GRAPH_INDEX);
1732 plot_fio_tput(plot, min_seconds, max_seconds);
1734 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1735 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1736 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1738 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1739 plot_cpu(plot, max_seconds, "CPU System Time",
1740 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1742 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1743 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1744 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1746 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1747 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1748 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1750 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1751 plot_cpu(plot, max_seconds, "CPU User Time",
1752 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1754 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1755 plot_latency(plot, min_seconds, max_seconds);
1757 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1758 plot_queue_depth(plot, min_seconds, max_seconds);
1760 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1761 plot_iops(plot, min_seconds, max_seconds);
1765 close_plot_file(plot);