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