2 * gfio - gui front end for fio - the flexible io tester
4 * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com>
6 * The license below covers all files distributed with fio unless otherwise
7 * noted in the file itself.
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.
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.
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
32 #include "tickmarks.h"
35 #include "lib/prio_tree.h"
38 * Allowable difference to show tooltip
40 #define TOOLTIP_DELTA 1.02
47 struct graph_value *next;
48 struct prio_tree_node node;
55 struct graph_value *tail;
56 struct graph_value *values;
57 struct graph_label *next;
58 struct prio_tree_root prio_tree;
61 unsigned int tooltip_count;
74 unsigned int xdim, ydim;
75 double xoffset, yoffset;
76 struct graph_label *labels;
77 struct graph_label *tail;
80 graph_axis_unit_change_callback x_axis_unit_change_callback;
81 graph_axis_unit_change_callback y_axis_unit_change_callback;
82 unsigned int base_offset;
90 double xtick_zero_val;
93 double ytick_zero_val;
96 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
102 void graph_set_position(struct graph *g, double xoffset, double yoffset)
104 g->xoffset = xoffset;
105 g->yoffset = yoffset;
108 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
112 g = calloc(1, sizeof(*g));
113 graph_set_size(g, xdim, ydim);
114 g->per_label_limit = -1;
121 void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
123 g->x_axis_unit_change_callback = f;
126 void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
128 g->y_axis_unit_change_callback = f;
131 static int count_labels(struct graph_label *labels)
134 struct graph_label *i;
136 for (i = labels; i; i = i->next)
141 static int count_values(struct graph_value *values)
144 struct graph_value *i;
146 for (i = values; i; i = i->next)
151 typedef double (*double_comparator)(double a, double b);
153 static double mindouble(double a, double b)
155 return a < b ? a : b;
158 static double maxdouble(double a, double b)
160 return a < b ? b : a;
163 static double find_double_values(struct graph_value *values, double_comparator cmp)
165 struct graph_value *i;
169 assert(values != NULL);
170 answer = 0.0; /* shut the compiler up, might need to think harder though. */
171 for (i = values; i; i = i->next) {
172 tmp = *(double *) i->value;
177 answer = cmp(answer, tmp);
183 static double find_double_data(struct graph_label *labels, double_comparator cmp)
185 struct graph_label *i;
189 assert(labels != NULL);
190 answer = 0.0; /* shut the compiler up, might need to think harder though. */
191 for (i = labels; i; i = i->next) {
192 tmp = find_double_values(i->values, cmp);
197 answer = cmp(tmp, answer);
203 static double find_min_data(struct graph_label *labels)
205 return find_double_data(labels, mindouble);
208 static double find_max_data(struct graph_label *labels)
210 return find_double_data(labels, maxdouble);
213 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
214 double label_offset, double bar_width,
215 double mindata, double maxdata)
217 struct graph_value *i;
218 double x1, y1, x2, y2;
220 double domain, range, v;
222 domain = (maxdata - mindata);
223 range = (double) bg->ydim * 0.80; /* FIXME */
225 for (i = lb->values; i; i = i->next) {
227 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
228 x2 = x1 + bar_width * 0.90;
229 y2 = bg->ydim * 0.90;
230 v = *(double *) i->value;
231 y1 = y2 - (((v - mindata) / domain) * range);
232 cairo_move_to(cr, x1, y1);
233 cairo_line_to(cr, x1, y2);
234 cairo_line_to(cr, x2, y2);
235 cairo_line_to(cr, x2, y1);
236 cairo_close_path(cr);
243 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
244 double fontsize, const char *text, int alignment)
247 #define LEFT_JUSTIFIED 1
248 #define RIGHT_JUSTIFIED 2
250 double factor, direction;
251 cairo_text_extents_t extents;
258 case RIGHT_JUSTIFIED:
268 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
270 cairo_set_font_size(cr, fontsize);
271 cairo_text_extents(cr, text, &extents);
272 x = x + direction * (factor * extents.width + extents.x_bearing);
273 y = y - (extents.height / 2 + extents.y_bearing);
275 cairo_move_to(cr, x, y);
276 cairo_show_text(cr, text);
279 static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
280 double fontsize, const char *text)
282 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
285 static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
287 double fontsize, const char *text)
289 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
292 static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
294 double fontsize, const char *text)
296 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
299 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
300 double y, double fontsize,
304 cairo_text_extents_t extents;
306 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
308 cairo_set_font_size(cr, fontsize);
309 cairo_text_extents(cr, text, &extents);
312 y = y + (extents.width / 2.0 + extents.x_bearing);
313 x = x - (extents.height / 2.0 + extents.y_bearing);
315 cairo_move_to(cr, x, y);
317 cairo_translate(cr, -sx, -sy);
318 cairo_rotate(cr, -90.0 * M_PI / 180.0);
319 cairo_translate(cr, sx, sy);
320 cairo_show_text(cr, text);
324 static void graph_draw_common(struct graph *g, cairo_t *cr,
325 double *x1, double *y1, double *x2, double *y2)
327 cairo_set_source_rgb(cr, 0, 0, 0);
328 cairo_set_line_width (cr, 0.8);
330 *x1 = 0.10 * g->xdim;
331 *x2 = 0.95 * g->xdim;
332 *y1 = 0.10 * g->ydim;
333 *y2 = 0.90 * g->ydim;
335 cairo_move_to(cr, *x1, *y1);
336 cairo_line_to(cr, *x1, *y2);
337 cairo_line_to(cr, *x2, *y2);
338 cairo_line_to(cr, *x2, *y1);
339 cairo_line_to(cr, *x1, *y1);
342 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
343 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
344 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
348 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
349 double x1, double y1, double x2, double y2,
350 double minx, double maxx, int nticks, int add_tm_text)
355 static double dash[] = { 1.0, 2.0 };
357 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
358 g->x_axis_unit_change_callback == NULL, g->base_offset);
359 if (g->x_axis_unit_change_callback)
360 g->x_axis_unit_change_callback(g, power_of_ten);
362 for (i = 0; i < nticks; i++) {
363 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
370 g->xtick_zero_val = tm[0].value;
372 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
374 /* really tx < yx || tx > x2, but protect against rounding */
375 if (x1 - tx > 0.01 || tx - x2 > 0.01)
379 cairo_set_line_width(cr, 0.8);
380 cairo_move_to(cr, tx, y2);
381 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
384 /* draw grid lines */
386 cairo_set_dash(cr, dash, 2, 2.0);
387 cairo_set_line_width(cr, 0.5);
388 cairo_move_to(cr, tx, y1);
389 cairo_line_to(cr, tx, y2);
396 /* draw tickmark label */
397 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
402 static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
403 double x1, double y1, double x2, double y2,
404 double miny, double maxy, int nticks, int add_tm_text)
409 static double dash[] = { 2.0, 2.0 };
411 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
412 g->y_axis_unit_change_callback == NULL, g->base_offset);
413 if (g->y_axis_unit_change_callback)
414 g->y_axis_unit_change_callback(g, power_of_ten);
417 * Use highest tickmark as top of graph, not highest value. Otherwise
418 * it's impossible to see what the max value is, if the graph is
421 maxy = tm[nticks - 1].value;
423 for (i = 0; i < nticks; i++) {
424 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
431 g->ytick_zero_val = tm[0].value;
433 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
435 /* really ty < y1 || ty > y2, but protect against rounding */
436 if (y1 - ty > 0.01 || ty - y2 > 0.01)
440 cairo_move_to(cr, x1, ty);
441 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
444 /* draw grid lines */
446 cairo_set_dash(cr, dash, 2, 2.0);
447 cairo_set_line_width(cr, 0.5);
448 cairo_move_to(cr, x1, ty);
449 cairo_line_to(cr, x2, ty);
456 /* draw tickmark label */
457 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
462 * Return new max to use
467 void bar_graph_draw(struct graph *bg, cairo_t *cr)
469 double x1, y1, x2, y2;
470 double space_per_label, bar_width;
471 double label_offset, mindata, maxdata;
473 struct graph_label *lb;
476 cairo_translate(cr, bg->xoffset, bg->yoffset);
477 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
479 nlabels = count_labels(bg->labels);
480 space_per_label = (x2 - x1) / (double) nlabels;
483 * Start bars at 0 unless we have negative values, otherwise we
484 * present a skewed picture comparing label X and X+1.
486 mindata = find_min_data(bg->labels);
490 maxdata = find_max_data(bg->labels);
492 if (fabs(maxdata - mindata) < 1e-20) {
493 draw_centered_text(bg, cr,
494 x1 + (x2 - x1) / 2.0,
495 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
499 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
501 for (lb = bg->labels; lb; lb = lb->next) {
503 nvalues = count_values(lb->values);
504 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
505 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
506 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
507 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
508 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
516 typedef double (*xy_value_extractor)(struct graph_value *v);
518 static double getx(struct graph_value *v)
520 struct xyvalue *xy = v->value;
524 static double gety(struct graph_value *v)
526 struct xyvalue *xy = v->value;
530 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
532 double tmp, answer = 0.0;
533 struct graph_label *i;
534 struct graph_value *j;
537 for (i = g->labels; i; i = i->next)
538 for (j = i->values; j; j = j->next) {
544 answer = cmp(tmp, answer);
549 void line_graph_draw(struct graph *g, cairo_t *cr)
551 double x1, y1, x2, y2;
552 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
553 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
554 struct graph_label *i;
555 struct graph_value *j;
556 int good_data = 1, first = 1;
559 cairo_translate(cr, g->xoffset, g->yoffset);
560 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
562 minx = find_xy_value(g, getx, mindouble);
563 maxx = find_xy_value(g, getx, maxdouble);
564 miny = find_xy_value(g, gety, mindouble);
567 * Start graphs at zero, unless we have a value below. Otherwise
568 * it's hard to visually compare the read and write graph, since
569 * the lowest valued one will be the floor of the graph view.
574 maxy = find_xy_value(g, gety, maxdouble);
576 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
589 if (g->top_extra > 0.001)
590 top_extra = fabs(maxy - miny) * g->top_extra;
591 if (g->bottom_extra > 0.001)
592 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
593 if (g->left_extra > 0.001)
594 left_extra = fabs(maxx - minx) * g->left_extra;
595 if (g->right_extra > 0.001)
596 right_extra = fabs(maxx - minx) * g->right_extra;
598 gminx = minx - left_extra;
599 gmaxx = maxx + right_extra;
600 gminy = miny - bottom_extra;
601 gmaxy = maxy + top_extra;
603 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
604 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
609 cairo_set_line_width(cr, 1.5);
610 for (i = g->labels; i; i = i->next) {
612 if (i->r < 0) /* invisible data */
615 cairo_set_source_rgb(cr, i->r, i->g, i->b);
616 for (j = i->values; j; j = j->next) {
617 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
618 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
620 cairo_move_to(cr, tx, ty);
623 cairo_line_to(cr, tx, ty);
634 static void setstring(char **str, const char *value)
637 *str = strdup(value);
640 void graph_title(struct graph *bg, const char *title)
642 setstring(&bg->title, title);
645 void graph_x_title(struct graph *bg, const char *title)
647 setstring(&bg->xtitle, title);
650 void graph_y_title(struct graph *bg, const char *title)
652 setstring(&bg->ytitle, title);
655 static struct graph_label *graph_find_label(struct graph *bg,
658 struct graph_label *i;
660 for (i = bg->labels; i; i = i->next)
661 if (strcmp(label, i->label) == 0)
666 void graph_add_label(struct graph *bg, const char *label)
668 struct graph_label *i;
670 i = graph_find_label(bg, label);
672 return; /* already present. */
673 i = calloc(1, sizeof(*i));
675 setstring(&i->label, label);
682 INIT_PRIO_TREE_ROOT(&i->prio_tree);
685 static void graph_label_add_value(struct graph_label *i, void *value,
688 struct graph_value *x;
690 x = malloc(sizeof(*x));
691 memset(x, 0, sizeof(*x));
694 x->tooltip = strdup(tooltip);
707 double yval = gety(x);
708 double miny = yval / TOOLTIP_DELTA;
709 double maxy = yval * TOOLTIP_DELTA;
710 struct prio_tree_node *ret;
712 INIT_PRIO_TREE_NODE(&x->node);
713 x->node.start = miny;
715 if (x->node.last == x->node.start)
719 * If ret != &x->node, we have an alias. Since the values
720 * should be identical, we can drop it
722 ret = prio_tree_insert(&i->prio_tree, &x->node);
723 if (ret != &x->node) {
730 if (i->parent->per_label_limit != -1 &&
731 i->value_count > i->parent->per_label_limit) {
735 * If the limit was dynamically reduced, making us more
736 * than 1 entry ahead after adding this one, drop two
737 * entries. This will make us (eventually) reach the
740 if (i->value_count - i->parent->per_label_limit >= 2)
745 i->values = i->values->next;
748 prio_tree_remove(&i->prio_tree, &x->node);
758 int graph_add_data(struct graph *bg, const char *label, const double value)
760 struct graph_label *i;
763 d = malloc(sizeof(*d));
766 i = graph_find_label(bg, label);
769 graph_label_add_value(i, d, NULL);
773 int graph_add_xy_data(struct graph *bg, const char *label,
774 const double x, const double y, const char *tooltip)
776 struct graph_label *i;
779 xy = malloc(sizeof(*xy));
783 i = graph_find_label(bg, label);
787 graph_label_add_value(i, xy, tooltip);
791 static void graph_free_values(struct graph_label *l, struct graph_value *values)
793 struct graph_value *i, *next;
795 for (i = values; i; i = next) {
800 prio_tree_remove(&l->prio_tree, &i->node);
807 static void graph_free_labels(struct graph_label *labels)
809 struct graph_label *i, *next;
811 for (i = labels; i; i = next) {
813 graph_free_values(i, i->values);
818 void graph_set_color(struct graph *gr, const char *label,
819 double red, double green, double blue)
821 struct graph_label *i;
824 if (red < 0.0) { /* invisible color */
841 for (i = gr->labels; i; i = i->next)
842 if (strcmp(i->label, label) == 0) {
850 void graph_free(struct graph *bg)
855 graph_free_labels(bg->labels);
858 /* For each line in the line graph, up to per_label_limit segments may
859 * be added. After that, adding more data to the end of the line
860 * causes data to drop off of the front of the line.
862 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
864 g->per_label_limit = per_label_limit;
867 void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
868 double top_percent, double bottom_percent)
870 g->left_extra = left_percent;
871 g->right_extra = right_percent;
872 g->top_extra = top_percent;
873 g->bottom_extra = bottom_percent;
877 * Normally values are logged in a base unit of 0, but for other purposes
878 * it makes more sense to log in higher unit. For instance for bandwidth
879 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
881 void graph_set_base_offset(struct graph *g, unsigned int base_offset)
883 g->base_offset = base_offset;
886 int graph_has_tooltips(struct graph *g)
888 struct graph_label *i;
890 for (i = g->labels; i; i = i->next)
891 if (i->tooltip_count)
897 int graph_contains_xy(struct graph *g, int x, int y)
899 int first_x = g->xoffset;
900 int last_x = g->xoffset + g->xdim;
901 int first_y = g->yoffset;
902 int last_y = g->yoffset + g->ydim;
904 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
907 const char *graph_find_tooltip(struct graph *g, int ix, int iy)
909 double x = ix, y = iy;
910 struct prio_tree_iter iter;
911 struct prio_tree_node *n;
912 struct graph_label *i;
913 struct graph_value *best = NULL;
920 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
921 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
923 maxx = x * TOOLTIP_DELTA;
924 minx = x / TOOLTIP_DELTA;
925 best_delta = UINT_MAX;
928 INIT_PRIO_TREE_ITER(&iter);
929 prio_tree_iter_init(&iter, &i->prio_tree, y, y);
931 n = prio_tree_next(&iter);
936 struct graph_value *v;
939 v = container_of(n, struct graph_value, node);
948 * zero delta, or within or match critera, break
950 if (xdiff < best_delta) {
953 (xval >= minx && xval <= maxx)) {
958 } while ((n = prio_tree_next(&iter)) != NULL);
961 * If we got matches in one label, don't check others.
964 } while ((i = i->next) != NULL);
967 return best->tooltip;