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 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->seconds = seconds;
83 gld->stop_seconds = stop_seconds;
87 void free_line_data(struct graph_line_data *gld)
93 struct graph_dot_data *alloc_dot_data(int seconds, u64 max_offset, int stop_seconds)
97 int rows = graph_height * io_graph_scale;
98 int cols = graph_width;
99 struct graph_dot_data *gdd;
101 size = sizeof(struct graph_dot_data);
103 /* the number of bits */
104 arr_size = (rows + 1) * cols;
106 /* the number of bytes */
109 gdd = calloc(1, size + arr_size);
111 fprintf(stderr, "Unable to allocate memory for graph data\n");
114 gdd->seconds = seconds;
115 gdd->stop_seconds = stop_seconds;
118 gdd->max_offset = max_offset;
122 void free_dot_data(struct graph_dot_data *gdd)
127 void set_gdd_bit(struct graph_dot_data *gdd, u64 offset, double bytes, double time)
129 double bytes_per_row = (double)gdd->max_offset / gdd->rows;
131 double secs_per_col = (double)gdd->seconds / gdd->cols;
139 double mod = bytes_per_row;
141 if (offset > gdd->max_offset)
145 time = time / 1000000000.0;
147 row = (double)offset / bytes_per_row;
148 col = time / secs_per_col;
150 col_int = floor(col);
151 row_int = floor(row);
152 bit_index = row_int * gdd->cols + col_int;
153 arr_index = bit_index / 8;
154 bit_mod = bit_index % 8;
156 gdd->data[arr_index] |= 1 << bit_mod;
162 void print_gdd(struct graph_dot_data *gdd)
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);
177 val = gdd->data[arr_index];
178 if (val & (1 << bit_mod))
187 static double rolling_avg(struct graph_line_pair *data, int index, int distance)
194 if (distance > index) {
197 start = index - distance;
200 while (start <= index) {
203 if (data[start].count)
204 avg = ((double)data[start].sum) / data[start].count;
212 return sum / distance;
215 void write_svg_header(int fd)
217 char *spaces = " \n";
218 char *header = "<svg xmlns=\"http://www.w3.org/2000/svg\">\n";
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 "
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 "
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 "
236 char *defs_start = "<defs>\n";
237 char *defs_close = "</defs>\n";
241 write(fd, header, strlen(header));
242 /* write a bunch of spaces so we can stuff in the width and height later */
243 write(fd, spaces, strlen(spaces));
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));
252 void write_drop_shadow(struct plot *plot)
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));
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));
266 snprintf(line, line_len, "<path d=\"M %d %d H %d V %d h %d V %d H %d Z\" "
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 */
272 plot->start_y_offset, /* vline back down */
274 write(plot->fd, line, strlen(line));
276 plot->start_y_offset += 45;
279 /* svg y offset for the traditional 0,0 (bottom left corner) of the plot */
280 static int axis_y(void)
282 return plot_label_height + graph_height + graph_inner_y_margin;
285 /* this gives you the correct pixel for a given offset from the bottom left y axis */
286 static double axis_y_off_double(double y)
288 return plot_label_height + graph_height - y;
291 static int axis_y_off(int y)
293 return axis_y_off_double(y);
296 /* svg x axis offset from 0 */
297 static int axis_x(void)
299 return graph_left_pad;
302 /* the correct pixel for a given X offset */
303 static double axis_x_off_double(double x)
305 return graph_left_pad + graph_inner_x_margin + x;
308 static int axis_x_off(int x)
310 return (int)axis_x_off_double(x);
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.
319 void setup_axis(struct plot *plot)
324 int bump_height = tick_font_size * 3 + axis_label_font_size;
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;
329 if (plot->add_xlabel)
330 plot->total_height += bump_height;
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);
338 write(fd, line, len);
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);
345 write(fd, line, len);
346 plot->total_height += 20;
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;
353 /* create an svg object for all our coords to be relative against */
354 snprintf(line, line_len, "<svg x=\"%d\" y=\"%d\">\n", plot->start_x_offset, plot->start_y_offset);
355 write(fd, line, strlen(line));
357 snprintf(line, 1024, "<path d=\"M%d %d h %d V %d H %d Z\" stroke=\"black\" stroke-width=\"2\" fill=\"none\"/>\n",
359 graph_width + graph_inner_x_margin * 2, axis_y_off(graph_height) - graph_inner_y_margin,
362 ret = write(fd, line, len);
364 fprintf(stderr, "failed to write svg axis\n");
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.
374 void setup_axis_spindle(struct plot *plot)
378 int bump_height = tick_font_size * 3 + axis_label_font_size;
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;
383 if (plot->add_xlabel)
384 plot->total_height += bump_height;
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);
392 write(fd, line, len);
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);
399 write(fd, line, len);
400 plot->total_height += 20;
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;
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));
413 /* draw a plot title. This should be done only once,
414 * and it bumps the plot width/height numbers by
417 * Call this before setting up the first axis
419 void set_plot_title(struct plot *plot, char *title)
424 plot->total_height = plot_title_height;
425 plot->total_width = axis_x_off(graph_width) + graph_left_pad / 2 + legend_width;
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);
431 write(fd, line, len);
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;
440 write(fd, line, len);
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
447 void set_xticks(struct plot *plot, int num_ticks, int first, int last)
449 int pixels_per_tick = graph_width / num_ticks;
450 int step = (last - first) / num_ticks;
452 int tick_y = axis_y_off(graph_tick_len) + graph_inner_y_margin;
453 int tick_x = axis_x();
454 int tick_only = plot->add_xlabel == 0;
456 int text_y = axis_y() + tick_label_pad;
458 char *middle = "middle";
459 char *start = "start";
461 for (i = 0; i < num_ticks; i++) {
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));
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));
478 tick_x += pixels_per_tick;
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));
490 void set_ylabel(struct plot *plot, char *label)
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),
501 graph_left_pad / 2 - axis_label_font_size,
502 (int)axis_y_off(graph_height / 2),
503 axis_label_font_size, "middle", label);
505 write(fd, line, len);
508 void set_xlabel(struct plot *plot, char *label)
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,
518 axis_label_font_size, "middle", label);
520 write(fd, line, len);
525 * create evenly spread out ticks along the y axis.
526 * The ticks are labeled as it goes
528 void set_yticks(struct plot *plot, int num_ticks, int first, int last, char *units)
530 int pixels_per_tick = graph_height / num_ticks;
531 int step = (last - first) / num_ticks;
534 int text_x = axis_x() - 6;
535 int tick_x = axis_x();
536 char *anchor = "end";
538 for (i = 0; i < num_ticks; i++) {
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));
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",
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;
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));
561 void set_plot_label(struct plot *plot, char *label)
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);
572 write(fd, line, len);
575 static void close_svg(int fd)
577 char *close_line = "</svg>\n";
579 write(fd, close_line, strlen(close_line));
582 int close_plot(struct plot *plot)
585 plot->start_y_offset += plot->total_height;
586 plot->add_xlabel = 0;
590 struct plot *alloc_plot(void)
593 plot = calloc(1, sizeof(*plot));
595 fprintf(stderr, "Unable to allocate memory %s\n", strerror(errno));
602 int close_plot_file(struct plot *plot)
605 ret = lseek(plot->fd, 0, SEEK_SET);
606 if (ret == (off_t)-1) {
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));
621 void set_plot_output(struct plot *plot, char *filename)
626 close_plot_file(plot);
627 fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
629 fprintf(stderr, "Unable to open output file %s err %s\n", filename, strerror(errno));
633 plot->start_y_offset = plot->start_x_offset = 0;
634 write_svg_header(fd);
637 char *byte_unit_names[] = { "", "K", "M", "G", "T", "P", "E", "Z", "Y", "unobtainium" };
638 int MAX_BYTE_UNIT_SCALE = 9;
640 char *time_unit_names[] = { "n", "u", "m", "s" };
641 int MAX_TIME_UNIT_SCALE = 3;
643 void scale_line_graph_bytes(u64 *max, char **units, u64 factor)
648 while (val > factor * 64) {
653 *units = byte_unit_names[scale];
657 if (scale > MAX_BYTE_UNIT_SCALE)
658 scale = MAX_BYTE_UNIT_SCALE;
663 void scale_line_graph_time(u64 *max, char **units)
668 while (val > 1000 * 10) {
672 if (scale == MAX_TIME_UNIT_SCALE)
675 *units = time_unit_names[scale];
682 int svg_line_graph(struct plot *plot, struct graph_line_data *gld, char *color, int thresh1, int thresh2)
689 char *start = "<path d=\"";
690 double yscale = ((double)gld->max) / graph_height;
691 double xscale = (double)(gld->seconds - 1) / graph_width;
694 int printed_header = 0;
695 int printed_lines = 0;
697 if (thresh1 && thresh2)
699 else if (rolling_avg_secs)
700 rolling = rolling_avg_secs;
702 rolling = gld->stop_seconds / 25;
704 for (i = 0; i < gld->stop_seconds; i++) {
705 avg = rolling_avg(gld->data, i, rolling);
711 if (val > graph_height)
716 x = (double)i / xscale;
717 if (!thresh1 && !thresh2) {
719 if (!printed_header) {
720 write(fd, start, strlen(start));
724 /* in full line mode, everything in the graph is connected */
725 snprintf(line, line_len, "%c %d %d ", c, axis_x_off(x), axis_y_off(val));
727 write(fd, line, strlen(line));
729 } else if (avg > thresh1 || avg > thresh2) {
731 if (!printed_header) {
732 write(fd, start, strlen(start));
736 /* otherwise, we just print a bar up there to show this one data point */
737 if (i >= gld->stop_seconds - 2)
741 * we don't use the rolling averages here to show high
744 snprintf(line, line_len, "M %d %d h %d ", axis_x_off(x),
745 axis_y_off(val), len);
746 write(fd, line, strlen(line));
752 snprintf(line, line_len, "\" fill=\"none\" stroke=\"%s\" stroke-width=\"2\"/>\n", color);
753 write(fd, line, strlen(line));
759 void svg_write_time_line(struct plot *plot, int col)
761 snprintf(line, line_len, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" "
762 "style=\"stroke:black;stroke-width:2;\"/>\n",
763 axis_x_off(col), axis_y_off(0),
764 axis_x_off(col), axis_y_off(graph_height));
765 write(plot->fd, line, strlen(line));
768 static int svg_add_io(int fd, double row, double col, double width, double height, char *color)
772 snprintf(line, line_len, "<rect x=\"%.2f\" y=\"%.2f\" width=\"%.1f\" height=\"%.1f\" "
773 "rx=\"%.2f\" style=\"stroke:none;fill:%s;stroke-width:0\"/>\n",
774 axis_x_off_double(col), axis_y_off_double(row), width, height, rx, color);
775 return write(fd, line, strlen(line));
778 int svg_io_graph_movie_array(struct plot *plot, struct plot_history *ph)
785 for (i = 0; i < ph->num_used; i++) {
786 cell_index = ph->history[i];
787 movie_row = floor(cell_index / graph_width);
788 movie_col = cell_index - movie_row * graph_width;
789 svg_add_io(plot->fd, movie_row, movie_col, 4, 4, ph->color);
794 static float spindle_steps = 0;
796 void rewind_spindle_steps(int num)
798 spindle_steps -= num * 0.01;
801 int svg_io_graph_movie_array_spindle(struct plot *plot, struct plot_history *ph)
806 double cells_per_circle;
808 double degrees_per_cell;
812 double graph_width_extra = graph_width + graph_circle_extra;
813 double graph_height_extra = graph_height + graph_circle_extra;
816 if (graph_width_extra > graph_height_extra)
817 graph_width_extra = graph_height_extra;
819 if (graph_width_extra < graph_height_extra)
820 graph_height_extra = graph_width_extra;
822 radius = graph_width_extra;
824 center_x = axis_x_off_double(graph_width_extra / 2);
825 center_y = axis_y_off_double(graph_height_extra / 2);
827 snprintf(line, line_len, "<g transform=\"rotate(%.4f, %.2f, %.2f)\"> "
828 "<circle cx=\"%.2f\" cy=\"%.2f\" "
829 "stroke=\"black\" stroke-width=\"6\" "
830 "r=\"%.2f\" fill=\"none\"/>\n",
831 -spindle_steps * 1.2, center_x, center_y, center_x, center_y, graph_width_extra / 2);
832 write(plot->fd, line, strlen(line));
833 snprintf(line, line_len, "<circle cx=\"%.2f\" cy=\"%.2f\" "
834 "stroke=\"none\" fill=\"red\" r=\"%.2f\"/>\n</g>\n",
835 axis_x_off_double(graph_width_extra), center_y, 4.5);
836 write(plot->fd, line, strlen(line));
837 spindle_steps += 0.01;
839 radius = floor(radius / 2);
840 num_circles = radius / 4 - 3;
841 cells_per_circle = ph->history_max / num_circles;
842 degrees_per_cell = 360 / cells_per_circle;
844 for (i = 0; i < ph->num_used; i++) {
845 cell_index = ph->history[i];
846 circle_num = floor(cell_index / cells_per_circle);
847 rot = cell_index - circle_num * cells_per_circle;
848 circle_num = num_circles - circle_num;
849 radius = circle_num * 4;
851 rot = rot * degrees_per_cell;
852 rot -= spindle_steps;
853 snprintf(line, line_len, "<path transform=\"rotate(%.4f, %.2f, %.2f)\" "
854 "d=\"M %.2f %.2f a %.2f %.2f 0 0 1 0 5\" "
855 "stroke=\"%s\" stroke-width=\"4\"/>\n",
856 rot, center_x, center_y,
857 axis_x_off_double(graph_width_extra / 2 + radius) + 8, center_y,
858 radius, radius, ph->color);
860 write(plot->fd, line, strlen(line));
865 static int add_plot_history(struct plot_history *ph, double val)
867 if (ph->num_used == ph->history_len) {
868 ph->history = realloc(ph->history,
869 (ph->history_len + 4096) * sizeof(double));
871 perror("Unable to allocate memory");
874 ph->history_len += 4096;
876 ph->history[ph->num_used++] = val;
880 int svg_io_graph_movie(struct graph_dot_data *gdd, struct plot_history *ph, int col)
887 double blocks_per_row = gdd->max_offset / gdd->rows;
888 double movie_blocks_per_cell = gdd->max_offset / (graph_width * graph_height);
890 int margin_orig = graph_inner_y_margin;
892 graph_inner_y_margin += 5;
893 ph->history_max = gdd->max_offset / movie_blocks_per_cell;
895 for (row = gdd->rows - 1; row >= 0; row--) {
896 bit_index = row * gdd->cols + col;
897 arr_index = bit_index / 8;
898 bit_mod = bit_index % 8;
902 val = gdd->data[arr_index];
903 if (val & (1 << bit_mod)) {
904 /* in bytes, linear offset from the start of the drive */
905 cell_index = (double)row * blocks_per_row;
907 /* a cell number in the graph */
908 cell_index /= movie_blocks_per_cell;
910 add_plot_history(ph, cell_index);
913 graph_inner_y_margin = margin_orig;
917 int svg_io_graph(struct plot *plot, struct graph_dot_data *gdd, char *color)
927 for (row = gdd->rows - 1; row >= 0; row--) {
928 for (col = 0; col < gdd->cols; col++) {
929 bit_index = row * gdd->cols + col;
930 arr_index = bit_index / 8;
931 bit_mod = bit_index % 8;
935 val = gdd->data[arr_index];
936 if (val & (1 << bit_mod))
937 svg_add_io(fd, floor(row / io_graph_scale), col, 1.5, 1.5, color);
943 void svg_alloc_legend(struct plot *plot, int num_lines)
945 char **lines = calloc(num_lines, sizeof(char *));
946 plot->legend_index = 0;
947 plot->legend_lines = lines;
948 plot->num_legend_lines = num_lines;
951 void svg_free_legend(struct plot *plot)
954 for (i = 0; i < plot->legend_index; i++)
955 free(plot->legend_lines[i]);
956 free(plot->legend_lines);
957 plot->legend_lines = NULL;
958 plot->legend_index = 0;
961 void svg_write_legend(struct plot *plot)
963 int legend_line_x = axis_x_off(graph_width) + legend_x_off;
964 int legend_line_y = axis_y_off(graph_height) + legend_y_off;
967 if (plot->legend_index == 0)
970 snprintf(line, line_len, "<rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "
971 "fill=\"white\" filter=\"url(#shadow)\"/>\n",
975 plot->legend_index * legend_font_size + legend_font_size / 2 + 12);
977 write(plot->fd, line, strlen(line));
978 for (i = 0; i < plot->legend_index; i++) {
979 write(plot->fd, plot->legend_lines[i],
980 strlen(plot->legend_lines[i]));
981 free(plot->legend_lines[i]);
983 free(plot->legend_lines);
984 plot->legend_lines = NULL;
985 plot->legend_index = 0;
988 void svg_add_legend(struct plot *plot, char *text, char *extra, char *color)
990 int legend_line_x = axis_x_off(graph_width) + legend_x_off;
991 int legend_line_y = axis_y_off(graph_height) + legend_y_off;
993 if (!text && (!extra || strlen(extra) == 0))
996 legend_line_y += plot->legend_index * legend_font_size + legend_font_size / 2;
997 snprintf(line, line_len, "<path d=\"M %d %d h 8\" stroke=\"%s\" stroke-width=\"8\" "
998 "filter=\"url(#labelshadow)\"/> "
999 "<text x=\"%d\" y=\"%d\" font-family=\"%s\" font-size=\"%d\" "
1000 "fill=\"black\" style=\"text-anchor: left\">%s%s</text>\n",
1001 legend_line_x, legend_line_y,
1002 color, legend_line_x + 13,
1003 legend_line_y + 4, font_family, legend_font_size,
1006 plot->legend_lines[plot->legend_index++] = strdup(line);
1009 void set_legend_width(int longest_str)
1012 legend_width = longest_str * (legend_font_size * 3 / 4) + 25;
1017 void set_rolling_avg(int rolling)
1019 rolling_avg_secs = rolling;
1022 void set_io_graph_scale(int scale)
1024 io_graph_scale = scale;
1027 void set_graph_size(int width, int height)
1029 graph_width = width;
1030 graph_height = height;
1033 void get_graph_size(int *width, int *height)
1035 *width = graph_width;
1036 *height = graph_height;
1039 void set_graph_height(int h)
1043 void set_graph_width(int w)