iowatcher: Add a new movie mode that maps the IOs onto a platter.
[blktrace.git] / iowatcher / plot.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
34#include "plot.h"
35
ba758825 36static int io_graph_scale = 8;
abf08f96
CM
37static int graph_width = 700;
38static int graph_height = 250;
39static int graph_circle_extra = 30;
ba758825
CM
40static int graph_inner_x_margin = 2;
41static int graph_inner_y_margin = 2;
9e066e23
CM
42static int graph_tick_len = 5;
43static int graph_left_pad = 120;
44static int tick_label_pad = 16;
45static int tick_font_size = 15;
46static char *font_family = "sans-serif";
47
48/* this is the title for the whole page */
49static int plot_title_height = 50;
50static int plot_title_font_size = 25;
51
52/* this is the label at the top of each plot */
53static int plot_label_height = 60;
54static int plot_label_font_size = 20;
55
56/* label for each axis is slightly smaller */
57static int axis_label_font_size = 16;
58
59int legend_x_off = 45;
60int legend_y_off = -10;
61int legend_font_size = 15;
62int legend_width = 80;
63
e199d546
CM
64static int rolling_avg_secs = 0;
65
9e066e23
CM
66static int line_len = 1024;
67static char line[1024];
68
abf08f96
CM
69static int final_height = 0;
70static int final_width = 0;
71
9e066e23
CM
72struct graph_line_data *alloc_line_data(int seconds, int stop_seconds)
73{
74 int size = sizeof(struct graph_line_data) + (stop_seconds + 1) * sizeof(struct graph_line_pair);
75 struct graph_line_data *gld;
76
77 gld = calloc(1, size);
78 if (!gld) {
79 fprintf(stderr, "Unable to allocate memory for graph data\n");
80 exit(1);
81 }
82 gld->seconds = seconds;
83 gld->stop_seconds = stop_seconds;
84 return gld;
85}
86
87void free_line_data(struct graph_line_data *gld)
88{
89 free(gld->label);
90 free(gld);
91}
92
93struct graph_dot_data *alloc_dot_data(int seconds, u64 max_offset, int stop_seconds)
94{
95 int size;
96 int arr_size;
ba758825 97 int rows = graph_height * io_graph_scale;
9e066e23
CM
98 int cols = graph_width;
99 struct graph_dot_data *gdd;
100
101 size = sizeof(struct graph_dot_data);
102
103 /* the number of bits */
104 arr_size = (rows + 1) * cols;
105
106 /* the number of bytes */
107 arr_size /= 8;
108
109 gdd = calloc(1, size + arr_size);
110 if (!gdd) {
111 fprintf(stderr, "Unable to allocate memory for graph data\n");
112 exit(1);
113 }
114 gdd->seconds = seconds;
115 gdd->stop_seconds = stop_seconds;
116 gdd->rows = rows;
117 gdd->cols = cols;
118 gdd->max_offset = max_offset;
119 return gdd;
120}
121
122void free_dot_data(struct graph_dot_data *gdd)
123{
124 free(gdd);
125}
126
ba758825 127void set_gdd_bit(struct graph_dot_data *gdd, u64 offset, double bytes, double time)
9e066e23
CM
128{
129 double bytes_per_row = (double)gdd->max_offset / gdd->rows;
ba758825 130
9e066e23
CM
131 double secs_per_col = (double)gdd->seconds / gdd->cols;
132 double col;
133 double row;
134 int col_int;
135 int row_int;
136 int bit_index;
137 int arr_index;
138 int bit_mod;
ba758825 139 double mod = bytes_per_row;
9e066e23
CM
140
141 if (offset > gdd->max_offset)
142 return;
143
144 gdd->total_ios++;
ba758825 145 time = time / 1000000000.0;
9e066e23 146 while (bytes > 0) {
9e066e23
CM
147 row = (double)offset / bytes_per_row;
148 col = time / secs_per_col;
149
150 col_int = floor(col);
151 row_int = floor(row);
9e066e23
CM
152 bit_index = row_int * gdd->cols + col_int;
153 arr_index = bit_index / 8;
154 bit_mod = bit_index % 8;
155
156 gdd->data[arr_index] |= 1 << bit_mod;
157 offset += mod;
158 bytes -= mod;
159 }
160}
161
162void print_gdd(struct graph_dot_data *gdd)
163{
164 int col = 0;
165 int row = 0;
166 int arr_index;
167 u64 val;
168 int bit_index;
169 int bit_mod;
170
171 for (row = gdd->rows - 1; row >= 0; row--) {
172 for (col = 0; col < gdd->cols; col++) {
173 bit_index = row * gdd->cols + col;
174 arr_index = bit_index / sizeof(unsigned long);
175 bit_mod = bit_index % sizeof(unsigned long);
176
177 val = gdd->data[arr_index];
178 if (val & (1 << bit_mod))
179 printf("*");
180 else
181 printf(" ");
182 }
183 printf("\n");
184 }
185}
186
187static double rolling_avg(struct graph_line_pair *data, int index, int distance)
188{
189 double sum = 0;
190 int start;
191
192 if (distance < 0)
193 distance = 1;
194 if (distance > index) {
195 start = 0;
196 } else {
197 start = index - distance;
198 }
199 distance = 0;
200 while (start <= index) {
201 double avg;
202
203 if (data[start].count)
204 avg = ((double)data[start].sum) / data[start].count;
205 else
206 avg= 0;
207
208 sum += avg;
209 distance++;
210 start++;
211 }
212 return sum / distance;
213}
214
215void write_svg_header(int fd)
216{
abf08f96
CM
217 char *spaces = " \n";
218 char *header = "<svg xmlns=\"http://www.w3.org/2000/svg\">\n";
9e066e23
CM
219 char *filter1 ="<filter id=\"shadow\">\n "
220 "<feOffset result=\"offOut\" in=\"SourceAlpha\" dx=\"4\" dy=\"4\" />\n "
221 "<feGaussianBlur result=\"blurOut\" in=\"offOut\" stdDeviation=\"2\" />\n "
222 "<feBlend in=\"SourceGraphic\" in2=\"blurOut\" mode=\"normal\" />\n "
223 "</filter>\n";
224 char *filter2 ="<filter id=\"textshadow\" x=\"0\" y=\"0\" width=\"200%\" height=\"200%\">\n "
225 "<feOffset result=\"offOut\" in=\"SourceAlpha\" dx=\"1\" dy=\"1\" />\n "
226 "<feGaussianBlur result=\"blurOut\" in=\"offOut\" stdDeviation=\"1.5\" />\n "
227 "<feBlend in=\"SourceGraphic\" in2=\"blurOut\" mode=\"normal\" />\n "
228 "</filter>\n";
229 char *filter3 ="<filter id=\"labelshadow\" x=\"0\" y=\"0\" width=\"200%\" height=\"200%\">\n "
230 "<feOffset result=\"offOut\" in=\"SourceGraphic\" dx=\"3\" dy=\"3\" />\n "
231 "<feColorMatrix result=\"matrixOut\" in=\"offOut\" type=\"matrix\" "
232 "values=\"0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0\" /> "
233 "<feGaussianBlur result=\"blurOut\" in=\"offOut\" stdDeviation=\"2\" />\n "
234 "<feBlend in=\"SourceGraphic\" in2=\"blurOut\" mode=\"normal\" />\n "
235 "</filter>\n";
236 char *defs_start = "<defs>\n";
237 char *defs_close = "</defs>\n";
abf08f96
CM
238 final_width = 0;
239 final_height = 0;
9e066e23
CM
240
241 write(fd, header, strlen(header));
abf08f96
CM
242 /* write a bunch of spaces so we can stuff in the width and height later */
243 write(fd, spaces, strlen(spaces));
244
9e066e23
CM
245 write(fd, defs_start, strlen(defs_start));
246 write(fd, filter1, strlen(filter1));
247 write(fd, filter2, strlen(filter2));
248 write(fd, filter3, strlen(filter3));
249 write(fd, defs_close, strlen(defs_close));
250}
251
252void write_drop_shadow(struct plot *plot)
253{
254 snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"white\"/>\n",
255 plot->start_y_offset, plot->total_width, 45);
256 write(plot->fd, line, strlen(line));
257
258 snprintf(line, line_len, "<path d=\"M %d %d h %d v %d h %d t %d %d V %d H %d Z\" "
259 "fill=\"white\" filter=\"url(#shadow)\"/>",
260 0, plot->start_y_offset,
261 plot->total_width - graph_left_pad / 2,
262 -plot->total_height, 24, 1, 1,
263 plot->start_y_offset + 10, 0);
264 write(plot->fd, line, strlen(line));
265
266 snprintf(line, line_len, "<path d=\"M %d %d H %d V %d h %d V %d H %d Z\" "
267 "fill=\"white\"/>",
268 0, plot->start_y_offset - 15, /* start */
269 plot->total_width - graph_left_pad / 2 - 10, /* hline over */
270 plot->start_y_offset - plot->total_height, /* vline up */
271 15, /*hline over */
272 plot->start_y_offset, /* vline back down */
273 0);
274 write(plot->fd, line, strlen(line));
275
276 plot->start_y_offset += 45;
277}
278
279/* svg y offset for the traditional 0,0 (bottom left corner) of the plot */
280static int axis_y(void)
281{
ba758825 282 return plot_label_height + graph_height + graph_inner_y_margin;
9e066e23
CM
283}
284
285/* this gives you the correct pixel for a given offset from the bottom left y axis */
ba758825 286static double axis_y_off_double(double y)
9e066e23
CM
287{
288 return plot_label_height + graph_height - y;
289}
290
ba758825
CM
291static int axis_y_off(int y)
292{
293 return axis_y_off_double(y);
294}
295
9e066e23
CM
296/* svg x axis offset from 0 */
297static int axis_x(void)
298{
299 return graph_left_pad;
300}
301
302/* the correct pixel for a given X offset */
ba758825
CM
303static double axis_x_off_double(double x)
304{
305 return graph_left_pad + graph_inner_x_margin + x;
306}
307
9e066e23
CM
308static int axis_x_off(int x)
309{
ba758825 310 return (int)axis_x_off_double(x);
9e066e23
CM
311}
312
ba758825 313
9e066e23
CM
314/*
315 * this draws a backing rectangle for the plot and it
316 * also creates a new svg element so our offsets can
317 * be relative to this one plot.
318 */
319void setup_axis(struct plot *plot)
320{
321 int ret;
322 int len;
323 int fd = plot->fd;
324 int bump_height = tick_font_size * 3 + axis_label_font_size;
325
9e066e23
CM
326 plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + legend_width;
327 plot->total_height = axis_y() + tick_label_pad + tick_font_size;
328
329 if (plot->add_xlabel)
330 plot->total_height += bump_height;
331
332 /* backing rect */
333 snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" "
334 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
335 plot->start_y_offset, plot->total_width + 40,
336 plot->total_height + 20);
337 len = strlen(line);
338 write(fd, line, len);
abf08f96 339
9e066e23
CM
340 snprintf(line, line_len, "<rect x=\"15\" y=\"%d\" width=\"%d\" "
341 "filter=\"url(#shadow)\" "
342 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
343 plot->start_y_offset, plot->total_width, plot->total_height);
344 len = strlen(line);
345 write(fd, line, len);
346 plot->total_height += 20;
347
abf08f96
CM
348 if (plot->total_height + plot->start_y_offset > final_height)
349 final_height = plot->total_height + plot->start_y_offset;
350 if (plot->total_width + 40 > final_width)
351 final_width = plot->total_width + 40;
9e066e23
CM
352
353 /* create an svg object for all our coords to be relative against */
abf08f96 354 snprintf(line, line_len, "<svg x=\"%d\" y=\"%d\">\n", plot->start_x_offset, plot->start_y_offset);
9e066e23
CM
355 write(fd, line, strlen(line));
356
357 snprintf(line, 1024, "<path d=\"M%d %d h %d V %d H %d Z\" stroke=\"black\" stroke-width=\"2\" fill=\"none\"/>\n",
358 axis_x(), axis_y(),
ba758825 359 graph_width + graph_inner_x_margin * 2, axis_y_off(graph_height) - graph_inner_y_margin,
9e066e23
CM
360 axis_x());
361 len = strlen(line);
362 ret = write(fd, line, len);
363 if (ret != len) {
364 fprintf(stderr, "failed to write svg axis\n");
365 exit(1);
366 }
367}
368
abf08f96
CM
369/*
370 * this draws a backing rectangle for the plot and it
371 * also creates a new svg element so our offsets can
372 * be relative to this one plot.
373 */
374void setup_axis_spindle(struct plot *plot)
375{
376 int len;
377 int fd = plot->fd;
378 int bump_height = tick_font_size * 3 + axis_label_font_size;
379
380 plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + legend_width;
381 plot->total_height = axis_y() + tick_label_pad + tick_font_size;
382
383 if (plot->add_xlabel)
384 plot->total_height += bump_height;
385
386 /* backing rect */
387 snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" "
388 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
389 plot->start_y_offset, plot->total_width + 40,
390 plot->total_height + 20);
391 len = strlen(line);
392 write(fd, line, len);
393
394 snprintf(line, line_len, "<rect x=\"15\" y=\"%d\" width=\"%d\" "
395 "filter=\"url(#shadow)\" "
396 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
397 plot->start_y_offset, plot->total_width, plot->total_height);
398 len = strlen(line);
399 write(fd, line, len);
400 plot->total_height += 20;
401
402 if (plot->total_height + plot->start_y_offset > final_height)
403 final_height = plot->total_height + plot->start_y_offset;
404 if (plot->total_width + 40 > final_width)
405 final_width = plot->total_width + 40;
406
407 /* create an svg object for all our coords to be relative against */
408 snprintf(line, line_len, "<svg x=\"%d\" y=\"%d\">\n", plot->start_x_offset, plot->start_y_offset);
409 write(fd, line, strlen(line));
410
411}
412
9e066e23
CM
413/* draw a plot title. This should be done only once,
414 * and it bumps the plot width/height numbers by
415 * what it draws.
416 *
417 * Call this before setting up the first axis
418 */
419void set_plot_title(struct plot *plot, char *title)
420{
421 int len;
422 int fd = plot->fd;
423
424 plot->total_height = plot_title_height;
425 plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + legend_width;
426
427 /* backing rect */
428 snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"white\" stroke=\"none\"/>",
429 plot->start_y_offset, plot->total_width + 40, plot_title_height + 20);
430 len = strlen(line);
431 write(fd, line, len);
432
433 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
434 "font-weight=\"bold\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
435 axis_x_off(graph_width / 2),
436 plot->start_y_offset + plot_title_height / 2,
437 font_family, plot_title_font_size, "middle", title);
438 plot->start_y_offset += plot_title_height;
439 len = strlen(line);
440 write(fd, line, len);
441}
442
443/*
444 * create evenly spread out ticks along the xaxis. if tick only is set
445 * this just makes the ticks, otherwise it labels each tick as it goes
446 */
447void set_xticks(struct plot *plot, int num_ticks, int first, int last)
448{
449 int pixels_per_tick = graph_width / num_ticks;
450 int step = (last - first) / num_ticks;
451 int i;
ba758825 452 int tick_y = axis_y_off(graph_tick_len) + graph_inner_y_margin;
9e066e23
CM
453 int tick_x = axis_x();
454 int tick_only = plot->add_xlabel == 0;
455
456 int text_y = axis_y() + tick_label_pad;
457
458 char *middle = "middle";
459 char *start = "start";
460
461 for (i = 0; i < num_ticks; i++) {
462 char *anchor;
463 if (i != 0) {
464 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"2\" height=\"%d\" style=\"stroke:none;fill:black;\"/>\n",
465 tick_x, tick_y, graph_tick_len);
466 write(plot->fd, line, strlen(line));
467 anchor = middle;
468 } else {
469 anchor = start;
470 }
471
472 if (!tick_only) {
473 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
474 "fill=\"black\" style=\"text-anchor: %s\">%d</text>\n",
475 tick_x, text_y, font_family, tick_font_size, anchor, step * i);
476 write(plot->fd, line, strlen(line));
477 }
478 tick_x += pixels_per_tick;
479 }
480
481 if (!tick_only) {
482 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
483 "fill=\"black\" style=\"text-anchor: middle\">%d</text>\n",
484 axis_x_off(graph_width - 2),
485 text_y, font_family, tick_font_size, last);
486 write(plot->fd, line, strlen(line));
487 }
488}
489
490void set_ylabel(struct plot *plot, char *label)
491{
492 int len;
493 int fd = plot->fd;
494
495 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" "
496 "transform=\"rotate(-90 %d %d)\" font-weight=\"bold\" "
497 "font-size=\"%d\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
498 graph_left_pad / 2 - axis_label_font_size,
499 axis_y_off(graph_height / 2),
500 font_family,
501 graph_left_pad / 2 - axis_label_font_size,
ba758825 502 (int)axis_y_off(graph_height / 2),
9e066e23
CM
503 axis_label_font_size, "middle", label);
504 len = strlen(line);
505 write(fd, line, len);
506}
507
508void set_xlabel(struct plot *plot, char *label)
509{
510 int len;
511 int fd = plot->fd;
512 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" "
513 "font-weight=\"bold\" "
514 "font-size=\"%d\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
515 axis_x_off(graph_width / 2),
516 axis_y() + tick_font_size * 3 + axis_label_font_size / 2,
517 font_family,
518 axis_label_font_size, "middle", label);
519 len = strlen(line);
520 write(fd, line, len);
521
522}
523
524/*
525 * create evenly spread out ticks along the y axis.
526 * The ticks are labeled as it goes
527 */
528void set_yticks(struct plot *plot, int num_ticks, int first, int last, char *units)
529{
530 int pixels_per_tick = graph_height / num_ticks;
531 int step = (last - first) / num_ticks;
532 int i;
533 int tick_y = 0;
534 int text_x = axis_x() - 6;
535 int tick_x = axis_x();
536 char *anchor = "end";
537
538 for (i = 0; i < num_ticks; i++) {
539 if (i != 0) {
540 snprintf(line, line_len, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" "
541 "style=\"stroke:lightgray;stroke-width:2;stroke-dasharray:9,12;\"/>\n",
542 tick_x, axis_y_off(tick_y),
543 axis_x_off(graph_width), axis_y_off(tick_y));
544 write(plot->fd, line, strlen(line));
545 }
546
547 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
548 "fill=\"black\" style=\"text-anchor: %s\">%d%s</text>\n",
549 text_x,
550 axis_y_off(tick_y - tick_font_size / 2),
551 font_family, tick_font_size, anchor, step * i, units);
552 write(plot->fd, line, strlen(line));
553 tick_y += pixels_per_tick;
554 }
555 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
556 "fill=\"black\" style=\"text-anchor: %s\">%d%s</text>\n",
557 text_x, axis_y_off(graph_height), font_family, tick_font_size, anchor, last, units);
558 write(plot->fd, line, strlen(line));
559}
560
561void set_plot_label(struct plot *plot, char *label)
562{
563 int len;
564 int fd = plot->fd;
565
566 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" "
567 "font-size=\"%d\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
568 axis_x() + graph_width / 2,
569 plot_label_height / 2,
570 font_family, plot_label_font_size, "middle", label);
571 len = strlen(line);
572 write(fd, line, len);
573}
574
575static void close_svg(int fd)
576{
577 char *close_line = "</svg>\n";
578
579 write(fd, close_line, strlen(close_line));
580}
581
582int close_plot(struct plot *plot)
583{
584 close_svg(plot->fd);
585 plot->start_y_offset += plot->total_height;
586 plot->add_xlabel = 0;
587 return 0;
588}
589
ba758825 590struct plot *alloc_plot(void)
9e066e23
CM
591{
592 struct plot *plot;
593 plot = calloc(1, sizeof(*plot));
594 if (!plot) {
595 fprintf(stderr, "Unable to allocate memory %s\n", strerror(errno));
596 exit(1);
597 }
ba758825 598 plot->fd = 0;
9e066e23
CM
599 return plot;
600}
601
abf08f96
CM
602int close_plot_file(struct plot *plot)
603{
604 int ret;
605 ret = lseek(plot->fd, 0, SEEK_SET);
606 if (ret == (off_t)-1) {
607 perror("seek");
608 exit(1);
609 }
610 final_width = ((final_width + 1) / 2) * 2;
611 final_height = ((final_height + 1) / 2) * 2;
612 snprintf(line, line_len, "<svg xmlns=\"http://www.w3.org/2000/svg\" "
613 "width=\"%d\" height=\"%d\">\n",
614 final_width, final_height);
615 write(plot->fd, line, strlen(line));
616 close(plot->fd);
617 plot->fd = 0;
618 return 0;
619}
620
ba758825
CM
621void set_plot_output(struct plot *plot, char *filename)
622{
623 int fd;
624
625 if (plot->fd)
abf08f96 626 close_plot_file(plot);
ba758825
CM
627 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
628 if (fd < 0) {
629 fprintf(stderr, "Unable to open output file %s err %s\n", filename, strerror(errno));
630 exit(1);
631 }
632 plot->fd = fd;
633 plot->start_y_offset = plot->start_x_offset = 0;
634 write_svg_header(fd);
635}
636
9e066e23
CM
637char *byte_unit_names[] = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", "unobtainium" };
638int MAX_BYTE_UNIT_SCALE = 9;
639
640char *time_unit_names[] = { "n", "u", "m", "s" };
641int MAX_TIME_UNIT_SCALE = 3;
642
643void scale_line_graph_bytes(u64 *max, char **units, u64 factor)
644{
645 int scale = 0;
646 u64 val = *max;
647 u64 div = 1;
648 while (val > factor * 64) {
649 val /= factor;
650 scale++;
651 div *= factor;
652 }
653 *units = byte_unit_names[scale];
654 if (scale == 0)
655 return;
656
657 if (scale > MAX_BYTE_UNIT_SCALE)
658 scale = MAX_BYTE_UNIT_SCALE;
659
660 *max /= div;
661}
662
663void scale_line_graph_time(u64 *max, char **units)
664{
665 int scale = 0;
666 u64 val = *max;
667 u64 div = 1;
668 while (val > 1000 * 10) {
669 val /= 1000;
670 scale++;
671 div *= 1000;
672 if (scale == MAX_TIME_UNIT_SCALE)
673 break;
674 }
675 *units = time_unit_names[scale];
676 if (scale == 0)
677 return;
678
679 *max /= div;
680}
681
682int svg_line_graph(struct plot *plot, struct graph_line_data *gld, char *color)
683{
684 int i;
685 double val;
686 int rolling;
687 int fd = plot->fd;
688 char *start = "<path d=\"";
689 double yscale = ((double)gld->max) / graph_height;
690 double xscale = (double)(gld->seconds - 1) / graph_width;
691 char c = 'M';
692 double x;
693
694 if (rolling_avg_secs)
695 rolling = rolling_avg_secs;
696 else
697 rolling = gld->stop_seconds / 25;
698
699 write(fd, start, strlen(start));
700 for (i = 0; i < gld->stop_seconds; i++) {
701 val = rolling_avg(gld->data, i, rolling);
702 val = val / yscale;
703 x = (double)i / xscale;
704 snprintf(line, line_len, "%c %d %d ", c, axis_x_off(x), axis_y_off(val));
705
706 c = 'L';
707 write(fd, line, strlen(line));
708 }
709 snprintf(line, line_len, "\" fill=\"none\" stroke=\"%s\" stroke-width=\"2\"/>\n", color);
710 write(fd, line, strlen(line));
711
712 return 0;
713}
714
ba758825 715void svg_write_time_line(struct plot *plot, int col)
9e066e23 716{
ba758825
CM
717 snprintf(line, line_len, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" "
718 "style=\"stroke:black;stroke-width:2;\"/>\n",
719 axis_x_off(col), axis_y_off(0),
720 axis_x_off(col), axis_y_off(graph_height));
721 write(plot->fd, line, strlen(line));
722}
723
abf08f96 724static int svg_add_io(int fd, double row, double col, double width, double height, char *color)
ba758825
CM
725{
726 float rx = 0;
727
728 snprintf(line, line_len, "<rect x=\"%.2f\" y=\"%.2f\" width=\"%.1f\" height=\"%.1f\" "
abf08f96
CM
729 "rx=\"%.2f\" style=\"stroke:none;fill:%s;stroke-width:0\"/>\n",
730 axis_x_off_double(col), axis_y_off_double(row), width, height, rx, color);
9e066e23
CM
731 return write(fd, line, strlen(line));
732}
733
abf08f96 734int svg_io_graph_movie_array(struct plot *plot, struct plot_history *ph)
ba758825
CM
735{
736 double cell_index;
737 double movie_row;
738 double movie_col;
739 int i;
740
741 for (i = 0; i < ph->num_used; i++) {
742 cell_index = ph->history[i];
743 movie_row = floor(cell_index / graph_width);
744 movie_col = cell_index - movie_row * graph_width;
abf08f96
CM
745 svg_add_io(plot->fd, movie_row, movie_col, 4, 4, ph->color);
746 }
747 return 0;
748}
749
750static float spindle_steps = 0;
751
752void rewind_spindle_steps(int num)
753{
754 spindle_steps -= num * 0.01;
755}
756
757int svg_io_graph_movie_array_spindle(struct plot *plot, struct plot_history *ph)
758{
759 double cell_index;
760 int i;
761 int num_circles = 0;
762 double cells_per_circle;
763 double circle_num;
764 double degrees_per_cell;
765 double rot;
766 double center_x;
767 double center_y;
768 double graph_width_extra = graph_width + graph_circle_extra;
769 double graph_height_extra = graph_height + graph_circle_extra;
770 double radius;;
771
772 if (graph_width_extra > graph_height_extra)
773 graph_width_extra = graph_height_extra;
774
775 if (graph_width_extra < graph_height_extra)
776 graph_height_extra = graph_width_extra;
777
778 radius = graph_width_extra;
779
780 center_x = axis_x_off_double(graph_width_extra / 2);
781 center_y = axis_y_off_double(graph_height_extra / 2);
782
783 snprintf(line, line_len, "<g transform=\"rotate(%.4f, %.2f, %.2f)\"> "
784 "<circle cx=\"%.2f\" cy=\"%.2f\" "
785 "stroke=\"black\" stroke-width=\"6\" "
786 "r=\"%.2f\" fill=\"none\"/>\n",
787 -spindle_steps * 1.2, center_x, center_y, center_x, center_y, graph_width_extra / 2);
788 write(plot->fd, line, strlen(line));
789 snprintf(line, line_len, "<circle cx=\"%.2f\" cy=\"%.2f\" "
790 "stroke=\"none\" fill=\"red\" r=\"%.2f\"/>\n</g>\n",
791 axis_x_off_double(graph_width_extra), center_y, 4.5);
792 write(plot->fd, line, strlen(line));
793 spindle_steps += 0.01;
794
795 radius = floor(radius / 2);
796 num_circles = radius / 4 - 3;
797 cells_per_circle = ph->history_max / num_circles;
798 degrees_per_cell = 360 / cells_per_circle;
799
800 for (i = 0; i < ph->num_used; i++) {
801 cell_index = ph->history[i];
802 circle_num = floor(cell_index / cells_per_circle);
803 rot = cell_index - circle_num * cells_per_circle;
804 circle_num = num_circles - circle_num;
805 radius = circle_num * 4;
806
807 rot = rot * degrees_per_cell;
808 rot -= spindle_steps;
809 snprintf(line, line_len, "<path transform=\"rotate(%.4f, %.2f, %.2f)\" "
810 "d=\"M %.2f %.2f a %.2f %.2f 0 0 1 0 5\" "
811 "stroke=\"%s\" stroke-width=\"4\"/>\n",
812 rot, center_x, center_y,
813 axis_x_off_double(graph_width_extra / 2 + radius) + 8, center_y,
814 radius, radius, ph->color);
815
816 write(plot->fd, line, strlen(line));
ba758825
CM
817 }
818 return 0;
819}
820
821static int add_plot_history(struct plot_history *ph, double val)
822{
823 if (ph->num_used == ph->history_len) {
824 ph->history = realloc(ph->history,
825 (ph->history_len + 4096) * sizeof(double));
826 if (!ph->history) {
827 perror("Unable to allocate memory");
828 exit(1);
829 }
830 ph->history_len += 4096;
831 }
832 ph->history[ph->num_used++] = val;
833 return 0;
834}
835
836int svg_io_graph_movie(struct graph_dot_data *gdd, struct plot_history *ph, int col)
837{
838 int row = 0;
839 int arr_index;
840 unsigned char val;
841 int bit_index;
842 int bit_mod;
843 double blocks_per_row = gdd->max_offset / gdd->rows;
844 double movie_blocks_per_cell = gdd->max_offset / (graph_width * graph_height);
845 double cell_index;
846 int margin_orig = graph_inner_y_margin;
847
848 graph_inner_y_margin += 5;
abf08f96 849 ph->history_max = gdd->max_offset / movie_blocks_per_cell;
ba758825
CM
850
851 for (row = gdd->rows - 1; row >= 0; row--) {
852 bit_index = row * gdd->cols + col;
853 arr_index = bit_index / 8;
854 bit_mod = bit_index % 8;
855
856 if (arr_index < 0)
857 continue;
858 val = gdd->data[arr_index];
859 if (val & (1 << bit_mod)) {
860 /* in bytes, linear offset from the start of the drive */
861 cell_index = (double)row * blocks_per_row;
862
863 /* a cell number in the graph */
864 cell_index /= movie_blocks_per_cell;
865
866 add_plot_history(ph, cell_index);
867 }
868 }
869 graph_inner_y_margin = margin_orig;
870 return 0;
871}
872
9e066e23
CM
873int svg_io_graph(struct plot *plot, struct graph_dot_data *gdd, char *color)
874{
875 int fd = plot->fd;;
876 int col = 0;
877 int row = 0;
878 int arr_index;
879 unsigned char val;
880 int bit_index;
881 int bit_mod;
882
883 for (row = gdd->rows - 1; row >= 0; row--) {
884 for (col = 0; col < gdd->cols; col++) {
885 bit_index = row * gdd->cols + col;
886 arr_index = bit_index / 8;
887 bit_mod = bit_index % 8;
888
889 if (arr_index < 0)
890 continue;
891 val = gdd->data[arr_index];
892 if (val & (1 << bit_mod))
abf08f96 893 svg_add_io(fd, floor(row / io_graph_scale), col, 1.5, 1.5, color);
9e066e23
CM
894 }
895 }
896 return 0;
897}
898
899void svg_alloc_legend(struct plot *plot, int num_lines)
900{
901 char **lines = calloc(num_lines, sizeof(char *));
902 plot->legend_index = 0;
903 plot->legend_lines = lines;
904 plot->num_legend_lines = num_lines;
905}
906
e199d546 907void svg_free_legend(struct plot *plot)
9e066e23
CM
908{
909 int i;
e199d546
CM
910 for (i = 0; i < plot->legend_index; i++)
911 free(plot->legend_lines[i]);
912 free(plot->legend_lines);
913 plot->legend_lines = NULL;
914 plot->legend_index = 0;
915}
916
917void svg_write_legend(struct plot *plot)
918{
9e066e23
CM
919 int legend_line_x = axis_x_off(graph_width) + legend_x_off;
920 int legend_line_y = axis_y_off(graph_height) + legend_y_off;
e199d546 921 int i;
9e066e23
CM
922
923 if (plot->legend_index == 0)
924 return;
925
926 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "
927 "fill=\"white\" filter=\"url(#shadow)\"/>\n",
928 legend_line_x - 15,
929 legend_line_y - 12,
930 legend_width,
931 plot->legend_index * legend_font_size + legend_font_size / 2 + 12);
932
933 write(plot->fd, line, strlen(line));
934 for (i = 0; i < plot->legend_index; i++) {
935 write(plot->fd, plot->legend_lines[i],
936 strlen(plot->legend_lines[i]));
937 free(plot->legend_lines[i]);
938 }
939 free(plot->legend_lines);
e199d546 940 plot->legend_lines = NULL;
9e066e23 941 plot->legend_index = 0;
9e066e23
CM
942}
943
944void svg_add_legend(struct plot *plot, char *text, char *extra, char *color)
945{
946 int legend_line_x = axis_x_off(graph_width) + legend_x_off;
947 int legend_line_y = axis_y_off(graph_height) + legend_y_off;
948
949 if (!text && (!extra || strlen(extra) == 0))
950 return;
951
952 legend_line_y += plot->legend_index * legend_font_size + legend_font_size / 2;
953 snprintf(line, line_len, "<path d=\"M %d %d h 8\" stroke=\"%s\" stroke-width=\"8\" "
954 "filter=\"url(#labelshadow)\"/> "
955 "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
956 "fill=\"black\" style=\"text-anchor: left\">%s%s</text>\n",
957 legend_line_x, legend_line_y,
958 color, legend_line_x + 13,
959 legend_line_y + 4, font_family, legend_font_size,
960 text, extra);
961
962 plot->legend_lines[plot->legend_index++] = strdup(line);
963}
964
965void set_legend_width(int longest_str)
966{
967 if (longest_str)
968 legend_width = longest_str * (legend_font_size * 3 / 4) + 25;
969 else
970 legend_width = 0;
971}
972
973void set_rolling_avg(int rolling)
974{
975 rolling_avg_secs = rolling;
976}
977
ba758825
CM
978void set_io_graph_scale(int scale)
979{
980 io_graph_scale = scale;
981}
982
983void set_graph_size(int width, int height)
984{
985 graph_width = width;
986 graph_height = height;
987}
988
989void get_graph_size(int *width, int *height)
990{
991 *width = graph_width;
992 *height = graph_height;
993}
994
995void set_graph_height(int h)
996{
997 graph_height = h;
998}
999void set_graph_width(int w)
1000{
1001 graph_width = w;
1002}