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