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 long nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1051 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
1052 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P %ld rsvg-convert -o {}.png {}",
1053 movie_dir, nr_cpus);
1057 static void mencode_movie(char *movie_dir)
1059 fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
1060 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
1061 "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
1066 static void tencode_movie(char *movie_dir)
1068 fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
1069 snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
1070 output_filename, movie_dir, output_filename);
1074 static void encode_movie(char *movie_dir)
1076 char *last_dot = strrchr(output_filename, '.');
1079 (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
1080 tencode_movie(movie_dir);
1082 mencode_movie(movie_dir);
1085 static void cleanup_movie(char *movie_dir)
1087 if (keep_movie_svgs) {
1088 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
1091 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
1092 snprintf(line, line_len, "rm %s/*", movie_dir);
1095 snprintf(line, line_len, "rmdir %s", movie_dir);
1099 static void plot_io_movie(struct plot *plot)
1101 struct trace_file *tf;
1103 struct plot_history *history;
1106 int movie_frames_per_sec = 20;
1107 int total_frames = movie_len * movie_frames_per_sec;
1110 int graph_width_factor = 5;
1112 char movie_dir[] = "io-movie-XXXXXX";
1114 if (mkdtemp(movie_dir) == NULL) {
1115 perror("Unable to create temp directory for movie files");
1119 get_graph_size(&cols, &rows);
1120 batch_count = cols / total_frames;
1122 if (batch_count == 0)
1125 list_for_each_entry(tf, &all_traces, list) {
1126 char *prefix = tf->label ? tf->label : "";
1130 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
1131 set_plot_output(plot, line);
1132 set_plot_title(plot, graph_title);
1133 orig_y_offset = plot->start_y_offset;
1135 plot->no_legend = 1;
1137 set_graph_size(cols / graph_width_factor, rows / 8);
1138 plot->timeline = i / graph_width_factor;
1140 plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
1142 plot_cpu(plot, tf->max_seconds,
1143 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1145 plot->direction = PLOT_ACROSS;
1146 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
1148 /* movie graph starts here */
1149 plot->start_y_offset = orig_y_offset;
1150 set_graph_size(cols - cols / graph_width_factor, rows);
1151 plot->no_legend = 0;
1153 plot->direction = PLOT_DOWN;;
1155 if (movie_style == MOVIE_SPINDLE)
1156 setup_axis_spindle(plot);
1160 svg_alloc_legend(plot, count_io_plot_types() * 2);
1162 history = alloc_plot_history(tf);
1165 for (pid = 0; pid < tf->io_plots; pid++) {
1166 if (tf->gdd_reads[pid])
1167 plot_io_legend(plot, tf->gdd_reads[pid], prefix, " Reads");
1168 if (tf->gdd_writes[pid])
1169 plot_io_legend(plot, tf->gdd_writes[pid], prefix, " Writes");
1173 while (i < cols && batch_i < batch_count) {
1174 for (pid = 0; pid < tf->io_plots; pid++) {
1175 if (tf->gdd_reads[pid]) {
1176 svg_io_graph_movie(tf->gdd_reads[pid],
1177 history->read_pid_history[pid],
1180 if (tf->gdd_writes[pid]) {
1181 svg_io_graph_movie(tf->gdd_writes[pid],
1182 history->write_pid_history[pid],
1190 add_history(history, &movie_history);
1192 plot_movie_history(plot, &movie_history);
1194 svg_write_legend(plot);
1198 close_plot_file(plot);
1200 free_all_plot_history(&movie_history);
1202 convert_movie_files(movie_dir);
1203 encode_movie(movie_dir);
1204 cleanup_movie(movie_dir);
1207 static void plot_latency(struct plot *plot, unsigned int min_seconds,
1208 unsigned int max_seconds)
1210 struct trace_file *tf;
1215 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1219 svg_alloc_legend(plot, num_traces);
1221 list_for_each_entry(tf, &all_traces, list) {
1222 val = line_graph_roll_avg_max(tf->latency_gld);
1227 list_for_each_entry(tf, &all_traces, list)
1228 tf->latency_gld->max = max;
1231 set_plot_label(plot, "IO Latency");
1233 tf = list_entry(all_traces.next, struct trace_file, list);
1235 scale_line_graph_time(&max, &units);
1236 sprintf(line, "latency (%ss)", units);
1237 set_ylabel(plot, line);
1238 set_yticks(plot, num_yticks, 0, max, "");
1239 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1241 list_for_each_entry(tf, &all_traces, list) {
1242 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1244 svg_add_legend(plot, tf->label, "", tf->line_color);
1247 if (plot->add_xlabel)
1248 set_xlabel(plot, "Time (seconds)");
1250 svg_write_legend(plot);
1252 total_graphs_written++;
1255 static void plot_iops(struct plot *plot, unsigned int min_seconds,
1256 unsigned int max_seconds)
1258 struct trace_file *tf;
1262 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1265 list_for_each_entry(tf, &all_traces, list) {
1266 val = line_graph_roll_avg_max(tf->iop_gld);
1271 list_for_each_entry(tf, &all_traces, list)
1272 tf->iop_gld->max = max;
1275 set_plot_label(plot, "IOPs");
1277 svg_alloc_legend(plot, num_traces);
1279 tf = list_entry(all_traces.next, struct trace_file, list);
1281 scale_line_graph_bytes(&max, &units, 1000);
1282 set_ylabel(plot, "IO/s");
1284 set_yticks(plot, num_yticks, 0, max, units);
1285 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1287 list_for_each_entry(tf, &all_traces, list) {
1288 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1290 svg_add_legend(plot, tf->label, "", tf->line_color);
1293 if (plot->add_xlabel)
1294 set_xlabel(plot, "Time (seconds)");
1296 svg_write_legend(plot);
1299 total_graphs_written++;
1302 static void check_plot_columns(struct plot *plot, int index)
1306 if (columns > 1 && (total_graphs_written == 0 ||
1307 total_graphs_written % columns != 0)) {
1308 count = graphs_left(index);
1309 if (plot->direction == PLOT_DOWN) {
1310 plot->start_x_offset = 0;
1311 if (count <= columns)
1312 plot->add_xlabel = 1;
1314 plot->direction = PLOT_ACROSS;
1317 plot->direction = PLOT_DOWN;
1318 if (index == last_active_graph)
1319 plot->add_xlabel = 1;
1328 char *option_string = "+F:T:t:o:l:r:O:N:d:D:pm::h:w:c:x:y:a:C:PK";
1329 static struct option long_options[] = {
1330 {"columns", required_argument, 0, 'c'},
1331 {"fio-trace", required_argument, 0, 'F'},
1332 {"title", required_argument, 0, 'T'},
1333 {"trace", required_argument, 0, 't'},
1334 {"output", required_argument, 0, 'o'},
1335 {"label", required_argument, 0, 'l'},
1336 {"rolling", required_argument, 0, 'r'},
1337 {"no-graph", required_argument, 0, 'N'},
1338 {"only-graph", required_argument, 0, 'O'},
1339 {"device", required_argument, 0, 'd'},
1340 {"blktrace-destination", required_argument, 0, 'D'},
1341 {"prog", no_argument, 0, 'p'},
1342 {"movie", optional_argument, 0, 'm'},
1343 {"codec", optional_argument, 0, 'C'},
1344 {"keep-movie-svgs", no_argument, 0, 'K'},
1345 {"width", required_argument, 0, 'w'},
1346 {"height", required_argument, 0, 'h'},
1347 {"xzoom", required_argument, 0, 'x'},
1348 {"yzoom", required_argument, 0, 'y'},
1349 {"io-plot-action", required_argument, 0, 'a'},
1350 {"per-process-io", no_argument, 0, 'P'},
1351 {"help", no_argument, 0, HELP_LONG_OPT},
1355 static void print_usage(void)
1357 fprintf(stderr, "iowatcher usage:\n"
1358 "\t-d (--device): device for blktrace to trace\n"
1359 "\t-D (--blktrace-destination): destination for blktrace\n"
1360 "\t-t (--trace): trace file name (more than one allowed)\n"
1361 "\t-F (--fio-trace): fio bandwidth trace (more than one allowed)\n"
1362 "\t-l (--label): trace label in the graph\n"
1363 "\t-o (--output): output file name for the SVG image or video\n"
1364 "\t-p (--prog): run a program while blktrace is run\n"
1365 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
1366 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1367 "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
1368 "\t-r (--rolling): number of seconds in the rolling averge\n"
1369 "\t-T (--title): graph title\n"
1370 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue-depth, \n"
1371 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1372 "\t-O (--only-graph): add a single graph to the output\n"
1373 "\t-h (--height): set the height of each graph\n"
1374 "\t-w (--width): set the width of each graph\n"
1375 "\t-c (--columns): numbers of columns in graph output\n"
1376 "\t-x (--xzoom): limit processed time to min:max\n"
1377 "\t-y (--yzoom): limit processed sectors to min:max\n"
1378 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1379 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1384 static int parse_double_range(char *str, double *min, double *max)
1388 /* Empty lower bound - leave original value */
1389 if (str[0] != ':') {
1390 *min = strtod(str, &end);
1391 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1397 /* Empty upper bound - leave original value */
1399 *max = strtod(end+1, &end);
1400 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1410 static int parse_ull_range(char *str, unsigned long long *min,
1411 unsigned long long *max)
1415 /* Empty lower bound - leave original value */
1416 if (str[0] != ':') {
1417 *min = strtoull(str, &end, 10);
1418 if (*min == ULLONG_MAX && errno == ERANGE)
1424 /* Empty upper bound - leave original value */
1426 *max = strtoull(end+1, &end, 10);
1427 if (*max == ULLONG_MAX && errno == ERANGE)
1437 static int parse_options(int ac, char **av)
1444 int option_index = 0;
1446 c = getopt_long(ac, av, option_string,
1447 long_options, &option_index);
1454 graph_title = strdup(optarg);
1457 add_trace_file(optarg);
1458 set_blktrace_outfile(optarg);
1461 add_fio_trace_file(optarg);
1464 output_filename = strdup(optarg);
1467 set_trace_label(optarg);
1470 set_rolling_avg(atoi(optarg));
1474 disable_all_graphs();
1477 enable_one_graph(optarg);
1480 disable_one_graph(optarg);
1483 if (num_blktrace_devices == MAX_DEVICES_PER_TRACE - 1) {
1484 fprintf(stderr, "Too many blktrace devices provided\n");
1487 blktrace_devices[num_blktrace_devices++] = strdup(optarg);
1490 blktrace_dest_dir = strdup(optarg);
1491 if (!strcmp(blktrace_dest_dir, "")) {
1492 fprintf(stderr, "Need a directory\n");
1500 keep_movie_svgs = 1;
1505 movie_style = lookup_movie_style(optarg);
1506 if (movie_style < 0) {
1507 fprintf(stderr, "Unknown movie style %s\n", optarg);
1511 fprintf(stderr, "Using movie style: %s\n",
1512 movie_styles[movie_style]);
1515 ffmpeg_codec = strdup(optarg);
1518 opt_graph_height = atoi(optarg);
1521 opt_graph_width = atoi(optarg);
1524 columns = atoi(optarg);
1527 if (parse_double_range(optarg, &min_time, &max_time)
1529 fprintf(stderr, "Cannot parse time range %s\n",
1535 if (parse_ull_range(optarg, &min_mb, &max_mb)
1538 "Cannot parse offset range %s\n",
1542 if (max_mb > ULLONG_MAX >> 20) {
1544 "Upper range limit too big."
1545 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1550 if (strlen(optarg) != 1) {
1552 fprintf(stderr, "Action must be one of Q, D, C.");
1555 plot_io_action = action_char_to_num(optarg[0]);
1556 if (plot_io_action < 0)
1571 if (optind < ac && p_flagged) {
1572 prog_argv = &av[optind];
1573 prog_argc = ac - optind;
1574 } else if (p_flagged) {
1575 fprintf(stderr, "--prog or -p given but no program specified\n");
1577 } else if (optind < ac) {
1578 fprintf(stderr, "Extra arguments '%s'... (and --prog not specified)\n", av[optind]);
1585 static void dest_mkdir(char *dir)
1589 ret = mkdir(dir, 0777);
1590 if (ret && errno != EEXIST) {
1591 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1596 int main(int ac, char **av)
1599 unsigned int min_seconds = 0;
1600 unsigned int max_seconds = 0;
1602 u64 min_offset = ~(u64)0;
1603 struct trace_file *tf;
1607 init_io_hash_table();
1608 init_process_hash_table();
1610 enable_all_graphs();
1612 parse_options(ac, av);
1614 last_active_graph = last_graph();
1616 set_io_graph_scale(256);
1617 if (movie_style == MOVIE_SPINDLE)
1618 set_graph_size(750, 550);
1620 set_graph_size(700, 400);
1623 * the plots in the movie don't have a seconds
1624 * line yet, this makes us skip it
1626 last_active_graph = TOTAL_GRAPHS + 1;
1628 if (opt_graph_height)
1629 set_graph_height(opt_graph_height);
1631 if (opt_graph_width)
1632 set_graph_width(opt_graph_width);
1634 if (list_empty(&all_traces) && list_empty(&fio_traces)) {
1635 fprintf(stderr, "No traces found, exiting\n");
1639 if (num_blktrace_devices) {
1640 char *path = join_path(blktrace_dest_dir, blktrace_outfile);
1641 dest_mkdir(blktrace_dest_dir);
1644 snprintf(line, line_len, "%s.dump", path);
1647 ret = start_blktrace(blktrace_devices, num_blktrace_devices,
1648 blktrace_outfile, blktrace_dest_dir);
1650 perror("Exiting due to blktrace failure");
1654 snprintf(line, line_len, "%s.mpstat", path);
1655 ret = start_mpstat(line);
1657 perror("Exiting due to mpstat failure");
1661 if (prog_argv && prog_argc) {
1662 run_program(prog_argc, prog_argv, 1, NULL, NULL);
1663 wait_for_tracers(SIGINT);
1665 printf("Tracing until interrupted...\n");
1666 wait_for_tracers(0);
1671 /* step one, read all the traces */
1674 /* step two, find the maxes for time and offset */
1675 list_for_each_entry(tf, &all_traces, list)
1676 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1677 list_for_each_entry(tf, &fio_traces, list)
1678 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1679 min_seconds = min_time;
1680 if (max_seconds > max_time)
1681 max_seconds = ceil(max_time);
1682 if (min_offset < min_mb << 20)
1683 min_offset = min_mb << 20;
1684 if (max_offset > max_mb << 20)
1685 max_offset = max_mb << 20;
1687 /* push the max we found into all the tfs */
1688 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1690 /* alloc graphing structs for all the traces */
1691 setup_trace_file_graphs();
1693 /* run through all the traces and read their events */
1694 read_trace_events();
1696 pick_line_graph_color();
1698 plot = alloc_plot();
1701 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1702 plot_io_movie(plot);
1706 set_plot_output(plot, output_filename);
1708 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1709 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1710 else if (num_traces >= 1 || num_fio_traces >= 1)
1711 set_legend_width(longest_label);
1713 set_legend_width(0);
1715 get_graph_size(&cols, &rows);
1717 plot->add_xlabel = 1;
1718 set_plot_title(plot, graph_title);
1720 check_plot_columns(plot, IO_GRAPH_INDEX);
1721 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1722 plot->add_xlabel = 0;
1725 set_graph_size(cols / columns, rows);
1726 num_xticks /= columns;
1733 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1734 plot_tput(plot, min_seconds, max_seconds, 1);
1736 check_plot_columns(plot, FIO_GRAPH_INDEX);
1737 plot_fio_tput(plot, min_seconds, max_seconds);
1739 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1740 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1741 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1743 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1744 plot_cpu(plot, max_seconds, "CPU System Time",
1745 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1747 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1748 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1749 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1751 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1752 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1753 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1755 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1756 plot_cpu(plot, max_seconds, "CPU User Time",
1757 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1759 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1760 plot_latency(plot, min_seconds, max_seconds);
1762 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1763 plot_queue_depth(plot, min_seconds, max_seconds);
1765 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1766 plot_iops(plot, min_seconds, max_seconds);
1770 close_plot_file(plot);