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>
41 LIST_HEAD(all_traces);
43 static char line[1024];
44 static int line_len = 1024;
45 static int found_mpstat = 0;
46 static int cpu_color_index = 0;
47 static int color_index = 0;
48 static int make_movie = 0;
49 static int opt_graph_width = 0;
50 static int opt_graph_height = 0;
52 static int columns = 1;
53 static int num_xticks = 9;
56 * this doesn't include the IO graph,
57 * but it counts the other graphs as they go out
59 static int total_graphs_written = 1;
64 "orange", "darkviolet",
71 char *pick_color(void) {
72 char *ret = colors[color_index];
75 ret = colors[color_index];
81 char *pick_cpu_color(void) {
82 char *ret = colors[cpu_color_index];
85 ret = colors[cpu_color_index];
100 QUEUE_DEPTH_GRAPH_INDEX,
114 static char *graphs_by_name[] = {
133 char *movie_styles[] = {
139 static int movie_style = 0;
141 static int lookup_movie_style(char *str)
145 for (i = 0; i < NUM_MOVIE_STYLES; i++) {
146 if (strcmp(str, movie_styles[i]) == 0)
152 static int active_graphs[TOTAL_GRAPHS];
153 static int last_active_graph = IOPS_GRAPH_INDEX;
155 static int label_index = 0;
156 static int num_traces = 0;
157 static int longest_label = 0;
160 struct list_head list;
164 int stop_seconds; /* Time when trace stops */
165 int min_seconds; /* Beginning of the interval we should plot */
166 int max_seconds; /* End of the interval we should plot */
173 struct graph_line_data *tput_gld;
174 struct graph_line_data *iop_gld;
175 struct graph_line_data *latency_gld;
176 struct graph_line_data *queue_depth_gld;
177 struct graph_dot_data *gdd_writes;
178 struct graph_dot_data *gdd_reads;
180 int mpstat_min_seconds;
181 int mpstat_max_seconds;
182 int mpstat_stop_seconds;
183 struct graph_line_data **mpstat_gld;
186 static void alloc_mpstat_gld(struct trace_file *tf)
188 struct graph_line_data **ptr;
190 if (tf->trace->mpstat_num_cpus == 0)
193 ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
194 sizeof(struct graph_line_data *));
196 perror("Unable to allocate mpstat arrays\n");
199 tf->mpstat_gld = ptr;
202 static void enable_all_graphs(void)
205 for (i = 0; i < TOTAL_GRAPHS; i++)
206 active_graphs[i] = 1;
209 static void disable_all_graphs(void)
212 for (i = 0; i < TOTAL_GRAPHS; i++)
213 active_graphs[i] = 0;
216 static int enable_one_graph(char *name)
219 for (i = 0; i < TOTAL_GRAPHS; i++) {
220 if (strcmp(name, graphs_by_name[i]) == 0) {
221 active_graphs[i] = 1;
228 static int disable_one_graph(char *name)
231 for (i = 0; i < TOTAL_GRAPHS; i++) {
232 if (strcmp(name, graphs_by_name[i]) == 0) {
233 active_graphs[i] = 0;
240 static int last_graph(void)
243 for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
244 if (active_graphs[i]) {
251 static int graphs_left(int cur)
255 for (i = cur; i < TOTAL_GRAPHS; i++) {
256 if (active_graphs[i])
262 static void add_trace_file(char *filename)
264 struct trace_file *tf;
266 tf = calloc(1, sizeof(*tf));
268 fprintf(stderr, "Unable to allocate memory\n");
272 tf->filename = strdup(filename);
273 list_add_tail(&tf->list, &all_traces);
274 tf->read_color = pick_color();
275 tf->write_color = pick_color();
279 static void setup_trace_file_graphs(void)
281 struct trace_file *tf;
284 list_for_each_entry(tf, &all_traces, list) {
285 tf->tput_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
286 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
287 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
288 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
289 tf->gdd_writes = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds);
290 tf->gdd_reads = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds);
292 if (tf->trace->mpstat_num_cpus == 0)
295 alloc_mpstat_gld(tf);
296 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
298 alloc_line_data(tf->mpstat_min_seconds,
299 tf->mpstat_max_seconds,
300 tf->mpstat_max_seconds);
301 tf->mpstat_gld[i]->max = 100;
306 static void read_traces(void)
308 struct trace_file *tf;
316 list_for_each_entry(tf, &all_traces, list) {
317 trace = open_trace(tf->filename);
321 last_time = find_last_time(trace);
323 tf->max_seconds = SECONDS(last_time);
324 tf->stop_seconds = SECONDS(last_time);
325 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
326 &max_bank, &max_bank_offset);
327 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
328 tf->min_offset = ymin;
329 tf->max_offset = ymax;
331 read_mpstat(trace, tf->filename);
332 tf->mpstat_stop_seconds = trace->mpstat_seconds;
333 tf->mpstat_max_seconds = trace->mpstat_seconds;
334 if (tf->mpstat_max_seconds)
339 static void read_trace_events(void)
342 struct trace_file *tf;
347 double user, sys, iowait, irq, soft;
348 double max_user = 0, max_sys = 0, max_iowait = 0,
349 max_irq = 0, max_soft = 0;
351 list_for_each_entry(tf, &all_traces, list) {
356 add_tput(trace, tf->tput_gld);
357 add_iop(trace, tf->iop_gld);
358 add_io(trace, tf->gdd_writes, tf->gdd_reads);
359 add_pending_io(trace, tf->queue_depth_gld);
360 add_completed_io(trace, tf->latency_gld);
361 ret = next_record(trace);
366 list_for_each_entry(tf, &all_traces, list) {
369 if (trace->mpstat_num_cpus == 0)
374 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
375 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
376 ret = read_mpstat_event(trace, &user, &sys,
377 &iowait, &irq, &soft);
380 if (next_mpstat_line(trace))
389 if (iowait > max_iowait)
392 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
393 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
394 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
395 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
396 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
398 if (next_mpstat(trace) == NULL)
404 list_for_each_entry(tf, &all_traces, list) {
407 if (trace->mpstat_num_cpus == 0)
410 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
411 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
412 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
413 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
414 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
419 static void set_trace_label(char *label)
422 struct trace_file *tf;
423 int len = strlen(label);
425 if (len > longest_label)
428 list_for_each_entry(tf, &all_traces, list) {
429 if (cur == label_index) {
430 tf->label = strdup(label);
438 static char *graph_title = "";
439 static char *output_filename = "trace.svg";
440 static char *blktrace_device = NULL;
441 static char *blktrace_outfile = "trace";
442 static char *blktrace_dest_dir = ".";
443 static char *program_to_run = NULL;
445 static void set_blktrace_outfile(char *arg)
447 char *s = strdup(arg);
448 char *last_dot = strrchr(s, '.');
451 if (strcmp(last_dot, ".dump") == 0)
454 blktrace_outfile = s;
458 static void compare_minmax_tf(struct trace_file *tf, int *max_seconds, u64 *min_offset, u64 *max_offset)
460 if (tf->max_seconds > *max_seconds)
461 *max_seconds = tf->max_seconds;
462 if (tf->max_offset > *max_offset)
463 *max_offset = tf->max_offset;
464 if (tf->min_offset < *min_offset)
465 *min_offset = tf->min_offset;
468 static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
470 struct trace_file *tf;
472 list_for_each_entry(tf, &all_traces, list) {
473 tf->min_seconds = min_seconds;
474 tf->max_seconds = max_seconds;
475 tf->min_offset = min_offset;
476 tf->max_offset = max_offset;
480 static char *create_movie_temp_dir(void)
483 char *pattern = strdup("io-movie-XXXXXX");;
485 ret = mkdtemp(pattern);
487 perror("Unable to create temp directory for movie files");
493 static struct plot_history *alloc_plot_history(char *color)
495 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
498 perror("memory allocation failed");
501 ph->history = calloc(4096, sizeof(double));
503 perror("memory allocation failed");
506 ph->history_len = 4096;
511 LIST_HEAD(movie_history_writes);
512 LIST_HEAD(movie_history_reads);
513 int num_histories = 0;
515 static void add_history(struct plot_history *ph, struct list_head *list)
517 struct plot_history *entry;
519 list_add_tail(&ph->list, list);
522 if (num_histories > 12) {
524 entry = list_entry(list->next, struct plot_history, list);
525 list_del(&entry->list);
526 free(entry->history);
531 static void plot_movie_history(struct plot *plot, struct list_head *list)
533 struct plot_history *ph;
535 if (num_histories > 2)
536 rewind_spindle_steps(num_histories - 1);
538 list_for_each_entry(ph, list, list) {
539 if (movie_style == MOVIE_SPINDLE)
540 svg_io_graph_movie_array_spindle(plot, ph);
542 svg_io_graph_movie_array(plot, ph);
546 static void free_all_plot_history(struct list_head *head)
548 struct plot_history *entry;
549 while (!list_empty(head)) {
550 entry = list_entry(head->next, struct plot_history, list);
551 list_del(&entry->list);
552 free(entry->history);
557 static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
559 struct trace_file *tf;
561 if (active_graphs[IO_GRAPH_INDEX] == 0)
566 svg_alloc_legend(plot, num_traces * 2);
568 set_plot_label(plot, "Device IO");
569 set_ylabel(plot, "Offset (MB)");
570 set_yticks(plot, 4, min_offset / (1024 * 1024),
571 max_offset / (1024 * 1024), "");
572 set_xticks(plot, num_xticks, min_seconds, max_seconds);
574 list_for_each_entry(tf, &all_traces, list) {
575 char *label = tf->label;
579 svg_io_graph(plot, tf->gdd_reads, tf->read_color);
580 if (tf->gdd_reads->total_ios)
581 svg_add_legend(plot, label, " Reads", tf->read_color);
583 svg_io_graph(plot, tf->gdd_writes, tf->write_color);
584 if (tf->gdd_writes->total_ios) {
585 svg_add_legend(plot, label, " Writes", tf->write_color);
588 if (plot->add_xlabel)
589 set_xlabel(plot, "Time (seconds)");
590 svg_write_legend(plot);
594 static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
596 struct trace_file *tf;
601 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
605 svg_alloc_legend(plot, num_traces);
606 list_for_each_entry(tf, &all_traces, list) {
607 if (tf->tput_gld->max > max)
608 max = tf->tput_gld->max;
610 list_for_each_entry(tf, &all_traces, list)
611 tf->tput_gld->max = max;
614 set_plot_label(plot, "Throughput");
616 tf = list_entry(all_traces.next, struct trace_file, list);
618 scale_line_graph_bytes(&max, &units, 1024);
619 sprintf(line, "%sB/s", units);
620 set_ylabel(plot, line);
621 set_yticks(plot, 4, 0, max, "");
622 set_xticks(plot, num_xticks, min_seconds, max_seconds);
624 list_for_each_entry(tf, &all_traces, list) {
625 svg_line_graph(plot, tf->tput_gld, tf->read_color, 0, 0);
627 svg_add_legend(plot, tf->label, "", tf->read_color);
630 if (plot->add_xlabel)
631 set_xlabel(plot, "Time (seconds)");
633 svg_write_legend(plot);
635 total_graphs_written++;
638 static void plot_cpu(struct plot *plot, int max_seconds, char *label,
639 int active_index, int gld_index)
641 struct trace_file *tf;
650 if (active_graphs[active_index] == 0)
653 list_for_each_entry(tf, &all_traces, list) {
654 if (tf->trace->mpstat_num_cpus > max)
655 max = tf->trace->mpstat_num_cpus;
660 tf = list_entry(all_traces.next, struct trace_file, list);
662 ymax = tf->mpstat_gld[gld_index]->max;
666 svg_alloc_legend(plot, num_traces * max);
669 set_plot_label(plot, label);
671 max_seconds = tf->mpstat_max_seconds;
673 set_yticks(plot, 4, 0, tf->mpstat_gld[gld_index]->max, "");
674 set_ylabel(plot, "Percent");
675 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
678 list_for_each_entry(tf, &all_traces, list) {
679 for (i = tf->mpstat_gld[0]->min_seconds;
680 i < tf->mpstat_gld[0]->stop_seconds; i++) {
681 if (tf->mpstat_gld[gld_index]->data[i].count) {
682 avg += (tf->mpstat_gld[gld_index]->data[i].sum /
683 tf->mpstat_gld[gld_index]->data[i].count);
686 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
687 tf->mpstat_gld[gld_index]->min_seconds;
688 color = pick_cpu_color();
689 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
690 svg_add_legend(plot, tf->label, " avg", color);
692 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
693 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
696 for (gld_i = gld->min_seconds;
697 gld_i < gld->stop_seconds; gld_i++) {
698 if (gld->data[i].count) {
699 this_avg += gld->data[i].sum /
704 this_avg /= gld->stop_seconds - gld->min_seconds;
706 for (gld_i = gld->min_seconds;
707 gld_i < gld->stop_seconds; gld_i++) {
710 if (gld->data[gld_i].count == 0)
712 val = (double)gld->data[gld_i].sum /
713 gld->data[gld_i].count;
715 if (this_avg > avg + 30 || val > 95) {
716 color = pick_cpu_color();
717 svg_line_graph(plot, gld, color, avg + 30, 95);
718 snprintf(line, line_len, " CPU %d\n", i - 1);
719 svg_add_legend(plot, tf->label, line, color);
728 if (plot->add_xlabel)
729 set_xlabel(plot, "Time (seconds)");
731 if (!plot->no_legend) {
732 svg_write_legend(plot);
733 svg_free_legend(plot);
736 total_graphs_written++;
739 static void plot_queue_depth(struct plot *plot, int min_seconds, int max_seconds)
741 struct trace_file *tf;
743 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
747 set_plot_label(plot, "Queue Depth");
749 svg_alloc_legend(plot, num_traces);
751 tf = list_entry(all_traces.next, struct trace_file, list);
752 set_ylabel(plot, "Pending IO");
753 set_yticks(plot, 4, 0, tf->queue_depth_gld->max, "");
754 set_xticks(plot, num_xticks, min_seconds, max_seconds);
756 list_for_each_entry(tf, &all_traces, list) {
757 svg_line_graph(plot, tf->queue_depth_gld, tf->read_color, 0, 0);
759 svg_add_legend(plot, tf->label, "", tf->read_color);
762 if (plot->add_xlabel)
763 set_xlabel(plot, "Time (seconds)");
765 svg_write_legend(plot);
767 total_graphs_written++;
770 static void convert_movie_files(char *movie_dir)
772 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
773 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
778 static void mencode_movie(char *movie_dir)
780 fprintf(stderr, "Creating movie %s\n", movie_dir);
781 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
782 "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
786 static void cleanup_movie(char *movie_dir)
788 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
789 snprintf(line, line_len, "rm %s/*", movie_dir);
792 snprintf(line, line_len, "rmdir %s", movie_dir);
796 static void plot_io_movie(struct plot *plot)
798 struct trace_file *tf;
799 char *movie_dir = create_movie_temp_dir();
801 struct plot_history *read_history;
802 struct plot_history *write_history;
805 int movie_frames_per_sec = 20;
806 int total_frames = movie_len * movie_frames_per_sec;
809 int graph_width_factor = 5;
812 get_graph_size(&cols, &rows);
813 batch_count = cols / total_frames;
815 if (batch_count == 0)
818 list_for_each_entry(tf, &all_traces, list) {
819 char *label = tf->label;
825 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
826 set_plot_output(plot, line);
827 set_plot_title(plot, graph_title);
828 orig_y_offset = plot->start_y_offset;
832 set_graph_size(cols / graph_width_factor, rows / 8);
833 plot->timeline = i / graph_width_factor;
835 plot_tput(plot, tf->gdd_reads->min_seconds,
836 tf->gdd_reads->max_seconds);
838 plot_cpu(plot, tf->gdd_reads->max_seconds,
839 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
841 plot->direction = PLOT_ACROSS;
842 plot_queue_depth(plot, tf->gdd_reads->min_seconds,
843 tf->gdd_reads->max_seconds);
845 /* movie graph starts here */
846 plot->start_y_offset = orig_y_offset;
847 set_graph_size(cols - cols / graph_width_factor, rows);
850 plot->direction = PLOT_DOWN;;
852 if (movie_style == MOVIE_SPINDLE)
853 setup_axis_spindle(plot);
857 svg_alloc_legend(plot, num_traces * 2);
859 read_history = alloc_plot_history(tf->read_color);
860 write_history = alloc_plot_history(tf->write_color);
861 read_history->col = i;
862 write_history->col = i;
864 if (tf->gdd_reads->total_ios)
865 svg_add_legend(plot, label, " Reads", tf->read_color);
866 if (tf->gdd_writes->total_ios)
867 svg_add_legend(plot, label, " Writes", tf->write_color);
870 while (i < cols && batch_i < batch_count) {
871 svg_io_graph_movie(tf->gdd_reads, read_history, i);
872 svg_io_graph_movie(tf->gdd_writes, write_history, i);
877 add_history(read_history, &movie_history_reads);
878 add_history(write_history, &movie_history_writes);
880 plot_movie_history(plot, &movie_history_reads);
881 plot_movie_history(plot, &movie_history_writes);
883 svg_write_legend(plot);
887 close_plot_file(plot);
889 free_all_plot_history(&movie_history_reads);
890 free_all_plot_history(&movie_history_writes);
892 convert_movie_files(movie_dir);
893 mencode_movie(movie_dir);
894 cleanup_movie(movie_dir);
898 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
900 struct trace_file *tf;
905 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
909 svg_alloc_legend(plot, num_traces);
910 list_for_each_entry(tf, &all_traces, list) {
911 if (tf->latency_gld->max > max)
912 max = tf->latency_gld->max;
914 list_for_each_entry(tf, &all_traces, list)
915 tf->latency_gld->max = max;
918 set_plot_label(plot, "IO Latency");
920 tf = list_entry(all_traces.next, struct trace_file, list);
922 scale_line_graph_time(&max, &units);
923 sprintf(line, "latency (%ss)", units);
924 set_ylabel(plot, line);
925 set_yticks(plot, 4, 0, max, "");
926 set_xticks(plot, num_xticks, min_seconds, max_seconds);
928 list_for_each_entry(tf, &all_traces, list) {
929 svg_line_graph(plot, tf->latency_gld, tf->read_color, 0, 0);
931 svg_add_legend(plot, tf->label, "", tf->read_color);
934 if (plot->add_xlabel)
935 set_xlabel(plot, "Time (seconds)");
937 svg_write_legend(plot);
939 total_graphs_written++;
942 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
944 struct trace_file *tf;
948 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
951 list_for_each_entry(tf, &all_traces, list) {
952 if (tf->iop_gld->max > max)
953 max = tf->iop_gld->max;
956 list_for_each_entry(tf, &all_traces, list)
957 tf->iop_gld->max = max;
960 set_plot_label(plot, "IOPs");
962 svg_alloc_legend(plot, num_traces);
964 tf = list_entry(all_traces.next, struct trace_file, list);
966 scale_line_graph_bytes(&max, &units, 1000);
967 set_ylabel(plot, "IO/s");
969 set_yticks(plot, 4, 0, max, units);
970 set_xticks(plot, num_xticks, min_seconds, max_seconds);
972 list_for_each_entry(tf, &all_traces, list) {
973 svg_line_graph(plot, tf->iop_gld, tf->read_color, 0, 0);
975 svg_add_legend(plot, tf->label, "", tf->read_color);
978 if (plot->add_xlabel)
979 set_xlabel(plot, "Time (seconds)");
981 svg_write_legend(plot);
984 total_graphs_written++;
987 static void check_plot_columns(struct plot *plot, int index)
991 if (columns > 1 && (total_graphs_written == 0 ||
992 total_graphs_written % columns != 0)) {
993 count = graphs_left(index);
994 if (plot->direction == PLOT_DOWN) {
995 plot->start_x_offset = 0;
996 if (count <= columns)
997 plot->add_xlabel = 1;
999 plot->direction = PLOT_ACROSS;
1002 plot->direction = PLOT_DOWN;
1003 if (index == last_active_graph)
1004 plot->add_xlabel = 1;
1013 char *option_string = "T:t:o:l:r:O:N:d:p:m::h:w:c:";
1014 static struct option long_options[] = {
1015 {"columns", required_argument, 0, 'c'},
1016 {"title", required_argument, 0, 'T'},
1017 {"trace", required_argument, 0, 't'},
1018 {"output", required_argument, 0, 'o'},
1019 {"label", required_argument, 0, 'l'},
1020 {"rolling", required_argument, 0, 'r'},
1021 {"no-graph", required_argument, 0, 'N'},
1022 {"only-graph", required_argument, 0, 'O'},
1023 {"device", required_argument, 0, 'd'},
1024 {"prog", required_argument, 0, 'p'},
1025 {"movie", optional_argument, 0, 'm'},
1026 {"width", required_argument, 0, 'w'},
1027 {"height", required_argument, 0, 'h'},
1028 {"help", no_argument, 0, HELP_LONG_OPT},
1032 static void print_usage(void)
1034 fprintf(stderr, "iowatcher usage:\n"
1035 "\t-d (--device): device for blktrace to trace\n"
1036 "\t-t (--trace): trace file name (more than one allowed)\n"
1037 "\t-l (--label): trace label in the graph\n"
1038 "\t-o (--output): output file name (SVG only)\n"
1039 "\t-p (--prog): program to run while blktrace is run\n"
1040 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1041 "\t-r (--rolling): number of seconds in the rolling averge\n"
1042 "\t-T (--title): graph title\n"
1043 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1044 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1045 "\t-O (--only-graph): add a single graph to the output\n"
1046 "\t-h (--height): set the height of each graph\n"
1047 "\t-w (--width): set the width of each graph\n"
1048 "\t-c (--columns): numbers of columns in graph output\n"
1053 static int parse_options(int ac, char **av)
1059 // int this_option_optind = optind ? optind : 1;
1060 int option_index = 0;
1062 c = getopt_long(ac, av, option_string,
1063 long_options, &option_index);
1070 graph_title = strdup(optarg);
1073 add_trace_file(optarg);
1074 set_blktrace_outfile(optarg);
1077 output_filename = strdup(optarg);
1080 set_trace_label(optarg);
1083 set_rolling_avg(atoi(optarg));
1087 disable_all_graphs();
1090 enable_one_graph(optarg);
1093 disable_one_graph(optarg);
1096 blktrace_device = strdup(optarg);
1099 program_to_run = strdup(optarg);
1104 movie_style = lookup_movie_style(optarg);
1105 if (movie_style < 0) {
1106 fprintf(stderr, "Unknown movie style %s\n", optarg);
1110 fprintf(stderr, "Using movie style: %s\n",
1111 movie_styles[movie_style]);
1114 opt_graph_height = atoi(optarg);
1117 opt_graph_width = atoi(optarg);
1120 columns = atoi(optarg);
1134 int main(int ac, char **av)
1137 int min_seconds = 0;
1138 int max_seconds = 0;
1140 u64 min_offset = ~(u64)0;
1141 struct trace_file *tf;
1145 init_io_hash_table();
1147 enable_all_graphs();
1149 parse_options(ac, av);
1151 last_active_graph = last_graph();
1153 set_io_graph_scale(256);
1154 if (movie_style == MOVIE_SPINDLE)
1155 set_graph_size(750, 550);
1157 set_graph_size(700, 400);
1160 * the plots in the movie don't have a seconds
1161 * line yet, this makes us skip it
1163 last_active_graph = TOTAL_GRAPHS + 1;
1165 if (opt_graph_height)
1166 set_graph_height(opt_graph_height);
1168 if (opt_graph_width)
1169 set_graph_width(opt_graph_width);
1171 if (list_empty(&all_traces)) {
1172 fprintf(stderr, "No traces found, exiting\n");
1176 if (blktrace_device) {
1177 ret = start_blktrace(blktrace_device, blktrace_outfile,
1180 fprintf(stderr, "exiting due to blktrace failure\n");
1183 start_mpstat(blktrace_outfile);
1184 if (program_to_run) {
1185 ret = run_program(program_to_run);
1187 fprintf(stderr, "failed to run %s\n",
1192 blktrace_to_dump(blktrace_outfile);
1194 /* no program specified, just wait for
1201 /* step one, read all the traces */
1204 /* step two, find the maxes for time and offset */
1205 list_for_each_entry(tf, &all_traces, list)
1206 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1207 /* push the max we found into all the tfs */
1208 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1210 /* alloc graphing structs for all the traces */
1211 setup_trace_file_graphs();
1213 /* run through all the traces and read their events */
1214 read_trace_events();
1216 plot = alloc_plot();
1219 plot_io_movie(plot);
1223 set_plot_output(plot, output_filename);
1225 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1226 set_legend_width(longest_label + strlen("writes"));
1227 else if (num_traces > 1)
1228 set_legend_width(longest_label);
1230 set_legend_width(0);
1232 get_graph_size(&cols, &rows);
1234 plot->add_xlabel = 1;
1235 set_plot_title(plot, graph_title);
1237 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1238 plot->add_xlabel = 0;
1241 set_graph_size(cols / columns, rows);
1242 num_xticks /= columns;
1247 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1248 plot_tput(plot, min_seconds, max_seconds);
1250 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1251 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1252 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1254 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1255 plot_cpu(plot, max_seconds, "CPU System Time",
1256 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1258 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1259 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1260 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1262 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1263 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1264 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1266 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1267 plot_cpu(plot, max_seconds, "CPU User Time",
1268 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1270 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1271 plot_latency(plot, min_seconds, max_seconds);
1273 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1274 plot_queue_depth(plot, min_seconds, max_seconds);
1276 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1277 plot_iops(plot, min_seconds, max_seconds);
1281 close_plot_file(plot);