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\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 cleanup_movie(char *movie_dir)
881 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
882 snprintf(line, line_len, "rm %s/*", movie_dir);
885 snprintf(line, line_len, "rmdir %s", movie_dir);
889 static void plot_io_movie(struct plot *plot)
891 struct trace_file *tf;
892 char *movie_dir = create_movie_temp_dir();
894 struct plot_history *history;
897 int movie_frames_per_sec = 20;
898 int total_frames = movie_len * movie_frames_per_sec;
901 int graph_width_factor = 5;
904 get_graph_size(&cols, &rows);
905 batch_count = cols / total_frames;
907 if (batch_count == 0)
910 list_for_each_entry(tf, &all_traces, list) {
917 strcpy(label, tf->label);
921 pos = label + strlen(label);
925 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
926 set_plot_output(plot, line);
927 set_plot_title(plot, graph_title);
928 orig_y_offset = plot->start_y_offset;
932 set_graph_size(cols / graph_width_factor, rows / 8);
933 plot->timeline = i / graph_width_factor;
935 plot_tput(plot, tf->min_seconds,
938 plot_cpu(plot, tf->max_seconds,
939 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
941 plot->direction = PLOT_ACROSS;
942 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
944 /* movie graph starts here */
945 plot->start_y_offset = orig_y_offset;
946 set_graph_size(cols - cols / graph_width_factor, rows);
949 plot->direction = PLOT_DOWN;;
951 if (movie_style == MOVIE_SPINDLE)
952 setup_axis_spindle(plot);
956 svg_alloc_legend(plot, count_io_plot_types() * 2);
958 history = alloc_plot_history(tf);
961 for (pid = 0; pid < tf->io_plots; pid++) {
962 if (tf->gdd_reads[pid]) {
964 strcpy(pos, tf->gdd_reads[pid]->label);
965 svg_add_legend(plot, label, " Reads", tf->gdd_reads[pid]->color);
967 if (tf->gdd_writes[pid]) {
969 strcpy(pos, tf->gdd_writes[pid]->label);
970 svg_add_legend(plot, label, " Writes", tf->gdd_writes[pid]->color);
975 while (i < cols && batch_i < batch_count) {
976 for (pid = 0; pid < tf->io_plots; pid++) {
977 if (tf->gdd_reads[pid]) {
978 svg_io_graph_movie(tf->gdd_reads[pid],
979 history->read_pid_history[pid],
982 if (tf->gdd_writes[pid]) {
983 svg_io_graph_movie(tf->gdd_writes[pid],
984 history->write_pid_history[pid],
992 add_history(history, &movie_history);
994 plot_movie_history(plot, &movie_history);
996 svg_write_legend(plot);
1000 close_plot_file(plot);
1002 free_all_plot_history(&movie_history);
1004 convert_movie_files(movie_dir);
1005 mencode_movie(movie_dir);
1006 cleanup_movie(movie_dir);
1010 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
1012 struct trace_file *tf;
1017 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1021 svg_alloc_legend(plot, num_traces);
1023 list_for_each_entry(tf, &all_traces, list) {
1024 if (tf->latency_gld->max > max)
1025 max = tf->latency_gld->max;
1028 list_for_each_entry(tf, &all_traces, list)
1029 tf->latency_gld->max = max;
1032 set_plot_label(plot, "IO Latency");
1034 tf = list_entry(all_traces.next, struct trace_file, list);
1036 scale_line_graph_time(&max, &units);
1037 sprintf(line, "latency (%ss)", units);
1038 set_ylabel(plot, line);
1039 set_yticks(plot, num_yticks, 0, max, "");
1040 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1042 list_for_each_entry(tf, &all_traces, list) {
1043 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1045 svg_add_legend(plot, tf->label, "", tf->line_color);
1048 if (plot->add_xlabel)
1049 set_xlabel(plot, "Time (seconds)");
1051 svg_write_legend(plot);
1053 total_graphs_written++;
1056 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
1058 struct trace_file *tf;
1062 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1065 list_for_each_entry(tf, &all_traces, list) {
1066 if (tf->iop_gld->max > max)
1067 max = tf->iop_gld->max;
1070 list_for_each_entry(tf, &all_traces, list)
1071 tf->iop_gld->max = max;
1074 set_plot_label(plot, "IOPs");
1076 svg_alloc_legend(plot, num_traces);
1078 tf = list_entry(all_traces.next, struct trace_file, list);
1080 scale_line_graph_bytes(&max, &units, 1000);
1081 set_ylabel(plot, "IO/s");
1083 set_yticks(plot, num_yticks, 0, max, units);
1084 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1086 list_for_each_entry(tf, &all_traces, list) {
1087 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1089 svg_add_legend(plot, tf->label, "", tf->line_color);
1092 if (plot->add_xlabel)
1093 set_xlabel(plot, "Time (seconds)");
1095 svg_write_legend(plot);
1098 total_graphs_written++;
1101 static void check_plot_columns(struct plot *plot, int index)
1105 if (columns > 1 && (total_graphs_written == 0 ||
1106 total_graphs_written % columns != 0)) {
1107 count = graphs_left(index);
1108 if (plot->direction == PLOT_DOWN) {
1109 plot->start_x_offset = 0;
1110 if (count <= columns)
1111 plot->add_xlabel = 1;
1113 plot->direction = PLOT_ACROSS;
1116 plot->direction = PLOT_DOWN;
1117 if (index == last_active_graph)
1118 plot->add_xlabel = 1;
1127 char *option_string = "T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:P";
1128 static struct option long_options[] = {
1129 {"columns", required_argument, 0, 'c'},
1130 {"title", required_argument, 0, 'T'},
1131 {"trace", required_argument, 0, 't'},
1132 {"output", required_argument, 0, 'o'},
1133 {"label", required_argument, 0, 'l'},
1134 {"rolling", required_argument, 0, 'r'},
1135 {"no-graph", required_argument, 0, 'N'},
1136 {"only-graph", required_argument, 0, 'O'},
1137 {"device", required_argument, 0, 'd'},
1138 {"blktrace-destination", required_argument, 0, 'D'},
1139 {"prog", required_argument, 0, 'p'},
1140 {"movie", optional_argument, 0, 'm'},
1141 {"width", required_argument, 0, 'w'},
1142 {"height", required_argument, 0, 'h'},
1143 {"xzoom", required_argument, 0, 'x'},
1144 {"yzoom", required_argument, 0, 'y'},
1145 {"io-plot-action", required_argument, 0, 'a'},
1146 {"per-process-io", no_argument, 0, 'P'},
1147 {"help", no_argument, 0, HELP_LONG_OPT},
1151 static void print_usage(void)
1153 fprintf(stderr, "iowatcher usage:\n"
1154 "\t-d (--device): device for blktrace to trace\n"
1155 "\t-D (--blktrace-destination): destination for blktrace\n"
1156 "\t-t (--trace): trace file name (more than one allowed)\n"
1157 "\t-l (--label): trace label in the graph\n"
1158 "\t-o (--output): output file name (SVG only)\n"
1159 "\t-p (--prog): program to run while blktrace is run\n"
1160 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1161 "\t-r (--rolling): number of seconds in the rolling averge\n"
1162 "\t-T (--title): graph title\n"
1163 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1164 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1165 "\t-O (--only-graph): add a single graph to the output\n"
1166 "\t-h (--height): set the height of each graph\n"
1167 "\t-w (--width): set the width of each graph\n"
1168 "\t-c (--columns): numbers of columns in graph output\n"
1169 "\t-x (--xzoom): limit processed time to min:max\n"
1170 "\t-y (--yzoom): limit processed sectors to min:max\n"
1171 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1172 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1177 static int parse_double_range(char *str, double *min, double *max)
1181 /* Empty lower bound - leave original value */
1182 if (str[0] != ':') {
1183 *min = strtod(str, &end);
1184 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1190 /* Empty upper bound - leave original value */
1192 *max = strtod(end+1, &end);
1193 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1203 static int parse_ull_range(char *str, unsigned long long *min,
1204 unsigned long long *max)
1208 /* Empty lower bound - leave original value */
1209 if (str[0] != ':') {
1210 *min = strtoull(str, &end, 10);
1211 if (*min == ULLONG_MAX && errno == ERANGE)
1217 /* Empty upper bound - leave original value */
1219 *max = strtoull(end+1, &end, 10);
1220 if (*max == ULLONG_MAX && errno == ERANGE)
1230 static int parse_options(int ac, char **av)
1236 // int this_option_optind = optind ? optind : 1;
1237 int option_index = 0;
1239 c = getopt_long(ac, av, option_string,
1240 long_options, &option_index);
1247 graph_title = strdup(optarg);
1250 add_trace_file(optarg);
1251 set_blktrace_outfile(optarg);
1254 output_filename = strdup(optarg);
1257 set_trace_label(optarg);
1260 set_rolling_avg(atoi(optarg));
1264 disable_all_graphs();
1267 enable_one_graph(optarg);
1270 disable_one_graph(optarg);
1273 blktrace_device = strdup(optarg);
1276 blktrace_dest_dir = strdup(optarg);
1277 if (!strcmp(blktrace_dest_dir, "")) {
1278 fprintf(stderr, "Need a directory\n");
1283 program_to_run = strdup(optarg);
1288 movie_style = lookup_movie_style(optarg);
1289 if (movie_style < 0) {
1290 fprintf(stderr, "Unknown movie style %s\n", optarg);
1294 fprintf(stderr, "Using movie style: %s\n",
1295 movie_styles[movie_style]);
1298 opt_graph_height = atoi(optarg);
1301 opt_graph_width = atoi(optarg);
1304 columns = atoi(optarg);
1307 if (parse_double_range(optarg, &min_time, &max_time)
1309 fprintf(stderr, "Cannot parse time range %s\n",
1315 if (parse_ull_range(optarg, &min_mb, &max_mb)
1318 "Cannot parse offset range %s\n",
1322 if (max_mb > ULLONG_MAX >> 20) {
1324 "Upper range limit too big."
1325 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1330 if (strlen(optarg) != 1) {
1332 fprintf(stderr, "Action must be one of Q, D, C.");
1335 plot_io_action = action_char_to_num(optarg[0]);
1336 if (plot_io_action < 0)
1353 static void dest_mkdir()
1357 ret = mkdir(blktrace_dest_dir, 0777);
1358 if (ret && errno != EEXIST) {
1359 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1364 int main(int ac, char **av)
1367 int min_seconds = 0;
1368 int max_seconds = 0;
1370 u64 min_offset = ~(u64)0;
1371 struct trace_file *tf;
1375 init_io_hash_table();
1376 init_process_hash_table();
1378 enable_all_graphs();
1380 parse_options(ac, av);
1382 last_active_graph = last_graph();
1384 set_io_graph_scale(256);
1385 if (movie_style == MOVIE_SPINDLE)
1386 set_graph_size(750, 550);
1388 set_graph_size(700, 400);
1391 * the plots in the movie don't have a seconds
1392 * line yet, this makes us skip it
1394 last_active_graph = TOTAL_GRAPHS + 1;
1396 if (opt_graph_height)
1397 set_graph_height(opt_graph_height);
1399 if (opt_graph_width)
1400 set_graph_width(opt_graph_width);
1402 if (list_empty(&all_traces)) {
1403 fprintf(stderr, "No traces found, exiting\n");
1407 if (blktrace_device) {
1411 path = join_path(blktrace_dest_dir, blktrace_outfile);
1413 ret = start_blktrace(blktrace_device, blktrace_outfile,
1416 fprintf(stderr, "exiting due to blktrace failure\n");
1421 if (program_to_run) {
1422 ret = run_program(program_to_run);
1424 fprintf(stderr, "failed to run %s\n",
1429 blktrace_to_dump(path);
1431 /* no program specified, just wait for
1439 /* step one, read all the traces */
1442 /* step two, find the maxes for time and offset */
1443 list_for_each_entry(tf, &all_traces, list)
1444 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1445 min_seconds = min_time;
1446 if (max_seconds > max_time)
1447 max_seconds = ceil(max_time);
1448 if (min_offset < min_mb << 20)
1449 min_offset = min_mb << 20;
1450 if (max_offset > max_mb << 20)
1451 max_offset = max_mb << 20;
1453 /* push the max we found into all the tfs */
1454 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1456 /* alloc graphing structs for all the traces */
1457 setup_trace_file_graphs();
1459 /* run through all the traces and read their events */
1460 read_trace_events();
1462 pick_line_graph_color();
1464 plot = alloc_plot();
1467 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1468 plot_io_movie(plot);
1472 set_plot_output(plot, output_filename);
1474 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1475 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1476 else if (num_traces > 1)
1477 set_legend_width(longest_label);
1479 set_legend_width(0);
1481 get_graph_size(&cols, &rows);
1483 plot->add_xlabel = 1;
1484 set_plot_title(plot, graph_title);
1486 check_plot_columns(plot, IO_GRAPH_INDEX);
1487 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1488 plot->add_xlabel = 0;
1491 set_graph_size(cols / columns, rows);
1492 num_xticks /= columns;
1499 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1500 plot_tput(plot, min_seconds, max_seconds);
1502 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1503 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1504 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1506 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1507 plot_cpu(plot, max_seconds, "CPU System Time",
1508 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1510 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1511 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1512 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1514 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1515 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1516 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1518 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1519 plot_cpu(plot, max_seconds, "CPU User Time",
1520 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1522 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1523 plot_latency(plot, min_seconds, max_seconds);
1525 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1526 plot_queue_depth(plot, min_seconds, max_seconds);
1528 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1529 plot_iops(plot, min_seconds, max_seconds);
1533 close_plot_file(plot);