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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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>
43 LIST_HEAD(all_traces);
45 static char line[1024];
46 static int line_len = 1024;
47 static int found_mpstat = 0;
48 static int cpu_color_index = 0;
49 static int color_index = 0;
50 static int make_movie = 0;
51 static int opt_graph_width = 0;
52 static int opt_graph_height = 0;
54 static int columns = 1;
55 static int num_xticks = 9;
57 static double min_time = 0;
58 static double max_time = DBL_MAX;
59 static unsigned long long min_mb = 0;
60 static unsigned long long max_mb = ULLONG_MAX >> 20;
63 * this doesn't include the IO graph,
64 * but it counts the other graphs as they go out
66 static int total_graphs_written = 1;
71 "orange", "darkviolet",
78 char *pick_color(void) {
79 char *ret = colors[color_index];
82 ret = colors[color_index];
88 char *pick_cpu_color(void) {
89 char *ret = colors[cpu_color_index];
92 ret = colors[cpu_color_index];
104 CPU_SOFT_GRAPH_INDEX,
105 CPU_USER_GRAPH_INDEX,
107 QUEUE_DEPTH_GRAPH_INDEX,
121 static char *graphs_by_name[] = {
140 char *movie_styles[] = {
146 static int movie_style = 0;
148 static int lookup_movie_style(char *str)
152 for (i = 0; i < NUM_MOVIE_STYLES; i++) {
153 if (strcmp(str, movie_styles[i]) == 0)
159 static int active_graphs[TOTAL_GRAPHS];
160 static int last_active_graph = IOPS_GRAPH_INDEX;
162 static int label_index = 0;
163 static int num_traces = 0;
164 static int longest_label = 0;
167 struct list_head list;
171 int stop_seconds; /* Time when trace stops */
172 int min_seconds; /* Beginning of the interval we should plot */
173 int max_seconds; /* End of the interval we should plot */
180 struct graph_line_data *tput_gld;
181 struct graph_line_data *iop_gld;
182 struct graph_line_data *latency_gld;
183 struct graph_line_data *queue_depth_gld;
184 struct graph_dot_data *gdd_writes;
185 struct graph_dot_data *gdd_reads;
187 int mpstat_min_seconds;
188 int mpstat_max_seconds;
189 int mpstat_stop_seconds;
190 struct graph_line_data **mpstat_gld;
193 static void alloc_mpstat_gld(struct trace_file *tf)
195 struct graph_line_data **ptr;
197 if (tf->trace->mpstat_num_cpus == 0)
200 ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
201 sizeof(struct graph_line_data *));
203 perror("Unable to allocate mpstat arrays\n");
206 tf->mpstat_gld = ptr;
209 static void enable_all_graphs(void)
212 for (i = 0; i < TOTAL_GRAPHS; i++)
213 active_graphs[i] = 1;
216 static void disable_all_graphs(void)
219 for (i = 0; i < TOTAL_GRAPHS; i++)
220 active_graphs[i] = 0;
223 static int enable_one_graph(char *name)
226 for (i = 0; i < TOTAL_GRAPHS; i++) {
227 if (strcmp(name, graphs_by_name[i]) == 0) {
228 active_graphs[i] = 1;
235 static int disable_one_graph(char *name)
238 for (i = 0; i < TOTAL_GRAPHS; i++) {
239 if (strcmp(name, graphs_by_name[i]) == 0) {
240 active_graphs[i] = 0;
247 static int last_graph(void)
250 for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
251 if (active_graphs[i]) {
258 static int graphs_left(int cur)
262 for (i = cur; i < TOTAL_GRAPHS; i++) {
263 if (active_graphs[i])
269 static void add_trace_file(char *filename)
271 struct trace_file *tf;
273 tf = calloc(1, sizeof(*tf));
275 fprintf(stderr, "Unable to allocate memory\n");
279 tf->filename = strdup(filename);
280 list_add_tail(&tf->list, &all_traces);
281 tf->read_color = pick_color();
282 tf->write_color = pick_color();
286 static void setup_trace_file_graphs(void)
288 struct trace_file *tf;
291 list_for_each_entry(tf, &all_traces, list) {
292 tf->tput_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
293 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
294 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
295 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
296 tf->gdd_writes = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds);
297 tf->gdd_reads = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds);
299 if (tf->trace->mpstat_num_cpus == 0)
302 alloc_mpstat_gld(tf);
303 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
305 alloc_line_data(tf->mpstat_min_seconds,
306 tf->mpstat_max_seconds,
307 tf->mpstat_max_seconds);
308 tf->mpstat_gld[i]->max = 100;
313 static void read_traces(void)
315 struct trace_file *tf;
323 list_for_each_entry(tf, &all_traces, list) {
324 trace = open_trace(tf->filename);
328 last_time = find_last_time(trace);
330 tf->max_seconds = SECONDS(last_time);
331 tf->stop_seconds = SECONDS(last_time);
332 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
333 &max_bank, &max_bank_offset);
334 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
335 tf->min_offset = ymin;
336 tf->max_offset = ymax;
338 read_mpstat(trace, tf->filename);
339 tf->mpstat_stop_seconds = trace->mpstat_seconds;
340 tf->mpstat_max_seconds = trace->mpstat_seconds;
341 if (tf->mpstat_max_seconds)
346 static void read_trace_events(void)
349 struct trace_file *tf;
354 double user, sys, iowait, irq, soft;
355 double max_user = 0, max_sys = 0, max_iowait = 0,
356 max_irq = 0, max_soft = 0;
358 list_for_each_entry(tf, &all_traces, list) {
363 add_tput(trace, tf->tput_gld);
364 add_iop(trace, tf->iop_gld);
365 add_io(trace, tf->gdd_writes, tf->gdd_reads);
366 add_pending_io(trace, tf->queue_depth_gld);
367 add_completed_io(trace, tf->latency_gld);
368 ret = next_record(trace);
373 list_for_each_entry(tf, &all_traces, list) {
376 if (trace->mpstat_num_cpus == 0)
381 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
382 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
383 ret = read_mpstat_event(trace, &user, &sys,
384 &iowait, &irq, &soft);
387 if (next_mpstat_line(trace))
396 if (iowait > max_iowait)
399 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
400 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
401 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
402 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
403 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
405 if (next_mpstat(trace) == NULL)
411 list_for_each_entry(tf, &all_traces, list) {
414 if (trace->mpstat_num_cpus == 0)
417 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
418 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
419 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
420 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
421 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
426 static void set_trace_label(char *label)
429 struct trace_file *tf;
430 int len = strlen(label);
432 if (len > longest_label)
435 list_for_each_entry(tf, &all_traces, list) {
436 if (cur == label_index) {
437 tf->label = strdup(label);
445 static char *graph_title = "";
446 static char *output_filename = "trace.svg";
447 static char *blktrace_device = NULL;
448 static char *blktrace_outfile = "trace";
449 static char *blktrace_dest_dir = ".";
450 static char *program_to_run = NULL;
452 static void set_blktrace_outfile(char *arg)
454 char *s = strdup(arg);
455 char *last_dot = strrchr(s, '.');
458 if (strcmp(last_dot, ".dump") == 0)
461 blktrace_outfile = s;
465 static void compare_minmax_tf(struct trace_file *tf, int *max_seconds, u64 *min_offset, u64 *max_offset)
467 if (tf->max_seconds > *max_seconds)
468 *max_seconds = tf->max_seconds;
469 if (tf->max_offset > *max_offset)
470 *max_offset = tf->max_offset;
471 if (tf->min_offset < *min_offset)
472 *min_offset = tf->min_offset;
475 static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
477 struct trace_file *tf;
479 list_for_each_entry(tf, &all_traces, list) {
480 tf->min_seconds = min_seconds;
481 tf->max_seconds = max_seconds;
482 if (tf->stop_seconds > max_seconds)
483 tf->stop_seconds = max_seconds;
484 if (tf->mpstat_max_seconds) {
485 tf->mpstat_min_seconds = min_seconds;
486 tf->mpstat_max_seconds = max_seconds;
487 if (tf->mpstat_stop_seconds > max_seconds)
488 tf->mpstat_stop_seconds = max_seconds;
490 tf->min_offset = min_offset;
491 tf->max_offset = max_offset;
495 static char *create_movie_temp_dir(void)
498 char *pattern = strdup("io-movie-XXXXXX");;
500 ret = mkdtemp(pattern);
502 perror("Unable to create temp directory for movie files");
508 static struct plot_history *alloc_plot_history(char *color)
510 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
513 perror("memory allocation failed");
516 ph->history = calloc(4096, sizeof(double));
518 perror("memory allocation failed");
521 ph->history_len = 4096;
526 LIST_HEAD(movie_history_writes);
527 LIST_HEAD(movie_history_reads);
528 int num_histories = 0;
530 static void add_history(struct plot_history *ph, struct list_head *list)
532 struct plot_history *entry;
534 list_add_tail(&ph->list, list);
537 if (num_histories > 12) {
539 entry = list_entry(list->next, struct plot_history, list);
540 list_del(&entry->list);
541 free(entry->history);
546 static void plot_movie_history(struct plot *plot, struct list_head *list)
548 struct plot_history *ph;
550 if (num_histories > 2)
551 rewind_spindle_steps(num_histories - 1);
553 list_for_each_entry(ph, list, list) {
554 if (movie_style == MOVIE_SPINDLE)
555 svg_io_graph_movie_array_spindle(plot, ph);
557 svg_io_graph_movie_array(plot, ph);
561 static void free_all_plot_history(struct list_head *head)
563 struct plot_history *entry;
564 while (!list_empty(head)) {
565 entry = list_entry(head->next, struct plot_history, list);
566 list_del(&entry->list);
567 free(entry->history);
572 static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
574 struct trace_file *tf;
576 if (active_graphs[IO_GRAPH_INDEX] == 0)
581 svg_alloc_legend(plot, num_traces * 2);
583 set_plot_label(plot, "Device IO");
584 set_ylabel(plot, "Offset (MB)");
585 set_yticks(plot, 4, min_offset / (1024 * 1024),
586 max_offset / (1024 * 1024), "");
587 set_xticks(plot, num_xticks, min_seconds, max_seconds);
589 list_for_each_entry(tf, &all_traces, list) {
590 char *label = tf->label;
594 svg_io_graph(plot, tf->gdd_reads, tf->read_color);
595 if (tf->gdd_reads->total_ios)
596 svg_add_legend(plot, label, " Reads", tf->read_color);
598 svg_io_graph(plot, tf->gdd_writes, tf->write_color);
599 if (tf->gdd_writes->total_ios) {
600 svg_add_legend(plot, label, " Writes", tf->write_color);
603 if (plot->add_xlabel)
604 set_xlabel(plot, "Time (seconds)");
605 svg_write_legend(plot);
609 static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
611 struct trace_file *tf;
616 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
620 svg_alloc_legend(plot, num_traces);
621 list_for_each_entry(tf, &all_traces, list) {
622 if (tf->tput_gld->max > max)
623 max = tf->tput_gld->max;
625 list_for_each_entry(tf, &all_traces, list)
626 tf->tput_gld->max = max;
629 set_plot_label(plot, "Throughput");
631 tf = list_entry(all_traces.next, struct trace_file, list);
633 scale_line_graph_bytes(&max, &units, 1024);
634 sprintf(line, "%sB/s", units);
635 set_ylabel(plot, line);
636 set_yticks(plot, 4, 0, max, "");
637 set_xticks(plot, num_xticks, min_seconds, max_seconds);
639 list_for_each_entry(tf, &all_traces, list) {
640 svg_line_graph(plot, tf->tput_gld, tf->read_color, 0, 0);
642 svg_add_legend(plot, tf->label, "", tf->read_color);
645 if (plot->add_xlabel)
646 set_xlabel(plot, "Time (seconds)");
648 svg_write_legend(plot);
650 total_graphs_written++;
653 static void plot_cpu(struct plot *plot, int max_seconds, char *label,
654 int active_index, int gld_index)
656 struct trace_file *tf;
665 if (active_graphs[active_index] == 0)
668 list_for_each_entry(tf, &all_traces, list) {
669 if (tf->trace->mpstat_num_cpus > max)
670 max = tf->trace->mpstat_num_cpus;
675 tf = list_entry(all_traces.next, struct trace_file, list);
677 ymax = tf->mpstat_gld[gld_index]->max;
681 svg_alloc_legend(plot, num_traces * max);
684 set_plot_label(plot, label);
686 max_seconds = tf->mpstat_max_seconds;
688 set_yticks(plot, 4, 0, tf->mpstat_gld[gld_index]->max, "");
689 set_ylabel(plot, "Percent");
690 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
693 list_for_each_entry(tf, &all_traces, list) {
694 for (i = tf->mpstat_gld[0]->min_seconds;
695 i < tf->mpstat_gld[0]->stop_seconds; i++) {
696 if (tf->mpstat_gld[gld_index]->data[i].count) {
697 avg += (tf->mpstat_gld[gld_index]->data[i].sum /
698 tf->mpstat_gld[gld_index]->data[i].count);
701 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
702 tf->mpstat_gld[gld_index]->min_seconds;
703 color = pick_cpu_color();
704 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
705 svg_add_legend(plot, tf->label, " avg", color);
707 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
708 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
711 for (gld_i = gld->min_seconds;
712 gld_i < gld->stop_seconds; gld_i++) {
713 if (gld->data[i].count) {
714 this_avg += gld->data[i].sum /
719 this_avg /= gld->stop_seconds - gld->min_seconds;
721 for (gld_i = gld->min_seconds;
722 gld_i < gld->stop_seconds; gld_i++) {
725 if (gld->data[gld_i].count == 0)
727 val = (double)gld->data[gld_i].sum /
728 gld->data[gld_i].count;
730 if (this_avg > avg + 30 || val > 95) {
731 color = pick_cpu_color();
732 svg_line_graph(plot, gld, color, avg + 30, 95);
733 snprintf(line, line_len, " CPU %d\n", i - 1);
734 svg_add_legend(plot, tf->label, line, color);
743 if (plot->add_xlabel)
744 set_xlabel(plot, "Time (seconds)");
746 if (!plot->no_legend) {
747 svg_write_legend(plot);
748 svg_free_legend(plot);
751 total_graphs_written++;
754 static void plot_queue_depth(struct plot *plot, int min_seconds, int max_seconds)
756 struct trace_file *tf;
758 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
762 set_plot_label(plot, "Queue Depth");
764 svg_alloc_legend(plot, num_traces);
766 tf = list_entry(all_traces.next, struct trace_file, list);
767 set_ylabel(plot, "Pending IO");
768 set_yticks(plot, 4, 0, tf->queue_depth_gld->max, "");
769 set_xticks(plot, num_xticks, min_seconds, max_seconds);
771 list_for_each_entry(tf, &all_traces, list) {
772 svg_line_graph(plot, tf->queue_depth_gld, tf->read_color, 0, 0);
774 svg_add_legend(plot, tf->label, "", tf->read_color);
777 if (plot->add_xlabel)
778 set_xlabel(plot, "Time (seconds)");
780 svg_write_legend(plot);
782 total_graphs_written++;
785 static void convert_movie_files(char *movie_dir)
787 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
788 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
793 static void mencode_movie(char *movie_dir)
795 fprintf(stderr, "Creating movie %s\n", movie_dir);
796 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
797 "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
801 static void cleanup_movie(char *movie_dir)
803 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
804 snprintf(line, line_len, "rm %s/*", movie_dir);
807 snprintf(line, line_len, "rmdir %s", movie_dir);
811 static void plot_io_movie(struct plot *plot)
813 struct trace_file *tf;
814 char *movie_dir = create_movie_temp_dir();
816 struct plot_history *read_history;
817 struct plot_history *write_history;
820 int movie_frames_per_sec = 20;
821 int total_frames = movie_len * movie_frames_per_sec;
824 int graph_width_factor = 5;
827 get_graph_size(&cols, &rows);
828 batch_count = cols / total_frames;
830 if (batch_count == 0)
833 list_for_each_entry(tf, &all_traces, list) {
834 char *label = tf->label;
840 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
841 set_plot_output(plot, line);
842 set_plot_title(plot, graph_title);
843 orig_y_offset = plot->start_y_offset;
847 set_graph_size(cols / graph_width_factor, rows / 8);
848 plot->timeline = i / graph_width_factor;
850 plot_tput(plot, tf->gdd_reads->min_seconds,
851 tf->gdd_reads->max_seconds);
853 plot_cpu(plot, tf->gdd_reads->max_seconds,
854 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
856 plot->direction = PLOT_ACROSS;
857 plot_queue_depth(plot, tf->gdd_reads->min_seconds,
858 tf->gdd_reads->max_seconds);
860 /* movie graph starts here */
861 plot->start_y_offset = orig_y_offset;
862 set_graph_size(cols - cols / graph_width_factor, rows);
865 plot->direction = PLOT_DOWN;;
867 if (movie_style == MOVIE_SPINDLE)
868 setup_axis_spindle(plot);
872 svg_alloc_legend(plot, num_traces * 2);
874 read_history = alloc_plot_history(tf->read_color);
875 write_history = alloc_plot_history(tf->write_color);
876 read_history->col = i;
877 write_history->col = i;
879 if (tf->gdd_reads->total_ios)
880 svg_add_legend(plot, label, " Reads", tf->read_color);
881 if (tf->gdd_writes->total_ios)
882 svg_add_legend(plot, label, " Writes", tf->write_color);
885 while (i < cols && batch_i < batch_count) {
886 svg_io_graph_movie(tf->gdd_reads, read_history, i);
887 svg_io_graph_movie(tf->gdd_writes, write_history, i);
892 add_history(read_history, &movie_history_reads);
893 add_history(write_history, &movie_history_writes);
895 plot_movie_history(plot, &movie_history_reads);
896 plot_movie_history(plot, &movie_history_writes);
898 svg_write_legend(plot);
902 close_plot_file(plot);
904 free_all_plot_history(&movie_history_reads);
905 free_all_plot_history(&movie_history_writes);
907 convert_movie_files(movie_dir);
908 mencode_movie(movie_dir);
909 cleanup_movie(movie_dir);
913 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
915 struct trace_file *tf;
920 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
924 svg_alloc_legend(plot, num_traces);
925 list_for_each_entry(tf, &all_traces, list) {
926 if (tf->latency_gld->max > max)
927 max = tf->latency_gld->max;
929 list_for_each_entry(tf, &all_traces, list)
930 tf->latency_gld->max = max;
933 set_plot_label(plot, "IO Latency");
935 tf = list_entry(all_traces.next, struct trace_file, list);
937 scale_line_graph_time(&max, &units);
938 sprintf(line, "latency (%ss)", units);
939 set_ylabel(plot, line);
940 set_yticks(plot, 4, 0, max, "");
941 set_xticks(plot, num_xticks, min_seconds, max_seconds);
943 list_for_each_entry(tf, &all_traces, list) {
944 svg_line_graph(plot, tf->latency_gld, tf->read_color, 0, 0);
946 svg_add_legend(plot, tf->label, "", tf->read_color);
949 if (plot->add_xlabel)
950 set_xlabel(plot, "Time (seconds)");
952 svg_write_legend(plot);
954 total_graphs_written++;
957 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
959 struct trace_file *tf;
963 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
966 list_for_each_entry(tf, &all_traces, list) {
967 if (tf->iop_gld->max > max)
968 max = tf->iop_gld->max;
971 list_for_each_entry(tf, &all_traces, list)
972 tf->iop_gld->max = max;
975 set_plot_label(plot, "IOPs");
977 svg_alloc_legend(plot, num_traces);
979 tf = list_entry(all_traces.next, struct trace_file, list);
981 scale_line_graph_bytes(&max, &units, 1000);
982 set_ylabel(plot, "IO/s");
984 set_yticks(plot, 4, 0, max, units);
985 set_xticks(plot, num_xticks, min_seconds, max_seconds);
987 list_for_each_entry(tf, &all_traces, list) {
988 svg_line_graph(plot, tf->iop_gld, tf->read_color, 0, 0);
990 svg_add_legend(plot, tf->label, "", tf->read_color);
993 if (plot->add_xlabel)
994 set_xlabel(plot, "Time (seconds)");
996 svg_write_legend(plot);
999 total_graphs_written++;
1002 static void check_plot_columns(struct plot *plot, int index)
1006 if (columns > 1 && (total_graphs_written == 0 ||
1007 total_graphs_written % columns != 0)) {
1008 count = graphs_left(index);
1009 if (plot->direction == PLOT_DOWN) {
1010 plot->start_x_offset = 0;
1011 if (count <= columns)
1012 plot->add_xlabel = 1;
1014 plot->direction = PLOT_ACROSS;
1017 plot->direction = PLOT_DOWN;
1018 if (index == last_active_graph)
1019 plot->add_xlabel = 1;
1028 char *option_string = "T:t:o:l:r:O:N:d:p:m::h:w:c:x:y:";
1029 static struct option long_options[] = {
1030 {"columns", required_argument, 0, 'c'},
1031 {"title", required_argument, 0, 'T'},
1032 {"trace", required_argument, 0, 't'},
1033 {"output", required_argument, 0, 'o'},
1034 {"label", required_argument, 0, 'l'},
1035 {"rolling", required_argument, 0, 'r'},
1036 {"no-graph", required_argument, 0, 'N'},
1037 {"only-graph", required_argument, 0, 'O'},
1038 {"device", required_argument, 0, 'd'},
1039 {"prog", required_argument, 0, 'p'},
1040 {"movie", optional_argument, 0, 'm'},
1041 {"width", required_argument, 0, 'w'},
1042 {"height", required_argument, 0, 'h'},
1043 {"xzoom", required_argument, 0, 'x'},
1044 {"yzoom", required_argument, 0, 'y'},
1045 {"help", no_argument, 0, HELP_LONG_OPT},
1049 static void print_usage(void)
1051 fprintf(stderr, "iowatcher usage:\n"
1052 "\t-d (--device): device for blktrace to trace\n"
1053 "\t-t (--trace): trace file name (more than one allowed)\n"
1054 "\t-l (--label): trace label in the graph\n"
1055 "\t-o (--output): output file name (SVG only)\n"
1056 "\t-p (--prog): program to run while blktrace is run\n"
1057 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1058 "\t-r (--rolling): number of seconds in the rolling averge\n"
1059 "\t-T (--title): graph title\n"
1060 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1061 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1062 "\t-O (--only-graph): add a single graph to the output\n"
1063 "\t-h (--height): set the height of each graph\n"
1064 "\t-w (--width): set the width of each graph\n"
1065 "\t-c (--columns): numbers of columns in graph output\n"
1066 "\t-x (--xzoom): limit processed time to min:max\n"
1067 "\t-y (--yzoom): limit processed sectors to min:max\n"
1072 static int parse_double_range(char *str, double *min, double *max)
1076 /* Empty lower bound - leave original value */
1077 if (str[0] != ':') {
1078 *min = strtod(str, &end);
1079 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1085 /* Empty upper bound - leave original value */
1087 *max = strtod(end+1, &end);
1088 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1098 static int parse_ull_range(char *str, unsigned long long *min,
1099 unsigned long long *max)
1103 /* Empty lower bound - leave original value */
1104 if (str[0] != ':') {
1105 *min = strtoull(str, &end, 10);
1106 if (*min == ULLONG_MAX && errno == ERANGE)
1112 /* Empty upper bound - leave original value */
1114 *max = strtoull(end+1, &end, 10);
1115 if (*max == ULLONG_MAX && errno == ERANGE)
1125 static int parse_options(int ac, char **av)
1131 // int this_option_optind = optind ? optind : 1;
1132 int option_index = 0;
1134 c = getopt_long(ac, av, option_string,
1135 long_options, &option_index);
1142 graph_title = strdup(optarg);
1145 add_trace_file(optarg);
1146 set_blktrace_outfile(optarg);
1149 output_filename = strdup(optarg);
1152 set_trace_label(optarg);
1155 set_rolling_avg(atoi(optarg));
1159 disable_all_graphs();
1162 enable_one_graph(optarg);
1165 disable_one_graph(optarg);
1168 blktrace_device = strdup(optarg);
1171 program_to_run = strdup(optarg);
1176 movie_style = lookup_movie_style(optarg);
1177 if (movie_style < 0) {
1178 fprintf(stderr, "Unknown movie style %s\n", optarg);
1182 fprintf(stderr, "Using movie style: %s\n",
1183 movie_styles[movie_style]);
1186 opt_graph_height = atoi(optarg);
1189 opt_graph_width = atoi(optarg);
1192 columns = atoi(optarg);
1195 if (parse_double_range(optarg, &min_time, &max_time)
1197 fprintf(stderr, "Cannot parse time range %s\n",
1203 if (parse_ull_range(optarg, &min_mb, &max_mb)
1206 "Cannot parse offset range %s\n",
1210 if (max_mb > ULLONG_MAX >> 20) {
1212 "Upper range limit too big."
1213 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1229 int main(int ac, char **av)
1232 int min_seconds = 0;
1233 int max_seconds = 0;
1235 u64 min_offset = ~(u64)0;
1236 struct trace_file *tf;
1240 init_io_hash_table();
1242 enable_all_graphs();
1244 parse_options(ac, av);
1246 last_active_graph = last_graph();
1248 set_io_graph_scale(256);
1249 if (movie_style == MOVIE_SPINDLE)
1250 set_graph_size(750, 550);
1252 set_graph_size(700, 400);
1255 * the plots in the movie don't have a seconds
1256 * line yet, this makes us skip it
1258 last_active_graph = TOTAL_GRAPHS + 1;
1260 if (opt_graph_height)
1261 set_graph_height(opt_graph_height);
1263 if (opt_graph_width)
1264 set_graph_width(opt_graph_width);
1266 if (list_empty(&all_traces)) {
1267 fprintf(stderr, "No traces found, exiting\n");
1271 if (blktrace_device) {
1272 ret = start_blktrace(blktrace_device, blktrace_outfile,
1275 fprintf(stderr, "exiting due to blktrace failure\n");
1278 start_mpstat(blktrace_outfile);
1279 if (program_to_run) {
1280 ret = run_program(program_to_run);
1282 fprintf(stderr, "failed to run %s\n",
1287 blktrace_to_dump(blktrace_outfile);
1289 /* no program specified, just wait for
1296 /* step one, read all the traces */
1299 /* step two, find the maxes for time and offset */
1300 list_for_each_entry(tf, &all_traces, list)
1301 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1302 min_seconds = min_time;
1303 if (max_seconds > max_time)
1304 max_seconds = ceil(max_time);
1305 if (min_offset < min_mb << 20)
1306 min_offset = min_mb << 20;
1307 if (max_offset > max_mb << 20)
1308 max_offset = max_mb << 20;
1310 /* push the max we found into all the tfs */
1311 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1313 /* alloc graphing structs for all the traces */
1314 setup_trace_file_graphs();
1316 /* run through all the traces and read their events */
1317 read_trace_events();
1319 plot = alloc_plot();
1322 plot_io_movie(plot);
1326 set_plot_output(plot, output_filename);
1328 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1329 set_legend_width(longest_label + strlen("writes"));
1330 else if (num_traces > 1)
1331 set_legend_width(longest_label);
1333 set_legend_width(0);
1335 get_graph_size(&cols, &rows);
1337 plot->add_xlabel = 1;
1338 set_plot_title(plot, graph_title);
1340 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1341 plot->add_xlabel = 0;
1344 set_graph_size(cols / columns, rows);
1345 num_xticks /= columns;
1350 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1351 plot_tput(plot, min_seconds, max_seconds);
1353 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1354 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1355 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1357 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1358 plot_cpu(plot, max_seconds, "CPU System Time",
1359 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1361 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1362 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1363 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1365 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1366 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1367 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1369 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1370 plot_cpu(plot, max_seconds, "CPU User Time",
1371 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1373 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1374 plot_latency(plot, min_seconds, max_seconds);
1376 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1377 plot_queue_depth(plot, min_seconds, max_seconds);
1379 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1380 plot_iops(plot, min_seconds, max_seconds);
1384 close_plot_file(plot);