blob: 51eb3eff17cb1f5f4d99ee203ea3718f4c482774 [file] [log] [blame]
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +01001/*
2 * gfio - gui front end for fio - the flexible io tester
3 *
4 * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com>
5 *
6 * The license below covers all files distributed with fio unless otherwise
7 * noted in the file itself.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23#include <string.h>
24#include <malloc.h>
25#include <math.h>
26#include <assert.h>
Jens Axboe93e2db22012-03-13 09:45:22 +010027#include <stdlib.h>
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010028
29#include <cairo.h>
30#include <gtk/gtk.h>
31
32#include "tickmarks.h"
Stephen M. Cameron09ad20f2012-03-11 11:34:38 +010033#include "graph.h"
Jens Axboeb65c7ec2012-03-21 17:17:45 +010034#include "flist.h"
35#include "lib/prio_tree.h"
Jens Axboeb65c7ec2012-03-21 17:17:45 +010036
37/*
38 * Allowable difference to show tooltip
39 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +010040#define TOOLTIP_DELTA 0.08
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010041
42struct xyvalue {
43 double x, y;
44};
45
Jens Axboecdae5ff2012-03-22 09:24:05 +010046enum {
47 GV_F_ON_PRIO = 1,
Jens Axboe8dfd6072012-03-22 22:10:37 +010048 GV_F_PRIO_SKIP = 2,
Jens Axboecdae5ff2012-03-22 09:24:05 +010049};
50
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010051struct graph_value {
Jens Axboecdae5ff2012-03-22 09:24:05 +010052 struct flist_head list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010053 struct prio_tree_node node;
Jens Axboecdae5ff2012-03-22 09:24:05 +010054 struct flist_head alias;
55 unsigned int flags;
Jens Axboe93e2db22012-03-13 09:45:22 +010056 char *tooltip;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010057 void *value;
58};
59
60struct graph_label {
Jens Axboecdae5ff2012-03-22 09:24:05 +010061 struct flist_head list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010062 char *label;
Jens Axboecdae5ff2012-03-22 09:24:05 +010063 struct flist_head value_list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010064 struct prio_tree_root prio_tree;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010065 double r, g, b;
Jens Axboe01a947f2012-03-22 21:21:00 +010066 int hide;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010067 int value_count;
68 struct graph *parent;
69};
70
Jens Axboeb65c7ec2012-03-21 17:17:45 +010071struct tick_value {
72 unsigned int offset;
73 double value;
74};
75
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010076struct graph {
77 char *title;
78 char *xtitle;
79 char *ytitle;
Jens Axboe87d5f272012-03-07 12:31:40 +010080 unsigned int xdim, ydim;
Stephen M. Cameron57f9d282012-03-11 11:36:51 +010081 double xoffset, yoffset;
Jens Axboecdae5ff2012-03-22 09:24:05 +010082 struct flist_head label_list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010083 int per_label_limit;
Jens Axboef3e84402012-03-07 13:14:32 +010084 const char *font;
Stephen M. Cameron7175d912012-03-11 11:35:10 +010085 graph_axis_unit_change_callback x_axis_unit_change_callback;
86 graph_axis_unit_change_callback y_axis_unit_change_callback;
Jens Axboed8fbeef2012-03-14 10:25:44 +010087 unsigned int base_offset;
Jens Axboe01a947f2012-03-22 21:21:00 +010088 unsigned int dont_graph_all_zeroes;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +010089 double left_extra;
90 double right_extra;
91 double top_extra;
92 double bottom_extra;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010093
94 double xtick_zero;
95 double xtick_delta;
96 double xtick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +010097 double xtick_one_val;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010098 double ytick_zero;
99 double ytick_delta;
100 double ytick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100101 double ytick_one_val;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100102};
103
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100104void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
105{
106 g->xdim = xdim;
107 g->ydim = ydim;
108}
109
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100110void graph_set_position(struct graph *g, double xoffset, double yoffset)
111{
112 g->xoffset = xoffset;
113 g->yoffset = yoffset;
114}
115
Jens Axboef3e84402012-03-07 13:14:32 +0100116struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100117{
118 struct graph *g;
119
120 g = calloc(1, sizeof(*g));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100121 INIT_FLIST_HEAD(&g->label_list);
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100122 graph_set_size(g, xdim, ydim);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100123 g->per_label_limit = -1;
Jens Axboef3e84402012-03-07 13:14:32 +0100124 g->font = font;
125 if (!g->font)
Jens Axboea1e79722012-03-23 10:52:25 +0100126 g->font = GRAPH_DEFAULT_FONT;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100127 return g;
128}
129
Jens Axboea1e79722012-03-23 10:52:25 +0100130void graph_set_font(struct graph *g, const char *font)
131{
132 g->font = font;
133}
134
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100135void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
136{
137 g->x_axis_unit_change_callback = f;
138}
139
140void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
141{
142 g->y_axis_unit_change_callback = f;
143}
144
Jens Axboecdae5ff2012-03-22 09:24:05 +0100145static int count_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100146{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100147 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100148 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100149
Jens Axboecdae5ff2012-03-22 09:24:05 +0100150 flist_for_each(entry, &g->label_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100151 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100152
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100153 return count;
154}
155
Jens Axboecdae5ff2012-03-22 09:24:05 +0100156static int count_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100157{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100158 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100159 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100160
Jens Axboecdae5ff2012-03-22 09:24:05 +0100161 flist_for_each(entry, &l->value_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100162 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100163
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100164 return count;
165}
166
167typedef double (*double_comparator)(double a, double b);
168
169static double mindouble(double a, double b)
170{
171 return a < b ? a : b;
172}
173
174static double maxdouble(double a, double b)
175{
176 return a < b ? b : a;
177}
178
Jens Axboecdae5ff2012-03-22 09:24:05 +0100179static double find_double_values(struct graph_label *l, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100180{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100181 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100182 double answer, tmp;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100183 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100184
Jens Axboe01a947f2012-03-22 21:21:00 +0100185 if (flist_empty(&l->value_list))
186 return 0.0;
187
Jens Axboecdae5ff2012-03-22 09:24:05 +0100188 flist_for_each(entry, &l->value_list) {
189 struct graph_value *i;
190
191 i = flist_entry(entry, struct graph_value, list);
192 tmp = *(double *) i->value;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100193 if (first) {
194 answer = tmp;
195 first = 0;
196 } else {
197 answer = cmp(answer, tmp);
198 }
199 }
200 return answer;
201}
202
Jens Axboecdae5ff2012-03-22 09:24:05 +0100203static double find_double_data(struct graph *g, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100204{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100205 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100206 struct graph_label *i;
207 int first = 1;
208 double answer, tmp;
209
Jens Axboe01a947f2012-03-22 21:21:00 +0100210 if (flist_empty(&g->label_list))
211 return 0.0;
212
Jens Axboecdae5ff2012-03-22 09:24:05 +0100213 flist_for_each(entry, &g->label_list) {
214 i = flist_entry(entry, struct graph_label, list);
215 tmp = find_double_values(i, cmp);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100216 if (first) {
217 answer = tmp;
218 first = 0;
219 } else {
220 answer = cmp(tmp, answer);
221 }
222 }
223 return answer;
224}
225
Jens Axboecdae5ff2012-03-22 09:24:05 +0100226static double find_min_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100227{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100228 return find_double_data(g, mindouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100229}
230
Jens Axboecdae5ff2012-03-22 09:24:05 +0100231static double find_max_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100232{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100233 return find_double_data(g, maxdouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100234}
235
236static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
237 double label_offset, double bar_width,
238 double mindata, double maxdata)
239{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100240 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100241 double x1, y1, x2, y2;
242 int bar_num = 0;
243 double domain, range, v;
244
245 domain = (maxdata - mindata);
246 range = (double) bg->ydim * 0.80; /* FIXME */
247 cairo_stroke(cr);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100248 flist_for_each(entry, &lb->value_list) {
249 struct graph_value *i;
250
251 i = flist_entry(entry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100252
253 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
254 x2 = x1 + bar_width * 0.90;
255 y2 = bg->ydim * 0.90;
256 v = *(double *) i->value;
257 y1 = y2 - (((v - mindata) / domain) * range);
258 cairo_move_to(cr, x1, y1);
259 cairo_line_to(cr, x1, y2);
260 cairo_line_to(cr, x2, y2);
261 cairo_line_to(cr, x2, y1);
262 cairo_close_path(cr);
263 cairo_fill(cr);
264 cairo_stroke(cr);
265 bar_num++;
266 }
267}
268
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100269static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
270 double fontsize, const char *text, int alignment)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100271{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100272#define CENTERED 0
273#define LEFT_JUSTIFIED 1
274#define RIGHT_JUSTIFIED 2
275
276 double factor, direction;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100277 cairo_text_extents_t extents;
278
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100279 switch(alignment) {
280 case CENTERED:
281 direction = -1.0;
282 factor = 0.5;
283 break;
284 case RIGHT_JUSTIFIED:
285 direction = -1.0;
286 factor = 1.0;
287 break;
288 case LEFT_JUSTIFIED:
289 default:
290 direction = 1.0;
291 factor = 1.0;
292 break;
293 }
Jens Axboea1e79722012-03-23 10:52:25 +0100294 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100295
296 cairo_set_font_size(cr, fontsize);
297 cairo_text_extents(cr, text, &extents);
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100298 x = x + direction * (factor * extents.width + extents.x_bearing);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100299 y = y - (extents.height / 2 + extents.y_bearing);
300
301 cairo_move_to(cr, x, y);
302 cairo_show_text(cr, text);
303}
304
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100305static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
306 double fontsize, const char *text)
307{
308 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
309}
310
311static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
312 double x, double y,
313 double fontsize, const char *text)
314{
315 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
316}
317
318static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
319 double x, double y,
320 double fontsize, const char *text)
321{
322 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
323}
324
Jens Axboef3e84402012-03-07 13:14:32 +0100325static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
326 double y, double fontsize,
327 const char *text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100328{
329 double sx, sy;
330 cairo_text_extents_t extents;
331
Jens Axboef3e84402012-03-07 13:14:32 +0100332 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100333
334 cairo_set_font_size(cr, fontsize);
335 cairo_text_extents(cr, text, &extents);
336 sx = x;
337 sy = y;
338 y = y + (extents.width / 2.0 + extents.x_bearing);
339 x = x - (extents.height / 2.0 + extents.y_bearing);
340
341 cairo_move_to(cr, x, y);
342 cairo_save(cr);
343 cairo_translate(cr, -sx, -sy);
344 cairo_rotate(cr, -90.0 * M_PI / 180.0);
345 cairo_translate(cr, sx, sy);
346 cairo_show_text(cr, text);
347 cairo_restore(cr);
348}
349
350static void graph_draw_common(struct graph *g, cairo_t *cr,
351 double *x1, double *y1, double *x2, double *y2)
352{
Jens Axboe4e14f012012-03-23 08:18:57 +0100353 cairo_set_source_rgb(cr, 0, 0, 0);
354 cairo_set_line_width (cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100355
Jens Axboe9ede9c92012-03-14 10:43:20 +0100356 *x1 = 0.10 * g->xdim;
Stephen M. Cameron6bf86002012-03-08 16:58:50 +0100357 *x2 = 0.95 * g->xdim;
358 *y1 = 0.10 * g->ydim;
359 *y2 = 0.90 * g->ydim;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100360
361 cairo_move_to(cr, *x1, *y1);
362 cairo_line_to(cr, *x1, *y2);
363 cairo_line_to(cr, *x2, *y2);
364 cairo_line_to(cr, *x2, *y1);
365 cairo_line_to(cr, *x1, *y1);
366 cairo_stroke(cr);
367
Jens Axboef3e84402012-03-07 13:14:32 +0100368 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
369 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
370 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100371 cairo_stroke(cr);
372}
373
374static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
375 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100376 double minx, double maxx, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100377{
378 struct tickmark *tm;
379 double tx;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100380 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100381 static double dash[] = { 1.0, 2.0 };
382
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100383 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100384 g->x_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100385 if (g->x_axis_unit_change_callback)
386 g->x_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100387
388 for (i = 0; i < nticks; i++) {
389 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100390
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100391 /*
392 * Update tick delta
393 */
394 if (!i) {
395 g->xtick_zero = tx;
396 g->xtick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100397 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100398 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100399 g->xtick_one_val = tm[1].value;
400 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100401
Jens Axboed8fbeef2012-03-14 10:25:44 +0100402 /* really tx < yx || tx > x2, but protect against rounding */
403 if (x1 - tx > 0.01 || tx - x2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100404 continue;
405
406 /* Draw tick mark */
Jens Axboef3e84402012-03-07 13:14:32 +0100407 cairo_set_line_width(cr, 0.8);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100408 cairo_move_to(cr, tx, y2);
409 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
410 cairo_stroke(cr);
411
412 /* draw grid lines */
413 cairo_save(cr);
414 cairo_set_dash(cr, dash, 2, 2.0);
415 cairo_set_line_width(cr, 0.5);
416 cairo_move_to(cr, tx, y1);
417 cairo_line_to(cr, tx, y2);
418 cairo_stroke(cr);
419 cairo_restore(cr);
420
Jens Axboed8fbeef2012-03-14 10:25:44 +0100421 if (!add_tm_text)
422 continue;
423
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100424 /* draw tickmark label */
Jens Axboef3e84402012-03-07 13:14:32 +0100425 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100426 cairo_stroke(cr);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100427 }
428}
429
Jens Axboed8fbeef2012-03-14 10:25:44 +0100430static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100431 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100432 double miny, double maxy, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100433{
434 struct tickmark *tm;
435 double ty;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100436 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100437 static double dash[] = { 2.0, 2.0 };
438
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100439 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100440 g->y_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100441 if (g->y_axis_unit_change_callback)
442 g->y_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100443
Jens Axboed8fbeef2012-03-14 10:25:44 +0100444 /*
445 * Use highest tickmark as top of graph, not highest value. Otherwise
446 * it's impossible to see what the max value is, if the graph is
447 * fairly flat.
448 */
449 maxy = tm[nticks - 1].value;
450
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100451 for (i = 0; i < nticks; i++) {
452 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100453
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100454 /*
455 * Update tick delta
456 */
457 if (!i) {
458 g->ytick_zero = ty;
459 g->ytick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100460 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100461 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100462 g->ytick_one_val = tm[1].value;
463 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100464
Jens Axboed8fbeef2012-03-14 10:25:44 +0100465 /* really ty < y1 || ty > y2, but protect against rounding */
466 if (y1 - ty > 0.01 || ty - y2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100467 continue;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100468
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100469 /* draw tick mark */
470 cairo_move_to(cr, x1, ty);
471 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
472 cairo_stroke(cr);
473
474 /* draw grid lines */
475 cairo_save(cr);
476 cairo_set_dash(cr, dash, 2, 2.0);
477 cairo_set_line_width(cr, 0.5);
478 cairo_move_to(cr, x1, ty);
479 cairo_line_to(cr, x2, ty);
480 cairo_stroke(cr);
481 cairo_restore(cr);
482
Jens Axboed8fbeef2012-03-14 10:25:44 +0100483 if (!add_tm_text)
484 continue;
485
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100486 /* draw tickmark label */
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100487 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100488 cairo_stroke(cr);
489 }
Jens Axboed8fbeef2012-03-14 10:25:44 +0100490
491 /*
492 * Return new max to use
493 */
494 return maxy;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100495}
496
497void bar_graph_draw(struct graph *bg, cairo_t *cr)
498{
499 double x1, y1, x2, y2;
500 double space_per_label, bar_width;
501 double label_offset, mindata, maxdata;
502 int i, nlabels;
503 struct graph_label *lb;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100504 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100505
506 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100507 cairo_translate(cr, bg->xoffset, bg->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100508 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
509
Jens Axboecdae5ff2012-03-22 09:24:05 +0100510 nlabels = count_labels(bg);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100511 space_per_label = (x2 - x1) / (double) nlabels;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100512
Jens Axboebb393792012-03-15 10:52:38 +0100513 /*
514 * Start bars at 0 unless we have negative values, otherwise we
515 * present a skewed picture comparing label X and X+1.
516 */
Jens Axboecdae5ff2012-03-22 09:24:05 +0100517 mindata = find_min_data(bg);
Jens Axboebb393792012-03-15 10:52:38 +0100518 if (mindata > 0)
519 mindata = 0;
520
Jens Axboecdae5ff2012-03-22 09:24:05 +0100521 maxdata = find_max_data(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100522
523 if (fabs(maxdata - mindata) < 1e-20) {
Jens Axboef3e84402012-03-07 13:14:32 +0100524 draw_centered_text(bg, cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100525 x1 + (x2 - x1) / 2.0,
526 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
527 return;
528 }
529
Jens Axboebb393792012-03-15 10:52:38 +0100530 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100531 i = 0;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100532 flist_for_each(entry, &bg->label_list) {
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100533 int nvalues;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100534
535 lb = flist_entry(entry, struct graph_label, list);
536 nvalues = count_values(lb);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100537 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
538 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
539 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
540 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
Jens Axboef3e84402012-03-07 13:14:32 +0100541 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100542 12.0, lb->label);
543 i++;
544 }
545 cairo_stroke(cr);
546 cairo_restore(cr);
547}
548
549typedef double (*xy_value_extractor)(struct graph_value *v);
550
551static double getx(struct graph_value *v)
552{
553 struct xyvalue *xy = v->value;
554 return xy->x;
555}
556
557static double gety(struct graph_value *v)
558{
559 struct xyvalue *xy = v->value;
560 return xy->y;
561}
562
563static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
564{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100565 double tmp, answer = 0.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100566 struct graph_label *i;
567 struct graph_value *j;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100568 struct flist_head *jentry, *entry;
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100569 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100570
Jens Axboecdae5ff2012-03-22 09:24:05 +0100571 flist_for_each(entry, &g->label_list) {
572 i = flist_entry(entry, struct graph_label, list);
573
574 flist_for_each(jentry, &i->value_list) {
575 j = flist_entry(jentry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100576 tmp = getvalue(j);
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100577 if (first) {
578 first = 0;
579 answer = tmp;
580 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100581 answer = cmp(tmp, answer);
582 }
Jens Axboecdae5ff2012-03-22 09:24:05 +0100583 }
584
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100585 return answer;
Jens Axboe01a947f2012-03-22 21:21:00 +0100586}
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100587
588void line_graph_draw(struct graph *g, cairo_t *cr)
589{
590 double x1, y1, x2, y2;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100591 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
592 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100593 struct graph_label *i;
594 struct graph_value *j;
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100595 int good_data = 1, first = 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100596 struct flist_head *entry, *lentry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100597
598 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100599 cairo_translate(cr, g->xoffset, g->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100600 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
601
602 minx = find_xy_value(g, getx, mindouble);
603 maxx = find_xy_value(g, getx, maxdouble);
604 miny = find_xy_value(g, gety, mindouble);
Jens Axboe5aec6682012-03-16 10:17:08 +0100605
606 /*
607 * Start graphs at zero, unless we have a value below. Otherwise
608 * it's hard to visually compare the read and write graph, since
609 * the lowest valued one will be the floor of the graph view.
610 */
611 if (miny > 0)
612 miny = 0;
613
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100614 maxy = find_xy_value(g, gety, maxdouble);
615
616 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100617 good_data = 0;
618 minx = 0.0;
619 miny = 0.0;
620 maxx = 10.0;
621 maxy = 100.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100622 }
623
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100624 top_extra = 0.0;
625 bottom_extra = 0.0;
626 left_extra = 0.0;
627 right_extra = 0.0;
628
629 if (g->top_extra > 0.001)
630 top_extra = fabs(maxy - miny) * g->top_extra;
631 if (g->bottom_extra > 0.001)
632 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
633 if (g->left_extra > 0.001)
634 left_extra = fabs(maxx - minx) * g->left_extra;
635 if (g->right_extra > 0.001)
636 right_extra = fabs(maxx - minx) * g->right_extra;
637
638 gminx = minx - left_extra;
639 gmaxx = maxx + right_extra;
640 gminy = miny - bottom_extra;
641 gmaxy = maxy + top_extra;
642
Jens Axboed8fbeef2012-03-14 10:25:44 +0100643 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
644 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100645
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100646 if (!good_data)
647 goto skip_data;
648
Jens Axboef3e84402012-03-07 13:14:32 +0100649 cairo_set_line_width(cr, 1.5);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100650 flist_for_each(lentry, &g->label_list) {
651 i = flist_entry(lentry, struct graph_label, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100652 first = 1;
Jens Axboe01a947f2012-03-22 21:21:00 +0100653 if (i->hide || i->r < 0) /* invisible data */
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100654 continue;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100655
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100656 cairo_set_source_rgb(cr, i->r, i->g, i->b);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100657 flist_for_each(entry, &i->value_list) {
658 j = flist_entry(entry, struct graph_value, list);
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100659 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
660 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100661 if (first) {
662 cairo_move_to(cr, tx, ty);
663 first = 0;
664 } else {
665 cairo_line_to(cr, tx, ty);
666 }
667 }
668 cairo_stroke(cr);
669 }
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100670
671skip_data:
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100672 cairo_restore(cr);
673}
674
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100675static void setstring(char **str, const char *value)
676{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100677 free(*str);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100678 *str = strdup(value);
679}
680
681void graph_title(struct graph *bg, const char *title)
682{
683 setstring(&bg->title, title);
684}
685
686void graph_x_title(struct graph *bg, const char *title)
687{
688 setstring(&bg->xtitle, title);
689}
690
691void graph_y_title(struct graph *bg, const char *title)
692{
693 setstring(&bg->ytitle, title);
694}
695
696static struct graph_label *graph_find_label(struct graph *bg,
697 const char *label)
698{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100699 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100700 struct graph_label *i;
701
Jens Axboecdae5ff2012-03-22 09:24:05 +0100702 flist_for_each(entry, &bg->label_list) {
703 i = flist_entry(entry, struct graph_label, list);
704
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100705 if (strcmp(label, i->label) == 0)
706 return i;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100707 }
708
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100709 return NULL;
710}
711
Jens Axboe8dfd6072012-03-22 22:10:37 +0100712graph_label_t graph_add_label(struct graph *bg, const char *label)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100713{
714 struct graph_label *i;
715
716 i = graph_find_label(bg, label);
717 if (i)
Jens Axboe8dfd6072012-03-22 22:10:37 +0100718 return i; /* already present. */
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100719 i = calloc(1, sizeof(*i));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100720 INIT_FLIST_HEAD(&i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100721 i->parent = bg;
722 setstring(&i->label, label);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100723 flist_add_tail(&i->list, &bg->label_list);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100724 INIT_PRIO_TREE_ROOT(&i->prio_tree);
Jens Axboe8dfd6072012-03-22 22:10:37 +0100725 return i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100726}
727
Jens Axboecdae5ff2012-03-22 09:24:05 +0100728static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
729{
Jens Axboed0db8192012-03-22 20:48:02 +0100730 flist_del_init(&v->list);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100731 if (v->tooltip)
732 free(v->tooltip);
733 free(v->value);
734 free(v);
735 l->value_count--;
736}
737
738static void graph_value_drop(struct graph_label *l, struct graph_value *v)
739{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100740 if (v->flags & GV_F_PRIO_SKIP) {
741 __graph_value_drop(l, v);
742 return;
743 }
744
Jens Axboecdae5ff2012-03-22 09:24:05 +0100745 /*
746 * Find head, the guy that's on the prio tree
747 */
748 while (!(v->flags & GV_F_ON_PRIO)) {
749 assert(!flist_empty(&v->alias));
750 v = flist_entry(v->alias.next, struct graph_value, alias);
751 }
752
753 prio_tree_remove(&l->prio_tree, &v->node);
754
755 /*
756 * Free aliases
757 */
Jens Axboed0db8192012-03-22 20:48:02 +0100758 while (!flist_empty(&v->alias)) {
Jens Axboecdae5ff2012-03-22 09:24:05 +0100759 struct graph_value *a;
760
Jens Axboed0db8192012-03-22 20:48:02 +0100761 a = flist_entry(v->alias.next, struct graph_value, alias);
762 flist_del_init(&a->alias);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100763
764 __graph_value_drop(l, a);
765 }
766
767 __graph_value_drop(l, v);
768}
769
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100770static void graph_label_add_value(struct graph_label *i, void *value,
771 const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100772{
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100773 struct graph *g = i->parent;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100774 struct graph_value *x;
775
776 x = malloc(sizeof(*x));
Jens Axboe3ca30e62012-03-21 18:54:30 +0100777 memset(x, 0, sizeof(*x));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100778 INIT_FLIST_HEAD(&x->alias);
779 INIT_FLIST_HEAD(&x->list);
780 flist_add_tail(&x->list, &i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100781 i->value_count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100782 x->value = value;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100783
Jens Axboef00e43f2012-03-21 19:53:32 +0100784 if (tooltip) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100785 double xval = getx(x);
786 double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA);
787 double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA);
Jens Axboe4c0cd532012-03-21 19:48:32 +0100788 struct prio_tree_node *ret;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100789
Jens Axboe5fb8d792012-03-21 22:00:43 +0100790 /*
791 * use msec to avoid dropping too much precision when
792 * storing as an integer.
793 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100794 minx = minx * 1000.0;
795 maxx = maxx * 1000.0;
796
Jens Axboe3ca30e62012-03-21 18:54:30 +0100797 INIT_PRIO_TREE_NODE(&x->node);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100798 x->node.start = minx;
799 x->node.last = maxx;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100800 x->tooltip = strdup(tooltip);
Jens Axboeb5526822012-03-21 20:20:35 +0100801 if (x->node.last == x->node.start) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100802 x->node.last += fabs(g->xtick_delta);
803 if (x->node.last == x->node.start)
804 x->node.last++;
Jens Axboeb5526822012-03-21 20:20:35 +0100805 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100806
Jens Axboe4c0cd532012-03-21 19:48:32 +0100807 /*
808 * If ret != &x->node, we have an alias. Since the values
809 * should be identical, we can drop it
810 */
811 ret = prio_tree_insert(&i->prio_tree, &x->node);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100812 if (ret != &x->node) {
813 struct graph_value *alias;
814
815 alias = container_of(ret, struct graph_value, node);
816 flist_add_tail(&x->alias, &alias->alias);
Jens Axboebd10a062012-03-22 09:33:33 +0100817 } else
Jens Axboecdae5ff2012-03-22 09:24:05 +0100818 x->flags = GV_F_ON_PRIO;
Jens Axboe8dfd6072012-03-22 22:10:37 +0100819 } else
820 x->flags = GV_F_PRIO_SKIP;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100821
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100822 if (g->per_label_limit != -1 &&
823 i->value_count > g->per_label_limit) {
Jens Axboec148dae2012-03-11 21:35:47 +0100824 int to_drop = 1;
825
826 /*
827 * If the limit was dynamically reduced, making us more
828 * than 1 entry ahead after adding this one, drop two
829 * entries. This will make us (eventually) reach the
830 * specified limit.
831 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100832 if (i->value_count - g->per_label_limit >= 2)
Jens Axboec148dae2012-03-11 21:35:47 +0100833 to_drop = 2;
834
Jens Axboecdae5ff2012-03-22 09:24:05 +0100835 while (to_drop-- && !flist_empty(&i->value_list)) {
836 x = flist_entry(i->value_list.next, struct graph_value, list);
837 graph_value_drop(i, x);
838
839 /*
840 * If we have aliases, we could drop > 1 above.
841 */
842 if (i->value_count <= g->per_label_limit)
843 break;
Jens Axboec148dae2012-03-11 21:35:47 +0100844 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100845 }
846}
847
Jens Axboe8dfd6072012-03-22 22:10:37 +0100848int graph_add_data(struct graph *bg, graph_label_t label, const double value)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100849{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100850 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100851 double *d;
852
853 d = malloc(sizeof(*d));
854 *d = value;
855
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100856 graph_label_add_value(i, d, NULL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100857 return 0;
858}
859
Jens Axboe01a947f2012-03-22 21:21:00 +0100860static int graph_nonzero_y(struct graph_label *l)
861{
862 struct flist_head *entry;
863
864 flist_for_each(entry, &l->value_list) {
865 struct graph_value *v;
866
867 v = flist_entry(entry, struct graph_value, list);
868 if (gety(v) != 0.0)
869 return 1;
870 }
871
872 return 0;
873}
874
Jens Axboe8dfd6072012-03-22 22:10:37 +0100875int graph_add_xy_data(struct graph *bg, graph_label_t label,
Jens Axboe93e2db22012-03-13 09:45:22 +0100876 const double x, const double y, const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100877{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100878 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100879 struct xyvalue *xy;
880
Jens Axboe01a947f2012-03-22 21:21:00 +0100881 if (bg->dont_graph_all_zeroes && y == 0.0 && !graph_nonzero_y(i))
882 i->hide = 1;
883 else
884 i->hide = 0;
885
886 xy = malloc(sizeof(*xy));
887 xy->x = x;
888 xy->y = y;
889
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100890 graph_label_add_value(i, xy, tooltip);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100891 return 0;
892}
893
Jens Axboecdae5ff2012-03-22 09:24:05 +0100894static void graph_free_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100895{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100896 struct graph_value *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100897
Jens Axboecdae5ff2012-03-22 09:24:05 +0100898 while (!flist_empty(&l->value_list)) {
899 i = flist_entry(l->value_list.next, struct graph_value, list);
900 graph_value_drop(l, i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100901 }
902}
903
Jens Axboecdae5ff2012-03-22 09:24:05 +0100904static void graph_free_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100905{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100906 struct graph_label *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100907
Jens Axboecdae5ff2012-03-22 09:24:05 +0100908 while (!flist_empty(&g->label_list)) {
909 i = flist_entry(g->label_list.next, struct graph_label, list);
910 flist_del(&i->list);
911 graph_free_values(i);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100912 free(i);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100913 }
914}
915
Jens Axboe8dfd6072012-03-22 22:10:37 +0100916void graph_set_color(struct graph *gr, graph_label_t label, double red,
917 double green, double blue)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100918{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100919 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100920 double r, g, b;
921
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100922 if (red < 0.0) { /* invisible color */
923 r = -1.0;
924 g = -1.0;
925 b = -1.0;
926 } else {
927 r = fabs(red);
928 g = fabs(green);
929 b = fabs(blue);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100930
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100931 if (r > 1.0)
932 r = 1.0;
933 if (g > 1.0)
934 g = 1.0;
935 if (b > 1.0)
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100936 b = 1.0;
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100937 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100938
Jens Axboe8dfd6072012-03-22 22:10:37 +0100939 i->r = r;
940 i->g = g;
941 i->b = b;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100942}
943
944void graph_free(struct graph *bg)
945{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100946 free(bg->title);
947 free(bg->xtitle);
948 free(bg->ytitle);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100949 graph_free_labels(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100950}
951
952/* For each line in the line graph, up to per_label_limit segments may
953 * be added. After that, adding more data to the end of the line
954 * causes data to drop off of the front of the line.
955 */
956void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
957{
958 g->per_label_limit = per_label_limit;
959}
960
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100961void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
962 double top_percent, double bottom_percent)
963{
964 g->left_extra = left_percent;
965 g->right_extra = right_percent;
966 g->top_extra = top_percent;
967 g->bottom_extra = bottom_percent;
968}
969
Jens Axboed8fbeef2012-03-14 10:25:44 +0100970/*
971 * Normally values are logged in a base unit of 0, but for other purposes
972 * it makes more sense to log in higher unit. For instance for bandwidth
973 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
974 */
975void graph_set_base_offset(struct graph *g, unsigned int base_offset)
976{
977 g->base_offset = base_offset;
978}
979
Jens Axboe93e2db22012-03-13 09:45:22 +0100980int graph_has_tooltips(struct graph *g)
981{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100982 struct flist_head *entry;
Jens Axboe93e2db22012-03-13 09:45:22 +0100983 struct graph_label *i;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100984
Jens Axboecdae5ff2012-03-22 09:24:05 +0100985 flist_for_each(entry, &g->label_list) {
986 i = flist_entry(entry, struct graph_label, list);
987
988 if (!prio_tree_empty(&i->prio_tree))
Jens Axboe93e2db22012-03-13 09:45:22 +0100989 return 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100990 }
Jens Axboe93e2db22012-03-13 09:45:22 +0100991
992 return 0;
993}
994
995int graph_contains_xy(struct graph *g, int x, int y)
996{
997 int first_x = g->xoffset;
998 int last_x = g->xoffset + g->xdim;
999 int first_y = g->yoffset;
1000 int last_y = g->yoffset + g->ydim;
1001
1002 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
1003}
1004
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001005const char *graph_find_tooltip(struct graph *g, int ix, int iy)
Jens Axboe93e2db22012-03-13 09:45:22 +01001006{
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001007 double x = ix, y = iy;
1008 struct prio_tree_iter iter;
1009 struct prio_tree_node *n;
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001010 struct graph_value *best = NULL;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001011 struct flist_head *entry;
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001012 double best_delta;
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001013 double maxy, miny;
Jens Axboe93e2db22012-03-13 09:45:22 +01001014
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001015 x -= g->xoffset;
1016 y -= g->yoffset;
1017
1018 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
1019 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
1020
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001021 x = x * 1000.0;
1022 maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
1023 miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001024 best_delta = UINT_MAX;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001025 flist_for_each(entry, &g->label_list) {
1026 struct graph_label *i;
1027
1028 i = flist_entry(entry, struct graph_label, list);
Jens Axboe01a947f2012-03-22 21:21:00 +01001029 if (i->hide)
1030 continue;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001031
Jens Axboe08d7d5a2012-03-21 19:33:32 +01001032 INIT_PRIO_TREE_ITER(&iter);
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001033 prio_tree_iter_init(&iter, &i->prio_tree, x, x);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001034
1035 n = prio_tree_next(&iter);
1036 if (!n)
1037 continue;
1038
1039 do {
Jens Axboecdae5ff2012-03-22 09:24:05 +01001040 struct graph_value *v, *rootv;
Jens Axboeb7a69ad2012-03-21 21:55:21 +01001041 double yval, ydiff;
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001042
1043 v = container_of(n, struct graph_value, node);
Jens Axboecdae5ff2012-03-22 09:24:05 +01001044 rootv = v;
1045 do {
1046 yval = gety(v);
1047 ydiff = fabs(yval - y);
Jens Axboe93e2db22012-03-13 09:45:22 +01001048
Jens Axboecdae5ff2012-03-22 09:24:05 +01001049 /*
1050 * zero delta, or within or match critera, break
1051 */
1052 if (ydiff < best_delta) {
1053 best_delta = ydiff;
1054 if (!best_delta ||
1055 (yval >= miny && yval <= maxy)) {
1056 best = v;
1057 break;
1058 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001059 }
Jens Axboecdae5ff2012-03-22 09:24:05 +01001060 if (!flist_empty(&v->alias))
1061 v = flist_entry(v->alias.next, struct graph_value, alias);
1062 } while (v != rootv);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001063 } while ((n = prio_tree_next(&iter)) != NULL);
1064
1065 /*
1066 * If we got matches in one label, don't check others.
1067 */
Jens Axboed0db8192012-03-22 20:48:02 +01001068 if (best)
1069 break;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001070 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001071
1072 if (best)
1073 return best->tooltip;
Jens Axboe93e2db22012-03-13 09:45:22 +01001074
1075 return NULL;
1076}
Jens Axboe01a947f2012-03-22 21:21:00 +01001077
1078void graph_set_graph_all_zeroes(struct graph *g, unsigned int set)
1079{
1080 g->dont_graph_all_zeroes = !set;
1081}