iowatcher: Add a new movie mode that maps the IOs onto a platter.
[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;
288
289 list_for_each_entry(tf, &all_traces, list) {
290 trace = open_trace(tf->filename);
291 if (!trace)
292 exit(1);
293
294 last_time = find_last_time(trace);
295 tf->trace = trace;
296 tf->seconds = SECONDS(last_time);
297 tf->stop_seconds = SECONDS(last_time);
298 tf->max_offset = find_highest_offset(trace);
299
300 filter_outliers(trace, tf->max_offset, &ymin, &ymax);
301 tf->max_offset = ymax;
e199d546
CM
302
303 read_mpstat(trace, tf->filename);
304 tf->mpstat_stop_seconds = trace->mpstat_seconds;
305 tf->mpstat_seconds = trace->mpstat_seconds;
306 if (tf->mpstat_seconds)
307 found_mpstat = 1;
9e066e23
CM
308 }
309}
310
311static void read_trace_events(void)
312{
313
314 struct trace_file *tf;
315 struct trace *trace;
316 int ret;
e199d546
CM
317 int i;
318 int time;
319 double user, sys, iowait, irq, soft;
320 double max_user = 0, max_sys = 0, max_iowait = 0,
321 max_irq = 0, max_soft = 0;
9e066e23
CM
322
323 list_for_each_entry(tf, &all_traces, list) {
324 trace = tf->trace;
325 first_record(trace);
326 while (1) {
327 check_record(trace);
328 add_tput(trace, tf->tput_gld);
329 add_iop(trace, tf->iop_gld);
330 add_io(trace, tf->gdd_writes, tf->gdd_reads);
331 add_pending_io(trace, tf->queue_depth_gld);
332 add_completed_io(trace, tf->latency_gld);
333 ret = next_record(trace);
334 if (ret)
335 break;
336 }
337 }
e199d546
CM
338 list_for_each_entry(tf, &all_traces, list) {
339 trace = tf->trace;
340
341 if (trace->mpstat_num_cpus == 0)
342 continue;
343
344 first_mpstat(trace);
345
346 for (time = 0; time < tf->mpstat_stop_seconds; time ++) {
347 for (i = 0; i < (trace->mpstat_num_cpus + 1) * MPSTAT_GRAPHS; i += MPSTAT_GRAPHS) {
348 ret = read_mpstat_event(trace, &user, &sys,
349 &iowait, &irq, &soft);
350 if (ret)
351 goto mpstat_done;
352 if (next_mpstat_line(trace))
353 goto mpstat_done;
354
355 if (sys > max_sys)
356 max_sys = sys;
357 if (user > max_user)
358 max_user = user;
359 if (irq > max_irq)
360 max_irq = irq;
361 if (iowait > max_iowait)
362 max_iowait = iowait;
363
364 add_mpstat_gld(time, sys, tf->mpstat_gld[i + MPSTAT_SYS]);
365 add_mpstat_gld(time, irq, tf->mpstat_gld[i + MPSTAT_IRQ]);
366 add_mpstat_gld(time, soft, tf->mpstat_gld[i + MPSTAT_SOFT]);
367 add_mpstat_gld(time, user, tf->mpstat_gld[i + MPSTAT_USER]);
368 add_mpstat_gld(time, iowait, tf->mpstat_gld[i + MPSTAT_IO]);
369 }
370 if (next_mpstat(trace) == NULL)
371 break;
372 }
373 }
374
375mpstat_done:
376 list_for_each_entry(tf, &all_traces, list) {
377 trace = tf->trace;
378
379 if (trace->mpstat_num_cpus == 0)
380 continue;
381
382 tf->mpstat_gld[MPSTAT_SYS]->max = max_sys;
383 tf->mpstat_gld[MPSTAT_IRQ]->max = max_irq;
384 tf->mpstat_gld[MPSTAT_SOFT]->max = max_soft;
385 tf->mpstat_gld[MPSTAT_USER]->max = max_user;
386 tf->mpstat_gld[MPSTAT_IO]->max = max_iowait;;
387 }
388 return;
9e066e23
CM
389}
390
391static void set_trace_label(char *label)
392{
393 int cur = 0;
394 struct trace_file *tf;
395 int len = strlen(label);
396
397 if (len > longest_label)
398 longest_label = len;
399
400 list_for_each_entry(tf, &all_traces, list) {
401 if (cur == label_index) {
402 tf->label = strdup(label);
403 label_index++;
404 break;
405 }
406 cur++;
407 }
408}
409
410static char *graph_title = "";
411static char *output_filename = "trace.svg";
412static char *blktrace_device = NULL;
413static char *blktrace_outfile = "trace";
414static char *blktrace_dest_dir = ".";
415static char *program_to_run = NULL;
416
417static void set_blktrace_outfile(char *arg)
418{
419 char *s = strdup(arg);
420 char *last_dot = strrchr(s, '.');
421
422 if (last_dot) {
423 if (strcmp(last_dot, ".dump") == 0)
424 *last_dot = '\0';
425 }
426 blktrace_outfile = s;
427}
428
429
ba758825 430static void compare_max_tf(struct trace_file *tf, int *seconds, u64 *max_offset)
9e066e23 431{
ba758825
CM
432 if (tf->seconds > *seconds)
433 *seconds = tf->seconds;
434 if (tf->max_offset > *max_offset)
435 *max_offset = tf->max_offset;
9e066e23
CM
436}
437
ba758825 438static void set_all_max_tf(int seconds, u64 max_offset)
9e066e23 439{
ba758825 440 struct trace_file *tf;
9e066e23 441
ba758825
CM
442 list_for_each_entry(tf, &all_traces, list) {
443 tf->seconds = seconds;
444 tf->max_offset = max_offset;
445 }
446}
9e066e23 447
ba758825
CM
448static char *create_movie_temp_dir(void)
449{
450 char *ret;
abf08f96 451 char *pattern = strdup("io-movie-XXXXXX");;
9e066e23 452
ba758825
CM
453 ret = mkdtemp(pattern);
454 if (!ret) {
455 perror("Unable to create temp directory for movie files");
456 exit(1);
457 }
458 return ret;
459}
9e066e23 460
ba758825
CM
461static struct plot_history *alloc_plot_history(char *color)
462{
463 struct plot_history *ph = calloc(1, sizeof(struct plot_history));
464
465 if (!ph) {
466 perror("memory allocation failed");
467 exit(1);
9e066e23 468 }
ba758825
CM
469 ph->history = calloc(4096, sizeof(double));
470 if (!ph->history) {
471 perror("memory allocation failed");
472 exit(1);
473 }
474 ph->history_len = 4096;
475 ph->color = color;
476 return ph;
9e066e23
CM
477}
478
ba758825
CM
479LIST_HEAD(movie_history_writes);
480LIST_HEAD(movie_history_reads);
481int num_histories = 0;
482
483static void add_history(struct plot_history *ph, struct list_head *list)
9e066e23 484{
ba758825
CM
485 struct plot_history *entry;
486
487 list_add_tail(&ph->list, list);
488 num_histories++;
489
490 if (num_histories > 12) {
491 num_histories--;
492 entry = list_entry(list->next, struct plot_history, list);
493 list_del(&entry->list);
494 free(entry->history);
495 free(entry);
496 }
9e066e23
CM
497}
498
ba758825 499static void plot_movie_history(struct plot *plot, struct list_head *list)
9e066e23 500{
ba758825
CM
501 struct plot_history *ph;
502
abf08f96
CM
503 if (num_histories > 2)
504 rewind_spindle_steps(num_histories - 1);
505
ba758825 506 list_for_each_entry(ph, list, list) {
abf08f96
CM
507 if (movie_style == MOVIE_SPINDLE)
508 svg_io_graph_movie_array_spindle(plot, ph);
509 else
510 svg_io_graph_movie_array(plot, ph);
ba758825
CM
511 }
512}
9e066e23 513
ba758825
CM
514static void free_all_plot_history(struct list_head *head)
515{
516 struct plot_history *entry;
517 while (!list_empty(head)) {
518 entry = list_entry(head->next, struct plot_history, list);
519 list_del(&entry->list);
520 free(entry->history);
521 free(entry);
9e066e23
CM
522 }
523}
524
ba758825 525static void __plot_io(struct plot *plot, int seconds, u64 max_offset)
9e066e23
CM
526{
527 struct trace_file *tf;
528
529 if (active_graphs[IO_GRAPH_INDEX] == 0)
530 return;
531
9e066e23
CM
532 setup_axis(plot);
533
534 svg_alloc_legend(plot, num_traces * 2);
535
536 set_plot_label(plot, "Device IO");
537 set_ylabel(plot, "Offset (MB)");
538 set_yticks(plot, 4, 0, max_offset / (1024 * 1024), "");
539 set_xticks(plot, 9, 0, seconds);
540
541 list_for_each_entry(tf, &all_traces, list) {
542 char *label = tf->label;
543
544 if (!label)
545 label = "";
546 svg_io_graph(plot, tf->gdd_reads, tf->read_color);
547 if (tf->gdd_reads->total_ios)
548 svg_add_legend(plot, label, " Reads", tf->read_color);
549
550 svg_io_graph(plot, tf->gdd_writes, tf->write_color);
551 if (tf->gdd_writes->total_ios) {
552 svg_add_legend(plot, label, " Writes", tf->write_color);
553 }
554 }
555 if (plot->add_xlabel)
556 set_xlabel(plot, "Time (seconds)");
557 svg_write_legend(plot);
ba758825
CM
558}
559
560static void plot_io(struct plot *plot, int seconds, u64 max_offset)
561{
562 plot->add_xlabel = last_active_graph == IO_GRAPH_INDEX;
563
564 __plot_io(plot, seconds, max_offset);
9e066e23
CM
565 close_plot(plot);
566}
567
ba758825 568static void __plot_tput(struct plot *plot, int seconds)
9e066e23
CM
569{
570 struct trace_file *tf;
571 char *units;
572 char line[128];
573 u64 max = 0;
574
575 if (active_graphs[TPUT_GRAPH_INDEX] == 0)
576 return;
577
578 if (num_traces > 1)
579 svg_alloc_legend(plot, num_traces);
580 list_for_each_entry(tf, &all_traces, list) {
581 if (tf->tput_gld->max > max)
582 max = tf->tput_gld->max;
583 }
584 list_for_each_entry(tf, &all_traces, list)
585 tf->tput_gld->max = max;
586
9e066e23
CM
587 setup_axis(plot);
588 set_plot_label(plot, "Throughput");
589
590 tf = list_entry(all_traces.next, struct trace_file, list);
591
592 scale_line_graph_bytes(&max, &units, 1024);
593 sprintf(line, "%sB/s", units);
594 set_ylabel(plot, line);
595 set_yticks(plot, 4, 0, max, "");
596 set_xticks(plot, 9, 0, seconds);
597
598 list_for_each_entry(tf, &all_traces, list) {
599 svg_line_graph(plot, tf->tput_gld, tf->read_color);
600 if (num_traces > 1)
601 svg_add_legend(plot, tf->label, "", tf->read_color);
602 }
603
604 if (plot->add_xlabel)
605 set_xlabel(plot, "Time (seconds)");
606 if (num_traces > 1)
607 svg_write_legend(plot);
ba758825
CM
608}
609
610static void plot_tput(struct plot *plot, int seconds)
611{
612 plot->add_xlabel = last_active_graph == TPUT_GRAPH_INDEX;
613 __plot_tput(plot, seconds);
9e066e23
CM
614 close_plot(plot);
615}
616
ba758825
CM
617static void convert_movie_files(char *movie_dir)
618{
619 fprintf(stderr, "Converting svg files in %s\n", movie_dir);
620 snprintf(line, line_len, "find %s -name \\*.svg | xargs -I{} -n 1 -P 8 rsvg-convert -o {}.png {}",
621 movie_dir);
622 system(line);
623}
624
625static void mencode_movie(char *movie_dir)
626{
627 fprintf(stderr, "Creating movie %s\n", movie_dir);
abf08f96 628 snprintf(line, line_len, "ffmpeg -r 20 -y -i %s/%%10d-%s.svg.png -b:v 250k "
7a147342 629 "-vcodec libx264 %s", movie_dir, output_filename, output_filename);
ba758825
CM
630 system(line);
631}
632
633static void cleanup_movie(char *movie_dir)
634{
635 fprintf(stderr, "Removing movie dir %s\n", movie_dir);
636 snprintf(line, line_len, "rm %s/*", movie_dir);
637 system(line);
638
639 snprintf(line, line_len, "rmdir %s", movie_dir);
640 system(line);
641}
642
643static void plot_io_movie(struct plot *plot)
644{
645 struct trace_file *tf;
646 char *movie_dir = create_movie_temp_dir();
647 int i;
648 struct plot_history *read_history;
649 struct plot_history *write_history;
650 int batch_i;
651 int movie_len = 30;
abf08f96 652 int movie_frames_per_sec = 20;
ba758825
CM
653 int total_frames = movie_len * movie_frames_per_sec;
654 int rows, cols;
655 int batch_count;
656
657 get_graph_size(&cols, &rows);
658 batch_count = cols / total_frames;
659
660 if (batch_count == 0)
661 batch_count = 1;
662
663 list_for_each_entry(tf, &all_traces, list) {
664 char *label = tf->label;
665 if (!label)
666 label = "";
667
668 i = 0;
669 while (i < cols) {
670 snprintf(line, line_len, "%s/%010d-%s.svg", movie_dir, i, output_filename);
671 set_plot_output(plot, line);
672
673 set_plot_title(plot, graph_title);
abf08f96
CM
674 if (movie_style == MOVIE_SPINDLE)
675 setup_axis_spindle(plot);
676 else
677 setup_axis(plot);
678
ba758825
CM
679 svg_alloc_legend(plot, num_traces * 2);
680
681 read_history = alloc_plot_history(tf->read_color);
682 write_history = alloc_plot_history(tf->write_color);
683 read_history->col = i;
684 write_history->col = i;
685
686 if (tf->gdd_reads->total_ios)
687 svg_add_legend(plot, label, " Reads", tf->read_color);
688 if (tf->gdd_writes->total_ios)
689 svg_add_legend(plot, label, " Writes", tf->write_color);
690
691 batch_i = 0;
692 while (i < cols && batch_i < batch_count) {
ba758825 693 svg_io_graph_movie(tf->gdd_reads, read_history, i);
ba758825
CM
694 svg_io_graph_movie(tf->gdd_writes, write_history, i);
695 i++;
696 batch_i++;
697 }
698
699 add_history(read_history, &movie_history_reads);
700 add_history(write_history, &movie_history_writes);
701
702 plot_movie_history(plot, &movie_history_reads);
703 plot_movie_history(plot, &movie_history_writes);
704
705 svg_write_legend(plot);
706 close_plot(plot);
707
abf08f96 708 set_graph_size(cols, rows / 7);
ba758825
CM
709 plot->add_xlabel = 1;
710 __plot_tput(plot, tf->gdd_reads->seconds);
711 svg_write_time_line(plot, i);
712 close_plot(plot);
713 set_graph_size(cols, rows);
714
715 close_plot(plot);
abf08f96 716 close_plot_file(plot);
ba758825
CM
717 }
718 free_all_plot_history(&movie_history_reads);
719 free_all_plot_history(&movie_history_writes);
720 }
721 convert_movie_files(movie_dir);
722 mencode_movie(movie_dir);
723 cleanup_movie(movie_dir);
724 free(movie_dir);
725}
726
e199d546
CM
727static void plot_cpu(struct plot *plot, int seconds, char *label,
728 int active_index, int gld_index)
729{
730 struct trace_file *tf;
731 char line[128];
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++) {
771 avg += tf->mpstat_gld[gld_index]->data[i].sum;
772 }
773 avg /= tf->mpstat_gld[gld_index]->stop_seconds;
774 color = pick_cpu_color();
775 svg_line_graph(plot, tf->mpstat_gld[0], color);
776 svg_add_legend(plot, tf->label, " avg", color);
777
778 for (i = 1; i < tf->trace->mpstat_num_cpus + 1; i++) {
779 struct graph_line_data *gld = tf->mpstat_gld[i * MPSTAT_GRAPHS + gld_index];
780 double this_avg = 0;
781
782 for (gld_i = 0; gld_i < gld->stop_seconds; gld_i++)
783 this_avg += gld->data[i].sum;
784
785 this_avg /= gld->stop_seconds;
786
787 for (gld_i = 0; gld_i < gld->stop_seconds; gld_i++) {
788 if (this_avg > avg + 30 ||
789 gld->data[gld_i].sum > 95) {
790 color = pick_cpu_color();
791 svg_line_graph(plot, gld, color);
792 snprintf(line, 128, " CPU %d\n", i - 1);
793 svg_add_legend(plot, tf->label, line, color);
794 plotted++;
795 break;
796 }
797
798 }
799 }
800 }
801
802 if (plot->add_xlabel)
803 set_xlabel(plot, "Time (seconds)");
804
805 if (plot->legend_index <= 8)
806 svg_write_legend(plot);
807 else
808 svg_free_legend(plot);
809 close_plot(plot);
810}
811
9e066e23
CM
812static void plot_latency(struct plot *plot, int seconds)
813{
814 struct trace_file *tf;
815 char *units;
816 char line[128];
817 u64 max = 0;
818
819 if (active_graphs[LATENCY_GRAPH_INDEX] == 0)
820 return;
821
822 if (num_traces > 1)
823 svg_alloc_legend(plot, num_traces);
824 list_for_each_entry(tf, &all_traces, list) {
825 if (tf->latency_gld->max > max)
826 max = tf->latency_gld->max;
827 }
828 list_for_each_entry(tf, &all_traces, list)
829 tf->latency_gld->max = max;
830
831 plot->add_xlabel = last_active_graph == TPUT_GRAPH_INDEX;
832 setup_axis(plot);
833 set_plot_label(plot, "IO Latency");
834
835 tf = list_entry(all_traces.next, struct trace_file, list);
836
837 scale_line_graph_time(&max, &units);
838 sprintf(line, "latency (%ss)", units);
839 set_ylabel(plot, line);
840 set_yticks(plot, 4, 0, max, "");
841 set_xticks(plot, 9, 0, seconds);
842
843 list_for_each_entry(tf, &all_traces, list) {
844 svg_line_graph(plot, tf->latency_gld, tf->read_color);
845 if (num_traces > 1)
846 svg_add_legend(plot, tf->label, "", tf->read_color);
847 }
848
849 if (plot->add_xlabel)
850 set_xlabel(plot, "Time (seconds)");
851 if (num_traces > 1)
852 svg_write_legend(plot);
853 close_plot(plot);
854}
855
856static void plot_queue_depth(struct plot *plot, int seconds)
857{
858 struct trace_file *tf;
859
860 if (active_graphs[QUEUE_DEPTH_GRAPH_INDEX] == 0)
861 return;
862
863 plot->add_xlabel = last_active_graph == QUEUE_DEPTH_GRAPH_INDEX;
864
865 setup_axis(plot);
866 set_plot_label(plot, "Queue Depth");
867 if (num_traces > 1)
868 svg_alloc_legend(plot, num_traces);
869
870 tf = list_entry(all_traces.next, struct trace_file, list);
871 set_ylabel(plot, "Pending IO");
872 set_yticks(plot, 4, 0, tf->queue_depth_gld->max, "");
873 set_xticks(plot, 9, 0, seconds);
874
875 list_for_each_entry(tf, &all_traces, list) {
876 svg_line_graph(plot, tf->queue_depth_gld, tf->read_color);
877 if (num_traces > 1)
878 svg_add_legend(plot, tf->label, "", tf->read_color);
879 }
880
881 if (plot->add_xlabel)
882 set_xlabel(plot, "Time (seconds)");
883 if (num_traces > 1)
884 svg_write_legend(plot);
885 close_plot(plot);
886}
887
888static void plot_iops(struct plot *plot, int seconds)
889{
890 struct trace_file *tf;
891 char *units;
892 u64 max = 0;
893
894 if (active_graphs[IOPS_GRAPH_INDEX] == 0)
895 return;
896
897 list_for_each_entry(tf, &all_traces, list) {
898 if (tf->iop_gld->max > max)
899 max = tf->iop_gld->max;
900 }
901
902 list_for_each_entry(tf, &all_traces, list)
903 tf->iop_gld->max = max;
904
905
906 plot->add_xlabel = last_active_graph == IOPS_GRAPH_INDEX;
907 setup_axis(plot);
908 set_plot_label(plot, "IOPs");
909 if (num_traces > 1)
910 svg_alloc_legend(plot, num_traces);
911
912 tf = list_entry(all_traces.next, struct trace_file, list);
913
914 scale_line_graph_bytes(&max, &units, 1000);
915 set_ylabel(plot, "IO/s");
916
917 set_yticks(plot, 4, 0, max, units);
918 set_xticks(plot, 9, 0, seconds);
919
920 list_for_each_entry(tf, &all_traces, list) {
921 svg_line_graph(plot, tf->iop_gld, tf->read_color);
922 if (num_traces > 1)
923 svg_add_legend(plot, tf->label, "", tf->read_color);
924 }
925
926 if (plot->add_xlabel)
927 set_xlabel(plot, "Time (seconds)");
928 if (num_traces > 1)
929 svg_write_legend(plot);
930
931 close_plot(plot);
932}
933
ba758825
CM
934enum {
935 HELP_LONG_OPT = 1,
936};
937
abf08f96 938char *option_string = "T:t:o:l:r:O:N:d:p:m::h:w:";
ba758825
CM
939static struct option long_options[] = {
940 {"title", required_argument, 0, 'T'},
941 {"trace", required_argument, 0, 't'},
942 {"output", required_argument, 0, 'o'},
943 {"label", required_argument, 0, 'l'},
944 {"rolling", required_argument, 0, 'r'},
945 {"no-graph", required_argument, 0, 'N'},
946 {"only-graph", required_argument, 0, 'O'},
947 {"device", required_argument, 0, 'd'},
948 {"prog", required_argument, 0, 'p'},
abf08f96 949 {"movie", optional_argument, 0, 'm'},
ba758825
CM
950 {"width", required_argument, 0, 'w'},
951 {"height", required_argument, 0, 'h'},
5a870f23 952 {"help", no_argument, 0, HELP_LONG_OPT},
ba758825
CM
953 {0, 0, 0, 0}
954};
955
956static void print_usage(void)
957{
958 fprintf(stderr, "iowatcher usage:\n"
959 "\t-d (--device): device for blktrace to trace\n"
960 "\t-t (--trace): trace file name (more than one allowed)\n"
961 "\t-l (--label): trace label in the graph\n"
962 "\t-o (--output): output file name (SVG only)\n"
963 "\t-p (--prog): program to run while blktrace is run\n"
abf08f96 964 "\t-p (--movie [=spindle|rect]): create IO animations\n"
ba758825
CM
965 "\t-r (--rolling): number of seconds in the rolling averge\n"
966 "\t-T (--title): graph title\n"
967 "\t-N (--no-graph): skip a single graph (io, tput, latency, queue_depth, iops)\n"
968 "\t-h (--height): set the height of each graph\n"
969 "\t-w (--width): set the width of each graph\n"
970 );
971 exit(1);
972}
973
974static int parse_options(int ac, char **av)
975{
976 int c;
977 int disabled = 0;
978
979 while (1) {
980 // int this_option_optind = optind ? optind : 1;
981 int option_index = 0;
982
983 c = getopt_long(ac, av, option_string,
984 long_options, &option_index);
985
986 if (c == -1)
987 break;
988
989 switch(c) {
990 case 'T':
991 graph_title = strdup(optarg);
992 break;
993 case 't':
994 add_trace_file(optarg);
995 set_blktrace_outfile(optarg);
996 break;
997 case 'o':
998 output_filename = strdup(optarg);
999 break;
1000 case 'l':
1001 set_trace_label(optarg);
1002 break;
1003 case 'r':
1004 set_rolling_avg(atoi(optarg));
1005 break;
1006 case 'O':
1007 if (!disabled) {
1008 disable_all_graphs();
1009 disabled = 1;
1010 }
1011 enable_one_graph(optarg);
1012 break;
1013 case 'N':
1014 disable_one_graph(optarg);
1015 break;
1016 case 'd':
1017 blktrace_device = strdup(optarg);
1018 break;
1019 case 'p':
1020 program_to_run = strdup(optarg);
1021 break;
1022 case 'm':
1023 make_movie = 1;
abf08f96
CM
1024 if (optarg) {
1025 movie_style = lookup_movie_style(optarg);
1026 if (movie_style < 0) {
1027 fprintf(stderr, "Unknown movie style %s\n", optarg);
1028 print_usage();
1029 }
1030 }
1031 fprintf(stderr, "Using movie style: %s\n",
1032 movie_styles[movie_style]);
ba758825
CM
1033 break;
1034 case 'h':
1035 opt_graph_height = atoi(optarg);
1036 break;
1037 case 'w':
1038 opt_graph_width = atoi(optarg);
1039 break;
1040 case '?':
1041 case HELP_LONG_OPT:
1042 print_usage();
1043 break;
1044 default:
1045 break;
1046 }
1047 }
1048 return 0;
1049}
1050
1051
9e066e23
CM
1052int main(int ac, char **av)
1053{
1054 struct plot *plot;
1055 int seconds = 0;
1056 u64 max_offset = 0;
9e066e23
CM
1057 struct trace_file *tf;
1058 int ret;
1059
1060 init_io_hash_table();
1061
1062 enable_all_graphs();
1063
1064 parse_options(ac, av);
1065
1066 last_active_graph = last_graph();
ba758825
CM
1067 if (make_movie) {
1068 set_io_graph_scale(256);
abf08f96
CM
1069 if (movie_style == MOVIE_SPINDLE)
1070 set_graph_size(550, 550);
1071 else
1072 set_graph_size(700, 400);
ba758825
CM
1073 }
1074 if (opt_graph_height)
1075 set_graph_height(opt_graph_height);
1076
1077 if (opt_graph_width)
1078 set_graph_width(opt_graph_height);
9e066e23
CM
1079
1080 if (list_empty(&all_traces)) {
1081 fprintf(stderr, "No traces found, exiting\n");
1082 exit(1);
1083 }
1084
1085 if (blktrace_device) {
1086 ret = start_blktrace(blktrace_device, blktrace_outfile,
1087 blktrace_dest_dir);
1088 if (ret) {
1089 fprintf(stderr, "exiting due to blktrace failure\n");
1090 exit(1);
1091 }
e199d546 1092 start_mpstat(blktrace_outfile);
9e066e23
CM
1093 if (program_to_run) {
1094 ret = run_program(program_to_run);
1095 if (ret) {
1096 fprintf(stderr, "failed to run %s\n",
1097 program_to_run);
1098 exit(1);
1099 }
1100 wait_for_tracers();
1101 blktrace_to_dump(blktrace_outfile);
1102 } else {
1103 /* no program specified, just wait for
1104 * blktrace to exit
1105 */
1106 wait_for_tracers();
1107 }
1108 }
1109
1110 /* step one, read all the traces */
1111 read_traces();
1112
1113 /* step two, find the maxes for time and offset */
1114 list_for_each_entry(tf, &all_traces, list)
1115 compare_max_tf(tf, &seconds, &max_offset);
1116
1117 /* push the max we found into all the tfs */
1118 set_all_max_tf(seconds, max_offset);
1119
1120 /* alloc graphing structs for all the traces */
1121 setup_trace_file_graphs();
1122
1123 /* run through all the traces and read their events */
1124 read_trace_events();
1125
ba758825
CM
1126 plot = alloc_plot();
1127
1128 if (make_movie) {
1129 plot_io_movie(plot);
1130 exit(0);
9e066e23
CM
1131 }
1132
ba758825 1133 set_plot_output(plot, output_filename);
9e066e23 1134
e199d546 1135 if (active_graphs[IO_GRAPH_INDEX] || found_mpstat)
9e066e23
CM
1136 set_legend_width(longest_label + strlen("writes"));
1137 else if (num_traces > 1)
1138 set_legend_width(longest_label);
1139 else
1140 set_legend_width(0);
1141
e4df8968 1142 set_plot_title(plot, graph_title);
9e066e23
CM
1143 plot_io(plot, seconds, max_offset);
1144 plot_tput(plot, seconds);
e199d546
CM
1145 plot_cpu(plot, seconds, "CPU IO Wait Time",
1146 CPU_IO_GRAPH_INDEX, MPSTAT_IO);
1147 plot_cpu(plot, seconds, "CPU System Time",
1148 CPU_SYS_GRAPH_INDEX, MPSTAT_SYS);
1149 plot_cpu(plot, seconds, "CPU IRQ Time",
1150 CPU_IRQ_GRAPH_INDEX, MPSTAT_IRQ);
1151 plot_cpu(plot, seconds, "CPU SoftIRQ Time",
1152 CPU_SOFT_GRAPH_INDEX, MPSTAT_SOFT);
1153 plot_cpu(plot, seconds, "CPU User Time",
1154 CPU_USER_GRAPH_INDEX, MPSTAT_USER);
1155
9e066e23
CM
1156 plot_latency(plot, seconds);
1157 plot_queue_depth(plot, seconds);
1158 plot_iops(plot, seconds);
1159
1160 /* once for all */
1161 close_plot(plot);
abf08f96 1162 close_plot_file(plot);
9e066e23
CM
1163 return 0;
1164}