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