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