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