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