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