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