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