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 void join_path(char *path, char *filename)
224 path = strcat(path, "/");
225 path = strcat(path, filename);
228 static void add_trace_file(char *filename)
230 struct trace_file *tf;
232 tf = calloc(1, sizeof(*tf));
234 fprintf(stderr, "Unable to allocate memory\n");
238 tf->filename = strdup(filename);
239 list_add_tail(&tf->list, &all_traces);
240 tf->line_color = "black";
244 static void setup_trace_file_graphs(void)
246 struct trace_file *tf;
254 list_for_each_entry(tf, &all_traces, list) {
255 tf->tput_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
256 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
257 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
258 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
259 tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
260 tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
261 tf->io_plots_allocated = alloc_ptrs;
263 if (tf->trace->mpstat_num_cpus == 0)
266 alloc_mpstat_gld(tf);
267 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
269 alloc_line_data(tf->mpstat_min_seconds,
270 tf->mpstat_max_seconds,
271 tf->mpstat_max_seconds);
272 tf->mpstat_gld[i]->max = 100;
277 static void read_traces(void)
279 struct trace_file *tf;
288 list_for_each_entry(tf, &all_traces, list) {
289 path = strdup(blktrace_dest_dir);
290 join_path(path, tf->filename);
292 trace = open_trace(path);
296 last_time = find_last_time(trace);
298 tf->max_seconds = SECONDS(last_time);
299 tf->stop_seconds = SECONDS(last_time);
300 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
301 &max_bank, &max_bank_offset);
302 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
303 tf->min_offset = ymin;
304 tf->max_offset = ymax;
306 read_mpstat(trace, path);
307 tf->mpstat_stop_seconds = trace->mpstat_seconds;
308 tf->mpstat_max_seconds = trace->mpstat_seconds;
309 if (tf->mpstat_max_seconds)
315 static void pick_line_graph_color(void)
317 struct trace_file *tf;
320 list_for_each_entry(tf, &all_traces, list) {
321 for (i = 0; i < tf->io_plots; i++) {
322 if (tf->gdd_reads[i]) {
323 tf->line_color = tf->gdd_reads[i]->color;
326 if (tf->gdd_writes[i]) {
327 tf->line_color = tf->gdd_writes[i]->color;
334 static void read_trace_events(void)
337 struct trace_file *tf;
342 double user, sys, iowait, irq, soft;
343 double max_user = 0, max_sys = 0, max_iowait = 0,
344 max_irq = 0, max_soft = 0;
346 list_for_each_entry(tf, &all_traces, list) {
351 add_tput(trace, tf->tput_gld);
352 add_iop(trace, tf->iop_gld);
354 add_pending_io(trace, tf->queue_depth_gld);
355 add_completed_io(trace, tf->latency_gld);
356 ret = next_record(trace);
361 list_for_each_entry(tf, &all_traces, list) {
364 if (trace->mpstat_num_cpus == 0)
369 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
370 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
371 ret = read_mpstat_event(trace, &user, &sys,
372 &iowait, &irq, &soft);
375 if (next_mpstat_line(trace))
384 if (iowait > max_iowait)
387 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
388 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
389 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
390 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
391 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
393 if (next_mpstat(trace) == NULL)
399 list_for_each_entry(tf, &all_traces, list) {
402 if (trace->mpstat_num_cpus == 0)
405 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
406 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
407 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
408 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
409 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
414 static void set_trace_label(char *label)
417 struct trace_file *tf;
418 int len = strlen(label);
420 if (len > longest_label)
423 list_for_each_entry(tf, &all_traces, list) {
424 if (cur == label_index) {
425 tf->label = strdup(label);
433 static void set_blktrace_outfile(char *arg)
435 char *s = strdup(arg);
436 char *last_dot = strrchr(s, '.');
439 if (strcmp(last_dot, ".dump") == 0)
442 blktrace_outfile = s;
446 static void compare_minmax_tf(struct trace_file *tf, int *max_seconds, u64 *min_offset, u64 *max_offset)
448 if (tf->max_seconds > *max_seconds)
449 *max_seconds = tf->max_seconds;
450 if (tf->max_offset > *max_offset)
451 *max_offset = tf->max_offset;
452 if (tf->min_offset < *min_offset)
453 *min_offset = tf->min_offset;
456 static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
458 struct trace_file *tf;
460 list_for_each_entry(tf, &all_traces, list) {
461 tf->min_seconds = min_seconds;
462 tf->max_seconds = max_seconds;
463 if (tf->stop_seconds > max_seconds)
464 tf->stop_seconds = max_seconds;
465 if (tf->mpstat_max_seconds) {
466 tf->mpstat_min_seconds = min_seconds;
467 tf->mpstat_max_seconds = max_seconds;
468 if (tf->mpstat_stop_seconds > max_seconds)
469 tf->mpstat_stop_seconds = max_seconds;
471 tf->min_offset = min_offset;
472 tf->max_offset = max_offset;
476 static char *create_movie_temp_dir(void)
479 char *pattern = strdup("io-movie-XXXXXX");;
481 ret = mkdtemp(pattern);
483 perror("Unable to create temp directory for movie files");
489 static struct pid_plot_history *alloc_pid_plot_history(char *color)
491 struct pid_plot_history *pph;
493 pph = calloc(1, sizeof(struct pid_plot_history));
495 perror("memory allocation failed");
498 pph->history = malloc(sizeof(double) * 4096);
500 perror("memory allocation failed");
503 pph->history_len = 4096;
509 static struct plot_history *alloc_plot_history(struct trace_file *tf)
511 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
515 perror("memory allocation failed");
518 ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
519 if (!ph->read_pid_history) {
520 perror("memory allocation failed");
523 ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
524 if (!ph->write_pid_history) {
525 perror("memory allocation failed");
528 ph->pid_history_count = tf->io_plots;
529 for (i = 0; i < tf->io_plots; i++) {
530 if (tf->gdd_reads[i])
531 ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
532 if (tf->gdd_writes[i])
533 ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
538 LIST_HEAD(movie_history);
539 int num_histories = 0;
541 static void free_plot_history(struct plot_history *ph)
545 for (pid = 0; pid < ph->pid_history_count; pid++) {
546 if (ph->read_pid_history[pid])
547 free(ph->read_pid_history[pid]);
548 if (ph->write_pid_history[pid])
549 free(ph->write_pid_history[pid]);
551 free(ph->read_pid_history);
552 free(ph->write_pid_history);
556 static void add_history(struct plot_history *ph, struct list_head *list)
558 struct plot_history *entry;
560 list_add_tail(&ph->list, list);
563 if (num_histories > 12) {
565 entry = list_entry(list->next, struct plot_history, list);
566 list_del(&entry->list);
567 free_plot_history(entry);
571 static void plot_movie_history(struct plot *plot, struct list_head *list)
573 struct plot_history *ph;
576 if (num_histories > 2)
577 rewind_spindle_steps(num_histories - 1);
579 list_for_each_entry(ph, list, list) {
580 for (pid = 0; pid < ph->pid_history_count; pid++) {
581 if (ph->read_pid_history[pid]) {
582 if (movie_style == MOVIE_SPINDLE) {
583 svg_io_graph_movie_array_spindle(plot,
584 ph->read_pid_history[pid]);
586 svg_io_graph_movie_array(plot,
587 ph->read_pid_history[pid]);
590 if (ph->write_pid_history[pid]) {
591 if (movie_style == MOVIE_SPINDLE) {
592 svg_io_graph_movie_array_spindle(plot,
593 ph->write_pid_history[pid]);
595 svg_io_graph_movie_array(plot,
596 ph->write_pid_history[pid]);
603 static void free_all_plot_history(struct list_head *head)
605 struct plot_history *entry;
607 while (!list_empty(head)) {
608 entry = list_entry(head->next, struct plot_history, list);
609 list_del(&entry->list);
610 free_plot_history(entry);
614 static int count_io_plot_types(void)
616 struct trace_file *tf;
618 int total_io_types = 0;
620 list_for_each_entry(tf, &all_traces, list) {
621 for (i = 0; i < tf->io_plots; i++) {
622 if (tf->gdd_reads[i])
624 if (tf->gdd_writes[i])
628 return total_io_types;
631 static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
633 struct trace_file *tf;
636 if (active_graphs[IO_GRAPH_INDEX] == 0)
641 svg_alloc_legend(plot, count_io_plot_types() * 2);
643 set_plot_label(plot, "Device IO");
644 set_ylabel(plot, "Offset (MB)");
645 set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
646 max_offset / (1024 * 1024), "");
647 set_xticks(plot, num_xticks, min_seconds, max_seconds);
649 list_for_each_entry(tf, &all_traces, list) {
656 strcpy(label, tf->label);
660 pos = label + strlen(label);
662 for (i = 0; i < tf->io_plots; i++) {
663 if (tf->gdd_reads[i]) {
664 svg_io_graph(plot, tf->gdd_reads[i]);
666 strcpy(pos, tf->gdd_reads[i]->label);
667 svg_add_legend(plot, label, " Reads", tf->gdd_reads[i]->color);
670 if (tf->gdd_writes[i]) {
671 svg_io_graph(plot, tf->gdd_writes[i]);
673 strcpy(pos, tf->gdd_writes[i]->label);
674 svg_add_legend(plot, label, " Writes", tf->gdd_writes[i]->color);
678 if (plot->add_xlabel)
679 set_xlabel(plot, "Time (seconds)");
680 svg_write_legend(plot);
684 static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
686 struct trace_file *tf;
691 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
695 svg_alloc_legend(plot, num_traces);
696 list_for_each_entry(tf, &all_traces, list) {
697 if (tf->tput_gld->max > max)
698 max = tf->tput_gld->max;
700 list_for_each_entry(tf, &all_traces, list)
701 tf->tput_gld->max = max;
704 set_plot_label(plot, "Throughput");
706 tf = list_entry(all_traces.next, struct trace_file, list);
708 scale_line_graph_bytes(&max, &units, 1024);
709 sprintf(line, "%sB/s", units);
710 set_ylabel(plot, line);
711 set_yticks(plot, num_yticks, 0, max, "");
712 set_xticks(plot, num_xticks, min_seconds, max_seconds);
714 list_for_each_entry(tf, &all_traces, list) {
715 svg_line_graph(plot, tf->tput_gld, tf->line_color, 0, 0);
717 svg_add_legend(plot, tf->label, "", tf->line_color);
720 if (plot->add_xlabel)
721 set_xlabel(plot, "Time (seconds)");
723 svg_write_legend(plot);
725 total_graphs_written++;
728 static void plot_cpu(struct plot *plot, int max_seconds, char *label,
729 int active_index, int gld_index)
731 struct trace_file *tf;
740 if (active_graphs[active_index] == 0)
743 list_for_each_entry(tf, &all_traces, list) {
744 if (tf->trace->mpstat_num_cpus > max)
745 max = tf->trace->mpstat_num_cpus;
750 tf = list_entry(all_traces.next, struct trace_file, list);
752 ymax = tf->mpstat_gld[gld_index]->max;
756 svg_alloc_legend(plot, num_traces * max);
759 set_plot_label(plot, label);
761 max_seconds = tf->mpstat_max_seconds;
763 set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
764 set_ylabel(plot, "Percent");
765 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
768 list_for_each_entry(tf, &all_traces, list) {
769 if (tf->mpstat_gld == 0)
771 for (i = tf->mpstat_gld[0]->min_seconds;
772 i < tf->mpstat_gld[0]->stop_seconds; i++) {
773 if (tf->mpstat_gld[gld_index]->data[i].count) {
774 avg += (tf->mpstat_gld[gld_index]->data[i].sum /
775 tf->mpstat_gld[gld_index]->data[i].count);
778 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
779 tf->mpstat_gld[gld_index]->min_seconds;
780 color = pick_cpu_color();
781 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
782 svg_add_legend(plot, tf->label, " avg", color);
784 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
785 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
788 for (gld_i = gld->min_seconds;
789 gld_i < gld->stop_seconds; gld_i++) {
790 if (gld->data[i].count) {
791 this_avg += gld->data[i].sum /
796 this_avg /= gld->stop_seconds - gld->min_seconds;
798 for (gld_i = gld->min_seconds;
799 gld_i < gld->stop_seconds; gld_i++) {
802 if (gld->data[gld_i].count == 0)
804 val = (double)gld->data[gld_i].sum /
805 gld->data[gld_i].count;
807 if (this_avg > avg + 30 || val > 95) {
808 color = pick_cpu_color();
809 svg_line_graph(plot, gld, color, avg + 30, 95);
810 snprintf(line, line_len, " CPU %d\n", i - 1);
811 svg_add_legend(plot, tf->label, line, color);
820 if (plot->add_xlabel)
821 set_xlabel(plot, "Time (seconds)");
823 if (!plot->no_legend) {
824 svg_write_legend(plot);
825 svg_free_legend(plot);
828 total_graphs_written++;
831 static void plot_queue_depth(struct plot *plot, int min_seconds, int max_seconds)
833 struct trace_file *tf;
835 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
839 set_plot_label(plot, "Queue Depth");
841 svg_alloc_legend(plot, num_traces);
843 tf = list_entry(all_traces.next, struct trace_file, list);
844 set_ylabel(plot, "Pending IO");
845 set_yticks(plot, num_yticks, 0, tf->queue_depth_gld->max, "");
846 set_xticks(plot, num_xticks, min_seconds, max_seconds);
848 list_for_each_entry(tf, &all_traces, list) {
849 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
851 svg_add_legend(plot, tf->label, "", tf->line_color);
854 if (plot->add_xlabel)
855 set_xlabel(plot, "Time (seconds)");
857 svg_write_legend(plot);
859 total_graphs_written++;
862 static void convert_movie_files(char *movie_dir)
864 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
865 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
870 static void mencode_movie(char *movie_dir)
872 fprintf(stderr, "Creating movie %s\n", movie_dir);
873 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
874 "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
878 static void cleanup_movie(char *movie_dir)
880 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
881 snprintf(line, line_len, "rm %s/*", movie_dir);
884 snprintf(line, line_len, "rmdir %s", movie_dir);
888 static void plot_io_movie(struct plot *plot)
890 struct trace_file *tf;
891 char *movie_dir = create_movie_temp_dir();
893 struct plot_history *history;
896 int movie_frames_per_sec = 20;
897 int total_frames = movie_len * movie_frames_per_sec;
900 int graph_width_factor = 5;
903 get_graph_size(&cols, &rows);
904 batch_count = cols / total_frames;
906 if (batch_count == 0)
909 list_for_each_entry(tf, &all_traces, list) {
916 strcpy(label, tf->label);
920 pos = label + strlen(label);
924 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
925 set_plot_output(plot, line);
926 set_plot_title(plot, graph_title);
927 orig_y_offset = plot->start_y_offset;
931 set_graph_size(cols / graph_width_factor, rows / 8);
932 plot->timeline = i / graph_width_factor;
934 plot_tput(plot, tf->min_seconds,
937 plot_cpu(plot, tf->max_seconds,
938 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
940 plot->direction = PLOT_ACROSS;
941 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
943 /* movie graph starts here */
944 plot->start_y_offset = orig_y_offset;
945 set_graph_size(cols - cols / graph_width_factor, rows);
948 plot->direction = PLOT_DOWN;;
950 if (movie_style == MOVIE_SPINDLE)
951 setup_axis_spindle(plot);
955 svg_alloc_legend(plot, count_io_plot_types() * 2);
957 history = alloc_plot_history(tf);
960 for (pid = 0; pid < tf->io_plots; pid++) {
961 if (tf->gdd_reads[pid]) {
963 strcpy(pos, tf->gdd_reads[pid]->label);
964 svg_add_legend(plot, label, " Reads", tf->gdd_reads[pid]->color);
966 if (tf->gdd_writes[pid]) {
968 strcpy(pos, tf->gdd_writes[pid]->label);
969 svg_add_legend(plot, label, " Writes", tf->gdd_writes[pid]->color);
974 while (i < cols && batch_i < batch_count) {
975 for (pid = 0; pid < tf->io_plots; pid++) {
976 if (tf->gdd_reads[pid]) {
977 svg_io_graph_movie(tf->gdd_reads[pid],
978 history->read_pid_history[pid],
981 if (tf->gdd_writes[pid]) {
982 svg_io_graph_movie(tf->gdd_writes[pid],
983 history->write_pid_history[pid],
991 add_history(history, &movie_history);
993 plot_movie_history(plot, &movie_history);
995 svg_write_legend(plot);
999 close_plot_file(plot);
1001 free_all_plot_history(&movie_history);
1003 convert_movie_files(movie_dir);
1004 mencode_movie(movie_dir);
1005 cleanup_movie(movie_dir);
1009 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
1011 struct trace_file *tf;
1016 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1020 svg_alloc_legend(plot, num_traces);
1022 list_for_each_entry(tf, &all_traces, list) {
1023 if (tf->latency_gld->max > max)
1024 max = tf->latency_gld->max;
1027 list_for_each_entry(tf, &all_traces, list)
1028 tf->latency_gld->max = max;
1031 set_plot_label(plot, "IO Latency");
1033 tf = list_entry(all_traces.next, struct trace_file, list);
1035 scale_line_graph_time(&max, &units);
1036 sprintf(line, "latency (%ss)", units);
1037 set_ylabel(plot, line);
1038 set_yticks(plot, num_yticks, 0, max, "");
1039 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1041 list_for_each_entry(tf, &all_traces, list) {
1042 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1044 svg_add_legend(plot, tf->label, "", tf->line_color);
1047 if (plot->add_xlabel)
1048 set_xlabel(plot, "Time (seconds)");
1050 svg_write_legend(plot);
1052 total_graphs_written++;
1055 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
1057 struct trace_file *tf;
1061 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1064 list_for_each_entry(tf, &all_traces, list) {
1065 if (tf->iop_gld->max > max)
1066 max = tf->iop_gld->max;
1069 list_for_each_entry(tf, &all_traces, list)
1070 tf->iop_gld->max = max;
1073 set_plot_label(plot, "IOPs");
1075 svg_alloc_legend(plot, num_traces);
1077 tf = list_entry(all_traces.next, struct trace_file, list);
1079 scale_line_graph_bytes(&max, &units, 1000);
1080 set_ylabel(plot, "IO/s");
1082 set_yticks(plot, num_yticks, 0, max, units);
1083 set_xticks(plot, num_xticks, min_seconds, max_seconds);
1085 list_for_each_entry(tf, &all_traces, list) {
1086 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1088 svg_add_legend(plot, tf->label, "", tf->line_color);
1091 if (plot->add_xlabel)
1092 set_xlabel(plot, "Time (seconds)");
1094 svg_write_legend(plot);
1097 total_graphs_written++;
1100 static void check_plot_columns(struct plot *plot, int index)
1104 if (columns > 1 && (total_graphs_written == 0 ||
1105 total_graphs_written % columns != 0)) {
1106 count = graphs_left(index);
1107 if (plot->direction == PLOT_DOWN) {
1108 plot->start_x_offset = 0;
1109 if (count <= columns)
1110 plot->add_xlabel = 1;
1112 plot->direction = PLOT_ACROSS;
1115 plot->direction = PLOT_DOWN;
1116 if (index == last_active_graph)
1117 plot->add_xlabel = 1;
1126 char *option_string = "T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:P";
1127 static struct option long_options[] = {
1128 {"columns", required_argument, 0, 'c'},
1129 {"title", required_argument, 0, 'T'},
1130 {"trace", required_argument, 0, 't'},
1131 {"output", required_argument, 0, 'o'},
1132 {"label", required_argument, 0, 'l'},
1133 {"rolling", required_argument, 0, 'r'},
1134 {"no-graph", required_argument, 0, 'N'},
1135 {"only-graph", required_argument, 0, 'O'},
1136 {"device", required_argument, 0, 'd'},
1137 {"blktrace-destination", required_argument, 0, 'D'},
1138 {"prog", required_argument, 0, 'p'},
1139 {"movie", optional_argument, 0, 'm'},
1140 {"width", required_argument, 0, 'w'},
1141 {"height", required_argument, 0, 'h'},
1142 {"xzoom", required_argument, 0, 'x'},
1143 {"yzoom", required_argument, 0, 'y'},
1144 {"io-plot-action", required_argument, 0, 'a'},
1145 {"per-process-io", no_argument, 0, 'P'},
1146 {"help", no_argument, 0, HELP_LONG_OPT},
1150 static void print_usage(void)
1152 fprintf(stderr, "iowatcher usage:\n"
1153 "\t-d (--device): device for blktrace to trace\n"
1154 "\t-D (--blktrace-destination): destination for blktrace\n"
1155 "\t-t (--trace): trace file name (more than one allowed)\n"
1156 "\t-l (--label): trace label in the graph\n"
1157 "\t-o (--output): output file name (SVG only)\n"
1158 "\t-p (--prog): program to run while blktrace is run\n"
1159 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1160 "\t-r (--rolling): number of seconds in the rolling averge\n"
1161 "\t-T (--title): graph title\n"
1162 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1163 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1164 "\t-O (--only-graph): add a single graph to the output\n"
1165 "\t-h (--height): set the height of each graph\n"
1166 "\t-w (--width): set the width of each graph\n"
1167 "\t-c (--columns): numbers of columns in graph output\n"
1168 "\t-x (--xzoom): limit processed time to min:max\n"
1169 "\t-y (--yzoom): limit processed sectors to min:max\n"
1170 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1171 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1176 static int parse_double_range(char *str, double *min, double *max)
1180 /* Empty lower bound - leave original value */
1181 if (str[0] != ':') {
1182 *min = strtod(str, &end);
1183 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1189 /* Empty upper bound - leave original value */
1191 *max = strtod(end+1, &end);
1192 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1202 static int parse_ull_range(char *str, unsigned long long *min,
1203 unsigned long long *max)
1207 /* Empty lower bound - leave original value */
1208 if (str[0] != ':') {
1209 *min = strtoull(str, &end, 10);
1210 if (*min == ULLONG_MAX && errno == ERANGE)
1216 /* Empty upper bound - leave original value */
1218 *max = strtoull(end+1, &end, 10);
1219 if (*max == ULLONG_MAX && errno == ERANGE)
1229 static int parse_options(int ac, char **av)
1235 // int this_option_optind = optind ? optind : 1;
1236 int option_index = 0;
1238 c = getopt_long(ac, av, option_string,
1239 long_options, &option_index);
1246 graph_title = strdup(optarg);
1249 add_trace_file(optarg);
1250 set_blktrace_outfile(optarg);
1253 output_filename = strdup(optarg);
1256 set_trace_label(optarg);
1259 set_rolling_avg(atoi(optarg));
1263 disable_all_graphs();
1266 enable_one_graph(optarg);
1269 disable_one_graph(optarg);
1272 blktrace_device = strdup(optarg);
1275 blktrace_dest_dir = strdup(optarg);
1276 if (!strcmp(blktrace_dest_dir, "")) {
1277 fprintf(stderr, "Need a directory\n");
1282 program_to_run = strdup(optarg);
1287 movie_style = lookup_movie_style(optarg);
1288 if (movie_style < 0) {
1289 fprintf(stderr, "Unknown movie style %s\n", optarg);
1293 fprintf(stderr, "Using movie style: %s\n",
1294 movie_styles[movie_style]);
1297 opt_graph_height = atoi(optarg);
1300 opt_graph_width = atoi(optarg);
1303 columns = atoi(optarg);
1306 if (parse_double_range(optarg, &min_time, &max_time)
1308 fprintf(stderr, "Cannot parse time range %s\n",
1314 if (parse_ull_range(optarg, &min_mb, &max_mb)
1317 "Cannot parse offset range %s\n",
1321 if (max_mb > ULLONG_MAX >> 20) {
1323 "Upper range limit too big."
1324 " Maximum is %llu.\n", ULLONG_MAX >> 20);
1329 if (strlen(optarg) != 1) {
1331 fprintf(stderr, "Action must be one of Q, D, C.");
1334 plot_io_action = action_char_to_num(optarg[0]);
1335 if (plot_io_action < 0)
1352 static void dest_mkdir()
1356 ret = mkdir(blktrace_dest_dir, 0777);
1357 if (ret && errno != EEXIST) {
1358 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1363 int main(int ac, char **av)
1366 int min_seconds = 0;
1367 int max_seconds = 0;
1369 u64 min_offset = ~(u64)0;
1370 struct trace_file *tf;
1374 init_io_hash_table();
1375 init_process_hash_table();
1377 enable_all_graphs();
1379 parse_options(ac, av);
1381 last_active_graph = last_graph();
1383 set_io_graph_scale(256);
1384 if (movie_style == MOVIE_SPINDLE)
1385 set_graph_size(750, 550);
1387 set_graph_size(700, 400);
1390 * the plots in the movie don't have a seconds
1391 * line yet, this makes us skip it
1393 last_active_graph = TOTAL_GRAPHS + 1;
1395 if (opt_graph_height)
1396 set_graph_height(opt_graph_height);
1398 if (opt_graph_width)
1399 set_graph_width(opt_graph_width);
1401 if (list_empty(&all_traces)) {
1402 fprintf(stderr, "No traces found, exiting\n");
1406 if (blktrace_device) {
1407 char *path = strdup(blktrace_dest_dir);
1410 join_path(path, blktrace_outfile);
1412 ret = start_blktrace(blktrace_device, blktrace_outfile,
1415 fprintf(stderr, "exiting due to blktrace failure\n");
1420 if (program_to_run) {
1421 ret = run_program(program_to_run);
1423 fprintf(stderr, "failed to run %s\n",
1428 blktrace_to_dump(path);
1430 /* no program specified, just wait for
1437 /* step one, read all the traces */
1440 /* step two, find the maxes for time and offset */
1441 list_for_each_entry(tf, &all_traces, list)
1442 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1443 min_seconds = min_time;
1444 if (max_seconds > max_time)
1445 max_seconds = ceil(max_time);
1446 if (min_offset < min_mb << 20)
1447 min_offset = min_mb << 20;
1448 if (max_offset > max_mb << 20)
1449 max_offset = max_mb << 20;
1451 /* push the max we found into all the tfs */
1452 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1454 /* alloc graphing structs for all the traces */
1455 setup_trace_file_graphs();
1457 /* run through all the traces and read their events */
1458 read_trace_events();
1460 pick_line_graph_color();
1462 plot = alloc_plot();
1465 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1466 plot_io_movie(plot);
1470 set_plot_output(plot, output_filename);
1472 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1473 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1474 else if (num_traces > 1)
1475 set_legend_width(longest_label);
1477 set_legend_width(0);
1479 get_graph_size(&cols, &rows);
1481 plot->add_xlabel = 1;
1482 set_plot_title(plot, graph_title);
1484 check_plot_columns(plot, IO_GRAPH_INDEX);
1485 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1486 plot->add_xlabel = 0;
1489 set_graph_size(cols / columns, rows);
1490 num_xticks /= columns;
1497 check_plot_columns(plot, TPUT_GRAPH_INDEX);
1498 plot_tput(plot, min_seconds, max_seconds);
1500 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1501 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1502 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1504 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1505 plot_cpu(plot, max_seconds, "CPU System Time",
1506 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1508 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1509 plot_cpu(plot, max_seconds, "CPU IRQ Time",
1510 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1512 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1513 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1514 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1516 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1517 plot_cpu(plot, max_seconds, "CPU User Time",
1518 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1520 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1521 plot_latency(plot, min_seconds, max_seconds);
1523 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1524 plot_queue_depth(plot, min_seconds, max_seconds);
1526 check_plot_columns(plot, IOPS_GRAPH_INDEX);
1527 plot_iops(plot, min_seconds, max_seconds);
1531 close_plot_file(plot);