blob: c45954c7f79939b2e38cebacbc028d932076c234 [file] [log] [blame]
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +01001/*
2 * gfio - gui front end for fio - the flexible io tester
3 *
Jens Axboe3c3ed072012-03-27 09:12:39 +02004 * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com>
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +01005 *
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"
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +020036#include "cairo_text_helpers.h"
Jens Axboeb65c7ec2012-03-21 17:17:45 +010037
38/*
39 * Allowable difference to show tooltip
40 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +010041#define TOOLTIP_DELTA 0.08
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010042
43struct xyvalue {
44 double x, y;
45};
46
Jens Axboecdae5ff2012-03-22 09:24:05 +010047enum {
48 GV_F_ON_PRIO = 1,
Jens Axboe8dfd6072012-03-22 22:10:37 +010049 GV_F_PRIO_SKIP = 2,
Jens Axboecdae5ff2012-03-22 09:24:05 +010050};
51
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010052struct graph_value {
Jens Axboecdae5ff2012-03-22 09:24:05 +010053 struct flist_head list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010054 struct prio_tree_node node;
Jens Axboecdae5ff2012-03-22 09:24:05 +010055 struct flist_head alias;
56 unsigned int flags;
Jens Axboe93e2db22012-03-13 09:45:22 +010057 char *tooltip;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010058 void *value;
59};
60
61struct graph_label {
Jens Axboecdae5ff2012-03-22 09:24:05 +010062 struct flist_head list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010063 char *label;
Jens Axboecdae5ff2012-03-22 09:24:05 +010064 struct flist_head value_list;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010065 struct prio_tree_root prio_tree;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010066 double r, g, b;
Jens Axboe01a947f2012-03-22 21:21:00 +010067 int hide;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010068 int value_count;
69 struct graph *parent;
70};
71
Jens Axboeb65c7ec2012-03-21 17:17:45 +010072struct tick_value {
73 unsigned int offset;
74 double value;
75};
76
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010077struct graph {
78 char *title;
79 char *xtitle;
80 char *ytitle;
Jens Axboe87d5f272012-03-07 12:31:40 +010081 unsigned int xdim, ydim;
Stephen M. Cameron57f9d282012-03-11 11:36:51 +010082 double xoffset, yoffset;
Jens Axboecdae5ff2012-03-22 09:24:05 +010083 struct flist_head label_list;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +010084 int per_label_limit;
Jens Axboef3e84402012-03-07 13:14:32 +010085 const char *font;
Stephen M. Cameron7175d912012-03-11 11:35:10 +010086 graph_axis_unit_change_callback x_axis_unit_change_callback;
87 graph_axis_unit_change_callback y_axis_unit_change_callback;
Jens Axboed8fbeef2012-03-14 10:25:44 +010088 unsigned int base_offset;
Jens Axboe01a947f2012-03-22 21:21:00 +010089 unsigned int dont_graph_all_zeroes;
Jens Axboe3c3ed072012-03-27 09:12:39 +020090 double left_extra;
91 double right_extra;
92 double top_extra;
93 double bottom_extra;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010094
95 double xtick_zero;
96 double xtick_delta;
97 double xtick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +010098 double xtick_one_val;
Jens Axboeb65c7ec2012-03-21 17:17:45 +010099 double ytick_zero;
100 double ytick_delta;
101 double ytick_zero_val;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100102 double ytick_one_val;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100103};
104
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100105void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
106{
107 g->xdim = xdim;
108 g->ydim = ydim;
109}
110
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100111void graph_set_position(struct graph *g, double xoffset, double yoffset)
112{
113 g->xoffset = xoffset;
114 g->yoffset = yoffset;
115}
116
Jens Axboef3e84402012-03-07 13:14:32 +0100117struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100118{
119 struct graph *g;
120
121 g = calloc(1, sizeof(*g));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100122 INIT_FLIST_HEAD(&g->label_list);
Stephen M. Cameron3ea48b82012-03-07 19:40:58 +0100123 graph_set_size(g, xdim, ydim);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100124 g->per_label_limit = -1;
Jens Axboef3e84402012-03-07 13:14:32 +0100125 g->font = font;
126 if (!g->font)
Jens Axboea1e79722012-03-23 10:52:25 +0100127 g->font = GRAPH_DEFAULT_FONT;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100128 return g;
129}
130
Jens Axboea1e79722012-03-23 10:52:25 +0100131void graph_set_font(struct graph *g, const char *font)
132{
133 g->font = font;
134}
135
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100136void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
137{
138 g->x_axis_unit_change_callback = f;
139}
140
141void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
142{
143 g->y_axis_unit_change_callback = f;
144}
145
Jens Axboecdae5ff2012-03-22 09:24:05 +0100146static int count_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100147{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100148 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100149 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100150
Jens Axboecdae5ff2012-03-22 09:24:05 +0100151 flist_for_each(entry, &g->label_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100152 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100153
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100154 return count;
155}
156
Jens Axboecdae5ff2012-03-22 09:24:05 +0100157static int count_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100158{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100159 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100160 int count = 0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100161
Jens Axboecdae5ff2012-03-22 09:24:05 +0100162 flist_for_each(entry, &l->value_list)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100163 count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100164
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100165 return count;
166}
167
168typedef double (*double_comparator)(double a, double b);
169
170static double mindouble(double a, double b)
171{
172 return a < b ? a : b;
173}
174
175static double maxdouble(double a, double b)
176{
177 return a < b ? b : a;
178}
179
Jens Axboecdae5ff2012-03-22 09:24:05 +0100180static double find_double_values(struct graph_label *l, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100181{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100182 struct flist_head *entry;
Jens Axboe4aaefbf2013-01-09 14:11:53 +0100183 double answer = 0.0, tmp;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100184 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100185
Jens Axboe01a947f2012-03-22 21:21:00 +0100186 if (flist_empty(&l->value_list))
187 return 0.0;
188
Jens Axboecdae5ff2012-03-22 09:24:05 +0100189 flist_for_each(entry, &l->value_list) {
190 struct graph_value *i;
191
192 i = flist_entry(entry, struct graph_value, list);
193 tmp = *(double *) i->value;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100194 if (first) {
195 answer = tmp;
196 first = 0;
197 } else {
198 answer = cmp(answer, tmp);
199 }
200 }
201 return answer;
202}
203
Jens Axboecdae5ff2012-03-22 09:24:05 +0100204static double find_double_data(struct graph *g, double_comparator cmp)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100205{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100206 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100207 struct graph_label *i;
208 int first = 1;
209 double answer, tmp;
210
Jens Axboe01a947f2012-03-22 21:21:00 +0100211 if (flist_empty(&g->label_list))
212 return 0.0;
213
Jens Axboecdae5ff2012-03-22 09:24:05 +0100214 flist_for_each(entry, &g->label_list) {
215 i = flist_entry(entry, struct graph_label, list);
216 tmp = find_double_values(i, cmp);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100217 if (first) {
218 answer = tmp;
219 first = 0;
220 } else {
221 answer = cmp(tmp, answer);
222 }
223 }
224 return answer;
225}
226
Jens Axboecdae5ff2012-03-22 09:24:05 +0100227static double find_min_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100228{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100229 return find_double_data(g, mindouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100230}
231
Jens Axboecdae5ff2012-03-22 09:24:05 +0100232static double find_max_data(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100233{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100234 return find_double_data(g, maxdouble);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100235}
236
237static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
238 double label_offset, double bar_width,
239 double mindata, double maxdata)
240{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100241 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100242 double x1, y1, x2, y2;
243 int bar_num = 0;
244 double domain, range, v;
245
246 domain = (maxdata - mindata);
247 range = (double) bg->ydim * 0.80; /* FIXME */
248 cairo_stroke(cr);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100249 flist_for_each(entry, &lb->value_list) {
250 struct graph_value *i;
251
252 i = flist_entry(entry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100253
254 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
255 x2 = x1 + bar_width * 0.90;
256 y2 = bg->ydim * 0.90;
257 v = *(double *) i->value;
258 y1 = y2 - (((v - mindata) / domain) * range);
259 cairo_move_to(cr, x1, y1);
260 cairo_line_to(cr, x1, y2);
261 cairo_line_to(cr, x2, y2);
262 cairo_line_to(cr, x2, y1);
263 cairo_close_path(cr);
264 cairo_fill(cr);
265 cairo_stroke(cr);
Jens Axboe3c3ed072012-03-27 09:12:39 +0200266 bar_num++;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100267 }
268}
269
Jens Axboef3a31762012-04-12 11:16:17 +0200270static void graph_draw_common(struct graph *g, cairo_t *cr, double *x1,
271 double *y1, double *x2, double *y2)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100272{
Jens Axboef3a31762012-04-12 11:16:17 +0200273 const double shade_col[3][3] = { { 0.55, 0.54, 0.54 },
274 { 0.80, 0.78, 0.78 },
275 { 0.93, 0.91, 0.91 } };
276 int i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100277
Jens Axboe3c3ed072012-03-27 09:12:39 +0200278 *x1 = 0.10 * g->xdim;
Stephen M. Cameron6bf86002012-03-08 16:58:50 +0100279 *x2 = 0.95 * g->xdim;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200280 *y1 = 0.10 * g->ydim;
Stephen M. Cameron6bf86002012-03-08 16:58:50 +0100281 *y2 = 0.90 * g->ydim;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100282
Jens Axboef3a31762012-04-12 11:16:17 +0200283 /*
284 * Add shade
285 */
286 cairo_set_line_width(cr, 1.0);
287 for (i = 0; i < 3; i++) {
288 float offset = i + 1.0;
289
290 cairo_set_source_rgb(cr, shade_col[i][0], shade_col[i][1], shade_col[i][2]);
291 cairo_move_to(cr, offset + *x1, *y1 - offset);
292 cairo_line_to(cr, *x2 + offset, *y1 - offset);
293 cairo_line_to(cr, *x2 + offset, *y2 - offset);
294 cairo_stroke(cr);
295 }
296
297 cairo_set_source_rgb(cr, 0, 0, 0);
298 cairo_set_line_width(cr, 1.2);
299
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100300 cairo_move_to(cr, *x1, *y1);
301 cairo_line_to(cr, *x1, *y2);
302 cairo_line_to(cr, *x2, *y2);
303 cairo_line_to(cr, *x2, *y1);
304 cairo_line_to(cr, *x1, *y1);
305 cairo_stroke(cr);
306
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200307 draw_centered_text(cr, g->font, g->xdim / 2, g->ydim / 20, 20.0, g->title);
308 draw_centered_text(cr, g->font, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
309 draw_vertical_centered_text(cr, g->font, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100310 cairo_stroke(cr);
311}
312
313static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
314 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100315 double minx, double maxx, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100316{
317 struct tickmark *tm;
318 double tx;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100319 int i, power_of_ten;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100320 static double dash[] = { 1.0, 2.0 };
321
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100322 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100323 g->x_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100324 if (g->x_axis_unit_change_callback)
325 g->x_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100326
327 for (i = 0; i < nticks; i++) {
328 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100329
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100330 /*
331 * Update tick delta
332 */
333 if (!i) {
334 g->xtick_zero = tx;
335 g->xtick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100336 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100337 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100338 g->xtick_one_val = tm[1].value;
339 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100340
Jens Axboed8fbeef2012-03-14 10:25:44 +0100341 /* really tx < yx || tx > x2, but protect against rounding */
342 if (x1 - tx > 0.01 || tx - x2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100343 continue;
344
345 /* Draw tick mark */
Jens Axboe6bc821b2012-04-13 13:54:02 +0200346 cairo_set_line_width(cr, 1.0);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100347 cairo_move_to(cr, tx, y2);
348 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
349 cairo_stroke(cr);
350
351 /* draw grid lines */
352 cairo_save(cr);
Jens Axboe6bc821b2012-04-13 13:54:02 +0200353 cairo_set_dash(cr, dash, 2, 0.66);
354 cairo_set_line_width(cr, 0.33);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100355 cairo_move_to(cr, tx, y1);
356 cairo_line_to(cr, tx, y2);
357 cairo_stroke(cr);
358 cairo_restore(cr);
359
Jens Axboed8fbeef2012-03-14 10:25:44 +0100360 if (!add_tm_text)
361 continue;
362
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100363 /* draw tickmark label */
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200364 draw_centered_text(cr, g->font, tx, y2 * 1.04, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100365 cairo_stroke(cr);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100366 }
367}
368
Jens Axboed8fbeef2012-03-14 10:25:44 +0100369static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100370 double x1, double y1, double x2, double y2,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100371 double miny, double maxy, int nticks, int add_tm_text)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100372{
373 struct tickmark *tm;
374 double ty;
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100375 int i, power_of_ten;
Jens Axboe6bc821b2012-04-13 13:54:02 +0200376 static double dash[] = { 1.0, 2.0 };
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100377
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100378 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
Jens Axboed8fbeef2012-03-14 10:25:44 +0100379 g->y_axis_unit_change_callback == NULL, g->base_offset);
Stephen M. Cameron7175d912012-03-11 11:35:10 +0100380 if (g->y_axis_unit_change_callback)
381 g->y_axis_unit_change_callback(g, power_of_ten);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100382
Jens Axboed8fbeef2012-03-14 10:25:44 +0100383 /*
384 * Use highest tickmark as top of graph, not highest value. Otherwise
385 * it's impossible to see what the max value is, if the graph is
386 * fairly flat.
387 */
388 maxy = tm[nticks - 1].value;
389
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100390 for (i = 0; i < nticks; i++) {
391 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100392
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100393 /*
394 * Update tick delta
395 */
396 if (!i) {
397 g->ytick_zero = ty;
398 g->ytick_zero_val = tm[0].value;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100399 } else if (i == 1) {
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100400 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100401 g->ytick_one_val = tm[1].value;
402 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100403
Jens Axboed8fbeef2012-03-14 10:25:44 +0100404 /* really ty < y1 || ty > y2, but protect against rounding */
405 if (y1 - ty > 0.01 || ty - y2 > 0.01)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100406 continue;
Jens Axboed8fbeef2012-03-14 10:25:44 +0100407
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100408 /* draw tick mark */
409 cairo_move_to(cr, x1, ty);
410 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
411 cairo_stroke(cr);
412
413 /* draw grid lines */
414 cairo_save(cr);
Jens Axboe6bc821b2012-04-13 13:54:02 +0200415 cairo_set_dash(cr, dash, 2, 0.66);
416 cairo_set_line_width(cr, 0.33);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100417 cairo_move_to(cr, x1, ty);
418 cairo_line_to(cr, x2, ty);
419 cairo_stroke(cr);
420 cairo_restore(cr);
421
Jens Axboed8fbeef2012-03-14 10:25:44 +0100422 if (!add_tm_text)
423 continue;
424
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100425 /* draw tickmark label */
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200426 draw_right_justified_text(cr, g->font, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100427 cairo_stroke(cr);
428 }
Jens Axboed8fbeef2012-03-14 10:25:44 +0100429
430 /*
431 * Return new max to use
432 */
433 return maxy;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100434}
435
436void bar_graph_draw(struct graph *bg, cairo_t *cr)
437{
438 double x1, y1, x2, y2;
439 double space_per_label, bar_width;
440 double label_offset, mindata, maxdata;
441 int i, nlabels;
442 struct graph_label *lb;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100443 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100444
445 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100446 cairo_translate(cr, bg->xoffset, bg->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100447 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
448
Jens Axboecdae5ff2012-03-22 09:24:05 +0100449 nlabels = count_labels(bg);
Jens Axboed8fbeef2012-03-14 10:25:44 +0100450 space_per_label = (x2 - x1) / (double) nlabels;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100451
Jens Axboebb393792012-03-15 10:52:38 +0100452 /*
453 * Start bars at 0 unless we have negative values, otherwise we
454 * present a skewed picture comparing label X and X+1.
455 */
Jens Axboecdae5ff2012-03-22 09:24:05 +0100456 mindata = find_min_data(bg);
Jens Axboebb393792012-03-15 10:52:38 +0100457 if (mindata > 0)
458 mindata = 0;
459
Jens Axboecdae5ff2012-03-22 09:24:05 +0100460 maxdata = find_max_data(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100461
462 if (fabs(maxdata - mindata) < 1e-20) {
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200463 draw_centered_text(cr, bg->font,
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100464 x1 + (x2 - x1) / 2.0,
465 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
466 return;
467 }
468
Jens Axboebb393792012-03-15 10:52:38 +0100469 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100470 i = 0;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100471 flist_for_each(entry, &bg->label_list) {
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100472 int nvalues;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100473
474 lb = flist_entry(entry, struct graph_label, list);
475 nvalues = count_values(lb);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100476 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
477 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
478 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
479 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
Stephen M. Cameronee2f55b2012-03-27 08:14:09 +0200480 draw_centered_text(cr, bg->font, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
Jens Axboe3c3ed072012-03-27 09:12:39 +0200481 12.0, lb->label);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100482 i++;
483 }
484 cairo_stroke(cr);
485 cairo_restore(cr);
486}
487
488typedef double (*xy_value_extractor)(struct graph_value *v);
489
490static double getx(struct graph_value *v)
491{
492 struct xyvalue *xy = v->value;
493 return xy->x;
494}
495
496static double gety(struct graph_value *v)
497{
498 struct xyvalue *xy = v->value;
499 return xy->y;
500}
501
502static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
503{
Stephen M. Cameron10e54cd2012-03-07 19:39:55 +0100504 double tmp, answer = 0.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100505 struct graph_label *i;
506 struct graph_value *j;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100507 struct flist_head *jentry, *entry;
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100508 int first = 1;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100509
Jens Axboecdae5ff2012-03-22 09:24:05 +0100510 flist_for_each(entry, &g->label_list) {
511 i = flist_entry(entry, struct graph_label, list);
512
513 flist_for_each(jentry, &i->value_list) {
514 j = flist_entry(jentry, struct graph_value, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100515 tmp = getvalue(j);
Stephen M. Camerond582bf72012-03-07 19:37:57 +0100516 if (first) {
517 first = 0;
518 answer = tmp;
519 }
Jens Axboe3c3ed072012-03-27 09:12:39 +0200520 answer = cmp(tmp, answer);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100521 }
Jens Axboecdae5ff2012-03-22 09:24:05 +0100522 }
523
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100524 return answer;
Jens Axboe01a947f2012-03-22 21:21:00 +0100525}
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100526
527void line_graph_draw(struct graph *g, cairo_t *cr)
528{
529 double x1, y1, x2, y2;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100530 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
531 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100532 struct graph_label *i;
533 struct graph_value *j;
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100534 int good_data = 1, first = 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100535 struct flist_head *entry, *lentry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100536
537 cairo_save(cr);
Stephen M. Cameron57f9d282012-03-11 11:36:51 +0100538 cairo_translate(cr, g->xoffset, g->yoffset);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100539 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
540
541 minx = find_xy_value(g, getx, mindouble);
542 maxx = find_xy_value(g, getx, maxdouble);
543 miny = find_xy_value(g, gety, mindouble);
Jens Axboe5aec6682012-03-16 10:17:08 +0100544
545 /*
546 * Start graphs at zero, unless we have a value below. Otherwise
547 * it's hard to visually compare the read and write graph, since
548 * the lowest valued one will be the floor of the graph view.
549 */
550 if (miny > 0)
551 miny = 0;
552
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100553 maxy = find_xy_value(g, gety, maxdouble);
554
555 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100556 good_data = 0;
557 minx = 0.0;
558 miny = 0.0;
559 maxx = 10.0;
560 maxy = 100.0;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100561 }
562
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100563 top_extra = 0.0;
564 bottom_extra = 0.0;
565 left_extra = 0.0;
566 right_extra = 0.0;
567
568 if (g->top_extra > 0.001)
569 top_extra = fabs(maxy - miny) * g->top_extra;
570 if (g->bottom_extra > 0.001)
571 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
572 if (g->left_extra > 0.001)
573 left_extra = fabs(maxx - minx) * g->left_extra;
574 if (g->right_extra > 0.001)
575 right_extra = fabs(maxx - minx) * g->right_extra;
576
577 gminx = minx - left_extra;
578 gmaxx = maxx + right_extra;
579 gminy = miny - bottom_extra;
580 gmaxy = maxy + top_extra;
581
Jens Axboed8fbeef2012-03-14 10:25:44 +0100582 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
583 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100584
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100585 if (!good_data)
586 goto skip_data;
587
Jens Axboef3e84402012-03-07 13:14:32 +0100588 cairo_set_line_width(cr, 1.5);
Jens Axboe10987b92012-04-13 14:23:17 +0200589 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
590
Jens Axboecdae5ff2012-03-22 09:24:05 +0100591 flist_for_each(lentry, &g->label_list) {
592 i = flist_entry(lentry, struct graph_label, list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100593 first = 1;
Jens Axboe01a947f2012-03-22 21:21:00 +0100594 if (i->hide || i->r < 0) /* invisible data */
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100595 continue;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100596
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100597 cairo_set_source_rgb(cr, i->r, i->g, i->b);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100598 flist_for_each(entry, &i->value_list) {
599 j = flist_entry(entry, struct graph_value, list);
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100600 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
601 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100602 if (first) {
603 cairo_move_to(cr, tx, ty);
604 first = 0;
Jens Axboe10987b92012-04-13 14:23:17 +0200605 } else
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100606 cairo_line_to(cr, tx, ty);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100607 }
608 cairo_stroke(cr);
609 }
Stephen M. Cameron9ce9cfb2012-03-07 19:37:25 +0100610
611skip_data:
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100612 cairo_restore(cr);
613}
614
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100615static void setstring(char **str, const char *value)
616{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100617 free(*str);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100618 *str = strdup(value);
619}
620
621void graph_title(struct graph *bg, const char *title)
622{
623 setstring(&bg->title, title);
624}
625
626void graph_x_title(struct graph *bg, const char *title)
627{
628 setstring(&bg->xtitle, title);
629}
630
631void graph_y_title(struct graph *bg, const char *title)
632{
633 setstring(&bg->ytitle, title);
634}
635
636static struct graph_label *graph_find_label(struct graph *bg,
637 const char *label)
638{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100639 struct flist_head *entry;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100640 struct graph_label *i;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200641
Jens Axboecdae5ff2012-03-22 09:24:05 +0100642 flist_for_each(entry, &bg->label_list) {
643 i = flist_entry(entry, struct graph_label, list);
644
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100645 if (strcmp(label, i->label) == 0)
646 return i;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100647 }
648
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100649 return NULL;
650}
651
Jens Axboe8dfd6072012-03-22 22:10:37 +0100652graph_label_t graph_add_label(struct graph *bg, const char *label)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100653{
654 struct graph_label *i;
Jens Axboe3c3ed072012-03-27 09:12:39 +0200655
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100656 i = graph_find_label(bg, label);
657 if (i)
Jens Axboe8dfd6072012-03-22 22:10:37 +0100658 return i; /* already present. */
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100659 i = calloc(1, sizeof(*i));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100660 INIT_FLIST_HEAD(&i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100661 i->parent = bg;
662 setstring(&i->label, label);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100663 flist_add_tail(&i->list, &bg->label_list);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100664 INIT_PRIO_TREE_ROOT(&i->prio_tree);
Jens Axboe8dfd6072012-03-22 22:10:37 +0100665 return i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100666}
667
Jens Axboecdae5ff2012-03-22 09:24:05 +0100668static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
669{
Jens Axboed0db8192012-03-22 20:48:02 +0100670 flist_del_init(&v->list);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100671 if (v->tooltip)
672 free(v->tooltip);
673 free(v->value);
674 free(v);
675 l->value_count--;
676}
677
678static void graph_value_drop(struct graph_label *l, struct graph_value *v)
679{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100680 if (v->flags & GV_F_PRIO_SKIP) {
681 __graph_value_drop(l, v);
682 return;
683 }
684
Jens Axboecdae5ff2012-03-22 09:24:05 +0100685 /*
686 * Find head, the guy that's on the prio tree
687 */
688 while (!(v->flags & GV_F_ON_PRIO)) {
689 assert(!flist_empty(&v->alias));
Jens Axboe12dbd062014-07-03 21:19:57 -0600690 v = flist_first_entry(&v->alias, struct graph_value, alias);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100691 }
692
693 prio_tree_remove(&l->prio_tree, &v->node);
694
695 /*
696 * Free aliases
697 */
Jens Axboed0db8192012-03-22 20:48:02 +0100698 while (!flist_empty(&v->alias)) {
Jens Axboecdae5ff2012-03-22 09:24:05 +0100699 struct graph_value *a;
700
Jens Axboe12dbd062014-07-03 21:19:57 -0600701 a = flist_first_entry(&v->alias, struct graph_value, alias);
Jens Axboed0db8192012-03-22 20:48:02 +0100702 flist_del_init(&a->alias);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100703
704 __graph_value_drop(l, a);
705 }
706
707 __graph_value_drop(l, v);
708}
709
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100710static void graph_label_add_value(struct graph_label *i, void *value,
711 const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100712{
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100713 struct graph *g = i->parent;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100714 struct graph_value *x;
715
716 x = malloc(sizeof(*x));
Jens Axboe3ca30e62012-03-21 18:54:30 +0100717 memset(x, 0, sizeof(*x));
Jens Axboecdae5ff2012-03-22 09:24:05 +0100718 INIT_FLIST_HEAD(&x->alias);
719 INIT_FLIST_HEAD(&x->list);
720 flist_add_tail(&x->list, &i->value_list);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100721 i->value_count++;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100722 x->value = value;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100723
Jens Axboef00e43f2012-03-21 19:53:32 +0100724 if (tooltip) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100725 double xval = getx(x);
726 double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA);
727 double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA);
Jens Axboe4c0cd532012-03-21 19:48:32 +0100728 struct prio_tree_node *ret;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100729
Jens Axboe5fb8d792012-03-21 22:00:43 +0100730 /*
731 * use msec to avoid dropping too much precision when
732 * storing as an integer.
733 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100734 minx = minx * 1000.0;
735 maxx = maxx * 1000.0;
736
Jens Axboe3ca30e62012-03-21 18:54:30 +0100737 INIT_PRIO_TREE_NODE(&x->node);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100738 x->node.start = minx;
739 x->node.last = maxx;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100740 x->tooltip = strdup(tooltip);
Jens Axboeb5526822012-03-21 20:20:35 +0100741 if (x->node.last == x->node.start) {
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100742 x->node.last += fabs(g->xtick_delta);
743 if (x->node.last == x->node.start)
744 x->node.last++;
Jens Axboeb5526822012-03-21 20:20:35 +0100745 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100746
Jens Axboe4c0cd532012-03-21 19:48:32 +0100747 /*
748 * If ret != &x->node, we have an alias. Since the values
749 * should be identical, we can drop it
750 */
751 ret = prio_tree_insert(&i->prio_tree, &x->node);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100752 if (ret != &x->node) {
753 struct graph_value *alias;
754
755 alias = container_of(ret, struct graph_value, node);
756 flist_add_tail(&x->alias, &alias->alias);
Jens Axboebd10a062012-03-22 09:33:33 +0100757 } else
Jens Axboecdae5ff2012-03-22 09:24:05 +0100758 x->flags = GV_F_ON_PRIO;
Jens Axboe8dfd6072012-03-22 22:10:37 +0100759 } else
760 x->flags = GV_F_PRIO_SKIP;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100761
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100762 if (g->per_label_limit != -1 &&
763 i->value_count > g->per_label_limit) {
Jens Axboec148dae2012-03-11 21:35:47 +0100764 int to_drop = 1;
765
766 /*
767 * If the limit was dynamically reduced, making us more
768 * than 1 entry ahead after adding this one, drop two
769 * entries. This will make us (eventually) reach the
770 * specified limit.
771 */
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100772 if (i->value_count - g->per_label_limit >= 2)
Jens Axboec148dae2012-03-11 21:35:47 +0100773 to_drop = 2;
774
Jens Axboecdae5ff2012-03-22 09:24:05 +0100775 while (to_drop-- && !flist_empty(&i->value_list)) {
Jens Axboe12dbd062014-07-03 21:19:57 -0600776 x = flist_first_entry(&i->value_list, struct graph_value, list);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100777 graph_value_drop(i, x);
778
779 /*
780 * If we have aliases, we could drop > 1 above.
781 */
782 if (i->value_count <= g->per_label_limit)
783 break;
Jens Axboec148dae2012-03-11 21:35:47 +0100784 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100785 }
786}
787
Jens Axboe8dfd6072012-03-22 22:10:37 +0100788int graph_add_data(struct graph *bg, graph_label_t label, const double value)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100789{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100790 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100791 double *d;
792
793 d = malloc(sizeof(*d));
794 *d = value;
795
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100796 graph_label_add_value(i, d, NULL);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100797 return 0;
798}
799
Jens Axboe01a947f2012-03-22 21:21:00 +0100800static int graph_nonzero_y(struct graph_label *l)
801{
802 struct flist_head *entry;
803
804 flist_for_each(entry, &l->value_list) {
805 struct graph_value *v;
806
807 v = flist_entry(entry, struct graph_value, list);
808 if (gety(v) != 0.0)
809 return 1;
810 }
811
812 return 0;
813}
814
Jens Axboe8dfd6072012-03-22 22:10:37 +0100815int graph_add_xy_data(struct graph *bg, graph_label_t label,
Jens Axboe93e2db22012-03-13 09:45:22 +0100816 const double x, const double y, const char *tooltip)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100817{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100818 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100819 struct xyvalue *xy;
820
Jens Axboe01a947f2012-03-22 21:21:00 +0100821 if (bg->dont_graph_all_zeroes && y == 0.0 && !graph_nonzero_y(i))
822 i->hide = 1;
823 else
824 i->hide = 0;
825
826 xy = malloc(sizeof(*xy));
827 xy->x = x;
828 xy->y = y;
829
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100830 graph_label_add_value(i, xy, tooltip);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100831 return 0;
832}
833
Jens Axboecdae5ff2012-03-22 09:24:05 +0100834static void graph_free_values(struct graph_label *l)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100835{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100836 struct graph_value *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100837
Jens Axboecdae5ff2012-03-22 09:24:05 +0100838 while (!flist_empty(&l->value_list)) {
Jens Axboe12dbd062014-07-03 21:19:57 -0600839 i = flist_first_entry(&l->value_list, struct graph_value, list);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100840 graph_value_drop(l, i);
Jens Axboe3c3ed072012-03-27 09:12:39 +0200841 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100842}
843
Jens Axboecdae5ff2012-03-22 09:24:05 +0100844static void graph_free_labels(struct graph *g)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100845{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100846 struct graph_label *i;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100847
Jens Axboecdae5ff2012-03-22 09:24:05 +0100848 while (!flist_empty(&g->label_list)) {
Jens Axboe12dbd062014-07-03 21:19:57 -0600849 i = flist_first_entry(&g->label_list, struct graph_label, list);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100850 flist_del(&i->list);
851 graph_free_values(i);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100852 free(i);
Jens Axboe3c3ed072012-03-27 09:12:39 +0200853 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100854}
855
Jens Axboe5721d272012-09-26 14:11:49 +0200856void graph_clear_values(struct graph *g)
857{
858 struct flist_head *node;
859 struct graph_label *i;
860
861 flist_for_each(node, &g->label_list) {
862 i = flist_entry(node, struct graph_label, list);
863 graph_free_values(i);
864 }
865}
866
Jens Axboe8dfd6072012-03-22 22:10:37 +0100867void graph_set_color(struct graph *gr, graph_label_t label, double red,
868 double green, double blue)
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100869{
Jens Axboe8dfd6072012-03-22 22:10:37 +0100870 struct graph_label *i = label;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100871 double r, g, b;
872
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100873 if (red < 0.0) { /* invisible color */
874 r = -1.0;
875 g = -1.0;
876 b = -1.0;
877 } else {
878 r = fabs(red);
879 g = fabs(green);
880 b = fabs(blue);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100881
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100882 if (r > 1.0)
883 r = 1.0;
884 if (g > 1.0)
885 g = 1.0;
886 if (b > 1.0)
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100887 b = 1.0;
Stephen M. Cameroncae08722012-03-07 14:47:38 +0100888 }
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100889
Jens Axboe8dfd6072012-03-22 22:10:37 +0100890 i->r = r;
891 i->g = g;
892 i->b = b;
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100893}
894
895void graph_free(struct graph *bg)
896{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100897 free(bg->title);
898 free(bg->xtitle);
899 free(bg->ytitle);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100900 graph_free_labels(bg);
Stephen M. Cameronaf58ef32012-03-07 07:56:16 +0100901}
902
903/* For each line in the line graph, up to per_label_limit segments may
904 * be added. After that, adding more data to the end of the line
905 * causes data to drop off of the front of the line.
906 */
907void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
908{
909 g->per_label_limit = per_label_limit;
910}
911
Jens Axboe3c3ed072012-03-27 09:12:39 +0200912void graph_add_extra_space(struct graph *g, double left_percent,
913 double right_percent, double top_percent,
914 double bottom_percent)
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100915{
Jens Axboe3c3ed072012-03-27 09:12:39 +0200916 g->left_extra = left_percent;
917 g->right_extra = right_percent;
918 g->top_extra = top_percent;
919 g->bottom_extra = bottom_percent;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100920}
921
Jens Axboed8fbeef2012-03-14 10:25:44 +0100922/*
923 * Normally values are logged in a base unit of 0, but for other purposes
924 * it makes more sense to log in higher unit. For instance for bandwidth
925 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
926 */
927void graph_set_base_offset(struct graph *g, unsigned int base_offset)
928{
929 g->base_offset = base_offset;
930}
931
Jens Axboe93e2db22012-03-13 09:45:22 +0100932int graph_has_tooltips(struct graph *g)
933{
Jens Axboecdae5ff2012-03-22 09:24:05 +0100934 struct flist_head *entry;
Jens Axboe93e2db22012-03-13 09:45:22 +0100935 struct graph_label *i;
Stephen M. Camerondef0ac22012-03-12 07:32:57 +0100936
Jens Axboecdae5ff2012-03-22 09:24:05 +0100937 flist_for_each(entry, &g->label_list) {
938 i = flist_entry(entry, struct graph_label, list);
939
940 if (!prio_tree_empty(&i->prio_tree))
Jens Axboe93e2db22012-03-13 09:45:22 +0100941 return 1;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100942 }
Jens Axboe93e2db22012-03-13 09:45:22 +0100943
944 return 0;
945}
946
947int graph_contains_xy(struct graph *g, int x, int y)
948{
949 int first_x = g->xoffset;
950 int last_x = g->xoffset + g->xdim;
951 int first_y = g->yoffset;
952 int last_y = g->yoffset + g->ydim;
953
954 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
955}
956
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100957const char *graph_find_tooltip(struct graph *g, int ix, int iy)
Jens Axboe93e2db22012-03-13 09:45:22 +0100958{
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100959 double x = ix, y = iy;
960 struct prio_tree_iter iter;
961 struct prio_tree_node *n;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100962 struct graph_value *best = NULL;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100963 struct flist_head *entry;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100964 double best_delta;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100965 double maxy, miny;
Jens Axboe93e2db22012-03-13 09:45:22 +0100966
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100967 x -= g->xoffset;
968 y -= g->yoffset;
969
970 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
971 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
972
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100973 x = x * 1000.0;
974 maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
975 miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100976 best_delta = UINT_MAX;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100977 flist_for_each(entry, &g->label_list) {
978 struct graph_label *i;
979
980 i = flist_entry(entry, struct graph_label, list);
Jens Axboe01a947f2012-03-22 21:21:00 +0100981 if (i->hide)
982 continue;
Jens Axboecdae5ff2012-03-22 09:24:05 +0100983
Jens Axboe08d7d5a2012-03-21 19:33:32 +0100984 INIT_PRIO_TREE_ITER(&iter);
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100985 prio_tree_iter_init(&iter, &i->prio_tree, x, x);
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100986
987 n = prio_tree_next(&iter);
988 if (!n)
989 continue;
990
991 do {
Jens Axboecdae5ff2012-03-22 09:24:05 +0100992 struct graph_value *v, *rootv;
Jens Axboeb7a69ad2012-03-21 21:55:21 +0100993 double yval, ydiff;
Jens Axboeb65c7ec2012-03-21 17:17:45 +0100994
995 v = container_of(n, struct graph_value, node);
Jens Axboecdae5ff2012-03-22 09:24:05 +0100996 rootv = v;
997 do {
998 yval = gety(v);
999 ydiff = fabs(yval - y);
Jens Axboe93e2db22012-03-13 09:45:22 +01001000
Jens Axboecdae5ff2012-03-22 09:24:05 +01001001 /*
1002 * zero delta, or within or match critera, break
1003 */
1004 if (ydiff < best_delta) {
1005 best_delta = ydiff;
1006 if (!best_delta ||
1007 (yval >= miny && yval <= maxy)) {
1008 best = v;
1009 break;
1010 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001011 }
Jens Axboecdae5ff2012-03-22 09:24:05 +01001012 if (!flist_empty(&v->alias))
Jens Axboe12dbd062014-07-03 21:19:57 -06001013 v = flist_first_entry(&v->alias, struct graph_value, alias);
Jens Axboecdae5ff2012-03-22 09:24:05 +01001014 } while (v != rootv);
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001015 } while ((n = prio_tree_next(&iter)) != NULL);
1016
1017 /*
1018 * If we got matches in one label, don't check others.
1019 */
Jens Axboed0db8192012-03-22 20:48:02 +01001020 if (best)
1021 break;
Jens Axboecdae5ff2012-03-22 09:24:05 +01001022 }
Jens Axboeb65c7ec2012-03-21 17:17:45 +01001023
1024 if (best)
1025 return best->tooltip;
Jens Axboe93e2db22012-03-13 09:45:22 +01001026
1027 return NULL;
1028}
Jens Axboe01a947f2012-03-22 21:21:00 +01001029
1030void graph_set_graph_all_zeroes(struct graph *g, unsigned int set)
1031{
1032 g->dont_graph_all_zeroes = !set;
1033}