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