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