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