iowatcher: spawn NPROCESSORS_ONLN for rsvg-convert-s
[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
660b0411 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
9e066e23
CM
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>
49559b61 36#include <signal.h>
9e066e23
CM
37
38#include "plot.h"
39#include "blkparse.h"
40#include "list.h"
41#include "tracers.h"
e199d546 42#include "mpstat.h"
24e48e03 43#include "fio.h"
9e066e23
CM
44
45LIST_HEAD(all_traces);
24e48e03 46LIST_HEAD(fio_traces);
9e066e23 47
ba758825
CM
48static char line[1024];
49static int line_len = 1024;
e199d546 50static int found_mpstat = 0;
ba758825 51static int make_movie = 0;
776c17af 52static int keep_movie_svgs = 0;
ba758825
CM
53static int opt_graph_width = 0;
54static int opt_graph_height = 0;
55
cc3d54d5 56static int columns = 1;
e4d4b5dc 57static int num_xticks = 9;
1e1e3f04 58static int num_yticks = 4;
cc3d54d5 59
d140b434
JK
60static double min_time = 0;
61static double max_time = DBL_MAX;
62static unsigned long long min_mb = 0;
63static unsigned long long max_mb = ULLONG_MAX >> 20;
64
f2e40ddd 65int plot_io_action = 0;
0a43b43f
JK
66int io_per_process = 0;
67unsigned int longest_proc_name = 0;
f2e40ddd 68
cc3d54d5
CM
69/*
70 * this doesn't include the IO graph,
71 * but it counts the other graphs as they go out
72 */
73static int total_graphs_written = 1;
74
9e066e23
CM
75enum {
76 IO_GRAPH_INDEX = 0,
77 TPUT_GRAPH_INDEX,
24e48e03 78 FIO_GRAPH_INDEX,
e199d546
CM
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,
9e066e23
CM
84 LATENCY_GRAPH_INDEX,
85 QUEUE_DEPTH_GRAPH_INDEX,
86 IOPS_GRAPH_INDEX,
87 TOTAL_GRAPHS
88};
89
e199d546
CM
90enum {
91 MPSTAT_SYS = 0,
92 MPSTAT_IRQ,
93 MPSTAT_IO,
94 MPSTAT_SOFT,
95 MPSTAT_USER,
96 MPSTAT_GRAPHS
97};
98
9e066e23
CM
99static char *graphs_by_name[] = {
100 "io",
101 "tput",
24e48e03 102 "fio",
e199d546
CM
103 "cpu-sys",
104 "cpu-io",
105 "cpu-irq",
106 "cpu-soft",
107 "cpu-user",
9e066e23
CM
108 "latency",
109 "queue-depth",
110 "iops",
111};
112
abf08f96
CM
113enum {
114 MOVIE_SPINDLE,
115 MOVIE_RECT,
116 NUM_MOVIE_STYLES,
117};
118
119char *movie_styles[] = {
120 "spindle",
121 "rect",
122 NULL
123};
124
125static int movie_style = 0;
126
127static 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
9e066e23
CM
138static int active_graphs[TOTAL_GRAPHS];
139static int last_active_graph = IOPS_GRAPH_INDEX;
140
141static int label_index = 0;
142static int num_traces = 0;
24e48e03 143static int num_fio_traces = 0;
9e066e23
CM
144static int longest_label = 0;
145
fb38c665
LB
146static char *graph_title = "";
147static char *output_filename = "trace.svg";
2203e914
CM
148static char *blktrace_devices[MAX_DEVICES_PER_TRACE];
149static int num_blktrace_devices = 0;
fb38c665
LB
150static char *blktrace_outfile = "trace";
151static char *blktrace_dest_dir = ".";
ce225d50
AP
152static char **prog_argv = NULL;
153static int prog_argc = 0;
a51ad4ae 154static char *ffmpeg_codec = "libx264";
fb38c665 155
e199d546
CM
156static 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
9e066e23
CM
172static void enable_all_graphs(void)
173{
174 int i;
175 for (i = 0; i < TOTAL_GRAPHS; i++)
176 active_graphs[i] = 1;
177}
178
179static void disable_all_graphs(void)
180{
181 int i;
182 for (i = 0; i < TOTAL_GRAPHS; i++)
183 active_graphs[i] = 0;
184}
185
186static 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
198static 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
210static 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}
cc3d54d5
CM
220
221static 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
d7921259 232static char * join_path(char *dest_dir, char *filename)
fb38c665 233{
d7921259
YL
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;
fb38c665
LB
239}
240
9e066e23
CM
241static 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 }
e199d546 250 tf->label = "";
9e066e23
CM
251 tf->filename = strdup(filename);
252 list_add_tail(&tf->list, &all_traces);
0a43b43f 253 tf->line_color = "black";
9e066e23
CM
254 num_traces++;
255}
256
24e48e03
CM
257static 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
9e066e23
CM
274static void setup_trace_file_graphs(void)
275{
276 struct trace_file *tf;
e199d546 277 int i;
0a43b43f 278 int alloc_ptrs;
9e066e23 279
0a43b43f
JK
280 if (io_per_process)
281 alloc_ptrs = 16;
282 else
283 alloc_ptrs = 1;
24e48e03 284
9e066e23 285 list_for_each_entry(tf, &all_traces, list) {
2203e914
CM
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);
230f0601
JK
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);
24e48e03 290
230f0601 291 tf->iop_gld = alloc_line_data(tf->min_seconds, tf->max_seconds, tf->stop_seconds);
7f64c192
AP
292 tf->gdd_writes = calloc(alloc_ptrs, sizeof(struct graph_dot_data *));
293 tf->gdd_reads = calloc(alloc_ptrs, sizeof(struct graph_dot_data *));
0a43b43f 294 tf->io_plots_allocated = alloc_ptrs;
e199d546
CM
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] =
230f0601
JK
302 alloc_line_data(tf->mpstat_min_seconds,
303 tf->mpstat_max_seconds,
f752a6eb 304 tf->mpstat_max_seconds);
e199d546
CM
305 tf->mpstat_gld[i]->max = 100;
306 }
9e066e23 307 }
24e48e03
CM
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
9e066e23
CM
317}
318
319static void read_traces(void)
320{
321 struct trace_file *tf;
322 struct trace *trace;
323 u64 last_time;
324 u64 ymin;
325 u64 ymax;
bfb0e441
CM
326 u64 max_bank;
327 u64 max_bank_offset;
fb38c665 328 char *path = NULL;
9e066e23
CM
329
330 list_for_each_entry(tf, &all_traces, list) {
8a544092
JK
331 if (num_blktrace_devices)
332 path = join_path(blktrace_dest_dir, tf->filename);
333 else
334 path = strdup(tf->filename);
24e48e03 335
fb38c665 336 trace = open_trace(path);
9e066e23
CM
337 if (!trace)
338 exit(1);
339
340 last_time = find_last_time(trace);
341 tf->trace = trace;
6c984075
CM
342 tf->max_seconds = SECONDS(last_time) + 1;
343 tf->stop_seconds = SECONDS(last_time) + 1;
2203e914 344
9b9fa04b
JK
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;
9e066e23 349 tf->max_offset = ymax;
e199d546 350
fb38c665 351 read_mpstat(trace, path);
e199d546 352 tf->mpstat_stop_seconds = trace->mpstat_seconds;
f752a6eb
JK
353 tf->mpstat_max_seconds = trace->mpstat_seconds;
354 if (tf->mpstat_max_seconds)
e199d546 355 found_mpstat = 1;
24e48e03 356
d7921259 357 free(path);
9e066e23 358 }
24e48e03
CM
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 }
9e066e23
CM
368}
369
0a43b43f
JK
370static 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;
2203e914 379 tf->reads_color = tf->gdd_reads[i]->color;
0a43b43f
JK
380 }
381 if (tf->gdd_writes[i]) {
382 tf->line_color = tf->gdd_writes[i]->color;
2203e914 383 tf->writes_color = tf->gdd_writes[i]->color;
0a43b43f 384 }
2203e914
CM
385 if (tf->writes_color && tf->reads_color)
386 break;
0a43b43f 387 }
2203e914
CM
388 if (!tf->reads_color)
389 tf->reads_color = tf->line_color;
390 if (!tf->writes_color)
391 tf->writes_color = tf->line_color;
0a43b43f
JK
392 }
393}
394
24e48e03
CM
395static 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
9e066e23
CM
415static void read_trace_events(void)
416{
417
418 struct trace_file *tf;
419 struct trace *trace;
420 int ret;
e199d546 421 int i;
22dd439a 422 unsigned int time;
e199d546
CM
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;
9e066e23 426
24e48e03
CM
427 list_for_each_entry(tf, &fio_traces, list)
428 read_fio_events(tf);
429
9e066e23
CM
430 list_for_each_entry(tf, &all_traces, list) {
431 trace = tf->trace;
24e48e03 432
9e066e23 433 first_record(trace);
6a079b02 434 do {
f53e6d27 435 check_record(trace);
6a079b02
JK
436 if (SECONDS(get_record_time(trace)) > tf->max_seconds)
437 continue;
2203e914 438 add_tput(trace, tf->tput_writes_gld, tf->tput_reads_gld);
9e066e23 439 add_iop(trace, tf->iop_gld);
0a43b43f 440 add_io(trace, tf);
9e066e23
CM
441 add_pending_io(trace, tf->queue_depth_gld);
442 add_completed_io(trace, tf->latency_gld);
6a079b02 443 } while (!(ret = next_record(trace)));
9e066e23 444 }
e199d546
CM
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
22dd439a 453 for (time = 0; time < tf->mpstat_stop_seconds; time++) {
e199d546
CM
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
482mpstat_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;
9e066e23
CM
496}
497
498static 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) {
24e48e03
CM
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) {
9e066e23
CM
517 if (cur == label_index) {
518 tf->label = strdup(label);
519 label_index++;
520 break;
521 }
522 cur++;
523 }
524}
525
9e066e23
CM
526static 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
22dd439a
JK
539static void compare_minmax_tf(struct trace_file *tf, unsigned int *max_seconds,
540 u64 *min_offset, u64 *max_offset)
9e066e23 541{
f752a6eb
JK
542 if (tf->max_seconds > *max_seconds)
543 *max_seconds = tf->max_seconds;
ba758825
CM
544 if (tf->max_offset > *max_offset)
545 *max_offset = tf->max_offset;
9b9fa04b
JK
546 if (tf->min_offset < *min_offset)
547 *min_offset = tf->min_offset;
9e066e23
CM
548}
549
22dd439a
JK
550static void set_all_minmax_tf(unsigned int min_seconds,
551 unsigned int max_seconds,
552 u64 min_offset, u64 max_offset)
9e066e23 553{
ba758825 554 struct trace_file *tf;
24e48e03
CM
555 struct list_head *traces = &all_traces;
556again:
557 list_for_each_entry(tf, traces, list) {
230f0601 558 tf->min_seconds = min_seconds;
f752a6eb 559 tf->max_seconds = max_seconds;
d140b434
JK
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 }
9b9fa04b 568 tf->min_offset = min_offset;
ba758825
CM
569 tf->max_offset = max_offset;
570 }
24e48e03
CM
571 if (traces == &all_traces) {
572 traces = &fio_traces;
573 goto again;
574 }
ba758825 575}
9e066e23 576
0a43b43f
JK
577static 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
597static struct plot_history *alloc_plot_history(struct trace_file *tf)
ba758825
CM
598{
599 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
0a43b43f 600 int i;
ba758825
CM
601
602 if (!ph) {
603 perror("memory allocation failed");
604 exit(1);
9e066e23 605 }
0a43b43f
JK
606 ph->read_pid_history = calloc(tf->io_plots, sizeof(struct pid_plot_history *));
607 if (!ph->read_pid_history) {
ba758825
CM
608 perror("memory allocation failed");
609 exit(1);
610 }
0a43b43f
JK
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 }
ba758825 623 return ph;
9e066e23
CM
624}
625
0a43b43f 626LIST_HEAD(movie_history);
ba758825
CM
627int num_histories = 0;
628
0a43b43f
JK
629static 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
ba758825 644static void add_history(struct plot_history *ph, struct list_head *list)
9e066e23 645{
ba758825
CM
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);
0a43b43f 655 free_plot_history(entry);
ba758825 656 }
9e066e23
CM
657}
658
ba758825 659static void plot_movie_history(struct plot *plot, struct list_head *list)
9e066e23 660{
ba758825 661 struct plot_history *ph;
0a43b43f 662 int pid;
ba758825 663
abf08f96
CM
664 if (num_histories > 2)
665 rewind_spindle_steps(num_histories - 1);
666
ba758825 667 list_for_each_entry(ph, list, list) {
0a43b43f
JK
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 }
ba758825
CM
688 }
689}
9e066e23 690
ba758825
CM
691static void free_all_plot_history(struct list_head *head)
692{
693 struct plot_history *entry;
0a43b43f 694
ba758825
CM
695 while (!list_empty(head)) {
696 entry = list_entry(head->next, struct plot_history, list);
697 list_del(&entry->list);
0a43b43f 698 free_plot_history(entry);
9e066e23
CM
699 }
700}
701
0a43b43f
JK
702static 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
177d648b
AP
719static 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
22dd439a
JK
735static void plot_io(struct plot *plot, unsigned int min_seconds,
736 unsigned int max_seconds, u64 min_offset, u64 max_offset)
9e066e23
CM
737{
738 struct trace_file *tf;
0a43b43f 739 int i;
9e066e23 740
cc3d54d5
CM
741 if (active_graphs[IO_GRAPH_INDEX] == 0)
742 return;
743
9e066e23
CM
744 setup_axis(plot);
745
0a43b43f 746 svg_alloc_legend(plot, count_io_plot_types() * 2);
9e066e23
CM
747
748 set_plot_label(plot, "Device IO");
749 set_ylabel(plot, "Offset (MB)");
a78b574c 750 set_yticks(plot, num_yticks, min_offset / (1024 * 1024),
9b9fa04b 751 max_offset / (1024 * 1024), "");
230f0601 752 set_xticks(plot, num_xticks, min_seconds, max_seconds);
9e066e23
CM
753
754 list_for_each_entry(tf, &all_traces, list) {
177d648b 755 char *prefix = tf->label ? tf->label : "";
0a43b43f
JK
756
757 for (i = 0; i < tf->io_plots; i++) {
2203e914
CM
758 if (tf->gdd_writes[i]) {
759 svg_io_graph(plot, tf->gdd_writes[i]);
177d648b 760 plot_io_legend(plot, tf->gdd_writes[i], prefix, " Writes");
2203e914 761 }
0a43b43f
JK
762 if (tf->gdd_reads[i]) {
763 svg_io_graph(plot, tf->gdd_reads[i]);
177d648b 764 plot_io_legend(plot, tf->gdd_reads[i], prefix, " Reads");
0a43b43f 765 }
9e066e23
CM
766 }
767 }
768 if (plot->add_xlabel)
769 set_xlabel(plot, "Time (seconds)");
770 svg_write_legend(plot);
771 close_plot(plot);
772}
773
22dd439a
JK
774static void plot_tput(struct plot *plot, unsigned int min_seconds,
775 unsigned int max_seconds, int with_legend)
9e066e23
CM
776{
777 struct trace_file *tf;
778 char *units;
779 char line[128];
b18f4c12 780 u64 max = 0, val;
9e066e23 781
cc3d54d5
CM
782 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
783 return;
784
7a51b395
CM
785 if (with_legend)
786 svg_alloc_legend(plot, num_traces * 2);
2203e914 787
9e066e23 788 list_for_each_entry(tf, &all_traces, list) {
b18f4c12
JK
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;
2203e914
CM
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;
9e066e23 801 }
9e066e23 802
9e066e23
CM
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);
1e1e3f04 811 set_yticks(plot, num_yticks, 0, max, "");
230f0601 812 set_xticks(plot, num_xticks, min_seconds, max_seconds);
9e066e23
CM
813
814 list_for_each_entry(tf, &all_traces, list) {
2203e914
CM
815 if (tf->tput_writes_gld->max > 0) {
816 svg_line_graph(plot, tf->tput_writes_gld, tf->writes_color, 0, 0);
7a51b395 817 if (with_legend)
765676b7 818 svg_add_legend(plot, tf->label, " Writes", tf->writes_color);
2203e914
CM
819 }
820 if (tf->tput_reads_gld->max > 0) {
821 svg_line_graph(plot, tf->tput_reads_gld, tf->reads_color, 0, 0);
7a51b395 822 if (with_legend)
765676b7 823 svg_add_legend(plot, tf->label, " Reads", tf->reads_color);
2203e914 824 }
9e066e23
CM
825 }
826
827 if (plot->add_xlabel)
828 set_xlabel(plot, "Time (seconds)");
2203e914 829
7a51b395
CM
830 if (with_legend)
831 svg_write_legend(plot);
832
9e066e23 833 close_plot(plot);
cc3d54d5 834 total_graphs_written++;
9e066e23
CM
835}
836
22dd439a
JK
837static void plot_fio_tput(struct plot *plot,
838 unsigned int min_seconds, unsigned int max_seconds)
24e48e03
CM
839{
840 struct trace_file *tf;
841 char *units;
842 char line[128];
b18f4c12 843 u64 max = 0, val;
24e48e03
CM
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) {
b18f4c12
JK
852 val = line_graph_roll_avg_max(tf->fio_gld);
853 if (val > max)
854 max = val;
24e48e03
CM
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
22dd439a 890static void plot_cpu(struct plot *plot, unsigned int max_seconds, char *label,
8ed9516f
CM
891 int active_index, int gld_index)
892{
893 struct trace_file *tf;
894 int max = 0;
895 int i;
22dd439a 896 unsigned int gld_i;
8ed9516f
CM
897 char *color;
898 double avg = 0;
899 int ymax;
900 int plotted = 0;
901
cc3d54d5
CM
902 if (active_graphs[active_index] == 0)
903 return;
904
8ed9516f
CM
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)
cc3d54d5 910 return;
8ed9516f
CM
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)
cc3d54d5 916 return;
8ed9516f
CM
917
918 svg_alloc_legend(plot, num_traces * max);
919
8ed9516f
CM
920 setup_axis(plot);
921 set_plot_label(plot, label);
922
f752a6eb 923 max_seconds = tf->mpstat_max_seconds;
8ed9516f 924
1e1e3f04 925 set_yticks(plot, num_yticks, 0, tf->mpstat_gld[gld_index]->max, "");
8ed9516f 926 set_ylabel(plot, "Percent");
230f0601 927 set_xticks(plot, num_xticks, tf->mpstat_min_seconds, max_seconds);
8ed9516f 928
0a43b43f 929 reset_cpu_color();
8ed9516f 930 list_for_each_entry(tf, &all_traces, list) {
1e1e3f04
CM
931 if (tf->mpstat_gld == 0)
932 break;
22dd439a
JK
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);
8ed9516f
CM
938 }
939 }
230f0601
JK
940 avg /= tf->mpstat_gld[gld_index]->stop_seconds -
941 tf->mpstat_gld[gld_index]->min_seconds;
8ed9516f
CM
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
230f0601
JK
950 for (gld_i = gld->min_seconds;
951 gld_i < gld->stop_seconds; gld_i++) {
f5f8c982
CM
952 if (gld->data[i].count) {
953 this_avg += gld->data[i].sum /
954 gld->data[i].count;
955 }
956 }
8ed9516f 957
230f0601 958 this_avg /= gld->stop_seconds - gld->min_seconds;
8ed9516f 959
230f0601
JK
960 for (gld_i = gld->min_seconds;
961 gld_i < gld->stop_seconds; gld_i++) {
8ed9516f
CM
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 }
cc3d54d5
CM
989 close_plot(plot);
990 total_graphs_written++;
8ed9516f
CM
991}
992
22dd439a
JK
993static void plot_queue_depth(struct plot *plot, unsigned int min_seconds,
994 unsigned int max_seconds)
8ed9516f
CM
995{
996 struct trace_file *tf;
5122a87b 997 u64 max = 0, val;
8ed9516f 998
cc3d54d5
CM
999 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
1000 return;
8ed9516f
CM
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
5122a87b
JK
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
8ed9516f 1016 set_ylabel(plot, "Pending IO");
5122a87b 1017 set_yticks(plot, num_yticks, 0, max, "");
230f0601 1018 set_xticks(plot, num_xticks, min_seconds, max_seconds);
8ed9516f
CM
1019
1020 list_for_each_entry(tf, &all_traces, list) {
0a43b43f 1021 svg_line_graph(plot, tf->queue_depth_gld, tf->line_color, 0, 0);
8ed9516f 1022 if (num_traces > 1)
0a43b43f 1023 svg_add_legend(plot, tf->label, "", tf->line_color);
8ed9516f
CM
1024 }
1025
1026 if (plot->add_xlabel)
1027 set_xlabel(plot, "Time (seconds)");
1028 if (num_traces > 1)
1029 svg_write_legend(plot);
8ed9516f 1030 close_plot(plot);
cc3d54d5 1031 total_graphs_written++;
8ed9516f
CM
1032}
1033
2cf87e60
JA
1034static 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
ba758825
CM
1044static void convert_movie_files(char *movie_dir)
1045{
10df4b69
JM
1046 long nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1047
1048 if (nr_cpus < 0)
1049 nr_cpus = 8;
1050
ba758825 1051 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
10df4b69
JM
1052 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P %ld rsvg-convert -o {}.png {}",
1053 movie_dir, nr_cpus);
2cf87e60 1054 system_check(line);
ba758825
CM
1055}
1056
1057static void mencode_movie(char *movie_dir)
1058{
aff25f0c 1059 fprintf(stderr, "Creating movie %s with ffmpeg\n", movie_dir);
abf08f96 1060 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
a51ad4ae
ND
1061 "-vcodec %s %s", movie_dir, output_filename, ffmpeg_codec,
1062 output_filename);
2cf87e60 1063 system_check(line);
ba758825
CM
1064}
1065
aff25f0c
ES
1066static 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);
2cf87e60 1071 system_check(line);
aff25f0c
ES
1072}
1073
1074static 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
ba758825
CM
1085static void cleanup_movie(char *movie_dir)
1086{
776c17af
CM
1087 if (keep_movie_svgs) {
1088 fprintf(stderr, "Keeping movie dir %s\n", movie_dir);
1089 return;
1090 }
ba758825
CM
1091 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
1092 snprintf(line, line_len, "rm %s/*", movie_dir);
2cf87e60 1093 system_check(line);
ba758825
CM
1094
1095 snprintf(line, line_len, "rmdir %s", movie_dir);
2cf87e60 1096 system_check(line);
ba758825
CM
1097}
1098
1099static void plot_io_movie(struct plot *plot)
1100{
1101 struct trace_file *tf;
0a43b43f
JK
1102 int i, pid;
1103 struct plot_history *history;
ba758825
CM
1104 int batch_i;
1105 int movie_len = 30;
abf08f96 1106 int movie_frames_per_sec = 20;
ba758825
CM
1107 int total_frames = movie_len * movie_frames_per_sec;
1108 int rows, cols;
1109 int batch_count;
8ed9516f
CM
1110 int graph_width_factor = 5;
1111 int orig_y_offset;
c9e749da
AP
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 }
ba758825
CM
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) {
177d648b 1126 char *prefix = tf->label ? tf->label : "";
ba758825
CM
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);
ba758825 1132 set_plot_title(plot, graph_title);
8ed9516f
CM
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);
cc3d54d5 1138 plot->timeline = i / graph_width_factor;
8ed9516f 1139
7a51b395 1140 plot_tput(plot, tf->min_seconds, tf->max_seconds, 0);
8ed9516f 1141
0a43b43f 1142 plot_cpu(plot, tf->max_seconds,
cc3d54d5 1143 "CPU System Time", CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
8ed9516f 1144
cc3d54d5 1145 plot->direction = PLOT_ACROSS;
0a43b43f 1146 plot_queue_depth(plot, tf->min_seconds, tf->max_seconds);
8ed9516f
CM
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;
cc3d54d5
CM
1152 plot->timeline = 0;
1153 plot->direction = PLOT_DOWN;;
8ed9516f 1154
abf08f96
CM
1155 if (movie_style == MOVIE_SPINDLE)
1156 setup_axis_spindle(plot);
1157 else
1158 setup_axis(plot);
1159
0a43b43f 1160 svg_alloc_legend(plot, count_io_plot_types() * 2);
ba758825 1161
0a43b43f
JK
1162 history = alloc_plot_history(tf);
1163 history->col = i;
ba758825 1164
0a43b43f 1165 for (pid = 0; pid < tf->io_plots; pid++) {
177d648b
AP
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");
0a43b43f 1170 }
ba758825
CM
1171
1172 batch_i = 0;
1173 while (i < cols && batch_i < batch_count) {
0a43b43f
JK
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 }
ba758825
CM
1186 i++;
1187 batch_i++;
1188 }
1189
0a43b43f 1190 add_history(history, &movie_history);
ba758825 1191
0a43b43f 1192 plot_movie_history(plot, &movie_history);
ba758825
CM
1193
1194 svg_write_legend(plot);
1195 close_plot(plot);
ba758825 1196 close_plot(plot);
ba758825 1197
abf08f96 1198 close_plot_file(plot);
ba758825 1199 }
0a43b43f 1200 free_all_plot_history(&movie_history);
ba758825
CM
1201 }
1202 convert_movie_files(movie_dir);
aff25f0c 1203 encode_movie(movie_dir);
ba758825 1204 cleanup_movie(movie_dir);
ba758825
CM
1205}
1206
22dd439a
JK
1207static void plot_latency(struct plot *plot, unsigned int min_seconds,
1208 unsigned int max_seconds)
9e066e23
CM
1209{
1210 struct trace_file *tf;
1211 char *units;
1212 char line[128];
b18f4c12 1213 u64 max = 0, val;
9e066e23
CM
1214
1215 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
1216 return;
1217
1218 if (num_traces > 1)
1219 svg_alloc_legend(plot, num_traces);
1e1e3f04 1220
9e066e23 1221 list_for_each_entry(tf, &all_traces, list) {
b18f4c12
JK
1222 val = line_graph_roll_avg_max(tf->latency_gld);
1223 if (val > max)
1224 max = val;
9e066e23 1225 }
1e1e3f04 1226
9e066e23
CM
1227 list_for_each_entry(tf, &all_traces, list)
1228 tf->latency_gld->max = max;
1229
9e066e23
CM
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);
1e1e3f04 1238 set_yticks(plot, num_yticks, 0, max, "");
230f0601 1239 set_xticks(plot, num_xticks, min_seconds, max_seconds);
9e066e23
CM
1240
1241 list_for_each_entry(tf, &all_traces, list) {
0a43b43f 1242 svg_line_graph(plot, tf->latency_gld, tf->line_color, 0, 0);
9e066e23 1243 if (num_traces > 1)
0a43b43f 1244 svg_add_legend(plot, tf->label, "", tf->line_color);
9e066e23
CM
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);
cc3d54d5 1252 total_graphs_written++;
9e066e23
CM
1253}
1254
22dd439a
JK
1255static void plot_iops(struct plot *plot, unsigned int min_seconds,
1256 unsigned int max_seconds)
9e066e23
CM
1257{
1258 struct trace_file *tf;
1259 char *units;
b18f4c12 1260 u64 max = 0, val;
9e066e23
CM
1261
1262 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
1263 return;
1264
1265 list_for_each_entry(tf, &all_traces, list) {
b18f4c12
JK
1266 val = line_graph_roll_avg_max(tf->iop_gld);
1267 if (val > max)
1268 max = val;
9e066e23
CM
1269 }
1270
1271 list_for_each_entry(tf, &all_traces, list)
1272 tf->iop_gld->max = max;
1273
9e066e23
CM
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
1e1e3f04 1284 set_yticks(plot, num_yticks, 0, max, units);
230f0601 1285 set_xticks(plot, num_xticks, min_seconds, max_seconds);
9e066e23
CM
1286
1287 list_for_each_entry(tf, &all_traces, list) {
0a43b43f 1288 svg_line_graph(plot, tf->iop_gld, tf->line_color, 0, 0);
9e066e23 1289 if (num_traces > 1)
0a43b43f 1290 svg_add_legend(plot, tf->label, "", tf->line_color);
9e066e23
CM
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);
cc3d54d5
CM
1299 total_graphs_written++;
1300}
1301
1302static 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
9e066e23
CM
1322}
1323
ba758825
CM
1324enum {
1325 HELP_LONG_OPT = 1,
1326};
1327
ce225d50 1328char *option_string = "+F:T:t:o:l:r:O:N:d:D:pm::h:w:c:x:y:a:C:PK";
ba758825 1329static struct option long_options[] = {
cc3d54d5 1330 {"columns", required_argument, 0, 'c'},
24e48e03 1331 {"fio-trace", required_argument, 0, 'F'},
ba758825
CM
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'},
fb38c665 1340 {"blktrace-destination", required_argument, 0, 'D'},
ce225d50 1341 {"prog", no_argument, 0, 'p'},
abf08f96 1342 {"movie", optional_argument, 0, 'm'},
a51ad4ae 1343 {"codec", optional_argument, 0, 'C'},
776c17af 1344 {"keep-movie-svgs", no_argument, 0, 'K'},
ba758825
CM
1345 {"width", required_argument, 0, 'w'},
1346 {"height", required_argument, 0, 'h'},
d140b434
JK
1347 {"xzoom", required_argument, 0, 'x'},
1348 {"yzoom", required_argument, 0, 'y'},
f2e40ddd 1349 {"io-plot-action", required_argument, 0, 'a'},
0a43b43f 1350 {"per-process-io", no_argument, 0, 'P'},
5a870f23 1351 {"help", no_argument, 0, HELP_LONG_OPT},
ba758825
CM
1352 {0, 0, 0, 0}
1353};
1354
1355static void print_usage(void)
1356{
1357 fprintf(stderr, "iowatcher usage:\n"
1358 "\t-d (--device): device for blktrace to trace\n"
fb38c665 1359 "\t-D (--blktrace-destination): destination for blktrace\n"
ba758825 1360 "\t-t (--trace): trace file name (more than one allowed)\n"
24e48e03 1361 "\t-F (--fio-trace): fio bandwidth trace (more than one allowed)\n"
ba758825 1362 "\t-l (--label): trace label in the graph\n"
1775e06b 1363 "\t-o (--output): output file name for the SVG image or video\n"
ce225d50 1364 "\t-p (--prog): run a program while blktrace is run\n"
776c17af 1365 "\t-K (--keep-movie-svgs keep svgs generated for movie mode\n"
c5245584 1366 "\t-m (--movie [=spindle|rect]): create IO animations\n"
a51ad4ae 1367 "\t-C (--codec): ffmpeg codec. Use ffmpeg -codecs to list\n"
ba758825
CM
1368 "\t-r (--rolling): number of seconds in the rolling averge\n"
1369 "\t-T (--title): graph title\n"
76449096 1370 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue-depth, \n"
5122a20d
CM
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"
ba758825
CM
1373 "\t-h (--height): set the height of each graph\n"
1374 "\t-w (--width): set the width of each graph\n"
cc3d54d5 1375 "\t-c (--columns): numbers of columns in graph output\n"
d140b434
JK
1376 "\t-x (--xzoom): limit processed time to min:max\n"
1377 "\t-y (--yzoom): limit processed sectors to min:max\n"
f2e40ddd 1378 "\t-a (--io-plot-action): plot given action (one of Q,D,C) in IO graph\n"
0a43b43f 1379 "\t-P (--per-process-io): distinguish between processes in IO graph\n"
ba758825
CM
1380 );
1381 exit(1);
1382}
1383
d140b434
JK
1384static 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
1410static 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
ba758825
CM
1437static int parse_options(int ac, char **av)
1438{
1439 int c;
1440 int disabled = 0;
ce225d50 1441 int p_flagged = 0;
ba758825
CM
1442
1443 while (1) {
ba758825
CM
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;
24e48e03
CM
1460 case 'F':
1461 add_fio_trace_file(optarg);
1462 break;
ba758825
CM
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':
2203e914
CM
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);
ba758825 1488 break;
fb38c665
LB
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;
ba758825 1496 case 'p':
ce225d50 1497 p_flagged = 1;
ba758825 1498 break;
776c17af
CM
1499 case 'K':
1500 keep_movie_svgs = 1;
1501 break;
ba758825
CM
1502 case 'm':
1503 make_movie = 1;
abf08f96
CM
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]);
ba758825 1513 break;
a51ad4ae
ND
1514 case 'C':
1515 ffmpeg_codec = strdup(optarg);
1516 break;
ba758825
CM
1517 case 'h':
1518 opt_graph_height = atoi(optarg);
1519 break;
1520 case 'w':
1521 opt_graph_width = atoi(optarg);
1522 break;
cc3d54d5
CM
1523 case 'c':
1524 columns = atoi(optarg);
1525 break;
d140b434
JK
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;
f2e40ddd
JK
1549 case 'a':
1550 if (strlen(optarg) != 1) {
1551action_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;
0a43b43f
JK
1559 case 'P':
1560 io_per_process = 1;
1561 break;
ba758825
CM
1562 case '?':
1563 case HELP_LONG_OPT:
1564 print_usage();
1565 break;
1566 default:
1567 break;
1568 }
1569 }
ce225d50
AP
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
ba758825
CM
1582 return 0;
1583}
1584
2203e914 1585static void dest_mkdir(char *dir)
fb38c665
LB
1586{
1587 int ret;
1588
2203e914 1589 ret = mkdir(dir, 0777);
fb38c665
LB
1590 if (ret && errno != EEXIST) {
1591 fprintf(stderr, "failed to mkdir error %s\n", strerror(errno));
1592 exit(errno);
1593 }
1594}
ba758825 1595
9e066e23
CM
1596int main(int ac, char **av)
1597{
1598 struct plot *plot;
22dd439a
JK
1599 unsigned int min_seconds = 0;
1600 unsigned int max_seconds = 0;
9e066e23 1601 u64 max_offset = 0;
9b9fa04b 1602 u64 min_offset = ~(u64)0;
9e066e23
CM
1603 struct trace_file *tf;
1604 int ret;
cc3d54d5 1605 int rows, cols;
9e066e23
CM
1606
1607 init_io_hash_table();
0a43b43f 1608 init_process_hash_table();
9e066e23
CM
1609
1610 enable_all_graphs();
1611
1612 parse_options(ac, av);
1613
1614 last_active_graph = last_graph();
ba758825
CM
1615 if (make_movie) {
1616 set_io_graph_scale(256);
abf08f96 1617 if (movie_style == MOVIE_SPINDLE)
8ed9516f 1618 set_graph_size(750, 550);
abf08f96
CM
1619 else
1620 set_graph_size(700, 400);
cc3d54d5
CM
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;
ba758825
CM
1627 }
1628 if (opt_graph_height)
1629 set_graph_height(opt_graph_height);
1630
1631 if (opt_graph_width)
cc3d54d5 1632 set_graph_width(opt_graph_width);
9e066e23 1633
24e48e03 1634 if (list_empty(&all_traces) && list_empty(&fio_traces)) {
9e066e23
CM
1635 fprintf(stderr, "No traces found, exiting\n");
1636 exit(1);
1637 }
1638
2203e914 1639 if (num_blktrace_devices) {
49559b61 1640 char *path = join_path(blktrace_dest_dir, blktrace_outfile);
2203e914 1641 dest_mkdir(blktrace_dest_dir);
49559b61 1642 dest_mkdir(path);
fb38c665 1643
2203e914
CM
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);
9e066e23 1649 if (ret) {
49559b61
AP
1650 perror("Exiting due to blktrace failure");
1651 exit(ret);
9e066e23 1652 }
fb38c665 1653
49559b61
AP
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 }
ce225d50
AP
1660
1661 if (prog_argv && prog_argc) {
49559b61
AP
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);
9e066e23 1667 }
d7921259 1668 free(path);
9e066e23
CM
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)
f752a6eb 1676 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
24e48e03
CM
1677 list_for_each_entry(tf, &fio_traces, list)
1678 compare_minmax_tf(tf, &max_seconds, &min_offset, &max_offset);
d140b434
JK
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;
9e066e23
CM
1686
1687 /* push the max we found into all the tfs */
230f0601 1688 set_all_minmax_tf(min_seconds, max_seconds, min_offset, max_offset);
9e066e23
CM
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
0a43b43f
JK
1696 pick_line_graph_color();
1697
ba758825
CM
1698 plot = alloc_plot();
1699
1700 if (make_movie) {
0a43b43f 1701 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
ba758825
CM
1702 plot_io_movie(plot);
1703 exit(0);
9e066e23
CM
1704 }
1705
ba758825 1706 set_plot_output(plot, output_filename);
9e066e23 1707
e199d546 1708 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
0a43b43f 1709 set_legend_width(longest_label + longest_proc_name + 1 + strlen("writes"));
24e48e03 1710 else if (num_traces >= 1 || num_fio_traces >= 1)
9e066e23
CM
1711 set_legend_width(longest_label);
1712 else
1713 set_legend_width(0);
1714
cc3d54d5
CM
1715 get_graph_size(&cols, &rows);
1716 if (columns > 1)
1717 plot->add_xlabel = 1;
e4df8968 1718 set_plot_title(plot, graph_title);
cc3d54d5 1719
6d5a91a7 1720 check_plot_columns(plot, IO_GRAPH_INDEX);
230f0601 1721 plot_io(plot, min_seconds, max_seconds, min_offset, max_offset);
cc3d54d5
CM
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 }
1e1e3f04
CM
1730 if (rows <= 50)
1731 num_yticks--;
cc3d54d5
CM
1732
1733 check_plot_columns(plot, TPUT_GRAPH_INDEX);
7a51b395 1734 plot_tput(plot, min_seconds, max_seconds, 1);
cc3d54d5 1735
24e48e03
CM
1736 check_plot_columns(plot, FIO_GRAPH_INDEX);
1737 plot_fio_tput(plot, min_seconds, max_seconds);
1738
cc3d54d5 1739 check_plot_columns(plot, CPU_IO_GRAPH_INDEX);
f752a6eb 1740 plot_cpu(plot, max_seconds, "CPU IO Wait Time",
e199d546 1741 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
cc3d54d5
CM
1742
1743 check_plot_columns(plot, CPU_SYS_GRAPH_INDEX);
f752a6eb 1744 plot_cpu(plot, max_seconds, "CPU System Time",
e199d546 1745 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
cc3d54d5
CM
1746
1747 check_plot_columns(plot, CPU_IRQ_GRAPH_INDEX);
f752a6eb 1748 plot_cpu(plot, max_seconds, "CPU IRQ Time",
e199d546 1749 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
cc3d54d5
CM
1750
1751 check_plot_columns(plot, CPU_SOFT_GRAPH_INDEX);
f752a6eb 1752 plot_cpu(plot, max_seconds, "CPU SoftIRQ Time",
e199d546 1753 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
cc3d54d5
CM
1754
1755 check_plot_columns(plot, CPU_USER_GRAPH_INDEX);
f752a6eb 1756 plot_cpu(plot, max_seconds, "CPU User Time",
e199d546
CM
1757 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1758
cc3d54d5 1759 check_plot_columns(plot, LATENCY_GRAPH_INDEX);
230f0601 1760 plot_latency(plot, min_seconds, max_seconds);
cc3d54d5
CM
1761
1762 check_plot_columns(plot, QUEUE_DEPTH_GRAPH_INDEX);
230f0601 1763 plot_queue_depth(plot, min_seconds, max_seconds);
cc3d54d5
CM
1764
1765 check_plot_columns(plot, IOPS_GRAPH_INDEX);
230f0601 1766 plot_iops(plot, min_seconds, max_seconds);
9e066e23
CM
1767
1768 /* once for all */
1769 close_plot(plot);
abf08f96 1770 close_plot_file(plot);
9e066e23
CM
1771 return 0;
1772}