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