2 * Copyright (C) 2012 Fusion-io
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.
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.
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
17 * Parts of this file were imported from Jens Axboe's blktrace sources (also GPL)
19 #include <sys/types.h>
28 #include <asm/types.h>
36 static int io_graph_scale = 8;
37 static int graph_width = 700;
38 static int graph_height = 250;
39 static int graph_circle_extra = 30;
40 static int graph_inner_x_margin = 2;
41 static int graph_inner_y_margin = 2;
42 static int graph_tick_len = 5;
43 static int graph_left_pad = 120;
44 static int tick_label_pad = 16;
45 static int tick_font_size = 15;
46 static char *font_family = "sans-serif";
48 /* this is the title for the whole page */
49 static int plot_title_height = 50;
50 static int plot_title_font_size = 25;
52 /* this is the label at the top of each plot */
53 static int plot_label_height = 60;
54 static int plot_label_font_size = 20;
56 /* label for each axis is slightly smaller */
57 static int axis_label_font_size = 16;
59 int legend_x_off = 45;
60 int legend_y_off = -10;
61 int legend_font_size = 15;
62 int legend_width = 80;
64 static int rolling_avg_secs = 0;
66 static int line_len = 1024;
67 static char line[1024];
69 static int final_height = 0;
70 static int final_width = 0;
72 struct graph_line_data *alloc_line_data(int min_seconds, int max_seconds, int stop_seconds)
74 int size = sizeof(struct graph_line_data) + (stop_seconds + 1) * sizeof(struct graph_line_pair);
75 struct graph_line_data *gld;
77 gld = calloc(1, size);
79 fprintf(stderr, "Unable to allocate memory for graph data\n");
82 gld->min_seconds = min_seconds;
83 gld->max_seconds = max_seconds;
84 gld->stop_seconds = stop_seconds;
88 void free_line_data(struct graph_line_data *gld)
94 struct graph_dot_data *alloc_dot_data(int min_seconds, int max_seconds, u64 min_offset, u64 max_offset, int stop_seconds)
98 int rows = graph_height * io_graph_scale;
99 int cols = graph_width;
100 struct graph_dot_data *gdd;
102 size = sizeof(struct graph_dot_data);
104 /* the number of bits */
105 arr_size = (rows + 1) * cols;
107 /* the number of bytes */
110 gdd = calloc(1, size + arr_size);
112 fprintf(stderr, "Unable to allocate memory for graph data\n");
115 gdd->min_seconds = min_seconds;
116 gdd->max_seconds = max_seconds;
117 gdd->stop_seconds = stop_seconds;
120 gdd->min_offset = min_offset;
121 gdd->max_offset = max_offset;
125 void free_dot_data(struct graph_dot_data *gdd)
130 void set_gdd_bit(struct graph_dot_data *gdd, u64 offset, double bytes, double time)
132 double bytes_per_row = (double)(gdd->max_offset - gdd->min_offset + 1) / gdd->rows;
133 double secs_per_col = (double)(gdd->max_seconds - gdd->min_seconds) / gdd->cols;
141 double mod = bytes_per_row;
143 if (offset > gdd->max_offset || offset < gdd->min_offset)
147 time = time / 1000000000.0;
149 row = (double)(offset - gdd->min_offset) / bytes_per_row;
150 col = (time - gdd->min_seconds) / secs_per_col;
152 col_int = floor(col);
153 row_int = floor(row);
154 bit_index = row_int * gdd->cols + col_int;
155 arr_index = bit_index / 8;
156 bit_mod = bit_index % 8;
158 gdd->data[arr_index] |= 1 << bit_mod;
164 void print_gdd(struct graph_dot_data *gdd)
173 for (row = gdd->rows - 1; row >= 0; row--) {
174 for (col = 0; col < gdd->cols; col++) {
175 bit_index = row * gdd->cols + col;
176 arr_index = bit_index / sizeof(unsigned long);
177 bit_mod = bit_index % sizeof(unsigned long);
179 val = gdd->data[arr_index];
180 if (val & (1 << bit_mod))
189 static double rolling_avg(struct graph_line_pair *data, int index, int distance)
196 if (distance > index) {
199 start = index - distance;
202 while (start <= index) {
205 if (data[start].count)
206 avg = ((double)data[start].sum) / data[start].count;
214 return sum / distance;
217 void write_svg_header(int fd)
219 char *spaces = " \n";
220 char *header = "<svg xmlns=\"http://www.w3.org/2000/svg\">\n";
221 char *filter1 ="<filter id=\"shadow\">\n "
222 "<feOffset result=\"offOut\" in=\"SourceAlpha\" dx=\"4\" dy=\"4\" />\n "
223 "<feGaussianBlur result=\"blurOut\" in=\"offOut\" stdDeviation=\"2\" />\n "
224 "<feBlend in=\"SourceGraphic\" in2=\"blurOut\" mode=\"normal\" />\n "
226 char *filter2 ="<filter id=\"textshadow\" x=\"0\" y=\"0\" width=\"200%\" height=\"200%\">\n "
227 "<feOffset result=\"offOut\" in=\"SourceAlpha\" dx=\"1\" dy=\"1\" />\n "
228 "<feGaussianBlur result=\"blurOut\" in=\"offOut\" stdDeviation=\"1.5\" />\n "
229 "<feBlend in=\"SourceGraphic\" in2=\"blurOut\" mode=\"normal\" />\n "
231 char *filter3 ="<filter id=\"labelshadow\" x=\"0\" y=\"0\" width=\"200%\" height=\"200%\">\n "
232 "<feOffset result=\"offOut\" in=\"SourceGraphic\" dx=\"3\" dy=\"3\" />\n "
233 "<feColorMatrix result=\"matrixOut\" in=\"offOut\" type=\"matrix\" "
234 "values=\"0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0\" /> "
235 "<feGaussianBlur result=\"blurOut\" in=\"offOut\" stdDeviation=\"2\" />\n "
236 "<feBlend in=\"SourceGraphic\" in2=\"blurOut\" mode=\"normal\" />\n "
238 char *defs_start = "<defs>\n";
239 char *defs_close = "</defs>\n";
243 write(fd, header, strlen(header));
244 /* write a bunch of spaces so we can stuff in the width and height later */
245 write(fd, spaces, strlen(spaces));
246 write(fd, spaces, strlen(spaces));
247 write(fd, spaces, strlen(spaces));
249 write(fd, defs_start, strlen(defs_start));
250 write(fd, filter1, strlen(filter1));
251 write(fd, filter2, strlen(filter2));
252 write(fd, filter3, strlen(filter3));
253 write(fd, defs_close, strlen(defs_close));
256 void write_drop_shadow(struct plot *plot)
258 snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"white\"/>\n",
259 plot->start_y_offset, plot->total_width, 45);
260 write(plot->fd, line, strlen(line));
262 snprintf(line, line_len, "<path d=\"M %d %d h %d v %d h %d t %d %d V %d H %d Z\" "
263 "fill=\"white\" filter=\"url(#shadow)\"/>",
264 0, plot->start_y_offset,
265 plot->total_width - graph_left_pad / 2,
266 -plot->total_height, 24, 1, 1,
267 plot->start_y_offset + 10, 0);
268 write(plot->fd, line, strlen(line));
270 snprintf(line, line_len, "<path d=\"M %d %d H %d V %d h %d V %d H %d Z\" "
272 0, plot->start_y_offset - 15, /* start */
273 plot->total_width - graph_left_pad / 2 - 10, /* hline over */
274 plot->start_y_offset - plot->total_height, /* vline up */
276 plot->start_y_offset, /* vline back down */
278 write(plot->fd, line, strlen(line));
280 plot->start_y_offset += 45;
283 /* svg y offset for the traditional 0,0 (bottom left corner) of the plot */
284 static int axis_y(void)
286 return plot_label_height + graph_height + graph_inner_y_margin;
289 /* this gives you the correct pixel for a given offset from the bottom left y axis */
290 static double axis_y_off_double(double y)
292 return plot_label_height + graph_height - y;
295 static int axis_y_off(int y)
297 return axis_y_off_double(y);
300 /* svg x axis offset from 0 */
301 static int axis_x(void)
303 return graph_left_pad;
306 /* the correct pixel for a given X offset */
307 static double axis_x_off_double(double x)
309 return graph_left_pad + graph_inner_x_margin + x;
312 static int axis_x_off(int x)
314 return (int)axis_x_off_double(x);
318 * this draws a backing rectangle for the plot and it
319 * also creates a new svg element so our offsets can
320 * be relative to this one plot.
322 void setup_axis(struct plot *plot)
327 int bump_height = tick_font_size * 3 + axis_label_font_size;
328 int local_legend_width = legend_width;
331 local_legend_width = 0;
333 plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + local_legend_width;
334 plot->total_height = axis_y() + tick_label_pad + tick_font_size;
336 if (plot->add_xlabel)
337 plot->total_height += bump_height;
340 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
341 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
342 plot->start_x_offset,
343 plot->start_y_offset, plot->total_width + 40,
344 plot->total_height + 20);
346 write(fd, line, len);
348 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
349 "filter=\"url(#shadow)\" "
350 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
351 plot->start_x_offset + 15,
352 plot->start_y_offset, plot->total_width, plot->total_height);
354 write(fd, line, len);
355 plot->total_height += 20;
356 plot->total_width += 20;
358 if (plot->total_height + plot->start_y_offset > final_height)
359 final_height = plot->total_height + plot->start_y_offset;
360 if (plot->start_x_offset + plot->total_width + 40 > final_width)
361 final_width = plot->start_x_offset + plot->total_width + 40;
363 /* create an svg object for all our coords to be relative against */
364 snprintf(line, line_len, "<svg x=\"%d\" y=\"%d\">\n", plot->start_x_offset, plot->start_y_offset);
365 write(fd, line, strlen(line));
367 snprintf(line, 1024, "<path d=\"M%d %d h %d V %d H %d Z\" stroke=\"black\" stroke-width=\"2\" fill=\"none\"/>\n",
369 graph_width + graph_inner_x_margin * 2, axis_y_off(graph_height) - graph_inner_y_margin,
372 ret = write(fd, line, len);
374 fprintf(stderr, "failed to write svg axis\n");
380 * this draws a backing rectangle for the plot and it
381 * also creates a new svg element so our offsets can
382 * be relative to this one plot.
384 void setup_axis_spindle(struct plot *plot)
388 int bump_height = tick_font_size * 3 + axis_label_font_size;
392 plot->total_width = axis_x_off(graph_width) + legend_width;
393 plot->total_height = axis_y() + tick_label_pad + tick_font_size;
395 if (plot->add_xlabel)
396 plot->total_height += bump_height;
399 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
400 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
401 plot->start_x_offset,
402 plot->start_y_offset, plot->total_width + 10,
403 plot->total_height + 20);
405 write(fd, line, len);
407 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" "
408 "filter=\"url(#shadow)\" "
409 "height=\"%d\" fill=\"white\" stroke=\"none\"/>",
410 plot->start_x_offset + 15,
411 plot->start_y_offset, plot->total_width - 30,
414 write(fd, line, len);
415 plot->total_height += 20;
417 if (plot->total_height + plot->start_y_offset > final_height)
418 final_height = plot->total_height + plot->start_y_offset;
419 if (plot->start_x_offset + plot->total_width + 40 > final_width)
420 final_width = plot->start_x_offset + plot->total_width + 40;
422 /* create an svg object for all our coords to be relative against */
423 snprintf(line, line_len, "<svg x=\"%d\" y=\"%d\">\n", plot->start_x_offset, plot->start_y_offset);
424 write(fd, line, strlen(line));
428 /* draw a plot title. This should be done only once,
429 * and it bumps the plot width/height numbers by
432 * Call this before setting up the first axis
434 void set_plot_title(struct plot *plot, char *title)
439 plot->total_height = plot_title_height;
440 plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + legend_width;
443 snprintf(line, line_len, "<rect x=\"0\" y=\"%d\" width=\"%d\" height=\"%d\" fill=\"white\" stroke=\"none\"/>",
444 plot->start_y_offset, plot->total_width + 40, plot_title_height + 20);
446 write(fd, line, len);
448 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
449 "font-weight=\"bold\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
450 axis_x_off(graph_width / 2),
451 plot->start_y_offset + plot_title_height / 2,
452 font_family, plot_title_font_size, "middle", title);
453 plot->start_y_offset += plot_title_height;
455 write(fd, line, len);
458 #define TICK_MINI_STEPS 3
460 static double find_step(double first, double last, int num_ticks)
462 int mini_step[TICK_MINI_STEPS] = { 1, 2, 5 };
463 int cur_mini_step = 0;
464 double step = (last - first) / num_ticks;
465 double log10 = log(10);
467 /* Round to power of 10 */
468 step = exp(floor(log(step) / log10) * log10);
469 /* Scale down step to provide enough ticks */
470 while ((last - first) / (step * mini_step[cur_mini_step]) > num_ticks
471 && cur_mini_step < TICK_MINI_STEPS)
473 step *= mini_step[cur_mini_step - 1];
479 * create evenly spread out ticks along the xaxis. if tick only is set
480 * this just makes the ticks, otherwise it labels each tick as it goes
482 void set_xticks(struct plot *plot, int num_ticks, int first, int last)
487 int tick_y = axis_y_off(graph_tick_len) + graph_inner_y_margin;
488 int tick_x = axis_x();
489 int tick_only = plot->add_xlabel == 0;
491 int text_y = axis_y() + tick_label_pad;
493 char *middle = "middle";
494 char *start = "start";
496 step = find_step(first, last, num_ticks);
498 * We don't want last two ticks to be too close together so subtract
499 * 20% of the step from the interval
501 num_ticks = (double)(last - first - step / 5) / step + 1;
502 pixels_per_tick = graph_width * step / (double)(last - first);
504 for (i = 0; i < num_ticks; i++) {
507 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"2\" height=\"%d\" style=\"stroke:none;fill:black;\"/>\n",
508 tick_x, tick_y, graph_tick_len);
509 write(plot->fd, line, strlen(line));
517 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
518 "fill=\"black\" style=\"text-anchor: %s\">%d</text>\n",
519 tick_x, text_y, font_family, tick_font_size, anchor,
520 (int)(first + step * i));
522 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
523 "fill=\"black\" style=\"text-anchor: %s\">%.2f</text>\n",
524 tick_x, text_y, font_family, tick_font_size, anchor,
526 write(plot->fd, line, strlen(line));
528 tick_x += pixels_per_tick;
533 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
534 "fill=\"black\" style=\"text-anchor: middle\">%d</text>\n",
535 axis_x_off(graph_width - 2),
536 text_y, font_family, tick_font_size, last);
538 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
539 "fill=\"black\" style=\"text-anchor: middle\">%.2f</text>\n",
540 axis_x_off(graph_width - 2),
541 text_y, font_family, tick_font_size, (double)last);
542 write(plot->fd, line, strlen(line));
546 void set_ylabel(struct plot *plot, char *label)
551 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" "
552 "transform=\"rotate(-90 %d %d)\" font-weight=\"bold\" "
553 "font-size=\"%d\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
554 graph_left_pad / 2 - axis_label_font_size,
555 axis_y_off(graph_height / 2),
557 graph_left_pad / 2 - axis_label_font_size,
558 (int)axis_y_off(graph_height / 2),
559 axis_label_font_size, "middle", label);
561 write(fd, line, len);
564 void set_xlabel(struct plot *plot, char *label)
568 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" "
569 "font-weight=\"bold\" "
570 "font-size=\"%d\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
571 axis_x_off(graph_width / 2),
572 axis_y() + tick_font_size * 3 + axis_label_font_size / 2,
574 axis_label_font_size, "middle", label);
576 write(fd, line, len);
581 * create evenly spread out ticks along the y axis.
582 * The ticks are labeled as it goes
584 void set_yticks(struct plot *plot, int num_ticks, int first, int last, char *units)
586 int pixels_per_tick = graph_height / num_ticks;
587 int step = (last - first) / num_ticks;
590 int text_x = axis_x() - 6;
591 int tick_x = axis_x();
592 char *anchor = "end";
594 for (i = 0; i < num_ticks; i++) {
596 snprintf(line, line_len, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" "
597 "style=\"stroke:lightgray;stroke-width:2;stroke-dasharray:9,12;\"/>\n",
598 tick_x, axis_y_off(tick_y),
599 axis_x_off(graph_width), axis_y_off(tick_y));
600 write(plot->fd, line, strlen(line));
603 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
604 "fill=\"black\" style=\"text-anchor: %s\">%d%s</text>\n",
606 axis_y_off(tick_y - tick_font_size / 2),
607 font_family, tick_font_size, anchor, first + step * i, units);
608 write(plot->fd, line, strlen(line));
609 tick_y += pixels_per_tick;
611 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
612 "fill=\"black\" style=\"text-anchor: %s\">%d%s</text>\n",
613 text_x, axis_y_off(graph_height), font_family, tick_font_size, anchor, last, units);
614 write(plot->fd, line, strlen(line));
617 void set_plot_label(struct plot *plot, char *label)
622 snprintf(line, line_len, "<text x=\"%d\" y=\"%d\" font-family=\"%s\" "
623 "font-size=\"%d\" fill=\"black\" style=\"text-anchor: %s\">%s</text>\n",
624 axis_x() + graph_width / 2,
625 plot_label_height / 2,
626 font_family, plot_label_font_size, "middle", label);
628 write(fd, line, len);
631 static void close_svg(int fd)
633 char *close_line = "</svg>\n";
635 write(fd, close_line, strlen(close_line));
638 int close_plot(struct plot *plot)
641 if (plot->direction == PLOT_DOWN)
642 plot->start_y_offset += plot->total_height;
643 else if (plot->direction == PLOT_ACROSS)
644 plot->start_x_offset += plot->total_width;
648 struct plot *alloc_plot(void)
651 plot = calloc(1, sizeof(*plot));
653 fprintf(stderr, "Unable to allocate memory %s\n", strerror(errno));
660 int close_plot_file(struct plot *plot)
663 ret = lseek(plot->fd, 0, SEEK_SET);
664 if (ret == (off_t)-1) {
668 final_width = ((final_width + 1) / 2) * 2;
669 final_height = ((final_height + 1) / 2) * 2;
670 snprintf(line, line_len, "<svg xmlns=\"http://www.w3.org/2000/svg\" "
671 "width=\"%d\" height=\"%d\">\n",
672 final_width, final_height);
673 write(plot->fd, line, strlen(line));
674 snprintf(line, line_len, "<rect x=\"0\" y=\"0\" width=\"%d\" "
675 "height=\"%d\" fill=\"white\"/>\n", final_width, final_height);
676 write(plot->fd, line, strlen(line));
682 void set_plot_output(struct plot *plot, char *filename)
687 close_plot_file(plot);
688 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
690 fprintf(stderr, "Unable to open output file %s err %s\n", filename, strerror(errno));
694 plot->start_y_offset = plot->start_x_offset = 0;
695 write_svg_header(fd);
698 char *byte_unit_names[] = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", "unobtainium" };
699 int MAX_BYTE_UNIT_SCALE = 9;
701 char *time_unit_names[] = { "n", "u", "m", "s" };
702 int MAX_TIME_UNIT_SCALE = 3;
704 void scale_line_graph_bytes(u64 *max, char **units, u64 factor)
709 while (val > factor * 64) {
714 *units = byte_unit_names[scale];
718 if (scale > MAX_BYTE_UNIT_SCALE)
719 scale = MAX_BYTE_UNIT_SCALE;
724 void scale_line_graph_time(u64 *max, char **units)
729 while (val > 1000 * 10) {
733 if (scale == MAX_TIME_UNIT_SCALE)
736 *units = time_unit_names[scale];
743 int svg_line_graph(struct plot *plot, struct graph_line_data *gld, char *color, int thresh1, int thresh2)
750 char *start = "<path d=\"";
751 double yscale = ((double)gld->max) / graph_height;
752 double xscale = (double)(gld->max_seconds - gld->min_seconds - 1) / graph_width;
755 int printed_header = 0;
756 int printed_lines = 0;
758 if (thresh1 && thresh2)
760 else if (rolling_avg_secs)
761 rolling = rolling_avg_secs;
763 rolling = (gld->stop_seconds - gld->min_seconds) / 25;
765 for (i = gld->min_seconds; i < gld->stop_seconds; i++) {
766 avg = rolling_avg(gld->data, i, rolling);
772 if (val > graph_height)
777 x = (double)(i - gld->min_seconds) / xscale;
778 if (!thresh1 && !thresh2) {
780 if (!printed_header) {
781 write(fd, start, strlen(start));
785 /* in full line mode, everything in the graph is connected */
786 snprintf(line, line_len, "%c %d %d ", c, axis_x_off(x), axis_y_off(val));
788 write(fd, line, strlen(line));
790 } else if (avg > thresh1 || avg > thresh2) {
792 if (!printed_header) {
793 write(fd, start, strlen(start));
797 /* otherwise, we just print a bar up there to show this one data point */
798 if (i >= gld->stop_seconds - 2)
802 * we don't use the rolling averages here to show high
805 snprintf(line, line_len, "M %d %d h %d ", axis_x_off(x),
806 axis_y_off(val), len);
807 write(fd, line, strlen(line));
813 snprintf(line, line_len, "\" fill=\"none\" stroke=\"%s\" stroke-width=\"2\"/>\n", color);
814 write(fd, line, strlen(line));
817 svg_write_time_line(plot, plot->timeline);
822 void svg_write_time_line(struct plot *plot, int col)
824 snprintf(line, line_len, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" "
825 "style=\"stroke:black;stroke-width:2;\"/>\n",
826 axis_x_off(col), axis_y_off(0),
827 axis_x_off(col), axis_y_off(graph_height));
828 write(plot->fd, line, strlen(line));
831 static int svg_add_io(int fd, double row, double col, double width, double height, char *color)
835 snprintf(line, line_len, "<rect x=\"%.2f\" y=\"%.2f\" width=\"%.1f\" height=\"%.1f\" "
836 "rx=\"%.2f\" style=\"stroke:none;fill:%s;stroke-width:0\"/>\n",
837 axis_x_off_double(col), axis_y_off_double(row), width, height, rx, color);
838 return write(fd, line, strlen(line));
841 int svg_io_graph_movie_array(struct plot *plot, struct plot_history *ph)
848 for (i = 0; i < ph->num_used; i++) {
849 cell_index = ph->history[i];
850 movie_row = floor(cell_index / graph_width);
851 movie_col = cell_index - movie_row * graph_width;
852 svg_add_io(plot->fd, movie_row, movie_col, 4, 4, ph->color);
857 static float spindle_steps = 0;
859 void rewind_spindle_steps(int num)
861 spindle_steps -= num * 0.01;
864 int svg_io_graph_movie_array_spindle(struct plot *plot, struct plot_history *ph)
869 double cells_per_circle;
871 double degrees_per_cell;
875 double graph_width_extra = graph_width + graph_circle_extra;
876 double graph_height_extra = graph_height + graph_circle_extra;
879 if (graph_width_extra > graph_height_extra)
880 graph_width_extra = graph_height_extra;
882 if (graph_width_extra < graph_height_extra)
883 graph_height_extra = graph_width_extra;
885 radius = graph_width_extra;
887 center_x = axis_x_off_double(graph_width_extra / 2);
888 center_y = axis_y_off_double(graph_height_extra / 2);
890 snprintf(line, line_len, "<g transform=\"rotate(%.4f, %.2f, %.2f)\"> "
891 "<circle cx=\"%.2f\" cy=\"%.2f\" "
892 "stroke=\"black\" stroke-width=\"6\" "
893 "r=\"%.2f\" fill=\"none\"/>\n",
894 -spindle_steps * 1.2, center_x, center_y, center_x, center_y, graph_width_extra / 2);
895 write(plot->fd, line, strlen(line));
896 snprintf(line, line_len, "<circle cx=\"%.2f\" cy=\"%.2f\" "
897 "stroke=\"none\" fill=\"red\" r=\"%.2f\"/>\n</g>\n",
898 axis_x_off_double(graph_width_extra), center_y, 4.5);
899 write(plot->fd, line, strlen(line));
900 spindle_steps += 0.01;
902 radius = floor(radius / 2);
903 num_circles = radius / 4 - 3;
904 cells_per_circle = ph->history_max / num_circles;
905 degrees_per_cell = 360 / cells_per_circle;
907 for (i = 0; i < ph->num_used; i++) {
908 cell_index = ph->history[i];
909 circle_num = floor(cell_index / cells_per_circle);
910 rot = cell_index - circle_num * cells_per_circle;
911 circle_num = num_circles - circle_num;
912 radius = circle_num * 4;
914 rot = rot * degrees_per_cell;
915 rot -= spindle_steps;
916 snprintf(line, line_len, "<path transform=\"rotate(%.4f, %.2f, %.2f)\" "
917 "d=\"M %.2f %.2f a %.2f %.2f 0 0 1 0 5\" "
918 "stroke=\"%s\" stroke-width=\"4\"/>\n",
919 rot, center_x, center_y,
920 axis_x_off_double(graph_width_extra / 2 + radius) + 8, center_y,
921 radius, radius, ph->color);
923 write(plot->fd, line, strlen(line));
928 static int add_plot_history(struct plot_history *ph, double val)
930 if (ph->num_used == ph->history_len) {
931 ph->history = realloc(ph->history,
932 (ph->history_len + 4096) * sizeof(double));
934 perror("Unable to allocate memory");
937 ph->history_len += 4096;
939 ph->history[ph->num_used++] = val;
943 int svg_io_graph_movie(struct graph_dot_data *gdd, struct plot_history *ph, int col)
950 double blocks_per_row = (gdd->max_offset - gdd->min_offset + 1) / gdd->rows;
951 double movie_blocks_per_cell = (gdd->max_offset - gdd->min_offset + 1) / (graph_width * graph_height);
953 int margin_orig = graph_inner_y_margin;
955 graph_inner_y_margin += 5;
956 ph->history_max = (gdd->max_offset - gdd->min_offset + 1) / movie_blocks_per_cell;
958 for (row = gdd->rows - 1; row >= 0; row--) {
959 bit_index = row * gdd->cols + col;
960 arr_index = bit_index / 8;
961 bit_mod = bit_index % 8;
965 val = gdd->data[arr_index];
966 if (val & (1 << bit_mod)) {
967 /* in bytes, linear offset from the start of the drive */
968 cell_index = (double)row * blocks_per_row;
970 /* a cell number in the graph */
971 cell_index /= movie_blocks_per_cell;
973 add_plot_history(ph, cell_index);
976 graph_inner_y_margin = margin_orig;
980 int svg_io_graph(struct plot *plot, struct graph_dot_data *gdd, char *color)
990 for (row = gdd->rows - 1; row >= 0; row--) {
991 for (col = 0; col < gdd->cols; col++) {
992 bit_index = row * gdd->cols + col;
993 arr_index = bit_index / 8;
994 bit_mod = bit_index % 8;
998 val = gdd->data[arr_index];
999 if (val & (1 << bit_mod))
1000 svg_add_io(fd, floor(row / io_graph_scale), col, 1.5, 1.5, color);
1006 void svg_alloc_legend(struct plot *plot, int num_lines)
1008 char **lines = calloc(num_lines, sizeof(char *));
1009 plot->legend_index = 0;
1010 plot->legend_lines = lines;
1011 plot->num_legend_lines = num_lines;
1014 void svg_free_legend(struct plot *plot)
1017 for (i = 0; i < plot->legend_index; i++)
1018 free(plot->legend_lines[i]);
1019 free(plot->legend_lines);
1020 plot->legend_lines = NULL;
1021 plot->legend_index = 0;
1024 void svg_write_legend(struct plot *plot)
1026 int legend_line_x = axis_x_off(graph_width) + legend_x_off;
1027 int legend_line_y = axis_y_off(graph_height) + legend_y_off;
1030 if (plot->legend_index == 0)
1033 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "
1034 "fill=\"white\" filter=\"url(#shadow)\"/>\n",
1038 plot->legend_index * legend_font_size + legend_font_size / 2 + 12);
1040 write(plot->fd, line, strlen(line));
1041 for (i = 0; i < plot->legend_index; i++) {
1042 write(plot->fd, plot->legend_lines[i],
1043 strlen(plot->legend_lines[i]));
1044 free(plot->legend_lines[i]);
1046 free(plot->legend_lines);
1047 plot->legend_lines = NULL;
1048 plot->legend_index = 0;
1051 void svg_add_legend(struct plot *plot, char *text, char *extra, char *color)
1053 int legend_line_x = axis_x_off(graph_width) + legend_x_off;
1054 int legend_line_y = axis_y_off(graph_height) + legend_y_off;
1056 if (!text && (!extra || strlen(extra) == 0))
1059 legend_line_y += plot->legend_index * legend_font_size + legend_font_size / 2;
1060 snprintf(line, line_len, "<path d=\"M %d %d h 8\" stroke=\"%s\" stroke-width=\"8\" "
1061 "filter=\"url(#labelshadow)\"/> "
1062 "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
1063 "fill=\"black\" style=\"text-anchor: left\">%s%s</text>\n",
1064 legend_line_x, legend_line_y,
1065 color, legend_line_x + 13,
1066 legend_line_y + 4, font_family, legend_font_size,
1069 plot->legend_lines[plot->legend_index++] = strdup(line);
1072 void set_legend_width(int longest_str)
1075 legend_width = longest_str * (legend_font_size * 3 / 4) + 25;
1080 void set_rolling_avg(int rolling)
1082 rolling_avg_secs = rolling;
1085 void set_io_graph_scale(int scale)
1087 io_graph_scale = scale;
1090 void set_graph_size(int width, int height)
1092 graph_width = width;
1093 graph_height = height;
1096 void get_graph_size(int *width, int *height)
1098 *width = graph_width;
1099 *height = graph_height;
1102 void set_graph_height(int h)
1106 void set_graph_width(int w)