iowatcher: spawn NPROCESSORS_ONLN for rsvg-convert-s
[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         long nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1047
1048         if (nr_cpus < 0)
1049                 nr_cpus = 8;
1050
1051         fprintf(stderr, "Converting svg files in %s\n", movie_dir);
1052         snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P %ld rsvg-convert -o {}.png {}",
1053                  movie_dir, nr_cpus);
1054         system_check(line);
1055 }
1056
1057 static void mencode_movie(char *movie_dir)
1058 {
1059         fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
1060         snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
1061                  "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
1062                  output_filename);
1063         system_check(line);
1064 }
1065
1066 static void tencode_movie(char *movie_dir)
1067 {
1068         fprintf(stderr, "Creating movie %s with png2theora\n", movie_dir);
1069         snprintf(line, line_len, "png2theora -o %s %s/%%010d-%s.svg.png",
1070                         output_filename, movie_dir, output_filename);
1071         system_check(line);
1072 }
1073
1074 static void encode_movie(char *movie_dir)
1075 {
1076         char *last_dot = strrchr(output_filename, '.');
1077
1078         if (last_dot &&
1079             (!strncmp(last_dot, ".ogg", 4) || !strncmp(last_dot, ".ogv", 4))) {
1080                 tencode_movie(movie_dir);
1081         } else
1082                 mencode_movie(movie_dir);
1083 }
1084
1085 static void cleanup_movie(char *movie_dir)
1086 {
1087         if (keep_movie_svgs) {
1088                 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
1089                 return;
1090         }
1091         fprintf(stderr, "Removing movie dir %s\n", movie_dir);
1092         snprintf(line, line_len, "rm %s/*", movie_dir);
1093         system_check(line);
1094
1095         snprintf(line, line_len, "rmdir %s", movie_dir);
1096         system_check(line);
1097 }
1098
1099 static void plot_io_movie(struct plot *plot)
1100 {
1101         struct trace_file *tf;
1102         int i, pid;
1103         struct plot_history *history;
1104         int batch_i;
1105         int movie_len = 30;
1106         int movie_frames_per_sec = 20;
1107         int total_frames = movie_len * movie_frames_per_sec;
1108         int rows, cols;
1109         int batch_count;
1110         int graph_width_factor = 5;
1111         int orig_y_offset;
1112         char movie_dir[] = "io-movie-XXXXXX";
1113
1114         if (mkdtemp(movie_dir) == NULL) {
1115                 perror("Unable to create temp directory for movie files");
1116                 exit(1);
1117         }
1118
1119         get_graph_size(&cols, &rows);
1120         batch_count = cols / total_frames;
1121
1122         if (batch_count == 0)
1123                 batch_count = 1;
1124
1125         list_for_each_entry(tf, &all_traces, list) {
1126                 char *prefix = tf->label ? tf->label : "";
1127
1128                 i = 0;
1129                 while (i < cols) {
1130                         snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
1131                         set_plot_output(plot, line);
1132                         set_plot_title(plot, graph_title);
1133                         orig_y_offset = plot->start_y_offset;
1134
1135                         plot->no_legend = 1;
1136
1137                         set_graph_size(cols / graph_width_factor, rows / 8);
1138                         plot->timeline = i / graph_width_factor;
1139
1140                         plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
1141
1142                         plot_cpu(plot, tf->max_seconds,
1143                                    "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1144
1145                         plot->direction = PLOT_ACROSS;
1146                         plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
1147
1148                         /* movie graph starts here */
1149                         plot->start_y_offset = orig_y_offset;
1150                         set_graph_size(cols - cols / graph_width_factor, rows);
1151                         plot->no_legend = 0;
1152                         plot->timeline = 0;
1153                         plot->direction = PLOT_DOWN;;
1154
1155                         if (movie_style == MOVIE_SPINDLE)
1156                                 setup_axis_spindle(plot);
1157                         else
1158                                 setup_axis(plot);
1159
1160                         svg_alloc_legend(plot, count_io_plot_types() * 2);
1161
1162                         history = alloc_plot_history(tf);
1163                         history->col = i;
1164
1165                         for (pid = 0; pid < tf->io_plots; pid++) {
1166                                 if (tf->gdd_reads[pid])
1167                                         plot_io_legend(plot, tf->gdd_reads[pid], prefix, " Reads");
1168                                 if (tf->gdd_writes[pid])
1169                                         plot_io_legend(plot, tf->gdd_writes[pid], prefix, " Writes");
1170                         }
1171
1172                         batch_i = 0;
1173                         while (i < cols && batch_i < batch_count) {
1174                                 for (pid = 0; pid < tf->io_plots; pid++) {
1175                                         if (tf->gdd_reads[pid]) {
1176                                                 svg_io_graph_movie(tf->gdd_reads[pid],
1177                                                                    history->read_pid_history[pid],
1178                                                                    i);
1179                                         }
1180                                         if (tf->gdd_writes[pid]) {
1181                                                 svg_io_graph_movie(tf->gdd_writes[pid],
1182                                                                    history->write_pid_history[pid],
1183                                                                    i);
1184                                         }
1185                                 }
1186                                 i++;
1187                                 batch_i++;
1188                         }
1189
1190                         add_history(history, &movie_history);
1191
1192                         plot_movie_history(plot, &movie_history);
1193
1194                         svg_write_legend(plot);
1195                         close_plot(plot);
1196                         close_plot(plot);
1197
1198                         close_plot_file(plot);
1199                 }
1200                 free_all_plot_history(&movie_history);
1201         }
1202         convert_movie_files(movie_dir);
1203         encode_movie(movie_dir);
1204         cleanup_movie(movie_dir);
1205 }
1206
1207 static void plot_latency(struct plot *plot, unsigned int min_seconds,
1208                          unsigned int max_seconds)
1209 {
1210         struct trace_file *tf;
1211         char *units;
1212         char line[128];
1213         u64 max = 0, val;
1214
1215         if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1216                 return;
1217
1218         if (num_traces > 1)
1219                 svg_alloc_legend(plot, num_traces);
1220
1221         list_for_each_entry(tf, &all_traces, list) {
1222                 val = line_graph_roll_avg_max(tf->latency_gld);
1223                 if (val > max)
1224                         max = val;
1225         }
1226
1227         list_for_each_entry(tf, &all_traces, list)
1228                 tf->latency_gld->max = max;
1229
1230         setup_axis(plot);
1231         set_plot_label(plot, "IO Latency");
1232
1233         tf = list_entry(all_traces.next, struct trace_file, list);
1234
1235         scale_line_graph_time(&max, &units);
1236         sprintf(line, "latency (%ss)", units);
1237         set_ylabel(plot, line);
1238         set_yticks(plot, num_yticks, 0, max, "");
1239         set_xticks(plot, num_xticks, min_seconds, max_seconds);
1240
1241         list_for_each_entry(tf, &all_traces, list) {
1242                 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
1243                 if (num_traces > 1)
1244                         svg_add_legend(plot, tf->label, "", tf->line_color);
1245         }
1246
1247         if (plot->add_xlabel)
1248                 set_xlabel(plot, "Time (seconds)");
1249         if (num_traces > 1)
1250                 svg_write_legend(plot);
1251         close_plot(plot);
1252         total_graphs_written++;
1253 }
1254
1255 static void plot_iops(struct plot *plot, unsigned int min_seconds,
1256                       unsigned int max_seconds)
1257 {
1258         struct trace_file *tf;
1259         char *units;
1260         u64 max = 0, val;
1261
1262         if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1263                 return;
1264
1265         list_for_each_entry(tf, &all_traces, list) {
1266                 val = line_graph_roll_avg_max(tf->iop_gld);
1267                 if (val > max)
1268                         max = val;
1269         }
1270
1271         list_for_each_entry(tf, &all_traces, list)
1272                 tf->iop_gld->max = max;
1273
1274         setup_axis(plot);
1275         set_plot_label(plot, "IOPs");
1276         if (num_traces > 1)
1277                 svg_alloc_legend(plot, num_traces);
1278
1279         tf = list_entry(all_traces.next, struct trace_file, list);
1280
1281         scale_line_graph_bytes(&max, &units, 1000);
1282         set_ylabel(plot, "IO/s");
1283
1284         set_yticks(plot, num_yticks, 0, max, units);
1285         set_xticks(plot, num_xticks, min_seconds, max_seconds);
1286
1287         list_for_each_entry(tf, &all_traces, list) {
1288                 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
1289                 if (num_traces > 1)
1290                         svg_add_legend(plot, tf->label, "", tf->line_color);
1291         }
1292
1293         if (plot->add_xlabel)
1294                 set_xlabel(plot, "Time (seconds)");
1295         if (num_traces > 1)
1296                 svg_write_legend(plot);
1297
1298         close_plot(plot);
1299         total_graphs_written++;
1300 }
1301
1302 static void check_plot_columns(struct plot *plot, int index)
1303 {
1304         int count;
1305
1306         if (columns > 1 && (total_graphs_written == 0 ||
1307             total_graphs_written % columns != 0)) {
1308                 count = graphs_left(index);
1309                 if (plot->direction == PLOT_DOWN) {
1310                         plot->start_x_offset = 0;
1311                         if (count <= columns)
1312                                 plot->add_xlabel = 1;
1313                 }
1314                 plot->direction = PLOT_ACROSS;
1315
1316         } else {
1317                 plot->direction = PLOT_DOWN;
1318                 if (index == last_active_graph)
1319                         plot->add_xlabel = 1;
1320         }
1321
1322 }
1323
1324 enum {
1325         HELP_LONG_OPT = 1,
1326 };
1327
1328 char *option_string = "+F:T:t:o:l:r:O:N:d:D:pm::h:w:c:x:y:a:C:PK";
1329 static struct option long_options[] = {
1330         {"columns", required_argument, 0, 'c'},
1331         {"fio-trace", required_argument, 0, 'F'},
1332         {"title", required_argument, 0, 'T'},
1333         {"trace", required_argument, 0, 't'},
1334         {"output", required_argument, 0, 'o'},
1335         {"label", required_argument, 0, 'l'},
1336         {"rolling", required_argument, 0, 'r'},
1337         {"no-graph", required_argument, 0, 'N'},
1338         {"only-graph", required_argument, 0, 'O'},
1339         {"device", required_argument, 0, 'd'},
1340         {"blktrace-destination", required_argument, 0, 'D'},
1341         {"prog", no_argument, 0, 'p'},
1342         {"movie", optional_argument, 0, 'm'},
1343         {"codec", optional_argument, 0, 'C'},
1344         {"keep-movie-svgs", no_argument, 0, 'K'},
1345         {"width", required_argument, 0, 'w'},
1346         {"height", required_argument, 0, 'h'},
1347         {"xzoom", required_argument, 0, 'x'},
1348         {"yzoom", required_argument, 0, 'y'},
1349         {"io-plot-action", required_argument, 0, 'a'},
1350         {"per-process-io", no_argument, 0, 'P'},
1351         {"help", no_argument, 0, HELP_LONG_OPT},
1352         {0, 0, 0, 0}
1353 };
1354
1355 static void print_usage(void)
1356 {
1357         fprintf(stderr, "iowatcher usage:\n"
1358                 "\t-d (--device): device for blktrace to trace\n"
1359                 "\t-D (--blktrace-destination): destination for blktrace\n"
1360                 "\t-t (--trace): trace file name (more than one allowed)\n"
1361                 "\t-F (--fio-trace): fio bandwidth trace (more than one allowed)\n"
1362                 "\t-l (--label): trace label in the graph\n"
1363                 "\t-o (--output): output file name for the SVG image or video\n"
1364                 "\t-p (--prog): run a program while blktrace is run\n"
1365                 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
1366                 "\t-m (--movie [=spindle|rect]): create IO animations\n"
1367                 "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
1368                 "\t-r (--rolling): number of seconds in the rolling averge\n"
1369                 "\t-T (--title): graph title\n"
1370                 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue-depth, \n"
1371                 "\t\t\tiops, cpu-sys, cpu-io, cpu-irq cpu-soft cpu-user)\n"
1372                 "\t-O (--only-graph): add a single graph to the output\n"
1373                 "\t-h (--height): set the height of each graph\n"
1374                 "\t-w (--width): set the width of each graph\n"
1375                 "\t-c (--columns): numbers of columns in graph output\n"
1376                 "\t-x (--xzoom): limit processed time to min:max\n"
1377                 "\t-y (--yzoom): limit processed sectors to min:max\n"
1378                 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
1379                 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
1380                );
1381         exit(1);
1382 }
1383
1384 static int parse_double_range(char *str, double *min, double *max)
1385 {
1386         char *end;
1387
1388         /* Empty lower bound - leave original value */
1389         if (str[0] != ':') {
1390                 *min = strtod(str, &end);
1391                 if (*min == HUGE_VAL || *min == -HUGE_VAL)
1392                         return -ERANGE;
1393                 if (*end != ':')
1394                         return -EINVAL;
1395         } else
1396                 end = str;
1397         /* Empty upper bound - leave original value */
1398         if (end[1]) {
1399                 *max = strtod(end+1, &end);
1400                 if (*max == HUGE_VAL || *max == -HUGE_VAL)
1401                         return -ERANGE;
1402                 if (*end != 0)
1403                         return -EINVAL;
1404         }
1405         if (*min > *max)
1406                 return -EINVAL;
1407         return 0;
1408 }
1409
1410 static int parse_ull_range(char *str, unsigned long long *min,
1411                            unsigned long long *max)
1412 {
1413         char *end;
1414
1415         /* Empty lower bound - leave original value */
1416         if (str[0] != ':') {
1417                 *min = strtoull(str, &end, 10);
1418                 if (*min == ULLONG_MAX && errno == ERANGE)
1419                         return -ERANGE;
1420                 if (*end != ':')
1421                         return -EINVAL;
1422         } else
1423                 end = str;
1424         /* Empty upper bound - leave original value */
1425         if (end[1]) {
1426                 *max = strtoull(end+1, &end, 10);
1427                 if (*max == ULLONG_MAX && errno == ERANGE)
1428                         return -ERANGE;
1429                 if (*end != 0)
1430                         return -EINVAL;
1431         }
1432         if (*min > *max)
1433                 return -EINVAL;
1434         return 0;
1435 }
1436
1437 static int parse_options(int ac, char **av)
1438 {
1439         int c;
1440         int disabled = 0;
1441         int p_flagged = 0;
1442
1443         while (1) {
1444                 int option_index = 0;
1445
1446                 c = getopt_long(ac, av, option_string,
1447                                 long_options, &option_index);
1448
1449                 if (c == -1)
1450                         break;
1451
1452                 switch(c) {
1453                 case 'T':
1454                         graph_title = strdup(optarg);
1455                         break;
1456                 case 't':
1457                         add_trace_file(optarg);
1458                         set_blktrace_outfile(optarg);
1459                         break;
1460                 case 'F':
1461                         add_fio_trace_file(optarg);
1462                         break;
1463                 case 'o':
1464                         output_filename = strdup(optarg);
1465                         break;
1466                 case 'l':
1467                         set_trace_label(optarg);
1468                         break;
1469                 case 'r':
1470                         set_rolling_avg(atoi(optarg));
1471                         break;
1472                 case 'O':
1473                         if (!disabled) {
1474                                 disable_all_graphs();
1475                                 disabled = 1;
1476                         }
1477                         enable_one_graph(optarg);
1478                         break;
1479                 case 'N':
1480                         disable_one_graph(optarg);
1481                         break;
1482                 case 'd':
1483                         if (num_blktrace_devices == MAX_DEVICES_PER_TRACE - 1) {
1484                                 fprintf(stderr, "Too many blktrace devices provided\n");
1485                                 exit(1);
1486                         }
1487                         blktrace_devices[num_blktrace_devices++] = strdup(optarg);
1488                         break;
1489                 case 'D':
1490                         blktrace_dest_dir = strdup(optarg);
1491                         if (!strcmp(blktrace_dest_dir, "")) {
1492                                 fprintf(stderr, "Need a directory\n");
1493                                 print_usage();
1494                         }
1495                         break;
1496                 case 'p':
1497                         p_flagged = 1;
1498                         break;
1499                 case 'K':
1500                         keep_movie_svgs = 1;
1501                         break;
1502                 case 'm':
1503                         make_movie = 1;
1504                         if (optarg) {
1505                                 movie_style = lookup_movie_style(optarg);
1506                                 if (movie_style < 0) {
1507                                         fprintf(stderr, "Unknown movie style %s\n", optarg);
1508                                         print_usage();
1509                                 }
1510                         }
1511                         fprintf(stderr, "Using movie style: %s\n",
1512                                 movie_styles[movie_style]);
1513                         break;
1514                 case 'C':
1515                         ffmpeg_codec = strdup(optarg);
1516                         break;
1517                 case 'h':
1518                         opt_graph_height = atoi(optarg);
1519                         break;
1520                 case 'w':
1521                         opt_graph_width = atoi(optarg);
1522                         break;
1523                 case 'c':
1524                         columns = atoi(optarg);
1525                         break;
1526                 case 'x':
1527                         if (parse_double_range(optarg, &min_time, &max_time)
1528                             < 0) {
1529                                 fprintf(stderr, "Cannot parse time range %s\n",
1530                                         optarg);
1531                                 exit(1);
1532                         }
1533                         break;
1534                 case 'y':
1535                         if (parse_ull_range(optarg, &min_mb, &max_mb)
1536                             < 0) {
1537                                 fprintf(stderr,
1538                                         "Cannot parse offset range %s\n",
1539                                         optarg);
1540                                 exit(1);
1541                         }
1542                         if (max_mb > ULLONG_MAX >> 20) {
1543                                 fprintf(stderr,
1544                                         "Upper range limit too big."
1545                                         " Maximum is %llu.\n", ULLONG_MAX >> 20);
1546                                 exit(1);
1547                         }
1548                         break;
1549                 case 'a':
1550                         if (strlen(optarg) != 1) {
1551 action_err:
1552                                 fprintf(stderr, "Action must be one of Q, D, C.");
1553                                 exit(1);
1554                         }
1555                         plot_io_action = action_char_to_num(optarg[0]);
1556                         if (plot_io_action < 0)
1557                                 goto action_err;
1558                         break;
1559                 case 'P':
1560                         io_per_process = 1;
1561                         break;
1562                 case '?':
1563                 case HELP_LONG_OPT:
1564                         print_usage();
1565                         break;
1566                 default:
1567                         break;
1568                 }
1569         }
1570
1571         if (optind < ac && p_flagged) {
1572                 prog_argv = &av[optind];
1573                 prog_argc = ac - optind;
1574         } else if (p_flagged) {
1575                 fprintf(stderr, "--prog or -p given but no program specified\n");
1576                 exit(1);
1577         } else if (optind < ac) {
1578                 fprintf(stderr, "Extra arguments '%s'... (and --prog not specified)\n", av[optind]);
1579                 exit(1);
1580         }
1581
1582         return 0;
1583 }
1584
1585 static void dest_mkdir(char *dir)
1586 {
1587         int ret;
1588
1589         ret = mkdir(dir, 0777);
1590         if (ret && errno != EEXIST) {
1591                 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1592                 exit(errno);
1593         }
1594 }
1595
1596 int main(int ac, char **av)
1597 {
1598         struct plot *plot;
1599         unsigned int min_seconds = 0;
1600         unsigned int max_seconds = 0;
1601         u64 max_offset = 0;
1602         u64 min_offset = ~(u64)0;
1603         struct trace_file *tf;
1604         int ret;
1605         int rows, cols;
1606
1607         init_io_hash_table();
1608         init_process_hash_table();
1609
1610         enable_all_graphs();
1611
1612         parse_options(ac, av);
1613
1614         last_active_graph = last_graph();
1615         if (make_movie) {
1616                 set_io_graph_scale(256);
1617                 if (movie_style == MOVIE_SPINDLE)
1618                         set_graph_size(750, 550);
1619                 else
1620                         set_graph_size(700, 400);
1621
1622                 /*
1623                  * the plots in the movie don't have a seconds
1624                  * line yet, this makes us skip it
1625                  */
1626                 last_active_graph = TOTAL_GRAPHS + 1;
1627         }
1628         if (opt_graph_height)
1629                 set_graph_height(opt_graph_height);
1630
1631         if (opt_graph_width)
1632                 set_graph_width(opt_graph_width);
1633
1634         if (list_empty(&all_traces) && list_empty(&fio_traces)) {
1635                 fprintf(stderr, "No traces found, exiting\n");
1636                 exit(1);
1637         }
1638
1639         if (num_blktrace_devices) {
1640                 char *path = join_path(blktrace_dest_dir, blktrace_outfile);
1641                 dest_mkdir(blktrace_dest_dir);
1642                 dest_mkdir(path);
1643
1644                 snprintf(line, line_len, "%s.dump", path);
1645                 unlink(line);
1646
1647                 ret = start_blktrace(blktrace_devices, num_blktrace_devices,
1648                                      blktrace_outfile, blktrace_dest_dir);
1649                 if (ret) {
1650                         perror("Exiting due to blktrace failure");
1651                         exit(ret);
1652                 }
1653
1654                 snprintf(line, line_len, "%s.mpstat", path);
1655                 ret = start_mpstat(line);
1656                 if (ret) {
1657                         perror("Exiting due to mpstat failure");
1658                         exit(ret);
1659                 }
1660
1661                 if (prog_argv && prog_argc) {
1662                         run_program(prog_argc, prog_argv, 1, NULL, NULL);
1663                         wait_for_tracers(SIGINT);
1664                 } else {
1665                         printf("Tracing until interrupted...\n");
1666                         wait_for_tracers(0);
1667                 }
1668                 free(path);
1669         }
1670
1671         /* step one, read all the traces */
1672         read_traces();
1673
1674         /* step two, find the maxes for time and offset */
1675         list_for_each_entry(tf, &all_traces, list)
1676                 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1677         list_for_each_entry(tf, &fio_traces, list)
1678                 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
1679         min_seconds = min_time;
1680         if (max_seconds > max_time)
1681                 max_seconds = ceil(max_time);
1682         if (min_offset < min_mb << 20)
1683                 min_offset = min_mb << 20;
1684         if (max_offset > max_mb << 20)
1685                 max_offset = max_mb << 20;
1686
1687         /* push the max we found into all the tfs */
1688         set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
1689
1690         /* alloc graphing structs for all the traces */
1691         setup_trace_file_graphs();
1692
1693         /* run through all the traces and read their events */
1694         read_trace_events();
1695
1696         pick_line_graph_color();
1697
1698         plot = alloc_plot();
1699
1700         if (make_movie) {
1701                 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1702                 plot_io_movie(plot);
1703                 exit(0);
1704         }
1705
1706         set_plot_output(plot, output_filename);
1707
1708         if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
1709                 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
1710         else if (num_traces >= 1 || num_fio_traces >= 1)
1711                 set_legend_width(longest_label);
1712         else
1713                 set_legend_width(0);
1714
1715         get_graph_size(&cols, &rows);
1716         if (columns > 1)
1717                 plot->add_xlabel = 1;
1718         set_plot_title(plot, graph_title);
1719
1720         check_plot_columns(plot, IO_GRAPH_INDEX);
1721         plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
1722         plot->add_xlabel = 0;
1723
1724         if (columns > 1) {
1725                 set_graph_size(cols / columns, rows);
1726                 num_xticks /= columns;
1727                 if (num_xticks < 2)
1728                         num_xticks = 2;
1729         }
1730         if (rows <= 50)
1731                 num_yticks--;
1732
1733         check_plot_columns(plot, TPUT_GRAPH_INDEX);
1734         plot_tput(plot, min_seconds, max_seconds, 1);
1735
1736         check_plot_columns(plot, FIO_GRAPH_INDEX);
1737         plot_fio_tput(plot, min_seconds, max_seconds);
1738
1739         check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
1740         plot_cpu(plot, max_seconds, "CPU IO Wait Time",
1741                  CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1742
1743         check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
1744         plot_cpu(plot, max_seconds, "CPU System Time",
1745                  CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1746
1747         check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
1748         plot_cpu(plot, max_seconds, "CPU IRQ Time",
1749                  CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1750
1751         check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
1752         plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
1753                  CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1754
1755         check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
1756         plot_cpu(plot, max_seconds, "CPU User Time",
1757                  CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1758
1759         check_plot_columns(plot, LATENCY_GRAPH_INDEX);
1760         plot_latency(plot, min_seconds, max_seconds);
1761
1762         check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
1763         plot_queue_depth(plot, min_seconds, max_seconds);
1764
1765         check_plot_columns(plot, IOPS_GRAPH_INDEX);
1766         plot_iops(plot, min_seconds, max_seconds);
1767
1768         /* once for all */
1769         close_plot(plot);
1770         close_plot_file(plot);
1771         return 0;
1772 }