iowatcher: Handle traces to more than once device at a time
[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 #include <limits.h>
35 #include <float.h>
36
37 #include "plot.h"
38 #include "blkparse.h"
39 #include "list.h"
40 #include "tracers.h"
41 #include "mpstat.h"
42
43 LIST_HEAD(all_traces);
44
45 static char line[1024];
46 static int line_len = 1024;
47 static int found_mpstat = 0;
48 static int make_movie = 0;
49 static int keep_movie_svgs = 0;
50 static int opt_graph_width = 0;
51 static int opt_graph_height = 0;
52
53 static int columns = 1;
54 static int num_xticks = 7;
55 static int num_yticks = 4;
56
57 static double min_time = 0;
58 static double max_time = DBL_MAX;
59 static unsigned long long min_mb = 0;
60 static unsigned long long max_mb = ULLONG_MAX >> 20;
61
62 int plot_io_action = 0;
63 int io_per_process = 0;
64 unsigned int longest_proc_name = 0;
65
66 /*
67  * this doesn't include the IO graph,
68  * but it counts the other graphs as they go out
69  */
70 static int total_graphs_written = 1;
71
72 enum {
73         IO_GRAPH_INDEX = 0,
74         TPUT_GRAPH_INDEX,
75         CPU_SYS_GRAPH_INDEX,
76         CPU_IO_GRAPH_INDEX,
77         CPU_IRQ_GRAPH_INDEX,
78         CPU_SOFT_GRAPH_INDEX,
79         CPU_USER_GRAPH_INDEX,
80         LATENCY_GRAPH_INDEX,
81         QUEUE_DEPTH_GRAPH_INDEX,
82         IOPS_GRAPH_INDEX,
83         TOTAL_GRAPHS
84 };
85
86 enum {
87         MPSTAT_SYS = 0,
88         MPSTAT_IRQ,
89         MPSTAT_IO,
90         MPSTAT_SOFT,
91         MPSTAT_USER,
92         MPSTAT_GRAPHS
93 };
94
95 static char *graphs_by_name[] = {
96         "io",
97         "tput",
98         "cpu-sys",
99         "cpu-io",
100         "cpu-irq",
101         "cpu-soft",
102         "cpu-user",
103         "latency",
104         "queue-depth",
105         "iops",
106 };
107
108 enum {
109         MOVIE_SPINDLE,
110         MOVIE_RECT,
111         NUM_MOVIE_STYLES,
112 };
113
114 char *movie_styles[] = {
115         "spindle",
116         "rect",
117         NULL
118 };
119
120 static int movie_style = 0;
121
122 static int lookup_movie_style(char *str)
123 {
124         int i;
125
126         for (i = 0; i < NUM_MOVIE_STYLES; i++) {
127                 if (strcmp(str, movie_styles[i]) == 0)
128                         return i;
129         }
130         return -1;
131 }
132
133 static int active_graphs[TOTAL_GRAPHS];
134 static int last_active_graph = IOPS_GRAPH_INDEX;
135
136 static int label_index = 0;
137 static int num_traces = 0;
138 static int longest_label = 0;
139
140 static char *graph_title = "";
141 static char *output_filename = "trace.svg";
142 static char *blktrace_devices[MAX_DEVICES_PER_TRACE];
143 static int num_blktrace_devices = 0;
144 static char *blktrace_outfile = "trace";
145 static char *blktrace_dest_dir = ".";
146 static char *program_to_run = NULL;
147 static char *ffmpeg_codec = "libx264";
148
149 static void alloc_mpstat_gld(struct trace_file *tf)
150 {
151         struct graph_line_data **ptr;
152
153         if (tf->trace->mpstat_num_cpus == 0)
154                 return;
155
156         ptr = calloc((tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS,
157                      sizeof(struct graph_line_data *));
158         if (!ptr) {
159                 perror("Unable to allocate mpstat arrays\n");
160                 exit(1);
161         }
162         tf->mpstat_gld = ptr;
163 }
164
165 static void enable_all_graphs(void)
166 {
167         int i;
168         for (i = 0; i < TOTAL_GRAPHS; i++)
169                 active_graphs[i] = 1;
170 }
171
172 static void disable_all_graphs(void)
173 {
174         int i;
175         for (i = 0; i < TOTAL_GRAPHS; i++)
176                 active_graphs[i] = 0;
177 }
178
179 static int enable_one_graph(char *name)
180 {
181         int i;
182         for (i = 0; i < TOTAL_GRAPHS; i++) {
183                 if (strcmp(name, graphs_by_name[i]) == 0) {
184                         active_graphs[i] = 1;
185                         return 0;
186                 }
187         }
188         return -ENOENT;
189 }
190
191 static int disable_one_graph(char *name)
192 {
193         int i;
194         for (i = 0; i < TOTAL_GRAPHS; i++) {
195                 if (strcmp(name, graphs_by_name[i]) == 0) {
196                         active_graphs[i] = 0;
197                         return 0;
198                 }
199         }
200         return -ENOENT;
201 }
202
203 static int last_graph(void)
204 {
205         int i;
206         for (i = TOTAL_GRAPHS - 1; i >= 0; i--) {
207                 if (active_graphs[i]) {
208                         return i;
209                 }
210         }
211         return -ENOENT;
212 }
213
214 static int graphs_left(int cur)
215 {
216         int i;
217         int left = 0;
218         for (i = cur; i < TOTAL_GRAPHS; i++) {
219                 if (active_graphs[i])
220                         left++;
221         }
222         return left;
223 }
224
225 static char * join_path(char *dest_dir, char *filename)
226 {
227         /* alloc 2 extra bytes for '/' and '\0' */
228         char *path = malloc(strlen(dest_dir) + strlen(filename) + 2);
229         sprintf(path, "%s%s%s", dest_dir, "/", filename);
230
231         return path;
232 }
233
234 static void add_trace_file(char *filename)
235 {
236         struct trace_file *tf;
237
238         tf = calloc(1, sizeof(*tf));
239         if (!tf) {
240                 fprintf(stderr, "Unable to allocate memory\n");
241                 exit(1);
242         }
243         tf->label = "";
244         tf->filename = strdup(filename);
245         list_add_tail(&tf->list, &all_traces);
246         tf->line_color = "black";
247         num_traces++;
248 }
249
250 static void setup_trace_file_graphs(void)
251 {
252         struct trace_file *tf;
253         int i;
254         int alloc_ptrs;
255
256         if (io_per_process)
257                 alloc_ptrs = 16;
258         else
259                 alloc_ptrs = 1;
260         list_for_each_entry(tf, &all_traces, list) {
261                 tf->tput_reads_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
262                 tf->tput_writes_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
263                 tf->latency_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
264                 tf->queue_depth_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
265                 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
266                 tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
267                 tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data));
268                 tf->io_plots_allocated = alloc_ptrs;
269
270                 if (tf->trace->mpstat_num_cpus == 0)
271                         continue;
272
273                 alloc_mpstat_gld(tf);
274                 for (i = 0; i < (tf->trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i++) {
275                         tf->mpstat_gld[i] =
276                                 alloc_line_data(tf->mpstat_min_seconds,
277                                                 tf->mpstat_max_seconds,
278                                                 tf->mpstat_max_seconds);
279                         tf->mpstat_gld[i]->max = 100;
280                 }
281         }
282 }
283
284 static void read_traces(void)
285 {
286         struct trace_file *tf;
287         struct trace *trace;
288         u64 last_time;
289         u64 ymin;
290         u64 ymax;
291         u64 max_bank;
292         u64 max_bank_offset;
293         char *path = NULL;
294
295         list_for_each_entry(tf, &all_traces, list) {
296                 path = join_path(blktrace_dest_dir, tf->filename);
297                 trace = open_trace(path);
298                 if (!trace)
299                         exit(1);
300
301                 last_time = find_last_time(trace);
302                 tf->trace = trace;
303                 tf->max_seconds = SECONDS(last_time) + 1;
304                 tf->stop_seconds = SECONDS(last_time) + 1;
305
306                 find_extreme_offsets(trace, &tf->min_offset, &tf->max_offset,
307                                     &max_bank, &max_bank_offset);
308                 filter_outliers(trace, tf->min_offset, tf->max_offset, &ymin, &ymax);
309                 tf->min_offset = ymin;
310                 tf->max_offset = ymax;
311
312                 read_mpstat(trace, path);
313                 tf->mpstat_stop_seconds = trace->mpstat_seconds;
314                 tf->mpstat_max_seconds = trace->mpstat_seconds;
315                 if (tf->mpstat_max_seconds)
316                         found_mpstat = 1;
317                 free(path);
318         }
319 }
320
321 static void pick_line_graph_color(void)
322 {
323         struct trace_file *tf;
324         int i;
325
326         list_for_each_entry(tf, &all_traces, list) {
327                 for (i = 0; i < tf->io_plots; i++) {
328                         if (tf->gdd_reads[i]) {
329                                 tf->line_color = tf->gdd_reads[i]->color;
330                                 tf->reads_color = tf->gdd_reads[i]->color;
331                         }
332                         if (tf->gdd_writes[i]) {
333                                 tf->line_color = tf->gdd_writes[i]->color;
334                                 tf->writes_color = tf->gdd_writes[i]->color;
335                         }
336                         if (tf->writes_color && tf->reads_color)
337                                 break;
338                 }
339                 if (!tf->reads_color)
340                         tf->reads_color = tf->line_color;
341                 if (!tf->writes_color)
342                         tf->writes_color = tf->line_color;
343         }
344 }
345
346 static void read_trace_events(void)
347 {
348
349         struct trace_file *tf;
350         struct trace *trace;
351         int ret;
352         int i;
353         int time;
354         double user, sys, iowait, irq, soft;
355         double max_user = 0, max_sys = 0, max_iowait = 0,
356                max_irq = 0, max_soft = 0;
357
358         list_for_each_entry(tf, &all_traces, list) {
359                 trace = tf->trace;
360                 first_record(trace);
361                 while (1) {
362                         check_record(trace);
363                         add_tput(trace, tf->tput_writes_gld, tf->tput_reads_gld);
364                         add_iop(trace, tf->iop_gld);
365                         add_io(trace, tf);
366                         add_pending_io(trace, tf->queue_depth_gld);
367                         add_completed_io(trace, tf->latency_gld);
368                         ret = next_record(trace);
369                         if (ret)
370                                 break;
371                 }
372         }
373         list_for_each_entry(tf, &all_traces, list) {
374                 trace = tf->trace;
375
376                 if (trace->mpstat_num_cpus == 0)
377                         continue;
378
379                 first_mpstat(trace);
380
381                 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
382                         for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
383                                 ret = read_mpstat_event(trace, &user, &sys,
384                                                         &iowait, &irq, &soft);
385                                 if (ret)
386                                         goto mpstat_done;
387                                 if (next_mpstat_line(trace))
388                                         goto mpstat_done;
389
390                                 if (sys > max_sys)
391                                         max_sys = sys;
392                                 if (user > max_user)
393                                         max_user = user;
394                                 if (irq > max_irq)
395                                         max_irq = irq;
396                                 if (iowait > max_iowait)
397                                         max_iowait = iowait;
398
399                                 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
400                                 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
401                                 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
402                                 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
403                                 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
404                         }
405                         if (next_mpstat(trace) == NULL)
406                                 break;
407                 }
408         }
409
410 mpstat_done:
411         list_for_each_entry(tf, &all_traces, list) {
412                 trace = tf->trace;
413
414                 if (trace->mpstat_num_cpus == 0)
415                         continue;
416
417                 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
418                 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
419                 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
420                 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
421                 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
422         }
423         return;
424 }
425
426 static void set_trace_label(char *label)
427 {
428         int cur = 0;
429         struct trace_file *tf;
430         int len = strlen(label);
431
432         if (len > longest_label)
433                 longest_label = len;
434
435         list_for_each_entry(tf, &all_traces, list) {
436                 if (cur == label_index) {
437                         tf->label = strdup(label);
438                         label_index++;
439                         break;
440                 }
441                 cur++;
442         }
443 }
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                 if (tf->stop_seconds > max_seconds)
476                         tf->stop_seconds = max_seconds;
477                 if (tf->mpstat_max_seconds) {
478                         tf->mpstat_min_seconds = min_seconds;
479                         tf->mpstat_max_seconds = max_seconds;
480                         if (tf->mpstat_stop_seconds > max_seconds)
481                                 tf->mpstat_stop_seconds = max_seconds;
482                 }
483                 tf->min_offset = min_offset;
484                 tf->max_offset = max_offset;
485         }
486 }
487
488 static char *create_movie_temp_dir(void)
489 {
490         char *ret;
491         char *pattern = strdup("io-movie-XXXXXX");;
492
493         ret = mkdtemp(pattern);
494         if (!ret) {
495                 perror("Unable to create temp directory for movie files");
496                 exit(1);
497         }
498         return ret;
499 }
500
501 static struct pid_plot_history *alloc_pid_plot_history(char *color)
502 {
503         struct pid_plot_history *pph;
504
505         pph = calloc(1, sizeof(struct pid_plot_history));
506         if (!pph) {
507                 perror("memory allocation failed");
508                 exit(1);
509         }
510         pph->history = malloc(sizeof(double) * 4096);
511         if (!pph->history) {
512                 perror("memory allocation failed");
513                 exit(1);
514         }
515         pph->history_len = 4096;
516         pph->color = color;
517
518         return pph;
519 }
520
521 static struct plot_history *alloc_plot_history(struct trace_file *tf)
522 {
523         struct plot_history *ph = calloc(1, sizeof(struct plot_history));
524         int i;
525
526         if (!ph) {
527                 perror("memory allocation failed");
528                 exit(1);
529         }
530         ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
531         if (!ph->read_pid_history) {
532                 perror("memory allocation failed");
533                 exit(1);
534         }
535         ph->write_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
536         if (!ph->write_pid_history) {
537                 perror("memory allocation failed");
538                 exit(1);
539         }
540         ph->pid_history_count = tf->io_plots;
541         for (i = 0; i < tf->io_plots; i++) {
542                 if (tf->gdd_reads[i])
543                         ph->read_pid_history[i] = alloc_pid_plot_history(tf->gdd_reads[i]->color);
544                 if (tf->gdd_writes[i])
545                         ph->write_pid_history[i] = alloc_pid_plot_history(tf->gdd_writes[i]->color);
546         }
547         return ph;
548 }
549
550 LIST_HEAD(movie_history);
551 int num_histories = 0;
552
553 static void free_plot_history(struct plot_history *ph)
554 {
555         int pid;
556
557         for (pid = 0; pid < ph->pid_history_count; pid++) {
558                 if (ph->read_pid_history[pid])
559                         free(ph->read_pid_history[pid]);
560                 if (ph->write_pid_history[pid])
561                         free(ph->write_pid_history[pid]);
562         }
563         free(ph->read_pid_history);
564         free(ph->write_pid_history);
565         free(ph);
566 }
567
568 static void add_history(struct plot_history *ph, struct list_head *list)
569 {
570         struct plot_history *entry;
571
572         list_add_tail(&ph->list, list);
573         num_histories++;
574
575         if (num_histories > 12) {
576                 num_histories--;
577                 entry = list_entry(list->next, struct plot_history, list);
578                 list_del(&entry->list);
579                 free_plot_history(entry);
580         }
581 }
582
583 static void plot_movie_history(struct plot *plot, struct list_head *list)
584 {
585         struct plot_history *ph;
586         int pid;
587
588         if (num_histories > 2)
589                 rewind_spindle_steps(num_histories - 1);
590
591         list_for_each_entry(ph, list, list) {
592                 for (pid = 0; pid < ph->pid_history_count; pid++) {
593                         if (ph->read_pid_history[pid]) {
594                                 if (movie_style == MOVIE_SPINDLE) {
595                                         svg_io_graph_movie_array_spindle(plot,
596                                                 ph->read_pid_history[pid]);
597                                 } else {
598                                         svg_io_graph_movie_array(plot,
599                                                 ph->read_pid_history[pid]);
600                                 }
601                         }
602                         if (ph->write_pid_history[pid]) {
603                                 if (movie_style == MOVIE_SPINDLE) {
604                                         svg_io_graph_movie_array_spindle(plot,
605                                                 ph->write_pid_history[pid]);
606                                 } else {
607                                         svg_io_graph_movie_array(plot,
608                                                 ph->write_pid_history[pid]);
609                                 }
610                         }
611                 }
612          }
613 }
614
615 static void free_all_plot_history(struct list_head *head)
616 {
617         struct plot_history *entry;
618
619         while (!list_empty(head)) {
620                 entry = list_entry(head->next, struct plot_history, list);
621                 list_del(&entry->list);
622                 free_plot_history(entry);
623         }
624 }
625
626 static int count_io_plot_types(void)
627 {
628         struct trace_file *tf;
629         int i;
630         int total_io_types = 0;
631
632         list_for_each_entry(tf, &all_traces, list) {
633                 for (i = 0; i < tf->io_plots; i++) {
634                         if (tf->gdd_reads[i])
635                                 total_io_types++;
636                         if (tf->gdd_writes[i])
637                                 total_io_types++;
638                 }
639         }
640         return total_io_types;
641 }
642
643 static void plot_io(struct plot *plot, int min_seconds, int max_seconds, u64 min_offset, u64 max_offset)
644 {
645         struct trace_file *tf;
646         int i;
647
648         if (active_graphs[IO_GRAPH_INDEX] == 0)
649                 return;
650
651         setup_axis(plot);
652
653         svg_alloc_legend(plot, count_io_plot_types() * 2);
654
655         set_plot_label(plot, "Device IO");
656         set_ylabel(plot, "Offset (MB)");
657         set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
658                    max_offset / (1024 * 1024), "");
659         set_xticks(plot, num_xticks, min_seconds, max_seconds);
660
661         list_for_each_entry(tf, &all_traces, list) {
662                 char label[256];
663                 char *pos;
664
665                 if (!tf->label)
666                         label[0] = 0;
667                 else {
668                         strcpy(label, tf->label);
669                         if (io_per_process)
670                                 strcat(label, " ");
671                 }
672                 pos = label + strlen(label);
673
674                 for (i = 0; i < tf->io_plots; i++) {
675                         if (tf->gdd_writes[i]) {
676                                 svg_io_graph(plot, tf->gdd_writes[i]);
677                                 if (io_per_process)
678                                         strcpy(pos, tf->gdd_writes[i]->label);
679                                 svg_add_legend(plot, label, " Writes", tf->gdd_writes[i]->color);
680                         }
681                         if (tf->gdd_reads[i]) {
682                                 svg_io_graph(plot, tf->gdd_reads[i]);
683                                 if (io_per_process)
684                                         strcpy(pos, tf->gdd_reads[i]->label);
685                                 svg_add_legend(plot, label, " Reads", tf->gdd_reads[i]->color);
686                         }
687
688                 }
689         }
690         if (plot->add_xlabel)
691                 set_xlabel(plot, "Time (seconds)");
692         svg_write_legend(plot);
693         close_plot(plot);
694 }
695
696 static void plot_tput(struct plot *plot, int min_seconds, int max_seconds)
697 {
698         struct trace_file *tf;
699         char *units;
700         char line[128];
701         u64 max = 0;
702
703         if (active_graphs[TPUT_GRAPH_INDEX] == 0)
704                 return;
705
706         svg_alloc_legend(plot, num_traces * 2);
707
708         list_for_each_entry(tf, &all_traces, list) {
709                 if (tf->tput_writes_gld->max > max)
710                         max = tf->tput_writes_gld->max;
711                 if (tf->tput_reads_gld->max > max)
712                         max = tf->tput_reads_gld->max;
713         }
714         list_for_each_entry(tf, &all_traces, list) {
715                 if (tf->tput_writes_gld->max > 0)
716                         tf->tput_writes_gld->max = max;
717                 if (tf->tput_reads_gld->max > 0)
718                         tf->tput_reads_gld->max = max;
719         }
720
721         setup_axis(plot);
722         set_plot_label(plot, "Throughput");
723
724         tf = list_entry(all_traces.next, struct trace_file, list);
725
726         scale_line_graph_bytes(&max, &units, 1024);
727         sprintf(line, "%sB/s", units);
728         set_ylabel(plot, line);
729         set_yticks(plot, num_yticks, 0, max, "");
730         set_xticks(plot, num_xticks, min_seconds, max_seconds);
731
732         list_for_each_entry(tf, &all_traces, list) {
733                 if (tf->tput_writes_gld->max > 0) {
734                         svg_line_graph(plot, tf->tput_writes_gld, tf->writes_color, 0, 0);
735                         svg_add_legend(plot, tf->label, "Writes", tf->writes_color);
736                 }
737                 if (tf->tput_reads_gld->max > 0) {
738                         svg_line_graph(plot, tf->tput_reads_gld, tf->reads_color, 0, 0);
739                         svg_add_legend(plot, tf->label, "Reads", tf->reads_color);
740                 }
741         }
742
743         if (plot->add_xlabel)
744                 set_xlabel(plot, "Time (seconds)");
745
746         svg_write_legend(plot);
747         close_plot(plot);
748         total_graphs_written++;
749 }
750
751 static void plot_cpu(struct plot *plot, int max_seconds, char *label,
752                      int active_index, int gld_index)
753 {
754         struct trace_file *tf;
755         int max = 0;
756         int i;
757         int gld_i;
758         char *color;
759         double avg = 0;
760         int ymax;
761         int plotted = 0;
762
763         if (active_graphs[active_index] == 0)
764                 return;
765
766         list_for_each_entry(tf, &all_traces, list) {
767                 if (tf->trace->mpstat_num_cpus > max)
768                         max = tf->trace->mpstat_num_cpus;
769         }
770         if (max == 0)
771                 return;
772
773         tf = list_entry(all_traces.next, struct trace_file, list);
774
775         ymax = tf->mpstat_gld[gld_index]->max;
776         if (ymax == 0)
777                 return;
778
779         svg_alloc_legend(plot, num_traces * max);
780
781         setup_axis(plot);
782         set_plot_label(plot, label);
783
784         max_seconds = tf->mpstat_max_seconds;
785
786         set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
787         set_ylabel(plot, "Percent");
788         set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
789
790         reset_cpu_color();
791         list_for_each_entry(tf, &all_traces, list) {
792                 if (tf->mpstat_gld == 0)
793                         break;
794                 for (i = tf->mpstat_gld[0]->min_seconds;
795                      i < tf->mpstat_gld[0]->stop_seconds; i++) {
796                         if (tf->mpstat_gld[gld_index]->data[i].count) {
797                                 avg += (tf->mpstat_gld[gld_index]->data[i].sum /
798                                         tf->mpstat_gld[gld_index]->data[i].count);
799                         }
800                 }
801                 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
802                        tf->mpstat_gld[gld_index]->min_seconds;
803                 color = pick_cpu_color();
804                 svg_line_graph(plot, tf->mpstat_gld[0], color, 0, 0);
805                 svg_add_legend(plot, tf->label, " avg", color);
806
807                 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
808                         struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
809                         double this_avg = 0;
810
811                         for (gld_i = gld->min_seconds;
812                              gld_i < gld->stop_seconds; gld_i++) {
813                                 if (gld->data[i].count) {
814                                         this_avg += gld->data[i].sum /
815                                                 gld->data[i].count;
816                                 }
817                         }
818
819                         this_avg /= gld->stop_seconds - gld->min_seconds;
820
821                         for (gld_i = gld->min_seconds;
822                              gld_i < gld->stop_seconds; gld_i++) {
823                                 double val;
824
825                                 if (gld->data[gld_i].count == 0)
826                                         continue;
827                                 val = (double)gld->data[gld_i].sum /
828                                         gld->data[gld_i].count;
829
830                                 if (this_avg > avg + 30 || val > 95) {
831                                         color = pick_cpu_color();
832                                         svg_line_graph(plot, gld, color, avg + 30, 95);
833                                         snprintf(line, line_len, " CPU %d\n", i - 1);
834                                         svg_add_legend(plot, tf->label, line, color);
835                                         plotted++;
836                                         break;
837                                 }
838
839                         }
840                 }
841         }
842
843         if (plot->add_xlabel)
844                 set_xlabel(plot, "Time (seconds)");
845
846         if (!plot->no_legend) {
847                 svg_write_legend(plot);
848                 svg_free_legend(plot);
849         }
850         close_plot(plot);
851         total_graphs_written++;
852 }
853
854 static void plot_queue_depth(struct plot *plot, int min_seconds, int max_seconds)
855 {
856         struct trace_file *tf;
857
858         if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
859                 return;
860
861         setup_axis(plot);
862         set_plot_label(plot, "Queue Depth");
863         if (num_traces > 1)
864                 svg_alloc_legend(plot, num_traces);
865
866         tf = list_entry(all_traces.next, struct trace_file, list);
867         set_ylabel(plot, "Pending IO");
868         set_yticks(plot, num_yticks, 0, tf->queue_depth_gld->max, "");
869         set_xticks(plot, num_xticks, min_seconds, max_seconds);
870
871         list_for_each_entry(tf, &all_traces, list) {
872                 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
873                 if (num_traces > 1)
874                         svg_add_legend(plot, tf->label, "", tf->line_color);
875         }
876
877         if (plot->add_xlabel)
878                 set_xlabel(plot, "Time (seconds)");
879         if (num_traces > 1)
880                 svg_write_legend(plot);
881         close_plot(plot);
882         total_graphs_written++;
883 }
884
885 static void convert_movie_files(char *movie_dir)
886 {
887         fprintf(stderr, "Converting svg files in %s\n", movie_dir);
888         snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
889                  movie_dir);
890         system(line);
891 }
892
893 static void mencode_movie(char *movie_dir)
894 {
895         fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
896         snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
897                  "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
898                  output_filename);
899         system(line);
900 }
901
902 static void tencode_movie(char *movie_dir)
903 {
904         fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
905         snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
906                         output_filename, movie_dir, output_filename);
907         system(line);
908 }
909
910 static void encode_movie(char *movie_dir)
911 {
912         char *last_dot = strrchr(output_filename, '.');
913
914         if (last_dot &&
915             (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
916                 tencode_movie(movie_dir);
917         } else
918                 mencode_movie(movie_dir);
919 }
920
921 static void cleanup_movie(char *movie_dir)
922 {
923         if (keep_movie_svgs) {
924                 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
925                 return;
926         }
927         fprintf(stderr, "Removing movie dir %s\n", movie_dir);
928         snprintf(line, line_len, "rm %s/*", movie_dir);
929         system(line);
930
931         snprintf(line, line_len, "rmdir %s", movie_dir);
932         system(line);
933 }
934
935 static void plot_io_movie(struct plot *plot)
936 {
937         struct trace_file *tf;
938         char *movie_dir = create_movie_temp_dir();
939         int i, pid;
940         struct plot_history *history;
941         int batch_i;
942         int movie_len = 30;
943         int movie_frames_per_sec = 20;
944         int total_frames = movie_len * movie_frames_per_sec;
945         int rows, cols;
946         int batch_count;
947         int graph_width_factor = 5;
948         int orig_y_offset;
949
950         get_graph_size(&cols, &rows);
951         batch_count = cols / total_frames;
952
953         if (batch_count == 0)
954                 batch_count = 1;
955
956         list_for_each_entry(tf, &all_traces, list) {
957                 char label[256];
958                 char *pos;
959
960                 if (!tf->label)
961                         label[0] = 0;
962                 else {
963                         strcpy(label, tf->label);
964                         if (io_per_process)
965                                 strcat(label, " ");
966                 }
967                 pos = label + strlen(label);
968
969                 i = 0;
970                 while (i < cols) {
971                         snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
972                         set_plot_output(plot, line);
973                         set_plot_title(plot, graph_title);
974                         orig_y_offset = plot->start_y_offset;
975
976                         plot->no_legend = 1;
977
978                         set_graph_size(cols / graph_width_factor, rows / 8);
979                         plot->timeline = i / graph_width_factor;
980
981                         plot_tput(plot, tf->min_seconds,
982                                   tf->max_seconds);
983
984                         plot_cpu(plot, tf->max_seconds,
985                                    "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
986
987                         plot->direction = PLOT_ACROSS;
988                         plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
989
990                         /* movie graph starts here */
991                         plot->start_y_offset = orig_y_offset;
992                         set_graph_size(cols - cols / graph_width_factor, rows);
993                         plot->no_legend = 0;
994                         plot->timeline = 0;
995                         plot->direction = PLOT_DOWN;;
996
997                         if (movie_style == MOVIE_SPINDLE)
998                                 setup_axis_spindle(plot);
999                         else
1000                                 setup_axis(plot);
1001
1002                         svg_alloc_legend(plot, count_io_plot_types() * 2);
1003
1004                         history = alloc_plot_history(tf);
1005                         history->col = i;
1006
1007                         for (pid = 0; pid < tf->io_plots; pid++) {
1008                                 if (tf->gdd_reads[pid]) {
1009                                         if (io_per_process)
1010                                                 strcpy(pos, tf->gdd_reads[pid]->label);
1011                                         svg_add_legend(plot, label, " Reads", tf->gdd_reads[pid]->color);
1012                                 }
1013                                 if (tf->gdd_writes[pid]) {
1014                                         if (io_per_process)
1015                                                 strcpy(pos, tf->gdd_writes[pid]->label);
1016                                         svg_add_legend(plot, label, " Writes", tf->gdd_writes[pid]->color);
1017                                 }
1018                         }
1019
1020                         batch_i = 0;
1021                         while (i < cols && batch_i < batch_count) {
1022                                 for (pid = 0; pid < tf->io_plots; pid++) {
1023                                         if (tf->gdd_reads[pid]) {
1024                                                 svg_io_graph_movie(tf->gdd_reads[pid],
1025                                                                    history->read_pid_history[pid],
1026                                                                    i);
1027                                         }
1028                                         if (tf->gdd_writes[pid]) {
1029                                                 svg_io_graph_movie(tf->gdd_writes[pid],
1030                                                                    history->write_pid_history[pid],
1031                                                                    i);
1032                                         }
1033                                 }
1034                                 i++;
1035                                 batch_i++;
1036                         }
1037
1038                         add_history(history, &movie_history);
1039
1040                         plot_movie_history(plot, &movie_history);
1041
1042                         svg_write_legend(plot);
1043                         close_plot(plot);
1044                         close_plot(plot);
1045
1046                         close_plot_file(plot);
1047                 }
1048                 free_all_plot_history(&movie_history);
1049         }
1050         convert_movie_files(movie_dir);
1051         encode_movie(movie_dir);
1052         cleanup_movie(movie_dir);
1053         free(movie_dir);
1054 }
1055
1056 static void plot_latency(struct plot *plot, int min_seconds, int max_seconds)
1057 {
1058         struct trace_file *tf;
1059         char *units;
1060         char line[128];
1061         u64 max = 0;
1062
1063         if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1064                 return;
1065
1066         if (num_traces > 1)
1067                 svg_alloc_legend(plot, num_traces);
1068
1069         list_for_each_entry(tf, &all_traces, list) {
1070                 if (tf->latency_gld->max > max)
1071                         max = tf->latency_gld->max;
1072         }
1073
1074         list_for_each_entry(tf, &all_traces, list)
1075                 tf->latency_gld->max = max;
1076
1077         setup_axis(plot);
1078         set_plot_label(plot, "IO Latency");
1079
1080         tf = list_entry(all_traces.next, struct trace_file, list);
1081
1082         scale_line_graph_time(&max, &units);
1083         sprintf(line, "latency (%ss)", units);
1084         set_ylabel(plot, line);
1085         set_yticks(plot, num_yticks, 0, max, "");
1086         set_xticks(plot, num_xticks, min_seconds, max_seconds);
1087
1088         list_for_each_entry(tf, &all_traces, list) {
1089                 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1090                 if (num_traces > 1)
1091                         svg_add_legend(plot, tf->label, "", tf->line_color);
1092         }
1093
1094         if (plot->add_xlabel)
1095                 set_xlabel(plot, "Time (seconds)");
1096         if (num_traces > 1)
1097                 svg_write_legend(plot);
1098         close_plot(plot);
1099         total_graphs_written++;
1100 }
1101
1102 static void plot_iops(struct plot *plot, int min_seconds, int max_seconds)
1103 {
1104         struct trace_file *tf;
1105         char *units;
1106         u64 max = 0;
1107
1108         if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1109                 return;
1110
1111         list_for_each_entry(tf, &all_traces, list) {
1112                 if (tf->iop_gld->max > max)
1113                         max = tf->iop_gld->max;
1114         }
1115
1116         list_for_each_entry(tf, &all_traces, list)
1117                 tf->iop_gld->max = max;
1118
1119         setup_axis(plot);
1120         set_plot_label(plot, "IOPs");
1121         if (num_traces > 1)
1122                 svg_alloc_legend(plot, num_traces);
1123
1124         tf = list_entry(all_traces.next, struct trace_file, list);
1125
1126         scale_line_graph_bytes(&max, &units, 1000);
1127         set_ylabel(plot, "IO/s");
1128
1129         set_yticks(plot, num_yticks, 0, max, units);
1130         set_xticks(plot, num_xticks, min_seconds, max_seconds);
1131
1132         list_for_each_entry(tf, &all_traces, list) {
1133                 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1134                 if (num_traces > 1)
1135                         svg_add_legend(plot, tf->label, "", tf->line_color);
1136         }
1137
1138         if (plot->add_xlabel)
1139                 set_xlabel(plot, "Time (seconds)");
1140         if (num_traces > 1)
1141                 svg_write_legend(plot);
1142
1143         close_plot(plot);
1144         total_graphs_written++;
1145 }
1146
1147 static void check_plot_columns(struct plot *plot, int index)
1148 {
1149         int count;
1150
1151         if (columns > 1 && (total_graphs_written == 0 ||
1152             total_graphs_written % columns != 0)) {
1153                 count = graphs_left(index);
1154                 if (plot->direction == PLOT_DOWN) {
1155                         plot->start_x_offset = 0;
1156                         if (count <= columns)
1157                                 plot->add_xlabel = 1;
1158                 }
1159                 plot->direction = PLOT_ACROSS;
1160
1161         } else {
1162                 plot->direction = PLOT_DOWN;
1163                 if (index == last_active_graph)
1164                         plot->add_xlabel = 1;
1165         }
1166
1167 }
1168
1169 enum {
1170         HELP_LONG_OPT = 1,
1171 };
1172
1173 char *option_string = "T:t:o:l:r:O:N:d:D:p:m::h:w:c:x:y:a:C:PK";
1174 static struct option long_options[] = {
1175         {"columns", required_argument, 0, 'c'},
1176         {"title", required_argument, 0, 'T'},
1177         {"trace", required_argument, 0, 't'},
1178         {"output", required_argument, 0, 'o'},
1179         {"label", required_argument, 0, 'l'},
1180         {"rolling", required_argument, 0, 'r'},
1181         {"no-graph", required_argument, 0, 'N'},
1182         {"only-graph", required_argument, 0, 'O'},
1183         {"device", required_argument, 0, 'd'},
1184         {"blktrace-destination", required_argument, 0, 'D'},
1185         {"prog", required_argument, 0, 'p'},
1186         {"movie", optional_argument, 0, 'm'},
1187         {"codec", optional_argument, 0, 'C'},
1188         {"keep-movie-svgs", no_argument, 0, 'K'},
1189         {"width", required_argument, 0, 'w'},
1190         {"height", required_argument, 0, 'h'},
1191         {"xzoom", required_argument, 0, 'x'},
1192         {"yzoom", required_argument, 0, 'y'},
1193         {"io-plot-action", required_argument, 0, 'a'},
1194         {"per-process-io", no_argument, 0, 'P'},
1195         {"help", no_argument, 0, HELP_LONG_OPT},
1196         {0, 0, 0, 0}
1197 };
1198
1199 static void print_usage(void)
1200 {
1201         fprintf(stderr, "iowatcher usage:\n"
1202                 "\t-d (--device): device for blktrace to trace\n"
1203                 "\t-D (--blktrace-destination): destination for blktrace\n"
1204                 "\t-t (--trace): trace file name (more than one allowed)\n"
1205                 "\t-l (--label): trace label in the graph\n"
1206                 "\t-o (--output): output file name (SVG only)\n"
1207                 "\t-p (--prog): program to run while blktrace is run\n"
1208                 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
1209                 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1210                 "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
1211                 "\t-r (--rolling): number of seconds in the rolling averge\n"
1212                 "\t-T (--title): graph title\n"
1213                 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, \n"
1214                 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1215                 "\t-O (--only-graph): add a single graph to the output\n"
1216                 "\t-h (--height): set the height of each graph\n"
1217                 "\t-w (--width): set the width of each graph\n"
1218                 "\t-c (--columns): numbers of columns in graph output\n"
1219                 "\t-x (--xzoom): limit processed time to min:max\n"
1220                 "\t-y (--yzoom): limit processed sectors to min:max\n"
1221                 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1222                 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1223                );
1224         exit(1);
1225 }
1226
1227 static int parse_double_range(char *str, double *min, double *max)
1228 {
1229         char *end;
1230
1231         /* Empty lower bound - leave original value */
1232         if (str[0] != ':') {
1233                 *min = strtod(str, &end);
1234                 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1235                         return -ERANGE;
1236                 if (*end != ':')
1237                         return -EINVAL;
1238         } else
1239                 end = str;
1240         /* Empty upper bound - leave original value */
1241         if (end[1]) {
1242                 *max = strtod(end+1, &end);
1243                 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1244                         return -ERANGE;
1245                 if (*end != 0)
1246                         return -EINVAL;
1247         }
1248         if (*min > *max)
1249                 return -EINVAL;
1250         return 0;
1251 }
1252
1253 static int parse_ull_range(char *str, unsigned long long *min,
1254                            unsigned long long *max)
1255 {
1256         char *end;
1257
1258         /* Empty lower bound - leave original value */
1259         if (str[0] != ':') {
1260                 *min = strtoull(str, &end, 10);
1261                 if (*min == ULLONG_MAX && errno == ERANGE)
1262                         return -ERANGE;
1263                 if (*end != ':')
1264                         return -EINVAL;
1265         } else
1266                 end = str;
1267         /* Empty upper bound - leave original value */
1268         if (end[1]) {
1269                 *max = strtoull(end+1, &end, 10);
1270                 if (*max == ULLONG_MAX && errno == ERANGE)
1271                         return -ERANGE;
1272                 if (*end != 0)
1273                         return -EINVAL;
1274         }
1275         if (*min > *max)
1276                 return -EINVAL;
1277         return 0;
1278 }
1279
1280 static int parse_options(int ac, char **av)
1281 {
1282         int c;
1283         int disabled = 0;
1284
1285         while (1) {
1286                 // int this_option_optind = optind ? optind : 1;
1287                 int option_index = 0;
1288
1289                 c = getopt_long(ac, av, option_string,
1290                                 long_options, &option_index);
1291
1292                 if (c == -1)
1293                         break;
1294
1295                 switch(c) {
1296                 case 'T':
1297                         graph_title = strdup(optarg);
1298                         break;
1299                 case 't':
1300                         add_trace_file(optarg);
1301                         set_blktrace_outfile(optarg);
1302                         break;
1303                 case 'o':
1304                         output_filename = strdup(optarg);
1305                         break;
1306                 case 'l':
1307                         set_trace_label(optarg);
1308                         break;
1309                 case 'r':
1310                         set_rolling_avg(atoi(optarg));
1311                         break;
1312                 case 'O':
1313                         if (!disabled) {
1314                                 disable_all_graphs();
1315                                 disabled = 1;
1316                         }
1317                         enable_one_graph(optarg);
1318                         break;
1319                 case 'N':
1320                         disable_one_graph(optarg);
1321                         break;
1322                 case 'd':
1323                         if (num_blktrace_devices == MAX_DEVICES_PER_TRACE - 1) {
1324                                 fprintf(stderr, "Too many blktrace devices provided\n");
1325                                 exit(1);
1326                         }
1327                         blktrace_devices[num_blktrace_devices++] = strdup(optarg);
1328                         break;
1329                 case 'D':
1330                         blktrace_dest_dir = strdup(optarg);
1331                         if (!strcmp(blktrace_dest_dir, "")) {
1332                                 fprintf(stderr, "Need a directory\n");
1333                                 print_usage();
1334                         }
1335                         break;
1336                 case 'p':
1337                         program_to_run = strdup(optarg);
1338                         break;
1339                 case 'K':
1340                         keep_movie_svgs = 1;
1341                         break;
1342                 case 'm':
1343                         make_movie = 1;
1344                         if (optarg) {
1345                                 movie_style = lookup_movie_style(optarg);
1346                                 if (movie_style < 0) {
1347                                         fprintf(stderr, "Unknown movie style %s\n", optarg);
1348                                         print_usage();
1349                                 }
1350                         }
1351                         fprintf(stderr, "Using movie style: %s\n",
1352                                 movie_styles[movie_style]);
1353                         break;
1354                 case 'C':
1355                         ffmpeg_codec = strdup(optarg);
1356                         break;
1357                 case 'h':
1358                         opt_graph_height = atoi(optarg);
1359                         break;
1360                 case 'w':
1361                         opt_graph_width = atoi(optarg);
1362                         break;
1363                 case 'c':
1364                         columns = atoi(optarg);
1365                         break;
1366                 case 'x':
1367                         if (parse_double_range(optarg, &min_time, &max_time)
1368                             < 0) {
1369                                 fprintf(stderr, "Cannot parse time range %s\n",
1370                                         optarg);
1371                                 exit(1);
1372                         }
1373                         break;
1374                 case 'y':
1375                         if (parse_ull_range(optarg, &min_mb, &max_mb)
1376                             < 0) {
1377                                 fprintf(stderr,
1378                                         "Cannot parse offset range %s\n",
1379                                         optarg);
1380                                 exit(1);
1381                         }
1382                         if (max_mb > ULLONG_MAX >> 20) {
1383                                 fprintf(stderr,
1384                                         "Upper range limit too big."
1385                                         " Maximum is %llu.\n", ULLONG_MAX >> 20);
1386                                 exit(1);
1387                         }
1388                         break;
1389                 case 'a':
1390                         if (strlen(optarg) != 1) {
1391 action_err:
1392                                 fprintf(stderr, "Action must be one of Q, D, C.");
1393                                 exit(1);
1394                         }
1395                         plot_io_action = action_char_to_num(optarg[0]);
1396                         if (plot_io_action < 0)
1397                                 goto action_err;
1398                         break;
1399                 case 'P':
1400                         io_per_process = 1;
1401                         break;
1402                 case '?':
1403                 case HELP_LONG_OPT:
1404                         print_usage();
1405                         break;
1406                 default:
1407                         break;
1408                 }
1409         }
1410         return 0;
1411 }
1412
1413 static void dest_mkdir(char *dir)
1414 {
1415         int ret;
1416
1417         ret = mkdir(dir, 0777);
1418         if (ret && errno != EEXIST) {
1419                 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1420                 exit(errno);
1421         }
1422 }
1423
1424 int main(int ac, char **av)
1425 {
1426         struct plot *plot;
1427         int min_seconds = 0;
1428         int max_seconds = 0;
1429         u64 max_offset = 0;
1430         u64 min_offset = ~(u64)0;
1431         struct trace_file *tf;
1432         int ret;
1433         int rows, cols;
1434
1435         init_io_hash_table();
1436         init_process_hash_table();
1437
1438         enable_all_graphs();
1439
1440         parse_options(ac, av);
1441
1442         last_active_graph = last_graph();
1443         if (make_movie) {
1444                 set_io_graph_scale(256);
1445                 if (movie_style == MOVIE_SPINDLE)
1446                         set_graph_size(750, 550);
1447                 else
1448                         set_graph_size(700, 400);
1449
1450                 /*
1451                  * the plots in the movie don't have a seconds
1452                  * line yet, this makes us skip it
1453                  */
1454                 last_active_graph = TOTAL_GRAPHS + 1;
1455         }
1456         if (opt_graph_height)
1457                 set_graph_height(opt_graph_height);
1458
1459         if (opt_graph_width)
1460                 set_graph_width(opt_graph_width);
1461
1462         if (list_empty(&all_traces)) {
1463                 fprintf(stderr, "No traces found, exiting\n");
1464                 exit(1);
1465         }
1466
1467         if (num_blktrace_devices) {
1468                 char *path;
1469
1470                 dest_mkdir(blktrace_dest_dir);
1471                 if (num_blktrace_devices > 1) {
1472                         snprintf(line, line_len, "%s/%s", blktrace_dest_dir,
1473                                  blktrace_outfile);
1474                         dest_mkdir(line);
1475                 }
1476
1477                 path = join_path(blktrace_dest_dir, blktrace_outfile);
1478
1479                 snprintf(line, line_len, "%s.dump", path);
1480                 unlink(line);
1481
1482                 ret = start_blktrace(blktrace_devices, num_blktrace_devices,
1483                                      blktrace_outfile, blktrace_dest_dir);
1484                 if (ret) {
1485                         fprintf(stderr, "exiting due to blktrace failure\n");
1486                         exit(1);
1487                 }
1488
1489                 start_mpstat(path);
1490                 if (program_to_run) {
1491                         ret = run_program(program_to_run);
1492                         if (ret) {
1493                                 fprintf(stderr, "failed to run %s\n",
1494                                         program_to_run);
1495                                 exit(1);
1496                         }
1497                         wait_for_tracers();
1498                 } else {
1499                         /* no program specified, just wait for
1500                          * blktrace to exit
1501                          */
1502                         wait_for_tracers();
1503                 }
1504                 free(path);
1505         }
1506
1507         /* step one, read all the traces */
1508         read_traces();
1509
1510         /* step two, find the maxes for time and offset */
1511         list_for_each_entry(tf, &all_traces, list)
1512                 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1513         min_seconds = min_time;
1514         if (max_seconds > max_time)
1515                 max_seconds = ceil(max_time);
1516         if (min_offset < min_mb << 20)
1517                 min_offset = min_mb << 20;
1518         if (max_offset > max_mb << 20)
1519                 max_offset = max_mb << 20;
1520
1521         /* push the max we found into all the tfs */
1522         set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1523
1524         /* alloc graphing structs for all the traces */
1525         setup_trace_file_graphs();
1526
1527         /* run through all the traces and read their events */
1528         read_trace_events();
1529
1530         pick_line_graph_color();
1531
1532         plot = alloc_plot();
1533
1534         if (make_movie) {
1535                 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1536                 plot_io_movie(plot);
1537                 exit(0);
1538         }
1539
1540         set_plot_output(plot, output_filename);
1541
1542         if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1543                 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1544         else if (num_traces > 1)
1545                 set_legend_width(longest_label);
1546         else
1547                 set_legend_width(0);
1548
1549         get_graph_size(&cols, &rows);
1550         if (columns > 1)
1551                 plot->add_xlabel = 1;
1552         set_plot_title(plot, graph_title);
1553
1554         check_plot_columns(plot, IO_GRAPH_INDEX);
1555         plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1556         plot->add_xlabel = 0;
1557
1558         if (columns > 1) {
1559                 set_graph_size(cols / columns, rows);
1560                 num_xticks /= columns;
1561                 if (num_xticks < 2)
1562                         num_xticks = 2;
1563         }
1564         if (rows <= 50)
1565                 num_yticks--;
1566
1567         check_plot_columns(plot, TPUT_GRAPH_INDEX);
1568         plot_tput(plot, min_seconds, max_seconds);
1569
1570         check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1571         plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1572                  CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1573
1574         check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1575         plot_cpu(plot, max_seconds, "CPU System Time",
1576                  CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1577
1578         check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1579         plot_cpu(plot, max_seconds, "CPU IRQ Time",
1580                  CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1581
1582         check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1583         plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1584                  CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1585
1586         check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1587         plot_cpu(plot, max_seconds, "CPU User Time",
1588                  CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1589
1590         check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1591         plot_latency(plot, min_seconds, max_seconds);
1592
1593         check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1594         plot_queue_depth(plot, min_seconds, max_seconds);
1595
1596         check_plot_columns(plot, IOPS_GRAPH_INDEX);
1597         plot_iops(plot, min_seconds, max_seconds);
1598
1599         /* once for all */
1600         close_plot(plot);
1601         close_plot_file(plot);
1602         return 0;
1603 }