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 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 = 7;
54 static int num_yticks = 4;
56 static double min_time = 0;
57 static double max_time = DBL_MAX;
58 static unsigned long long min_mb = 0;
59 static unsigned long long max_mb = ULLONG_MAX >> 20;
61 int plot_io_action = 0;
62 int io_per_process = 0;
63 unsigned int longest_proc_name = 0;
66 * this doesn't include the IO graph,
67 * but it counts the other graphs as they go out
69 static int total_graphs_written = 1;
80 QUEUE_DEPTH_GRAPH_INDEX,
94 static char *graphs_by_name[] = {
113 char *movie_styles[] = {
119 static int movie_style = 0;
121 static int lookup_movie_style(char *str)
125 for (i = 0; i < NUM_MOVIE_STYLES; i++) {
126 if (strcmp(str, movie_styles[i]) == 0)
132 static int active_graphs[TOTAL_GRAPHS];
133 static int last_active_graph = IOPS_GRAPH_INDEX;
135 static int label_index = 0;
136 static int num_traces = 0;
137 static int longest_label = 0;
139 static char *graph_title = "";
140 static char *output_filename = "trace.svg";
141 static char *blktrace_device = NULL;
142 static char *blktrace_outfile = "trace";
143 static char *blktrace_dest_dir = ".";
144 static char *program_to_run = NULL;
146 static void alloc_mpstat_gld(struct trace_file *tf)
148 struct graph_line_data **ptr;
150 if (tf->trace->mpstat_num_cpus == 0)
153 ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
154 sizeof(struct graph_line_data *));
156 perror("Unable to allocate mpstat arrays\n");
159 tf->mpstat_gld = ptr;
162 static void enable_all_graphs(void)
165 for (i = 0; i < TOTAL_GRAPHS; i++)
166 active_graphs[i] = 1;
169 static void disable_all_graphs(void)
172 for (i = 0; i < TOTAL_GRAPHS; i++)
173 active_graphs[i] = 0;
176 static int enable_one_graph(char *name)
179 for (i = 0; i < TOTAL_GRAPHS; i++) {
180 if (strcmp(name, graphs_by_name[i]) == 0) {
181 active_graphs[i] = 1;
188 static int disable_one_graph(char *name)
191 for (i = 0; i < TOTAL_GRAPHS; i++) {
192 if (strcmp(name, graphs_by_name[i]) == 0) {
193 active_graphs[i] = 0;
200 static int last_graph(void)
203 for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
204 if (active_graphs[i]) {
211 static int graphs_left(int cur)
215 for (i = cur; i < TOTAL_GRAPHS; i++) {
216 if (active_graphs[i])
222 static char * join_path(char *dest_dir, char *filename)
224 /* alloc 2 extra bytes for '/' and '\0' */
225 char *path = malloc(strlen(dest_dir) + strlen(filename) + 2);
226 sprintf(path, "%s%s%s", dest_dir, "/", filename);
231 static void add_trace_file(char *filename)
233 struct trace_file *tf;
235 tf = calloc(1, sizeof(*tf));
237 fprintf(stderr, "Unable to allocate memory\n");
241 tf->filename = strdup(filename);
242 list_add_tail(&tf->list, &all_traces);
243 tf->line_color = "black";
247 static void setup_trace_file_graphs(void)
249 struct trace_file *tf;
257 list_for_each_entry(tf, &all_traces, list) {
258 tf->tput_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
259 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
260 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
261 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
262 tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
263 tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
264 tf->io_plots_allocated = alloc_ptrs;
266 if (tf->trace->mpstat_num_cpus == 0)
269 alloc_mpstat_gld(tf);
270 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
272 alloc_line_data(tf->mpstat_min_seconds,
273 tf->mpstat_max_seconds,
274 tf->mpstat_max_seconds);
275 tf->mpstat_gld[i]->max = 100;
280 static void read_traces(void)
282 struct trace_file *tf;
291 list_for_each_entry(tf, &all_traces, list) {
292 path = join_path(blktrace_dest_dir, tf->filename);
293 trace = open_trace(path);
297 last_time = find_last_time(trace);
299 tf->max_seconds = SECONDS(last_time);
300 tf->stop_seconds = SECONDS(last_time);
301 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
302 &max_bank, &max_bank_offset);
303 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
304 tf->min_offset = ymin;
305 tf->max_offset = ymax;
307 read_mpstat(trace, path);
308 tf->mpstat_stop_seconds = trace->mpstat_seconds;
309 tf->mpstat_max_seconds = trace->mpstat_seconds;
310 if (tf->mpstat_max_seconds)
316 static void pick_line_graph_color(void)
318 struct trace_file *tf;
321 list_for_each_entry(tf, &all_traces, list) {
322 for (i = 0; i < tf->io_plots; i++) {
323 if (tf->gdd_reads[i]) {
324 tf->line_color = tf->gdd_reads[i]->color;
327 if (tf->gdd_writes[i]) {
328 tf->line_color = tf->gdd_writes[i]->color;
335 static void read_trace_events(void)
338 struct trace_file *tf;
343 double user, sys, iowait, irq, soft;
344 double max_user = 0, max_sys = 0, max_iowait = 0,
345 max_irq = 0, max_soft = 0;
347 list_for_each_entry(tf, &all_traces, list) {
352 add_tput(trace, tf->tput_gld);
353 add_iop(trace, tf->iop_gld);
355 add_pending_io(trace, tf->queue_depth_gld);
356 add_completed_io(trace, tf->latency_gld);
357 ret = next_record(trace);
362 list_for_each_entry(tf, &all_traces, list) {
365 if (trace->mpstat_num_cpus == 0)
370 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
371 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
372 ret = read_mpstat_event(trace, &user, &sys,
373 &iowait, &irq, &soft);
376 if (next_mpstat_line(trace))
385 if (iowait > max_iowait)
388 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
389 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
390 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
391 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
392 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
394 if (next_mpstat(trace) == NULL)
400 list_for_each_entry(tf, &all_traces, list) {
403 if (trace->mpstat_num_cpus == 0)
406 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
407 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
408 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
409 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
410 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
415 static void set_trace_label(char *label)
418 struct trace_file *tf;
419 int len = strlen(label);
421 if (len > longest_label)
424 list_for_each_entry(tf, &all_traces, list) {
425 if (cur == label_index) {
426 tf->label = strdup(label);
434 static void set_blktrace_outfile(char *arg)
436 char *s = strdup(arg);
437 char *last_dot = strrchr(s, '.');
440 if (strcmp(last_dot, ".dump") == 0)
443 blktrace_outfile = s;
447 static void compare_minmax_tf(struct trace_file *tf, int *max_seconds, u64 *min_offset, u64 *max_offset)
449 if (tf->max_seconds > *max_seconds)
450 *max_seconds = tf->max_seconds;
451 if (tf->max_offset > *max_offset)
452 *max_offset = tf->max_offset;
453 if (tf->min_offset < *min_offset)
454 *min_offset = tf->min_offset;
457 static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
459 struct trace_file *tf;
461 list_for_each_entry(tf, &all_traces, list) {
462 tf->min_seconds = min_seconds;
463 tf->max_seconds = max_seconds;
464 if (tf->stop_seconds > max_seconds)
465 tf->stop_seconds = max_seconds;
466 if (tf->mpstat_max_seconds) {
467 tf->mpstat_min_seconds = min_seconds;
468 tf->mpstat_max_seconds = max_seconds;
469 if (tf->mpstat_stop_seconds > max_seconds)
470 tf->mpstat_stop_seconds = max_seconds;
472 tf->min_offset = min_offset;
473 tf->max_offset = max_offset;
477 static char *create_movie_temp_dir(void)
480 char *pattern = strdup("io-movie-XXXXXX");;
482 ret = mkdtemp(pattern);
484 perror("Unable to create temp directory for movie files");
490 static struct pid_plot_history *alloc_pid_plot_history(char *color)
492 struct pid_plot_history *pph;
494 pph = calloc(1, sizeof(struct pid_plot_history));
496 perror("memory allocation failed");
499 pph->history = malloc(sizeof(double) * 4096);
501 perror("memory allocation failed");
504 pph->history_len = 4096;
510 static struct plot_history *alloc_plot_history(struct trace_file *tf)
512 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
516 perror("memory allocation failed");
519 ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
520 if (!ph->read_pid_history) {
521 perror("memory allocation failed");
524 ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
525 if (!ph->write_pid_history) {
526 perror("memory allocation failed");
529 ph->pid_history_count = tf->io_plots;
530 for (i = 0; i < tf->io_plots; i++) {
531 if (tf->gdd_reads[i])
532 ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
533 if (tf->gdd_writes[i])
534 ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
539 LIST_HEAD(movie_history);
540 int num_histories = 0;
542 static void free_plot_history(struct plot_history *ph)
546 for (pid = 0; pid < ph->pid_history_count; pid++) {
547 if (ph->read_pid_history[pid])
548 free(ph->read_pid_history[pid]);
549 if (ph->write_pid_history[pid])
550 free(ph->write_pid_history[pid]);
552 free(ph->read_pid_history);
553 free(ph->write_pid_history);
557 static void add_history(struct plot_history *ph, struct list_head *list)
559 struct plot_history *entry;
561 list_add_tail(&ph->list, list);
564 if (num_histories > 12) {
566 entry = list_entry(list->next, struct plot_history, list);
567 list_del(&entry->list);
568 free_plot_history(entry);
572 static void plot_movie_history(struct plot *plot, struct list_head *list)
574 struct plot_history *ph;
577 if (num_histories > 2)
578 rewind_spindle_steps(num_histories - 1);
580 list_for_each_entry(ph, list, list) {
581 for (pid = 0; pid < ph->pid_history_count; pid++) {
582 if (ph->read_pid_history[pid]) {
583 if (movie_style == MOVIE_SPINDLE) {
584 svg_io_graph_movie_array_spindle(plot,
585 ph->read_pid_history[pid]);
587 svg_io_graph_movie_array(plot,
588 ph->read_pid_history[pid]);
591 if (ph->write_pid_history[pid]) {
592 if (movie_style == MOVIE_SPINDLE) {
593 svg_io_graph_movie_array_spindle(plot,
594 ph->write_pid_history[pid]);
596 svg_io_graph_movie_array(plot,
597 ph->write_pid_history[pid]);
604 static void free_all_plot_history(struct list_head *head)
606 struct plot_history *entry;
608 while (!list_empty(head)) {
609 entry = list_entry(head->next, struct plot_history, list);
610 list_del(&entry->list);
611 free_plot_history(entry);
615 static int count_io_plot_types(void)
617 struct trace_file *tf;
619 int total_io_types = 0;
621 list_for_each_entry(tf, &all_traces, list) {
622 for (i = 0; i < tf->io_plots; i++) {
623 if (tf->gdd_reads[i])
625 if (tf->gdd_writes[i])
629 return total_io_types;
632 static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
634 struct trace_file *tf;
637 if (active_graphs[IO_GRAPH_INDEX] == 0)
642 svg_alloc_legend(plot, count_io_plot_types() * 2);
644 set_plot_label(plot, "Device IO");
645 set_ylabel(plot, "Offset (MB)");
646 set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
647 max_offset / (1024 * 1024), "");
648 set_xticks(plot, num_xticks, min_seconds, max_seconds);
650 list_for_each_entry(tf, &all_traces, list) {
657 strcpy(label, tf->label);
661 pos = label + strlen(label);
663 for (i = 0; i < tf->io_plots; i++) {
664 if (tf->gdd_reads[i]) {
665 svg_io_graph(plot, tf->gdd_reads[i]);
667 strcpy(pos, tf->gdd_reads[i]->label);
668 svg_add_legend(plot, label, " Reads", tf->gdd_reads[i]->color);
671 if (tf->gdd_writes[i]) {
672 svg_io_graph(plot, tf->gdd_writes[i]);
674 strcpy(pos, tf->gdd_writes[i]->label);
675 svg_add_legend(plot, label, " Writes", tf->gdd_writes[i]->color);
679 if (plot->add_xlabel)
680 set_xlabel(plot, "Time (seconds)");
681 svg_write_legend(plot);
685 static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
687 struct trace_file *tf;
692 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
696 svg_alloc_legend(plot, num_traces);
697 list_for_each_entry(tf, &all_traces, list) {
698 if (tf->tput_gld->max > max)
699 max = tf->tput_gld->max;
701 list_for_each_entry(tf, &all_traces, list)
702 tf->tput_gld->max = max;
705 set_plot_label(plot, "Throughput");
707 tf = list_entry(all_traces.next, struct trace_file, list);
709 scale_line_graph_bytes(&max, &units, 1024);
710 sprintf(line, "%sB/s", units);
711 set_ylabel(plot, line);
712 set_yticks(plot, num_yticks, 0, max, "");
713 set_xticks(plot, num_xticks, min_seconds, max_seconds);
715 list_for_each_entry(tf, &all_traces, list) {
716 svg_line_graph(plot, tf->tput_gld, tf->line_color, 0, 0);
718 svg_add_legend(plot, tf->label, "", tf->line_color);
721 if (plot->add_xlabel)
722 set_xlabel(plot, "Time (seconds)");
724 svg_write_legend(plot);
726 total_graphs_written++;
729 static void plot_cpu(struct plot *plot, int max_seconds, char *label,
730 int active_index, int gld_index)
732 struct trace_file *tf;
741 if (active_graphs[active_index] == 0)
744 list_for_each_entry(tf, &all_traces, list) {
745 if (tf->trace->mpstat_num_cpus > max)
746 max = tf->trace->mpstat_num_cpus;
751 tf = list_entry(all_traces.next, struct trace_file, list);
753 ymax = tf->mpstat_gld[gld_index]->max;
757 svg_alloc_legend(plot, num_traces * max);
760 set_plot_label(plot, label);
762 max_seconds = tf->mpstat_max_seconds;
764 set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
765 set_ylabel(plot, "Percent");
766 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
769 list_for_each_entry(tf, &all_traces, list) {
770 if (tf->mpstat_gld == 0)
772 for (i = tf->mpstat_gld[0]->min_seconds;
773 i < tf->mpstat_gld[0]->stop_seconds; i++) {
774 if (tf->mpstat_gld[gld_index]->data[i].count) {
775 avg += (tf->mpstat_gld[gld_index]->data[i].sum /
776 tf->mpstat_gld[gld_index]->data[i].count);
779 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
780 tf->mpstat_gld[gld_index]->min_seconds;
781 color = pick_cpu_color();
782 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
783 svg_add_legend(plot, tf->label, " avg", color);
785 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
786 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
789 for (gld_i = gld->min_seconds;
790 gld_i < gld->stop_seconds; gld_i++) {
791 if (gld->data[i].count) {
792 this_avg += gld->data[i].sum /
797 this_avg /= gld->stop_seconds - gld->min_seconds;
799 for (gld_i = gld->min_seconds;
800 gld_i < gld->stop_seconds; gld_i++) {
803 if (gld->data[gld_i].count == 0)
805 val = (double)gld->data[gld_i].sum /
806 gld->data[gld_i].count;
808 if (this_avg > avg + 30 || val > 95) {
809 color = pick_cpu_color();
810 svg_line_graph(plot, gld, color, avg + 30, 95);
811 snprintf(line, line_len, " CPU %d\n", i - 1);
812 svg_add_legend(plot, tf->label, line, color);
821 if (plot->add_xlabel)
822 set_xlabel(plot, "Time (seconds)");
824 if (!plot->no_legend) {
825 svg_write_legend(plot);
826 svg_free_legend(plot);
829 total_graphs_written++;
832 static void plot_queue_depth(struct plot *plot, int min_seconds, int max_seconds)
834 struct trace_file *tf;
836 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
840 set_plot_label(plot, "Queue Depth");
842 svg_alloc_legend(plot, num_traces);
844 tf = list_entry(all_traces.next, struct trace_file, list);
845 set_ylabel(plot, "Pending IO");
846 set_yticks(plot, num_yticks, 0, tf->queue_depth_gld->max, "");
847 set_xticks(plot, num_xticks, min_seconds, max_seconds);
849 list_for_each_entry(tf, &all_traces, list) {
850 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
852 svg_add_legend(plot, tf->label, "", tf->line_color);
855 if (plot->add_xlabel)
856 set_xlabel(plot, "Time (seconds)");
858 svg_write_legend(plot);
860 total_graphs_written++;
863 static void convert_movie_files(char *movie_dir)
865 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
866 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
871 static void mencode_movie(char *movie_dir)
873 fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
874 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
875 "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
879 static void tencode_movie(char *movie_dir)
881 fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
882 snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
883 output_filename, movie_dir, output_filename);
887 static void encode_movie(char *movie_dir)
889 char *last_dot = strrchr(output_filename, '.');
892 (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
893 tencode_movie(movie_dir);
895 mencode_movie(movie_dir);
898 static void cleanup_movie(char *movie_dir)
900 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
901 snprintf(line, line_len, "rm %s/*", movie_dir);
904 snprintf(line, line_len, "rmdir %s", movie_dir);
908 static void plot_io_movie(struct plot *plot)
910 struct trace_file *tf;
911 char *movie_dir = create_movie_temp_dir();
913 struct plot_history *history;
916 int movie_frames_per_sec = 20;
917 int total_frames = movie_len * movie_frames_per_sec;
920 int graph_width_factor = 5;
923 get_graph_size(&cols, &rows);
924 batch_count = cols / total_frames;
926 if (batch_count == 0)
929 list_for_each_entry(tf, &all_traces, list) {
936 strcpy(label, tf->label);
940 pos = label + strlen(label);
944 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
945 set_plot_output(plot, line);
946 set_plot_title(plot, graph_title);
947 orig_y_offset = plot->start_y_offset;
951 set_graph_size(cols / graph_width_factor, rows / 8);
952 plot->timeline = i / graph_width_factor;
954 plot_tput(plot, tf->min_seconds,
957 plot_cpu(plot, tf->max_seconds,
958 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
960 plot->direction = PLOT_ACROSS;
961 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
963 /* movie graph starts here */
964 plot->start_y_offset = orig_y_offset;
965 set_graph_size(cols - cols / graph_width_factor, rows);
968 plot->direction = PLOT_DOWN;;
970 if (movie_style == MOVIE_SPINDLE)
971 setup_axis_spindle(plot);
975 svg_alloc_legend(plot, count_io_plot_types() * 2);
977 history = alloc_plot_history(tf);
980 for (pid = 0; pid < tf->io_plots; pid++) {
981 if (tf->gdd_reads[pid]) {
983 strcpy(pos, tf->gdd_reads[pid]->label);
984 svg_add_legend(plot, label, " Reads", tf->gdd_reads[pid]->color);
986 if (tf->gdd_writes[pid]) {
988 strcpy(pos, tf->gdd_writes[pid]->label);
989 svg_add_legend(plot, label, " Writes", tf->gdd_writes[pid]->color);
994 while (i < cols && batch_i < batch_count) {
995 for (pid = 0; pid < tf->io_plots; pid++) {
996 if (tf->gdd_reads[pid]) {
997 svg_io_graph_movie(tf->gdd_reads[pid],
998 history->read_pid_history[pid],
1001 if (tf->gdd_writes[pid]) {
1002 svg_io_graph_movie(tf->gdd_writes[pid],
1003 history->write_pid_history[pid],
1011 add_history(history, &movie_history);
1013 plot_movie_history(plot, &movie_history);
1015 svg_write_legend(plot);
1019 close_plot_file(plot);
1021 free_all_plot_history(&movie_history);
1023 convert_movie_files(movie_dir);
1024 encode_movie(movie_dir);
1025 cleanup_movie(movie_dir);
1029 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
1031 struct trace_file *tf;
1036 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1040 svg_alloc_legend(plot, num_traces);
1042 list_for_each_entry(tf, &all_traces, list) {
1043 if (tf->latency_gld->max > max)
1044 max = tf->latency_gld->max;
1047 list_for_each_entry(tf, &all_traces, list)
1048 tf->latency_gld->max = max;
1051 set_plot_label(plot, "IO Latency");
1053 tf = list_entry(all_traces.next, struct trace_file, list);
1055 scale_line_graph_time(&max, &units);
1056 sprintf(line, "latency (%ss)", units);
1057 set_ylabel(plot, line);
1058 set_yticks(plot, num_yticks, 0, max, "");
1059 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1061 list_for_each_entry(tf, &all_traces, list) {
1062 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1064 svg_add_legend(plot, tf->label, "", tf->line_color);
1067 if (plot->add_xlabel)
1068 set_xlabel(plot, "Time (seconds)");
1070 svg_write_legend(plot);
1072 total_graphs_written++;
1075 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
1077 struct trace_file *tf;
1081 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1084 list_for_each_entry(tf, &all_traces, list) {
1085 if (tf->iop_gld->max > max)
1086 max = tf->iop_gld->max;
1089 list_for_each_entry(tf, &all_traces, list)
1090 tf->iop_gld->max = max;
1093 set_plot_label(plot, "IOPs");
1095 svg_alloc_legend(plot, num_traces);
1097 tf = list_entry(all_traces.next, struct trace_file, list);
1099 scale_line_graph_bytes(&max, &units, 1000);
1100 set_ylabel(plot, "IO/s");
1102 set_yticks(plot, num_yticks, 0, max, units);
1103 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1105 list_for_each_entry(tf, &all_traces, list) {
1106 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1108 svg_add_legend(plot, tf->label, "", tf->line_color);
1111 if (plot->add_xlabel)
1112 set_xlabel(plot, "Time (seconds)");
1114 svg_write_legend(plot);
1117 total_graphs_written++;
1120 static void check_plot_columns(struct plot *plot, int index)
1124 if (columns > 1 && (total_graphs_written == 0 ||
1125 total_graphs_written % columns != 0)) {
1126 count = graphs_left(index);
1127 if (plot->direction == PLOT_DOWN) {
1128 plot->start_x_offset = 0;
1129 if (count <= columns)
1130 plot->add_xlabel = 1;
1132 plot->direction = PLOT_ACROSS;
1135 plot->direction = PLOT_DOWN;
1136 if (index == last_active_graph)
1137 plot->add_xlabel = 1;
1146 char *option_string = "T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:P";
1147 static struct option long_options[] = {
1148 {"columns", required_argument, 0, 'c'},
1149 {"title", required_argument, 0, 'T'},
1150 {"trace", required_argument, 0, 't'},
1151 {"output", required_argument, 0, 'o'},
1152 {"label", required_argument, 0, 'l'},
1153 {"rolling", required_argument, 0, 'r'},
1154 {"no-graph", required_argument, 0, 'N'},
1155 {"only-graph", required_argument, 0, 'O'},
1156 {"device", required_argument, 0, 'd'},
1157 {"blktrace-destination", required_argument, 0, 'D'},
1158 {"prog", required_argument, 0, 'p'},
1159 {"movie", optional_argument, 0, 'm'},
1160 {"width", required_argument, 0, 'w'},
1161 {"height", required_argument, 0, 'h'},
1162 {"xzoom", required_argument, 0, 'x'},
1163 {"yzoom", required_argument, 0, 'y'},
1164 {"io-plot-action", required_argument, 0, 'a'},
1165 {"per-process-io", no_argument, 0, 'P'},
1166 {"help", no_argument, 0, HELP_LONG_OPT},
1170 static void print_usage(void)
1172 fprintf(stderr, "iowatcher usage:\n"
1173 "\t-d (--device): device for blktrace to trace\n"
1174 "\t-D (--blktrace-destination): destination for blktrace\n"
1175 "\t-t (--trace): trace file name (more than one allowed)\n"
1176 "\t-l (--label): trace label in the graph\n"
1177 "\t-o (--output): output file name (SVG only)\n"
1178 "\t-p (--prog): program to run while blktrace is run\n"
1179 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1180 "\t-r (--rolling): number of seconds in the rolling averge\n"
1181 "\t-T (--title): graph title\n"
1182 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1183 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1184 "\t-O (--only-graph): add a single graph to the output\n"
1185 "\t-h (--height): set the height of each graph\n"
1186 "\t-w (--width): set the width of each graph\n"
1187 "\t-c (--columns): numbers of columns in graph output\n"
1188 "\t-x (--xzoom): limit processed time to min:max\n"
1189 "\t-y (--yzoom): limit processed sectors to min:max\n"
1190 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1191 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1196 static int parse_double_range(char *str, double *min, double *max)
1200 /* Empty lower bound - leave original value */
1201 if (str[0] != ':') {
1202 *min = strtod(str, &end);
1203 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1209 /* Empty upper bound - leave original value */
1211 *max = strtod(end+1, &end);
1212 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1222 static int parse_ull_range(char *str, unsigned long long *min,
1223 unsigned long long *max)
1227 /* Empty lower bound - leave original value */
1228 if (str[0] != ':') {
1229 *min = strtoull(str, &end, 10);
1230 if (*min == ULLONG_MAX && errno == ERANGE)
1236 /* Empty upper bound - leave original value */
1238 *max = strtoull(end+1, &end, 10);
1239 if (*max == ULLONG_MAX && errno == ERANGE)
1249 static int parse_options(int ac, char **av)
1255 // int this_option_optind = optind ? optind : 1;
1256 int option_index = 0;
1258 c = getopt_long(ac, av, option_string,
1259 long_options, &option_index);
1266 graph_title = strdup(optarg);
1269 add_trace_file(optarg);
1270 set_blktrace_outfile(optarg);
1273 output_filename = strdup(optarg);
1276 set_trace_label(optarg);
1279 set_rolling_avg(atoi(optarg));
1283 disable_all_graphs();
1286 enable_one_graph(optarg);
1289 disable_one_graph(optarg);
1292 blktrace_device = strdup(optarg);
1295 blktrace_dest_dir = strdup(optarg);
1296 if (!strcmp(blktrace_dest_dir, "")) {
1297 fprintf(stderr, "Need a directory\n");
1302 program_to_run = strdup(optarg);
1307 movie_style = lookup_movie_style(optarg);
1308 if (movie_style < 0) {
1309 fprintf(stderr, "Unknown movie style %s\n", optarg);
1313 fprintf(stderr, "Using movie style: %s\n",
1314 movie_styles[movie_style]);
1317 opt_graph_height = atoi(optarg);
1320 opt_graph_width = atoi(optarg);
1323 columns = atoi(optarg);
1326 if (parse_double_range(optarg, &min_time, &max_time)
1328 fprintf(stderr, "Cannot parse time range %s\n",
1334 if (parse_ull_range(optarg, &min_mb, &max_mb)
1337 "Cannot parse offset range %s\n",
1341 if (max_mb > ULLONG_MAX >> 20) {
1343 "Upper range limit too big."
1344 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1349 if (strlen(optarg) != 1) {
1351 fprintf(stderr, "Action must be one of Q, D, C.");
1354 plot_io_action = action_char_to_num(optarg[0]);
1355 if (plot_io_action < 0)
1372 static void dest_mkdir()
1376 ret = mkdir(blktrace_dest_dir, 0777);
1377 if (ret && errno != EEXIST) {
1378 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1383 int main(int ac, char **av)
1386 int min_seconds = 0;
1387 int max_seconds = 0;
1389 u64 min_offset = ~(u64)0;
1390 struct trace_file *tf;
1394 init_io_hash_table();
1395 init_process_hash_table();
1397 enable_all_graphs();
1399 parse_options(ac, av);
1401 last_active_graph = last_graph();
1403 set_io_graph_scale(256);
1404 if (movie_style == MOVIE_SPINDLE)
1405 set_graph_size(750, 550);
1407 set_graph_size(700, 400);
1410 * the plots in the movie don't have a seconds
1411 * line yet, this makes us skip it
1413 last_active_graph = TOTAL_GRAPHS + 1;
1415 if (opt_graph_height)
1416 set_graph_height(opt_graph_height);
1418 if (opt_graph_width)
1419 set_graph_width(opt_graph_width);
1421 if (list_empty(&all_traces)) {
1422 fprintf(stderr, "No traces found, exiting\n");
1426 if (blktrace_device) {
1430 path = join_path(blktrace_dest_dir, blktrace_outfile);
1432 ret = start_blktrace(blktrace_device, blktrace_outfile,
1435 fprintf(stderr, "exiting due to blktrace failure\n");
1440 if (program_to_run) {
1441 ret = run_program(program_to_run);
1443 fprintf(stderr, "failed to run %s\n",
1448 blktrace_to_dump(path);
1450 /* no program specified, just wait for
1458 /* step one, read all the traces */
1461 /* step two, find the maxes for time and offset */
1462 list_for_each_entry(tf, &all_traces, list)
1463 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1464 min_seconds = min_time;
1465 if (max_seconds > max_time)
1466 max_seconds = ceil(max_time);
1467 if (min_offset < min_mb << 20)
1468 min_offset = min_mb << 20;
1469 if (max_offset > max_mb << 20)
1470 max_offset = max_mb << 20;
1472 /* push the max we found into all the tfs */
1473 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1475 /* alloc graphing structs for all the traces */
1476 setup_trace_file_graphs();
1478 /* run through all the traces and read their events */
1479 read_trace_events();
1481 pick_line_graph_color();
1483 plot = alloc_plot();
1486 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1487 plot_io_movie(plot);
1491 set_plot_output(plot, output_filename);
1493 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1494 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1495 else if (num_traces > 1)
1496 set_legend_width(longest_label);
1498 set_legend_width(0);
1500 get_graph_size(&cols, &rows);
1502 plot->add_xlabel = 1;
1503 set_plot_title(plot, graph_title);
1505 check_plot_columns(plot, IO_GRAPH_INDEX);
1506 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1507 plot->add_xlabel = 0;
1510 set_graph_size(cols / columns, rows);
1511 num_xticks /= columns;
1518 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1519 plot_tput(plot, min_seconds, max_seconds);
1521 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1522 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1523 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1525 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1526 plot_cpu(plot, max_seconds, "CPU System Time",
1527 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1529 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1530 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1531 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1533 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1534 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1535 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1537 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1538 plot_cpu(plot, max_seconds, "CPU User Time",
1539 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1541 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1542 plot_latency(plot, min_seconds, max_seconds);
1544 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1545 plot_queue_depth(plot, min_seconds, max_seconds);
1547 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1548 plot_iops(plot, min_seconds, max_seconds);
1552 close_plot_file(plot);