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