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