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 keep_movie_svgs = 0;
50 static int opt_graph_width = 0;
51 static int opt_graph_height = 0;
53 static int columns = 1;
54 static int num_xticks = 7;
55 static int num_yticks = 4;
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;
62 int plot_io_action = 0;
63 int io_per_process = 0;
64 unsigned int longest_proc_name = 0;
67 * this doesn't include the IO graph,
68 * but it counts the other graphs as they go out
70 static int total_graphs_written = 1;
81 QUEUE_DEPTH_GRAPH_INDEX,
95 static char *graphs_by_name[] = {
114 char *movie_styles[] = {
120 static int movie_style = 0;
122 static int lookup_movie_style(char *str)
126 for (i = 0; i < NUM_MOVIE_STYLES; i++) {
127 if (strcmp(str, movie_styles[i]) == 0)
133 static int active_graphs[TOTAL_GRAPHS];
134 static int last_active_graph = IOPS_GRAPH_INDEX;
136 static int label_index = 0;
137 static int num_traces = 0;
138 static int longest_label = 0;
140 static char *graph_title = "";
141 static char *output_filename = "trace.svg";
142 static char *blktrace_device = NULL;
143 static char *blktrace_outfile = "trace";
144 static char *blktrace_dest_dir = ".";
145 static char *program_to_run = NULL;
147 static void alloc_mpstat_gld(struct trace_file *tf)
149 struct graph_line_data **ptr;
151 if (tf->trace->mpstat_num_cpus == 0)
154 ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
155 sizeof(struct graph_line_data *));
157 perror("Unable to allocate mpstat arrays\n");
160 tf->mpstat_gld = ptr;
163 static void enable_all_graphs(void)
166 for (i = 0; i < TOTAL_GRAPHS; i++)
167 active_graphs[i] = 1;
170 static void disable_all_graphs(void)
173 for (i = 0; i < TOTAL_GRAPHS; i++)
174 active_graphs[i] = 0;
177 static int enable_one_graph(char *name)
180 for (i = 0; i < TOTAL_GRAPHS; i++) {
181 if (strcmp(name, graphs_by_name[i]) == 0) {
182 active_graphs[i] = 1;
189 static int disable_one_graph(char *name)
192 for (i = 0; i < TOTAL_GRAPHS; i++) {
193 if (strcmp(name, graphs_by_name[i]) == 0) {
194 active_graphs[i] = 0;
201 static int last_graph(void)
204 for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
205 if (active_graphs[i]) {
212 static int graphs_left(int cur)
216 for (i = cur; i < TOTAL_GRAPHS; i++) {
217 if (active_graphs[i])
223 static char * join_path(char *dest_dir, char *filename)
225 /* alloc 2 extra bytes for '/' and '\0' */
226 char *path = malloc(strlen(dest_dir) + strlen(filename) + 2);
227 sprintf(path, "%s%s%s", dest_dir, "/", filename);
232 static void add_trace_file(char *filename)
234 struct trace_file *tf;
236 tf = calloc(1, sizeof(*tf));
238 fprintf(stderr, "Unable to allocate memory\n");
242 tf->filename = strdup(filename);
243 list_add_tail(&tf->list, &all_traces);
244 tf->line_color = "black";
248 static void setup_trace_file_graphs(void)
250 struct trace_file *tf;
258 list_for_each_entry(tf, &all_traces, list) {
259 tf->tput_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
260 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
261 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
262 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
263 tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
264 tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
265 tf->io_plots_allocated = alloc_ptrs;
267 if (tf->trace->mpstat_num_cpus == 0)
270 alloc_mpstat_gld(tf);
271 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
273 alloc_line_data(tf->mpstat_min_seconds,
274 tf->mpstat_max_seconds,
275 tf->mpstat_max_seconds);
276 tf->mpstat_gld[i]->max = 100;
281 static void read_traces(void)
283 struct trace_file *tf;
292 list_for_each_entry(tf, &all_traces, list) {
293 path = join_path(blktrace_dest_dir, tf->filename);
294 trace = open_trace(path);
298 last_time = find_last_time(trace);
300 tf->max_seconds = SECONDS(last_time) + 1;
301 tf->stop_seconds = SECONDS(last_time) + 1;
302 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
303 &max_bank, &max_bank_offset);
304 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
305 tf->min_offset = ymin;
306 tf->max_offset = ymax;
308 read_mpstat(trace, path);
309 tf->mpstat_stop_seconds = trace->mpstat_seconds;
310 tf->mpstat_max_seconds = trace->mpstat_seconds;
311 if (tf->mpstat_max_seconds)
317 static void pick_line_graph_color(void)
319 struct trace_file *tf;
322 list_for_each_entry(tf, &all_traces, list) {
323 for (i = 0; i < tf->io_plots; i++) {
324 if (tf->gdd_reads[i]) {
325 tf->line_color = tf->gdd_reads[i]->color;
328 if (tf->gdd_writes[i]) {
329 tf->line_color = tf->gdd_writes[i]->color;
336 static void read_trace_events(void)
339 struct trace_file *tf;
344 double user, sys, iowait, irq, soft;
345 double max_user = 0, max_sys = 0, max_iowait = 0,
346 max_irq = 0, max_soft = 0;
348 list_for_each_entry(tf, &all_traces, list) {
353 add_tput(trace, tf->tput_gld);
354 add_iop(trace, tf->iop_gld);
356 add_pending_io(trace, tf->queue_depth_gld);
357 add_completed_io(trace, tf->latency_gld);
358 ret = next_record(trace);
363 list_for_each_entry(tf, &all_traces, list) {
366 if (trace->mpstat_num_cpus == 0)
371 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
372 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
373 ret = read_mpstat_event(trace, &user, &sys,
374 &iowait, &irq, &soft);
377 if (next_mpstat_line(trace))
386 if (iowait > max_iowait)
389 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
390 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
391 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
392 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
393 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
395 if (next_mpstat(trace) == NULL)
401 list_for_each_entry(tf, &all_traces, list) {
404 if (trace->mpstat_num_cpus == 0)
407 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
408 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
409 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
410 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
411 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
416 static void set_trace_label(char *label)
419 struct trace_file *tf;
420 int len = strlen(label);
422 if (len > longest_label)
425 list_for_each_entry(tf, &all_traces, list) {
426 if (cur == label_index) {
427 tf->label = strdup(label);
435 static void set_blktrace_outfile(char *arg)
437 char *s = strdup(arg);
438 char *last_dot = strrchr(s, '.');
441 if (strcmp(last_dot, ".dump") == 0)
444 blktrace_outfile = s;
448 static void compare_minmax_tf(struct trace_file *tf, int *max_seconds, u64 *min_offset, u64 *max_offset)
450 if (tf->max_seconds > *max_seconds)
451 *max_seconds = tf->max_seconds;
452 if (tf->max_offset > *max_offset)
453 *max_offset = tf->max_offset;
454 if (tf->min_offset < *min_offset)
455 *min_offset = tf->min_offset;
458 static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
460 struct trace_file *tf;
462 list_for_each_entry(tf, &all_traces, list) {
463 tf->min_seconds = min_seconds;
464 tf->max_seconds = max_seconds;
465 if (tf->stop_seconds > max_seconds)
466 tf->stop_seconds = max_seconds;
467 if (tf->mpstat_max_seconds) {
468 tf->mpstat_min_seconds = min_seconds;
469 tf->mpstat_max_seconds = max_seconds;
470 if (tf->mpstat_stop_seconds > max_seconds)
471 tf->mpstat_stop_seconds = max_seconds;
473 tf->min_offset = min_offset;
474 tf->max_offset = max_offset;
478 static char *create_movie_temp_dir(void)
481 char *pattern = strdup("io-movie-XXXXXX");;
483 ret = mkdtemp(pattern);
485 perror("Unable to create temp directory for movie files");
491 static struct pid_plot_history *alloc_pid_plot_history(char *color)
493 struct pid_plot_history *pph;
495 pph = calloc(1, sizeof(struct pid_plot_history));
497 perror("memory allocation failed");
500 pph->history = malloc(sizeof(double) * 4096);
502 perror("memory allocation failed");
505 pph->history_len = 4096;
511 static struct plot_history *alloc_plot_history(struct trace_file *tf)
513 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
517 perror("memory allocation failed");
520 ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
521 if (!ph->read_pid_history) {
522 perror("memory allocation failed");
525 ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
526 if (!ph->write_pid_history) {
527 perror("memory allocation failed");
530 ph->pid_history_count = tf->io_plots;
531 for (i = 0; i < tf->io_plots; i++) {
532 if (tf->gdd_reads[i])
533 ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
534 if (tf->gdd_writes[i])
535 ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
540 LIST_HEAD(movie_history);
541 int num_histories = 0;
543 static void free_plot_history(struct plot_history *ph)
547 for (pid = 0; pid < ph->pid_history_count; pid++) {
548 if (ph->read_pid_history[pid])
549 free(ph->read_pid_history[pid]);
550 if (ph->write_pid_history[pid])
551 free(ph->write_pid_history[pid]);
553 free(ph->read_pid_history);
554 free(ph->write_pid_history);
558 static void add_history(struct plot_history *ph, struct list_head *list)
560 struct plot_history *entry;
562 list_add_tail(&ph->list, list);
565 if (num_histories > 12) {
567 entry = list_entry(list->next, struct plot_history, list);
568 list_del(&entry->list);
569 free_plot_history(entry);
573 static void plot_movie_history(struct plot *plot, struct list_head *list)
575 struct plot_history *ph;
578 if (num_histories > 2)
579 rewind_spindle_steps(num_histories - 1);
581 list_for_each_entry(ph, list, list) {
582 for (pid = 0; pid < ph->pid_history_count; pid++) {
583 if (ph->read_pid_history[pid]) {
584 if (movie_style == MOVIE_SPINDLE) {
585 svg_io_graph_movie_array_spindle(plot,
586 ph->read_pid_history[pid]);
588 svg_io_graph_movie_array(plot,
589 ph->read_pid_history[pid]);
592 if (ph->write_pid_history[pid]) {
593 if (movie_style == MOVIE_SPINDLE) {
594 svg_io_graph_movie_array_spindle(plot,
595 ph->write_pid_history[pid]);
597 svg_io_graph_movie_array(plot,
598 ph->write_pid_history[pid]);
605 static void free_all_plot_history(struct list_head *head)
607 struct plot_history *entry;
609 while (!list_empty(head)) {
610 entry = list_entry(head->next, struct plot_history, list);
611 list_del(&entry->list);
612 free_plot_history(entry);
616 static int count_io_plot_types(void)
618 struct trace_file *tf;
620 int total_io_types = 0;
622 list_for_each_entry(tf, &all_traces, list) {
623 for (i = 0; i < tf->io_plots; i++) {
624 if (tf->gdd_reads[i])
626 if (tf->gdd_writes[i])
630 return total_io_types;
633 static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
635 struct trace_file *tf;
638 if (active_graphs[IO_GRAPH_INDEX] == 0)
643 svg_alloc_legend(plot, count_io_plot_types() * 2);
645 set_plot_label(plot, "Device IO");
646 set_ylabel(plot, "Offset (MB)");
647 set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
648 max_offset / (1024 * 1024), "");
649 set_xticks(plot, num_xticks, min_seconds, max_seconds);
651 list_for_each_entry(tf, &all_traces, list) {
658 strcpy(label, tf->label);
662 pos = label + strlen(label);
664 for (i = 0; i < tf->io_plots; i++) {
665 if (tf->gdd_reads[i]) {
666 svg_io_graph(plot, tf->gdd_reads[i]);
668 strcpy(pos, tf->gdd_reads[i]->label);
669 svg_add_legend(plot, label, " Reads", tf->gdd_reads[i]->color);
672 if (tf->gdd_writes[i]) {
673 svg_io_graph(plot, tf->gdd_writes[i]);
675 strcpy(pos, tf->gdd_writes[i]->label);
676 svg_add_legend(plot, label, " Writes", tf->gdd_writes[i]->color);
680 if (plot->add_xlabel)
681 set_xlabel(plot, "Time (seconds)");
682 svg_write_legend(plot);
686 static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
688 struct trace_file *tf;
693 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
697 svg_alloc_legend(plot, num_traces);
698 list_for_each_entry(tf, &all_traces, list) {
699 if (tf->tput_gld->max > max)
700 max = tf->tput_gld->max;
702 list_for_each_entry(tf, &all_traces, list)
703 tf->tput_gld->max = max;
706 set_plot_label(plot, "Throughput");
708 tf = list_entry(all_traces.next, struct trace_file, list);
710 scale_line_graph_bytes(&max, &units, 1024);
711 sprintf(line, "%sB/s", units);
712 set_ylabel(plot, line);
713 set_yticks(plot, num_yticks, 0, max, "");
714 set_xticks(plot, num_xticks, min_seconds, max_seconds);
716 list_for_each_entry(tf, &all_traces, list) {
717 svg_line_graph(plot, tf->tput_gld, tf->line_color, 0, 0);
719 svg_add_legend(plot, tf->label, "", tf->line_color);
722 if (plot->add_xlabel)
723 set_xlabel(plot, "Time (seconds)");
725 svg_write_legend(plot);
727 total_graphs_written++;
730 static void plot_cpu(struct plot *plot, int max_seconds, char *label,
731 int active_index, int gld_index)
733 struct trace_file *tf;
742 if (active_graphs[active_index] == 0)
745 list_for_each_entry(tf, &all_traces, list) {
746 if (tf->trace->mpstat_num_cpus > max)
747 max = tf->trace->mpstat_num_cpus;
752 tf = list_entry(all_traces.next, struct trace_file, list);
754 ymax = tf->mpstat_gld[gld_index]->max;
758 svg_alloc_legend(plot, num_traces * max);
761 set_plot_label(plot, label);
763 max_seconds = tf->mpstat_max_seconds;
765 set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
766 set_ylabel(plot, "Percent");
767 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
770 list_for_each_entry(tf, &all_traces, list) {
771 if (tf->mpstat_gld == 0)
773 for (i = tf->mpstat_gld[0]->min_seconds;
774 i < tf->mpstat_gld[0]->stop_seconds; i++) {
775 if (tf->mpstat_gld[gld_index]->data[i].count) {
776 avg += (tf->mpstat_gld[gld_index]->data[i].sum /
777 tf->mpstat_gld[gld_index]->data[i].count);
780 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
781 tf->mpstat_gld[gld_index]->min_seconds;
782 color = pick_cpu_color();
783 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
784 svg_add_legend(plot, tf->label, " avg", color);
786 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
787 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
790 for (gld_i = gld->min_seconds;
791 gld_i < gld->stop_seconds; gld_i++) {
792 if (gld->data[i].count) {
793 this_avg += gld->data[i].sum /
798 this_avg /= gld->stop_seconds - gld->min_seconds;
800 for (gld_i = gld->min_seconds;
801 gld_i < gld->stop_seconds; gld_i++) {
804 if (gld->data[gld_i].count == 0)
806 val = (double)gld->data[gld_i].sum /
807 gld->data[gld_i].count;
809 if (this_avg > avg + 30 || val > 95) {
810 color = pick_cpu_color();
811 svg_line_graph(plot, gld, color, avg + 30, 95);
812 snprintf(line, line_len, " CPU %d\n", i - 1);
813 svg_add_legend(plot, tf->label, line, color);
822 if (plot->add_xlabel)
823 set_xlabel(plot, "Time (seconds)");
825 if (!plot->no_legend) {
826 svg_write_legend(plot);
827 svg_free_legend(plot);
830 total_graphs_written++;
833 static void plot_queue_depth(struct plot *plot, int min_seconds, int max_seconds)
835 struct trace_file *tf;
837 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
841 set_plot_label(plot, "Queue Depth");
843 svg_alloc_legend(plot, num_traces);
845 tf = list_entry(all_traces.next, struct trace_file, list);
846 set_ylabel(plot, "Pending IO");
847 set_yticks(plot, num_yticks, 0, tf->queue_depth_gld->max, "");
848 set_xticks(plot, num_xticks, min_seconds, max_seconds);
850 list_for_each_entry(tf, &all_traces, list) {
851 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
853 svg_add_legend(plot, tf->label, "", tf->line_color);
856 if (plot->add_xlabel)
857 set_xlabel(plot, "Time (seconds)");
859 svg_write_legend(plot);
861 total_graphs_written++;
864 static void convert_movie_files(char *movie_dir)
866 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
867 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
872 static void mencode_movie(char *movie_dir)
874 fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
875 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
876 "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
880 static void tencode_movie(char *movie_dir)
882 fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
883 snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
884 output_filename, movie_dir, output_filename);
888 static void encode_movie(char *movie_dir)
890 char *last_dot = strrchr(output_filename, '.');
893 (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
894 tencode_movie(movie_dir);
896 mencode_movie(movie_dir);
899 static void cleanup_movie(char *movie_dir)
901 if (keep_movie_svgs) {
902 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
905 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
906 snprintf(line, line_len, "rm %s/*", movie_dir);
909 snprintf(line, line_len, "rmdir %s", movie_dir);
913 static void plot_io_movie(struct plot *plot)
915 struct trace_file *tf;
916 char *movie_dir = create_movie_temp_dir();
918 struct plot_history *history;
921 int movie_frames_per_sec = 20;
922 int total_frames = movie_len * movie_frames_per_sec;
925 int graph_width_factor = 5;
928 get_graph_size(&cols, &rows);
929 batch_count = cols / total_frames;
931 if (batch_count == 0)
934 list_for_each_entry(tf, &all_traces, list) {
941 strcpy(label, tf->label);
945 pos = label + strlen(label);
949 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
950 set_plot_output(plot, line);
951 set_plot_title(plot, graph_title);
952 orig_y_offset = plot->start_y_offset;
956 set_graph_size(cols / graph_width_factor, rows / 8);
957 plot->timeline = i / graph_width_factor;
959 plot_tput(plot, tf->min_seconds,
962 plot_cpu(plot, tf->max_seconds,
963 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
965 plot->direction = PLOT_ACROSS;
966 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
968 /* movie graph starts here */
969 plot->start_y_offset = orig_y_offset;
970 set_graph_size(cols - cols / graph_width_factor, rows);
973 plot->direction = PLOT_DOWN;;
975 if (movie_style == MOVIE_SPINDLE)
976 setup_axis_spindle(plot);
980 svg_alloc_legend(plot, count_io_plot_types() * 2);
982 history = alloc_plot_history(tf);
985 for (pid = 0; pid < tf->io_plots; pid++) {
986 if (tf->gdd_reads[pid]) {
988 strcpy(pos, tf->gdd_reads[pid]->label);
989 svg_add_legend(plot, label, " Reads", tf->gdd_reads[pid]->color);
991 if (tf->gdd_writes[pid]) {
993 strcpy(pos, tf->gdd_writes[pid]->label);
994 svg_add_legend(plot, label, " Writes", tf->gdd_writes[pid]->color);
999 while (i < cols && batch_i < batch_count) {
1000 for (pid = 0; pid < tf->io_plots; pid++) {
1001 if (tf->gdd_reads[pid]) {
1002 svg_io_graph_movie(tf->gdd_reads[pid],
1003 history->read_pid_history[pid],
1006 if (tf->gdd_writes[pid]) {
1007 svg_io_graph_movie(tf->gdd_writes[pid],
1008 history->write_pid_history[pid],
1016 add_history(history, &movie_history);
1018 plot_movie_history(plot, &movie_history);
1020 svg_write_legend(plot);
1024 close_plot_file(plot);
1026 free_all_plot_history(&movie_history);
1028 convert_movie_files(movie_dir);
1029 encode_movie(movie_dir);
1030 cleanup_movie(movie_dir);
1034 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
1036 struct trace_file *tf;
1041 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1045 svg_alloc_legend(plot, num_traces);
1047 list_for_each_entry(tf, &all_traces, list) {
1048 if (tf->latency_gld->max > max)
1049 max = tf->latency_gld->max;
1052 list_for_each_entry(tf, &all_traces, list)
1053 tf->latency_gld->max = max;
1056 set_plot_label(plot, "IO Latency");
1058 tf = list_entry(all_traces.next, struct trace_file, list);
1060 scale_line_graph_time(&max, &units);
1061 sprintf(line, "latency (%ss)", units);
1062 set_ylabel(plot, line);
1063 set_yticks(plot, num_yticks, 0, max, "");
1064 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1066 list_for_each_entry(tf, &all_traces, list) {
1067 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1069 svg_add_legend(plot, tf->label, "", tf->line_color);
1072 if (plot->add_xlabel)
1073 set_xlabel(plot, "Time (seconds)");
1075 svg_write_legend(plot);
1077 total_graphs_written++;
1080 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
1082 struct trace_file *tf;
1086 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1089 list_for_each_entry(tf, &all_traces, list) {
1090 if (tf->iop_gld->max > max)
1091 max = tf->iop_gld->max;
1094 list_for_each_entry(tf, &all_traces, list)
1095 tf->iop_gld->max = max;
1098 set_plot_label(plot, "IOPs");
1100 svg_alloc_legend(plot, num_traces);
1102 tf = list_entry(all_traces.next, struct trace_file, list);
1104 scale_line_graph_bytes(&max, &units, 1000);
1105 set_ylabel(plot, "IO/s");
1107 set_yticks(plot, num_yticks, 0, max, units);
1108 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1110 list_for_each_entry(tf, &all_traces, list) {
1111 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1113 svg_add_legend(plot, tf->label, "", tf->line_color);
1116 if (plot->add_xlabel)
1117 set_xlabel(plot, "Time (seconds)");
1119 svg_write_legend(plot);
1122 total_graphs_written++;
1125 static void check_plot_columns(struct plot *plot, int index)
1129 if (columns > 1 && (total_graphs_written == 0 ||
1130 total_graphs_written % columns != 0)) {
1131 count = graphs_left(index);
1132 if (plot->direction == PLOT_DOWN) {
1133 plot->start_x_offset = 0;
1134 if (count <= columns)
1135 plot->add_xlabel = 1;
1137 plot->direction = PLOT_ACROSS;
1140 plot->direction = PLOT_DOWN;
1141 if (index == last_active_graph)
1142 plot->add_xlabel = 1;
1151 char *option_string = "T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:PK";
1152 static struct option long_options[] = {
1153 {"columns", required_argument, 0, 'c'},
1154 {"title", required_argument, 0, 'T'},
1155 {"trace", required_argument, 0, 't'},
1156 {"output", required_argument, 0, 'o'},
1157 {"label", required_argument, 0, 'l'},
1158 {"rolling", required_argument, 0, 'r'},
1159 {"no-graph", required_argument, 0, 'N'},
1160 {"only-graph", required_argument, 0, 'O'},
1161 {"device", required_argument, 0, 'd'},
1162 {"blktrace-destination", required_argument, 0, 'D'},
1163 {"prog", required_argument, 0, 'p'},
1164 {"movie", optional_argument, 0, 'm'},
1165 {"keep-movie-svgs", no_argument, 0, 'K'},
1166 {"width", required_argument, 0, 'w'},
1167 {"height", required_argument, 0, 'h'},
1168 {"xzoom", required_argument, 0, 'x'},
1169 {"yzoom", required_argument, 0, 'y'},
1170 {"io-plot-action", required_argument, 0, 'a'},
1171 {"per-process-io", no_argument, 0, 'P'},
1172 {"help", no_argument, 0, HELP_LONG_OPT},
1176 static void print_usage(void)
1178 fprintf(stderr, "iowatcher usage:\n"
1179 "\t-d (--device): device for blktrace to trace\n"
1180 "\t-D (--blktrace-destination): destination for blktrace\n"
1181 "\t-t (--trace): trace file name (more than one allowed)\n"
1182 "\t-l (--label): trace label in the graph\n"
1183 "\t-o (--output): output file name (SVG only)\n"
1184 "\t-p (--prog): program to run while blktrace is run\n"
1185 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
1186 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1187 "\t-r (--rolling): number of seconds in the rolling averge\n"
1188 "\t-T (--title): graph title\n"
1189 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1190 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1191 "\t-O (--only-graph): add a single graph to the output\n"
1192 "\t-h (--height): set the height of each graph\n"
1193 "\t-w (--width): set the width of each graph\n"
1194 "\t-c (--columns): numbers of columns in graph output\n"
1195 "\t-x (--xzoom): limit processed time to min:max\n"
1196 "\t-y (--yzoom): limit processed sectors to min:max\n"
1197 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1198 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1203 static int parse_double_range(char *str, double *min, double *max)
1207 /* Empty lower bound - leave original value */
1208 if (str[0] != ':') {
1209 *min = strtod(str, &end);
1210 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1216 /* Empty upper bound - leave original value */
1218 *max = strtod(end+1, &end);
1219 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1229 static int parse_ull_range(char *str, unsigned long long *min,
1230 unsigned long long *max)
1234 /* Empty lower bound - leave original value */
1235 if (str[0] != ':') {
1236 *min = strtoull(str, &end, 10);
1237 if (*min == ULLONG_MAX && errno == ERANGE)
1243 /* Empty upper bound - leave original value */
1245 *max = strtoull(end+1, &end, 10);
1246 if (*max == ULLONG_MAX && errno == ERANGE)
1256 static int parse_options(int ac, char **av)
1262 // int this_option_optind = optind ? optind : 1;
1263 int option_index = 0;
1265 c = getopt_long(ac, av, option_string,
1266 long_options, &option_index);
1273 graph_title = strdup(optarg);
1276 add_trace_file(optarg);
1277 set_blktrace_outfile(optarg);
1280 output_filename = strdup(optarg);
1283 set_trace_label(optarg);
1286 set_rolling_avg(atoi(optarg));
1290 disable_all_graphs();
1293 enable_one_graph(optarg);
1296 disable_one_graph(optarg);
1299 blktrace_device = strdup(optarg);
1302 blktrace_dest_dir = strdup(optarg);
1303 if (!strcmp(blktrace_dest_dir, "")) {
1304 fprintf(stderr, "Need a directory\n");
1309 program_to_run = strdup(optarg);
1312 keep_movie_svgs = 1;
1317 movie_style = lookup_movie_style(optarg);
1318 if (movie_style < 0) {
1319 fprintf(stderr, "Unknown movie style %s\n", optarg);
1323 fprintf(stderr, "Using movie style: %s\n",
1324 movie_styles[movie_style]);
1327 opt_graph_height = atoi(optarg);
1330 opt_graph_width = atoi(optarg);
1333 columns = atoi(optarg);
1336 if (parse_double_range(optarg, &min_time, &max_time)
1338 fprintf(stderr, "Cannot parse time range %s\n",
1344 if (parse_ull_range(optarg, &min_mb, &max_mb)
1347 "Cannot parse offset range %s\n",
1351 if (max_mb > ULLONG_MAX >> 20) {
1353 "Upper range limit too big."
1354 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1359 if (strlen(optarg) != 1) {
1361 fprintf(stderr, "Action must be one of Q, D, C.");
1364 plot_io_action = action_char_to_num(optarg[0]);
1365 if (plot_io_action < 0)
1382 static void dest_mkdir()
1386 ret = mkdir(blktrace_dest_dir, 0777);
1387 if (ret && errno != EEXIST) {
1388 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1393 int main(int ac, char **av)
1396 int min_seconds = 0;
1397 int max_seconds = 0;
1399 u64 min_offset = ~(u64)0;
1400 struct trace_file *tf;
1404 init_io_hash_table();
1405 init_process_hash_table();
1407 enable_all_graphs();
1409 parse_options(ac, av);
1411 last_active_graph = last_graph();
1413 set_io_graph_scale(256);
1414 if (movie_style == MOVIE_SPINDLE)
1415 set_graph_size(750, 550);
1417 set_graph_size(700, 400);
1420 * the plots in the movie don't have a seconds
1421 * line yet, this makes us skip it
1423 last_active_graph = TOTAL_GRAPHS + 1;
1425 if (opt_graph_height)
1426 set_graph_height(opt_graph_height);
1428 if (opt_graph_width)
1429 set_graph_width(opt_graph_width);
1431 if (list_empty(&all_traces)) {
1432 fprintf(stderr, "No traces found, exiting\n");
1436 if (blktrace_device) {
1440 path = join_path(blktrace_dest_dir, blktrace_outfile);
1442 ret = start_blktrace(blktrace_device, blktrace_outfile,
1445 fprintf(stderr, "exiting due to blktrace failure\n");
1450 if (program_to_run) {
1451 ret = run_program(program_to_run);
1453 fprintf(stderr, "failed to run %s\n",
1458 blktrace_to_dump(path);
1460 /* no program specified, just wait for
1468 /* step one, read all the traces */
1471 /* step two, find the maxes for time and offset */
1472 list_for_each_entry(tf, &all_traces, list)
1473 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1474 min_seconds = min_time;
1475 if (max_seconds > max_time)
1476 max_seconds = ceil(max_time);
1477 if (min_offset < min_mb << 20)
1478 min_offset = min_mb << 20;
1479 if (max_offset > max_mb << 20)
1480 max_offset = max_mb << 20;
1482 /* push the max we found into all the tfs */
1483 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1485 /* alloc graphing structs for all the traces */
1486 setup_trace_file_graphs();
1488 /* run through all the traces and read their events */
1489 read_trace_events();
1491 pick_line_graph_color();
1493 plot = alloc_plot();
1496 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1497 plot_io_movie(plot);
1501 set_plot_output(plot, output_filename);
1503 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1504 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1505 else if (num_traces > 1)
1506 set_legend_width(longest_label);
1508 set_legend_width(0);
1510 get_graph_size(&cols, &rows);
1512 plot->add_xlabel = 1;
1513 set_plot_title(plot, graph_title);
1515 check_plot_columns(plot, IO_GRAPH_INDEX);
1516 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1517 plot->add_xlabel = 0;
1520 set_graph_size(cols / columns, rows);
1521 num_xticks /= columns;
1528 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1529 plot_tput(plot, min_seconds, max_seconds);
1531 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1532 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1533 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1535 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1536 plot_cpu(plot, max_seconds, "CPU System Time",
1537 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1539 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1540 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1541 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1543 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1544 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1545 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1547 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1548 plot_cpu(plot, max_seconds, "CPU User Time",
1549 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1551 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1552 plot_latency(plot, min_seconds, max_seconds);
1554 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1555 plot_queue_depth(plot, min_seconds, max_seconds);
1557 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1558 plot_iops(plot, min_seconds, max_seconds);
1562 close_plot_file(plot);