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