iowatcher: Add possibility to limit seconds from below
[blktrace.git] / iowatcher / main.c
1 /*
2  * Copyright (C) 2012 Fusion-io
3  *
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.
7  *
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.
12  *
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
16  *
17  *  Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
18  */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <math.h>
26 #include <inttypes.h>
27 #include <string.h>
28 #include <asm/types.h>
29 #include <errno.h>
30 #include <sys/mman.h>
31 #include <time.h>
32 #include <math.h>
33 #include <getopt.h>
34
35 #include "plot.h"
36 #include "blkparse.h"
37 #include "list.h"
38 #include "tracers.h"
39 #include "mpstat.h"
40
41 LIST_HEAD(all_traces);
42
43 static char line[1024];
44 static int line_len = 1024;
45 static int found_mpstat = 0;
46 static int cpu_color_index = 0;
47 static int color_index = 0;
48 static int make_movie = 0;
49 static int opt_graph_width = 0;
50 static int opt_graph_height = 0;
51
52 static int columns = 1;
53 static int num_xticks = 9;
54
55 /*
56  * this doesn't include the IO graph,
57  * but it counts the other graphs as they go out
58  */
59 static int total_graphs_written = 1;
60
61 char *colors[] = {
62         "blue", "darkgreen",
63         "red", "aqua",
64         "orange", "darkviolet",
65         "brown", "#00FF00",
66         "yellow", "coral",
67         "black", "darkred",
68         "fuchsia", "crimson",
69         NULL };
70
71 char *pick_color(void) {
72         char *ret = colors[color_index];
73         if (!ret) {
74                 color_index = 0;
75                 ret = colors[color_index];
76         }
77         color_index++;
78         return ret;
79 }
80
81 char *pick_cpu_color(void) {
82         char *ret = colors[cpu_color_index];
83         if (!ret) {
84                 color_index = 0;
85                 ret = colors[cpu_color_index];
86         }
87         cpu_color_index++;
88         return ret;
89 }
90
91 enum {
92         IO_GRAPH_INDEX = 0,
93         TPUT_GRAPH_INDEX,
94         CPU_SYS_GRAPH_INDEX,
95         CPU_IO_GRAPH_INDEX,
96         CPU_IRQ_GRAPH_INDEX,
97         CPU_SOFT_GRAPH_INDEX,
98         CPU_USER_GRAPH_INDEX,
99         LATENCY_GRAPH_INDEX,
100         QUEUE_DEPTH_GRAPH_INDEX,
101         IOPS_GRAPH_INDEX,
102         TOTAL_GRAPHS
103 };
104
105 enum {
106         MPSTAT_SYS = 0,
107         MPSTAT_IRQ,
108         MPSTAT_IO,
109         MPSTAT_SOFT,
110         MPSTAT_USER,
111         MPSTAT_GRAPHS
112 };
113
114 static char *graphs_by_name[] = {
115         "io",
116         "tput",
117         "cpu-sys",
118         "cpu-io",
119         "cpu-irq",
120         "cpu-soft",
121         "cpu-user",
122         "latency",
123         "queue-depth",
124         "iops",
125 };
126
127 enum {
128         MOVIE_SPINDLE,
129         MOVIE_RECT,
130         NUM_MOVIE_STYLES,
131 };
132
133 char *movie_styles[] = {
134         "spindle",
135         "rect",
136         NULL
137 };
138
139 static int movie_style = 0;
140
141 static int lookup_movie_style(char *str)
142 {
143         int i;
144
145         for (i = 0; i < NUM_MOVIE_STYLES; i++) {
146                 if (strcmp(str, movie_styles[i]) == 0)
147                         return i;
148         }
149         return -1;
150 }
151
152 static int active_graphs[TOTAL_GRAPHS];
153 static int last_active_graph = IOPS_GRAPH_INDEX;
154
155 static int label_index = 0;
156 static int num_traces = 0;
157 static int longest_label = 0;
158
159 struct trace_file {
160         struct list_head list;
161         char *filename;
162         char *label;
163         struct trace *trace;
164         int stop_seconds;       /* Time when trace stops */
165         int min_seconds;        /* Beginning of the interval we should plot */
166         int max_seconds;        /* End of the interval we should plot */
167         u64 min_offset;
168         u64 max_offset;
169
170         char *read_color;
171         char *write_color;
172
173         struct graph_line_data *tput_gld;
174         struct graph_line_data *iop_gld;
175         struct graph_line_data *latency_gld;
176         struct graph_line_data *queue_depth_gld;
177         struct graph_dot_data *gdd_writes;
178         struct graph_dot_data *gdd_reads;
179
180         int mpstat_min_seconds;
181         int mpstat_max_seconds;
182         int mpstat_stop_seconds;
183         struct graph_line_data **mpstat_gld;
184 };
185
186 static void alloc_mpstat_gld(struct trace_file *tf)
187 {
188         struct graph_line_data **ptr;
189
190         if (tf->trace->mpstat_num_cpus == 0)
191                 return;
192
193         ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
194                      sizeof(struct graph_line_data *));
195         if (!ptr) {
196                 perror("Unable to allocate mpstat arrays\n");
197                 exit(1);
198         }
199         tf->mpstat_gld = ptr;
200 }
201
202 static void enable_all_graphs(void)
203 {
204         int i;
205         for (i = 0; i < TOTAL_GRAPHS; i++)
206                 active_graphs[i] = 1;
207 }
208
209 static void disable_all_graphs(void)
210 {
211         int i;
212         for (i = 0; i < TOTAL_GRAPHS; i++)
213                 active_graphs[i] = 0;
214 }
215
216 static int enable_one_graph(char *name)
217 {
218         int i;
219         for (i = 0; i < TOTAL_GRAPHS; i++) {
220                 if (strcmp(name, graphs_by_name[i]) == 0) {
221                         active_graphs[i] = 1;
222                         return 0;
223                 }
224         }
225         return -ENOENT;
226 }
227
228 static int disable_one_graph(char *name)
229 {
230         int i;
231         for (i = 0; i < TOTAL_GRAPHS; i++) {
232                 if (strcmp(name, graphs_by_name[i]) == 0) {
233                         active_graphs[i] = 0;
234                         return 0;
235                 }
236         }
237         return -ENOENT;
238 }
239
240 static int last_graph(void)
241 {
242         int i;
243         for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
244                 if (active_graphs[i]) {
245                         return i;
246                 }
247         }
248         return -ENOENT;
249 }
250
251 static int graphs_left(int cur)
252 {
253         int i;
254         int left = 0;
255         for (i = cur; i < TOTAL_GRAPHS; i++) {
256                 if (active_graphs[i])
257                         left++;
258         }
259         return left;
260 }
261
262 static void add_trace_file(char *filename)
263 {
264         struct trace_file *tf;
265
266         tf = calloc(1, sizeof(*tf));
267         if (!tf) {
268                 fprintf(stderr, "Unable to allocate memory\n");
269                 exit(1);
270         }
271         tf->label = "";
272         tf->filename = strdup(filename);
273         list_add_tail(&tf->list, &all_traces);
274         tf->read_color = pick_color();
275         tf->write_color = pick_color();
276         num_traces++;
277 }
278
279 static void setup_trace_file_graphs(void)
280 {
281         struct trace_file *tf;
282         int i;
283
284         list_for_each_entry(tf, &all_traces, list) {
285                 tf->tput_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
286                 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
287                 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
288                 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
289                 tf->gdd_writes = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds);
290                 tf->gdd_reads = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds);
291
292                 if (tf->trace->mpstat_num_cpus == 0)
293                         continue;
294
295                 alloc_mpstat_gld(tf);
296                 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
297                         tf->mpstat_gld[i] =
298                                 alloc_line_data(tf->mpstat_min_seconds,
299                                                 tf->mpstat_max_seconds,
300                                                 tf->mpstat_max_seconds);
301                         tf->mpstat_gld[i]->max = 100;
302                 }
303         }
304 }
305
306 static void read_traces(void)
307 {
308         struct trace_file *tf;
309         struct trace *trace;
310         u64 last_time;
311         u64 ymin;
312         u64 ymax;
313         u64 max_bank;
314         u64 max_bank_offset;
315
316         list_for_each_entry(tf, &all_traces, list) {
317                 trace = open_trace(tf->filename);
318                 if (!trace)
319                         exit(1);
320
321                 last_time = find_last_time(trace);
322                 tf->trace = trace;
323                 tf->max_seconds = SECONDS(last_time);
324                 tf->stop_seconds = SECONDS(last_time);
325                 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
326                                     &max_bank, &max_bank_offset);
327                 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
328                 tf->min_offset = ymin;
329                 tf->max_offset = ymax;
330
331                 read_mpstat(trace, tf->filename);
332                 tf->mpstat_stop_seconds = trace->mpstat_seconds;
333                 tf->mpstat_max_seconds = trace->mpstat_seconds;
334                 if (tf->mpstat_max_seconds)
335                         found_mpstat = 1;
336         }
337 }
338
339 static void read_trace_events(void)
340 {
341
342         struct trace_file *tf;
343         struct trace *trace;
344         int ret;
345         int i;
346         int time;
347         double user, sys, iowait, irq, soft;
348         double max_user = 0, max_sys = 0, max_iowait = 0,
349                max_irq = 0, max_soft = 0;
350
351         list_for_each_entry(tf, &all_traces, list) {
352                 trace = tf->trace;
353                 first_record(trace);
354                 while (1) {
355                         check_record(trace);
356                         add_tput(trace, tf->tput_gld);
357                         add_iop(trace, tf->iop_gld);
358                         add_io(trace, tf->gdd_writes, tf->gdd_reads);
359                         add_pending_io(trace, tf->queue_depth_gld);
360                         add_completed_io(trace, tf->latency_gld);
361                         ret = next_record(trace);
362                         if (ret)
363                                 break;
364                 }
365         }
366         list_for_each_entry(tf, &all_traces, list) {
367                 trace = tf->trace;
368
369                 if (trace->mpstat_num_cpus == 0)
370                         continue;
371
372                 first_mpstat(trace);
373
374                 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
375                         for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
376                                 ret = read_mpstat_event(trace, &user, &sys,
377                                                         &iowait, &irq, &soft);
378                                 if (ret)
379                                         goto mpstat_done;
380                                 if (next_mpstat_line(trace))
381                                         goto mpstat_done;
382
383                                 if (sys > max_sys)
384                                         max_sys = sys;
385                                 if (user > max_user)
386                                         max_user = user;
387                                 if (irq > max_irq)
388                                         max_irq = irq;
389                                 if (iowait > max_iowait)
390                                         max_iowait = iowait;
391
392                                 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
393                                 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
394                                 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
395                                 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
396                                 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
397                         }
398                         if (next_mpstat(trace) == NULL)
399                                 break;
400                 }
401         }
402
403 mpstat_done:
404         list_for_each_entry(tf, &all_traces, list) {
405                 trace = tf->trace;
406
407                 if (trace->mpstat_num_cpus == 0)
408                         continue;
409
410                 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
411                 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
412                 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
413                 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
414                 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
415         }
416         return;
417 }
418
419 static void set_trace_label(char *label)
420 {
421         int cur = 0;
422         struct trace_file *tf;
423         int len = strlen(label);
424
425         if (len > longest_label)
426                 longest_label = len;
427
428         list_for_each_entry(tf, &all_traces, list) {
429                 if (cur == label_index) {
430                         tf->label = strdup(label);
431                         label_index++;
432                         break;
433                 }
434                 cur++;
435         }
436 }
437
438 static char *graph_title = "";
439 static char *output_filename = "trace.svg";
440 static char *blktrace_device = NULL;
441 static char *blktrace_outfile = "trace";
442 static char *blktrace_dest_dir = ".";
443 static char *program_to_run = NULL;
444
445 static void set_blktrace_outfile(char *arg)
446 {
447         char *s = strdup(arg);
448         char *last_dot = strrchr(s, '.');
449
450         if (last_dot) {
451                 if (strcmp(last_dot, ".dump") == 0)
452                         *last_dot = '\0';
453         }
454         blktrace_outfile = s;
455 }
456
457
458 static void compare_minmax_tf(struct trace_file *tf, int *max_seconds, u64 *min_offset, u64 *max_offset)
459 {
460         if (tf->max_seconds > *max_seconds)
461                 *max_seconds = tf->max_seconds;
462         if (tf->max_offset > *max_offset)
463                 *max_offset = tf->max_offset;
464         if (tf->min_offset < *min_offset)
465                 *min_offset = tf->min_offset;
466 }
467
468 static void set_all_minmax_tf(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
469 {
470         struct trace_file *tf;
471
472         list_for_each_entry(tf, &all_traces, list) {
473                 tf->min_seconds = min_seconds;
474                 tf->max_seconds = max_seconds;
475                 tf->min_offset = min_offset;
476                 tf->max_offset = max_offset;
477         }
478 }
479
480 static char *create_movie_temp_dir(void)
481 {
482         char *ret;
483         char *pattern = strdup("io-movie-XXXXXX");;
484
485         ret = mkdtemp(pattern);
486         if (!ret) {
487                 perror("Unable to create temp directory for movie files");
488                 exit(1);
489         }
490         return ret;
491 }
492
493 static struct plot_history *alloc_plot_history(char *color)
494 {
495         struct plot_history *ph = calloc(1, sizeof(struct plot_history));
496
497         if (!ph) {
498                 perror("memory allocation failed");
499                 exit(1);
500         }
501         ph->history = calloc(4096, sizeof(double));
502         if (!ph->history) {
503                 perror("memory allocation failed");
504                 exit(1);
505         }
506         ph->history_len = 4096;
507         ph->color = color;
508         return ph;
509 }
510
511 LIST_HEAD(movie_history_writes);
512 LIST_HEAD(movie_history_reads);
513 int num_histories = 0;
514
515 static void add_history(struct plot_history *ph, struct list_head *list)
516 {
517         struct plot_history *entry;
518
519         list_add_tail(&ph->list, list);
520         num_histories++;
521
522         if (num_histories > 12) {
523                 num_histories--;
524                 entry = list_entry(list->next, struct plot_history, list);
525                 list_del(&entry->list);
526                 free(entry->history);
527                 free(entry);
528         }
529 }
530
531 static void plot_movie_history(struct plot *plot, struct list_head *list)
532 {
533         struct plot_history *ph;
534
535         if (num_histories > 2)
536                 rewind_spindle_steps(num_histories - 1);
537
538         list_for_each_entry(ph, list, list) {
539                 if (movie_style == MOVIE_SPINDLE)
540                         svg_io_graph_movie_array_spindle(plot, ph);
541                 else
542                         svg_io_graph_movie_array(plot, ph);
543          }
544 }
545
546 static void free_all_plot_history(struct list_head *head)
547 {
548         struct plot_history *entry;
549         while (!list_empty(head)) {
550                 entry = list_entry(head->next, struct plot_history, list);
551                 list_del(&entry->list);
552                 free(entry->history);
553                 free(entry);
554         }
555 }
556
557 static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
558 {
559         struct trace_file *tf;
560
561         if (active_graphs[IO_GRAPH_INDEX] == 0)
562                 return;
563
564         setup_axis(plot);
565
566         svg_alloc_legend(plot, num_traces * 2);
567
568         set_plot_label(plot, "Device IO");
569         set_ylabel(plot, "Offset (MB)");
570         set_yticks(plot, 4, min_offset / (1024 * 1024),
571                    max_offset / (1024 * 1024), "");
572         set_xticks(plot, num_xticks, min_seconds, max_seconds);
573
574         list_for_each_entry(tf, &all_traces, list) {
575                 char *label = tf->label;
576
577                 if (!label)
578                         label = "";
579                 svg_io_graph(plot, tf->gdd_reads, tf->read_color);
580                 if (tf->gdd_reads->total_ios)
581                         svg_add_legend(plot, label, " Reads", tf->read_color);
582
583                 svg_io_graph(plot, tf->gdd_writes, tf->write_color);
584                 if (tf->gdd_writes->total_ios) {
585                         svg_add_legend(plot, label, " Writes", tf->write_color);
586                 }
587         }
588         if (plot->add_xlabel)
589                 set_xlabel(plot, "Time (seconds)");
590         svg_write_legend(plot);
591         close_plot(plot);
592 }
593
594 static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
595 {
596         struct trace_file *tf;
597         char *units;
598         char line[128];
599         u64 max = 0;
600
601         if (active_graphs[TPUT_GRAPH_INDEX] == 0)
602                 return;
603
604         if (num_traces > 1)
605                 svg_alloc_legend(plot, num_traces);
606         list_for_each_entry(tf, &all_traces, list) {
607                 if (tf->tput_gld->max > max)
608                         max = tf->tput_gld->max;
609         }
610         list_for_each_entry(tf, &all_traces, list)
611                 tf->tput_gld->max = max;
612
613         setup_axis(plot);
614         set_plot_label(plot, "Throughput");
615
616         tf = list_entry(all_traces.next, struct trace_file, list);
617
618         scale_line_graph_bytes(&max, &units, 1024);
619         sprintf(line, "%sB/s", units);
620         set_ylabel(plot, line);
621         set_yticks(plot, 4, 0, max, "");
622         set_xticks(plot, num_xticks, min_seconds, max_seconds);
623
624         list_for_each_entry(tf, &all_traces, list) {
625                 svg_line_graph(plot, tf->tput_gld, tf->read_color, 0, 0);
626                 if (num_traces > 1)
627                         svg_add_legend(plot, tf->label, "", tf->read_color);
628         }
629
630         if (plot->add_xlabel)
631                 set_xlabel(plot, "Time (seconds)");
632         if (num_traces > 1)
633                 svg_write_legend(plot);
634         close_plot(plot);
635         total_graphs_written++;
636 }
637
638 static void plot_cpu(struct plot *plot, int max_seconds, char *label,
639                      int active_index, int gld_index)
640 {
641         struct trace_file *tf;
642         int max = 0;
643         int i;
644         int gld_i;
645         char *color;
646         double avg = 0;
647         int ymax;
648         int plotted = 0;
649
650         if (active_graphs[active_index] == 0)
651                 return;
652
653         list_for_each_entry(tf, &all_traces, list) {
654                 if (tf->trace->mpstat_num_cpus > max)
655                         max = tf->trace->mpstat_num_cpus;
656         }
657         if (max == 0)
658                 return;
659
660         tf = list_entry(all_traces.next, struct trace_file, list);
661
662         ymax = tf->mpstat_gld[gld_index]->max;
663         if (ymax == 0)
664                 return;
665
666         svg_alloc_legend(plot, num_traces * max);
667
668         setup_axis(plot);
669         set_plot_label(plot, label);
670
671         max_seconds = tf->mpstat_max_seconds;
672
673         set_yticks(plot, 4, 0, tf->mpstat_gld[gld_index]->max, "");
674         set_ylabel(plot, "Percent");
675         set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
676
677         cpu_color_index = 0;
678         list_for_each_entry(tf, &all_traces, list) {
679                 for (i = tf->mpstat_gld[0]->min_seconds;
680                      i < tf->mpstat_gld[0]->stop_seconds; i++) {
681                         if (tf->mpstat_gld[gld_index]->data[i].count) {
682                                 avg += (tf->mpstat_gld[gld_index]->data[i].sum /
683                                         tf->mpstat_gld[gld_index]->data[i].count);
684                         }
685                 }
686                 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
687                        tf->mpstat_gld[gld_index]->min_seconds;
688                 color = pick_cpu_color();
689                 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
690                 svg_add_legend(plot, tf->label, " avg", color);
691
692                 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
693                         struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
694                         double this_avg = 0;
695
696                         for (gld_i = gld->min_seconds;
697                              gld_i < gld->stop_seconds; gld_i++) {
698                                 if (gld->data[i].count) {
699                                         this_avg += gld->data[i].sum /
700                                                 gld->data[i].count;
701                                 }
702                         }
703
704                         this_avg /= gld->stop_seconds - gld->min_seconds;
705
706                         for (gld_i = gld->min_seconds;
707                              gld_i < gld->stop_seconds; gld_i++) {
708                                 double val;
709
710                                 if (gld->data[gld_i].count == 0)
711                                         continue;
712                                 val = (double)gld->data[gld_i].sum /
713                                         gld->data[gld_i].count;
714
715                                 if (this_avg > avg + 30 || val > 95) {
716                                         color = pick_cpu_color();
717                                         svg_line_graph(plot, gld, color, avg + 30, 95);
718                                         snprintf(line, line_len, " CPU %d\n", i - 1);
719                                         svg_add_legend(plot, tf->label, line, color);
720                                         plotted++;
721                                         break;
722                                 }
723
724                         }
725                 }
726         }
727
728         if (plot->add_xlabel)
729                 set_xlabel(plot, "Time (seconds)");
730
731         if (!plot->no_legend) {
732                 svg_write_legend(plot);
733                 svg_free_legend(plot);
734         }
735         close_plot(plot);
736         total_graphs_written++;
737 }
738
739 static void plot_queue_depth(struct plot *plot, int min_seconds, int max_seconds)
740 {
741         struct trace_file *tf;
742
743         if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
744                 return;
745
746         setup_axis(plot);
747         set_plot_label(plot, "Queue Depth");
748         if (num_traces > 1)
749                 svg_alloc_legend(plot, num_traces);
750
751         tf = list_entry(all_traces.next, struct trace_file, list);
752         set_ylabel(plot, "Pending IO");
753         set_yticks(plot, 4, 0, tf->queue_depth_gld->max, "");
754         set_xticks(plot, num_xticks, min_seconds, max_seconds);
755
756         list_for_each_entry(tf, &all_traces, list) {
757                 svg_line_graph(plot, tf->queue_depth_gld, tf->read_color, 0, 0);
758                 if (num_traces > 1)
759                         svg_add_legend(plot, tf->label, "", tf->read_color);
760         }
761
762         if (plot->add_xlabel)
763                 set_xlabel(plot, "Time (seconds)");
764         if (num_traces > 1)
765                 svg_write_legend(plot);
766         close_plot(plot);
767         total_graphs_written++;
768 }
769
770 static void convert_movie_files(char *movie_dir)
771 {
772         fprintf(stderr, "Converting svg files in %s\n", movie_dir);
773         snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
774                  movie_dir);
775         system(line);
776 }
777
778 static void mencode_movie(char *movie_dir)
779 {
780         fprintf(stderr, "Creating movie %s\n", movie_dir);
781         snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
782                  "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
783         system(line);
784 }
785
786 static void cleanup_movie(char *movie_dir)
787 {
788         fprintf(stderr, "Removing movie dir %s\n", movie_dir);
789         snprintf(line, line_len, "rm %s/*", movie_dir);
790         system(line);
791
792         snprintf(line, line_len, "rmdir %s", movie_dir);
793         system(line);
794 }
795
796 static void plot_io_movie(struct plot *plot)
797 {
798         struct trace_file *tf;
799         char *movie_dir = create_movie_temp_dir();
800         int i;
801         struct plot_history *read_history;
802         struct plot_history *write_history;
803         int batch_i;
804         int movie_len = 30;
805         int movie_frames_per_sec = 20;
806         int total_frames = movie_len * movie_frames_per_sec;
807         int rows, cols;
808         int batch_count;
809         int graph_width_factor = 5;
810         int orig_y_offset;
811
812         get_graph_size(&cols, &rows);
813         batch_count = cols / total_frames;
814
815         if (batch_count == 0)
816                 batch_count = 1;
817
818         list_for_each_entry(tf, &all_traces, list) {
819                 char *label = tf->label;
820                 if (!label)
821                         label = "";
822
823                 i = 0;
824                 while (i < cols) {
825                         snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
826                         set_plot_output(plot, line);
827                         set_plot_title(plot, graph_title);
828                         orig_y_offset = plot->start_y_offset;
829
830                         plot->no_legend = 1;
831
832                         set_graph_size(cols / graph_width_factor, rows / 8);
833                         plot->timeline = i / graph_width_factor;
834
835                         plot_tput(plot, tf->gdd_reads->min_seconds,
836                                   tf->gdd_reads->max_seconds);
837
838                         plot_cpu(plot, tf->gdd_reads->max_seconds,
839                                    "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
840
841                         plot->direction = PLOT_ACROSS;
842                         plot_queue_depth(plot, tf->gdd_reads->min_seconds,
843                                          tf->gdd_reads->max_seconds);
844
845                         /* movie graph starts here */
846                         plot->start_y_offset = orig_y_offset;
847                         set_graph_size(cols - cols / graph_width_factor, rows);
848                         plot->no_legend = 0;
849                         plot->timeline = 0;
850                         plot->direction = PLOT_DOWN;;
851
852                         if (movie_style == MOVIE_SPINDLE)
853                                 setup_axis_spindle(plot);
854                         else
855                                 setup_axis(plot);
856
857                         svg_alloc_legend(plot, num_traces * 2);
858
859                         read_history = alloc_plot_history(tf->read_color);
860                         write_history = alloc_plot_history(tf->write_color);
861                         read_history->col = i;
862                         write_history->col = i;
863
864                         if (tf->gdd_reads->total_ios)
865                                 svg_add_legend(plot, label, " Reads", tf->read_color);
866                         if (tf->gdd_writes->total_ios)
867                                 svg_add_legend(plot, label, " Writes", tf->write_color);
868
869                         batch_i = 0;
870                         while (i < cols && batch_i < batch_count) {
871                                 svg_io_graph_movie(tf->gdd_reads, read_history, i);
872                                 svg_io_graph_movie(tf->gdd_writes, write_history, i);
873                                 i++;
874                                 batch_i++;
875                         }
876
877                         add_history(read_history, &movie_history_reads);
878                         add_history(write_history, &movie_history_writes);
879
880                         plot_movie_history(plot, &movie_history_reads);
881                         plot_movie_history(plot, &movie_history_writes);
882
883                         svg_write_legend(plot);
884                         close_plot(plot);
885                         close_plot(plot);
886
887                         close_plot_file(plot);
888                 }
889                 free_all_plot_history(&movie_history_reads);
890                 free_all_plot_history(&movie_history_writes);
891         }
892         convert_movie_files(movie_dir);
893         mencode_movie(movie_dir);
894         cleanup_movie(movie_dir);
895         free(movie_dir);
896 }
897
898 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
899 {
900         struct trace_file *tf;
901         char *units;
902         char line[128];
903         u64 max = 0;
904
905         if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
906                 return;
907
908         if (num_traces > 1)
909                 svg_alloc_legend(plot, num_traces);
910         list_for_each_entry(tf, &all_traces, list) {
911                 if (tf->latency_gld->max > max)
912                         max = tf->latency_gld->max;
913         }
914         list_for_each_entry(tf, &all_traces, list)
915                 tf->latency_gld->max = max;
916
917         setup_axis(plot);
918         set_plot_label(plot, "IO Latency");
919
920         tf = list_entry(all_traces.next, struct trace_file, list);
921
922         scale_line_graph_time(&max, &units);
923         sprintf(line, "latency (%ss)", units);
924         set_ylabel(plot, line);
925         set_yticks(plot, 4, 0, max, "");
926         set_xticks(plot, num_xticks, min_seconds, max_seconds);
927
928         list_for_each_entry(tf, &all_traces, list) {
929                 svg_line_graph(plot, tf->latency_gld, tf->read_color, 0, 0);
930                 if (num_traces > 1)
931                         svg_add_legend(plot, tf->label, "", tf->read_color);
932         }
933
934         if (plot->add_xlabel)
935                 set_xlabel(plot, "Time (seconds)");
936         if (num_traces > 1)
937                 svg_write_legend(plot);
938         close_plot(plot);
939         total_graphs_written++;
940 }
941
942 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
943 {
944         struct trace_file *tf;
945         char *units;
946         u64 max = 0;
947
948         if (active_graphs[IOPS_GRAPH_INDEX] == 0)
949                 return;
950
951         list_for_each_entry(tf, &all_traces, list) {
952                 if (tf->iop_gld->max > max)
953                         max = tf->iop_gld->max;
954         }
955
956         list_for_each_entry(tf, &all_traces, list)
957                 tf->iop_gld->max = max;
958
959         setup_axis(plot);
960         set_plot_label(plot, "IOPs");
961         if (num_traces > 1)
962                 svg_alloc_legend(plot, num_traces);
963
964         tf = list_entry(all_traces.next, struct trace_file, list);
965
966         scale_line_graph_bytes(&max, &units, 1000);
967         set_ylabel(plot, "IO/s");
968
969         set_yticks(plot, 4, 0, max, units);
970         set_xticks(plot, num_xticks, min_seconds, max_seconds);
971
972         list_for_each_entry(tf, &all_traces, list) {
973                 svg_line_graph(plot, tf->iop_gld, tf->read_color, 0, 0);
974                 if (num_traces > 1)
975                         svg_add_legend(plot, tf->label, "", tf->read_color);
976         }
977
978         if (plot->add_xlabel)
979                 set_xlabel(plot, "Time (seconds)");
980         if (num_traces > 1)
981                 svg_write_legend(plot);
982
983         close_plot(plot);
984         total_graphs_written++;
985 }
986
987 static void check_plot_columns(struct plot *plot, int index)
988 {
989         int count;
990
991         if (columns > 1 && (total_graphs_written == 0 ||
992             total_graphs_written % columns != 0)) {
993                 count = graphs_left(index);
994                 if (plot->direction == PLOT_DOWN) {
995                         plot->start_x_offset = 0;
996                         if (count <= columns)
997                                 plot->add_xlabel = 1;
998                 }
999                 plot->direction = PLOT_ACROSS;
1000
1001         } else {
1002                 plot->direction = PLOT_DOWN;
1003                 if (index == last_active_graph)
1004                         plot->add_xlabel = 1;
1005         }
1006
1007 }
1008
1009 enum {
1010         HELP_LONG_OPT = 1,
1011 };
1012
1013 char *option_string = "T:t:o:l:r:O:N:d:p:m::h:w:c:";
1014 static struct option long_options[] = {
1015         {"columns", required_argument, 0, 'c'},
1016         {"title", required_argument, 0, 'T'},
1017         {"trace", required_argument, 0, 't'},
1018         {"output", required_argument, 0, 'o'},
1019         {"label", required_argument, 0, 'l'},
1020         {"rolling", required_argument, 0, 'r'},
1021         {"no-graph", required_argument, 0, 'N'},
1022         {"only-graph", required_argument, 0, 'O'},
1023         {"device", required_argument, 0, 'd'},
1024         {"prog", required_argument, 0, 'p'},
1025         {"movie", optional_argument, 0, 'm'},
1026         {"width", required_argument, 0, 'w'},
1027         {"height", required_argument, 0, 'h'},
1028         {"help", no_argument, 0, HELP_LONG_OPT},
1029         {0, 0, 0, 0}
1030 };
1031
1032 static void print_usage(void)
1033 {
1034         fprintf(stderr, "iowatcher usage:\n"
1035                 "\t-d (--device): device for blktrace to trace\n"
1036                 "\t-t (--trace): trace file name (more than one allowed)\n"
1037                 "\t-l (--label): trace label in the graph\n"
1038                 "\t-o (--output): output file name (SVG only)\n"
1039                 "\t-p (--prog): program to run while blktrace is run\n"
1040                 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1041                 "\t-r (--rolling): number of seconds in the rolling averge\n"
1042                 "\t-T (--title): graph title\n"
1043                 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1044                 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1045                 "\t-O (--only-graph): add a single graph to the output\n"
1046                 "\t-h (--height): set the height of each graph\n"
1047                 "\t-w (--width): set the width of each graph\n"
1048                 "\t-c (--columns): numbers of columns in graph output\n"
1049                );
1050         exit(1);
1051 }
1052
1053 static int parse_options(int ac, char **av)
1054 {
1055         int c;
1056         int disabled = 0;
1057
1058         while (1) {
1059                 // int this_option_optind = optind ? optind : 1;
1060                 int option_index = 0;
1061
1062                 c = getopt_long(ac, av, option_string,
1063                                 long_options, &option_index);
1064
1065                 if (c == -1)
1066                         break;
1067
1068                 switch(c) {
1069                 case 'T':
1070                         graph_title = strdup(optarg);
1071                         break;
1072                 case 't':
1073                         add_trace_file(optarg);
1074                         set_blktrace_outfile(optarg);
1075                         break;
1076                 case 'o':
1077                         output_filename = strdup(optarg);
1078                         break;
1079                 case 'l':
1080                         set_trace_label(optarg);
1081                         break;
1082                 case 'r':
1083                         set_rolling_avg(atoi(optarg));
1084                         break;
1085                 case 'O':
1086                         if (!disabled) {
1087                                 disable_all_graphs();
1088                                 disabled = 1;
1089                         }
1090                         enable_one_graph(optarg);
1091                         break;
1092                 case 'N':
1093                         disable_one_graph(optarg);
1094                         break;
1095                 case 'd':
1096                         blktrace_device = strdup(optarg);
1097                         break;
1098                 case 'p':
1099                         program_to_run = strdup(optarg);
1100                         break;
1101                 case 'm':
1102                         make_movie = 1;
1103                         if (optarg) {
1104                                 movie_style = lookup_movie_style(optarg);
1105                                 if (movie_style < 0) {
1106                                         fprintf(stderr, "Unknown movie style %s\n", optarg);
1107                                         print_usage();
1108                                 }
1109                         }
1110                         fprintf(stderr, "Using movie style: %s\n",
1111                                 movie_styles[movie_style]);
1112                         break;
1113                 case 'h':
1114                         opt_graph_height = atoi(optarg);
1115                         break;
1116                 case 'w':
1117                         opt_graph_width = atoi(optarg);
1118                         break;
1119                 case 'c':
1120                         columns = atoi(optarg);
1121                         break;
1122                 case '?':
1123                 case HELP_LONG_OPT:
1124                         print_usage();
1125                         break;
1126                 default:
1127                         break;
1128                 }
1129         }
1130         return 0;
1131 }
1132
1133
1134 int main(int ac, char **av)
1135 {
1136         struct plot *plot;
1137         int min_seconds = 0;
1138         int max_seconds = 0;
1139         u64 max_offset = 0;
1140         u64 min_offset = ~(u64)0;
1141         struct trace_file *tf;
1142         int ret;
1143         int rows, cols;
1144
1145         init_io_hash_table();
1146
1147         enable_all_graphs();
1148
1149         parse_options(ac, av);
1150
1151         last_active_graph = last_graph();
1152         if (make_movie) {
1153                 set_io_graph_scale(256);
1154                 if (movie_style == MOVIE_SPINDLE)
1155                         set_graph_size(750, 550);
1156                 else
1157                         set_graph_size(700, 400);
1158
1159                 /*
1160                  * the plots in the movie don't have a seconds
1161                  * line yet, this makes us skip it
1162                  */
1163                 last_active_graph = TOTAL_GRAPHS + 1;
1164         }
1165         if (opt_graph_height)
1166                 set_graph_height(opt_graph_height);
1167
1168         if (opt_graph_width)
1169                 set_graph_width(opt_graph_width);
1170
1171         if (list_empty(&all_traces)) {
1172                 fprintf(stderr, "No traces found, exiting\n");
1173                 exit(1);
1174         }
1175
1176         if (blktrace_device) {
1177                 ret = start_blktrace(blktrace_device, blktrace_outfile,
1178                                      blktrace_dest_dir);
1179                 if (ret) {
1180                         fprintf(stderr, "exiting due to blktrace failure\n");
1181                         exit(1);
1182                 }
1183                 start_mpstat(blktrace_outfile);
1184                 if (program_to_run) {
1185                         ret = run_program(program_to_run);
1186                         if (ret) {
1187                                 fprintf(stderr, "failed to run %s\n",
1188                                         program_to_run);
1189                                 exit(1);
1190                         }
1191                         wait_for_tracers();
1192                         blktrace_to_dump(blktrace_outfile);
1193                 } else {
1194                         /* no program specified, just wait for
1195                          * blktrace to exit
1196                          */
1197                         wait_for_tracers();
1198                 }
1199         }
1200
1201         /* step one, read all the traces */
1202         read_traces();
1203
1204         /* step two, find the maxes for time and offset */
1205         list_for_each_entry(tf, &all_traces, list)
1206                 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1207         /* push the max we found into all the tfs */
1208         set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1209
1210         /* alloc graphing structs for all the traces */
1211         setup_trace_file_graphs();
1212
1213         /* run through all the traces and read their events */
1214         read_trace_events();
1215
1216         plot = alloc_plot();
1217
1218         if (make_movie) {
1219                 plot_io_movie(plot);
1220                 exit(0);
1221         }
1222
1223         set_plot_output(plot, output_filename);
1224
1225         if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1226                 set_legend_width(longest_label + strlen("writes"));
1227         else if (num_traces > 1)
1228                 set_legend_width(longest_label);
1229         else
1230                 set_legend_width(0);
1231
1232         get_graph_size(&cols, &rows);
1233         if (columns > 1)
1234                 plot->add_xlabel = 1;
1235         set_plot_title(plot, graph_title);
1236
1237         plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1238         plot->add_xlabel = 0;
1239
1240         if (columns > 1) {
1241                 set_graph_size(cols / columns, rows);
1242                 num_xticks /= columns;
1243                 if (num_xticks < 2)
1244                         num_xticks = 2;
1245         }
1246
1247         check_plot_columns(plot, TPUT_GRAPH_INDEX);
1248         plot_tput(plot, min_seconds, max_seconds);
1249
1250         check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1251         plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1252                  CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1253
1254         check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1255         plot_cpu(plot, max_seconds, "CPU System Time",
1256                  CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1257
1258         check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1259         plot_cpu(plot, max_seconds, "CPU IRQ Time",
1260                  CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1261
1262         check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1263         plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1264                  CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1265
1266         check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1267         plot_cpu(plot, max_seconds, "CPU User Time",
1268                  CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1269
1270         check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1271         plot_latency(plot, min_seconds, max_seconds);
1272
1273         check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1274         plot_queue_depth(plot, min_seconds, max_seconds);
1275
1276         check_plot_columns(plot, IOPS_GRAPH_INDEX);
1277         plot_iops(plot, min_seconds, max_seconds);
1278
1279         /* once for all */
1280         close_plot(plot);
1281         close_plot_file(plot);
1282         return 0;
1283 }