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"
36 #include "cairo_text_helpers.h"
39 * Allowable difference to show tooltip
41 #define TOOLTIP_DELTA 0.08
53 struct flist_head list;
54 struct prio_tree_node node;
55 struct flist_head alias;
62 struct flist_head list;
64 struct flist_head value_list;
65 struct prio_tree_root prio_tree;
81 unsigned int xdim, ydim;
82 double xoffset, yoffset;
83 struct flist_head label_list;
86 graph_axis_unit_change_callback x_axis_unit_change_callback;
87 graph_axis_unit_change_callback y_axis_unit_change_callback;
88 unsigned int base_offset;
89 unsigned int dont_graph_all_zeroes;
97 double xtick_zero_val;
101 double ytick_zero_val;
102 double ytick_one_val;
105 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
111 void graph_set_position(struct graph *g, double xoffset, double yoffset)
113 g->xoffset = xoffset;
114 g->yoffset = yoffset;
117 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
121 g = calloc(1, sizeof(*g));
122 INIT_FLIST_HEAD(&g->label_list);
123 graph_set_size(g, xdim, ydim);
124 g->per_label_limit = -1;
127 g->font = GRAPH_DEFAULT_FONT;
131 void graph_set_font(struct graph *g, const char *font)
136 void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
138 g->x_axis_unit_change_callback = f;
141 void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
143 g->y_axis_unit_change_callback = f;
146 static int count_labels(struct graph *g)
148 struct flist_head *entry;
151 flist_for_each(entry, &g->label_list)
157 static int count_values(struct graph_label *l)
159 struct flist_head *entry;
162 flist_for_each(entry, &l->value_list)
168 typedef double (*double_comparator)(double a, double b);
170 static double mindouble(double a, double b)
172 return a < b ? a : b;
175 static double maxdouble(double a, double b)
177 return a < b ? b : a;
180 static double find_double_values(struct graph_label *l, double_comparator cmp)
182 struct flist_head *entry;
186 if (flist_empty(&l->value_list))
189 flist_for_each(entry, &l->value_list) {
190 struct graph_value *i;
192 i = flist_entry(entry, struct graph_value, list);
193 tmp = *(double *) i->value;
198 answer = cmp(answer, tmp);
204 static double find_double_data(struct graph *g, double_comparator cmp)
206 struct flist_head *entry;
207 struct graph_label *i;
211 if (flist_empty(&g->label_list))
214 flist_for_each(entry, &g->label_list) {
215 i = flist_entry(entry, struct graph_label, list);
216 tmp = find_double_values(i, cmp);
221 answer = cmp(tmp, answer);
227 static double find_min_data(struct graph *g)
229 return find_double_data(g, mindouble);
232 static double find_max_data(struct graph *g)
234 return find_double_data(g, maxdouble);
237 static 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)
241 struct flist_head *entry;
242 double x1, y1, x2, y2;
244 double domain, range, v;
246 domain = (maxdata - mindata);
247 range = (double) bg->ydim * 0.80; /* FIXME */
249 flist_for_each(entry, &lb->value_list) {
250 struct graph_value *i;
252 i = flist_entry(entry, struct graph_value, list);
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);
270 static void graph_draw_common(struct graph *g, cairo_t *cr,
271 double *x1, double *y1, double *x2, double *y2)
273 cairo_set_source_rgb(cr, 0, 0, 0);
274 cairo_set_line_width(cr, 0.8);
276 *x1 = 0.10 * g->xdim;
277 *x2 = 0.95 * g->xdim;
278 *y1 = 0.10 * g->ydim;
279 *y2 = 0.90 * g->ydim;
281 cairo_move_to(cr, *x1, *y1);
282 cairo_line_to(cr, *x1, *y2);
283 cairo_line_to(cr, *x2, *y2);
284 cairo_line_to(cr, *x2, *y1);
285 cairo_line_to(cr, *x1, *y1);
288 draw_centered_text(cr, g->font, g->xdim / 2, g->ydim / 20, 20.0, g->title);
289 draw_centered_text(cr, g->font, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
290 draw_vertical_centered_text(cr, g->font, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
294 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
295 double x1, double y1, double x2, double y2,
296 double minx, double maxx, int nticks, int add_tm_text)
301 static double dash[] = { 1.0, 2.0 };
303 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
304 g->x_axis_unit_change_callback == NULL, g->base_offset);
305 if (g->x_axis_unit_change_callback)
306 g->x_axis_unit_change_callback(g, power_of_ten);
308 for (i = 0; i < nticks; i++) {
309 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
316 g->xtick_zero_val = tm[0].value;
318 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
319 g->xtick_one_val = tm[1].value;
322 /* really tx < yx || tx > x2, but protect against rounding */
323 if (x1 - tx > 0.01 || tx - x2 > 0.01)
327 cairo_set_line_width(cr, 0.8);
328 cairo_move_to(cr, tx, y2);
329 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
332 /* draw grid lines */
334 cairo_set_dash(cr, dash, 2, 2.0);
335 cairo_set_line_width(cr, 0.5);
336 cairo_move_to(cr, tx, y1);
337 cairo_line_to(cr, tx, y2);
344 /* draw tickmark label */
345 draw_centered_text(cr, g->font, tx, y2 * 1.04, 12.0, tm[i].string);
350 static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
351 double x1, double y1, double x2, double y2,
352 double miny, double maxy, int nticks, int add_tm_text)
357 static double dash[] = { 2.0, 2.0 };
359 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
360 g->y_axis_unit_change_callback == NULL, g->base_offset);
361 if (g->y_axis_unit_change_callback)
362 g->y_axis_unit_change_callback(g, power_of_ten);
365 * Use highest tickmark as top of graph, not highest value. Otherwise
366 * it's impossible to see what the max value is, if the graph is
369 maxy = tm[nticks - 1].value;
371 for (i = 0; i < nticks; i++) {
372 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
379 g->ytick_zero_val = tm[0].value;
381 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
382 g->ytick_one_val = tm[1].value;
385 /* really ty < y1 || ty > y2, but protect against rounding */
386 if (y1 - ty > 0.01 || ty - y2 > 0.01)
390 cairo_move_to(cr, x1, ty);
391 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
394 /* draw grid lines */
396 cairo_set_dash(cr, dash, 2, 2.0);
397 cairo_set_line_width(cr, 0.5);
398 cairo_move_to(cr, x1, ty);
399 cairo_line_to(cr, x2, ty);
406 /* draw tickmark label */
407 draw_right_justified_text(cr, g->font, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
412 * Return new max to use
417 void bar_graph_draw(struct graph *bg, cairo_t *cr)
419 double x1, y1, x2, y2;
420 double space_per_label, bar_width;
421 double label_offset, mindata, maxdata;
423 struct graph_label *lb;
424 struct flist_head *entry;
427 cairo_translate(cr, bg->xoffset, bg->yoffset);
428 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
430 nlabels = count_labels(bg);
431 space_per_label = (x2 - x1) / (double) nlabels;
434 * Start bars at 0 unless we have negative values, otherwise we
435 * present a skewed picture comparing label X and X+1.
437 mindata = find_min_data(bg);
441 maxdata = find_max_data(bg);
443 if (fabs(maxdata - mindata) < 1e-20) {
444 draw_centered_text(cr, bg->font,
445 x1 + (x2 - x1) / 2.0,
446 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
450 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
452 flist_for_each(entry, &bg->label_list) {
455 lb = flist_entry(entry, struct graph_label, list);
456 nvalues = count_values(lb);
457 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
458 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
459 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
460 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
461 draw_centered_text(cr, bg->font, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
469 typedef double (*xy_value_extractor)(struct graph_value *v);
471 static double getx(struct graph_value *v)
473 struct xyvalue *xy = v->value;
477 static double gety(struct graph_value *v)
479 struct xyvalue *xy = v->value;
483 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
485 double tmp, answer = 0.0;
486 struct graph_label *i;
487 struct graph_value *j;
488 struct flist_head *jentry, *entry;
491 flist_for_each(entry, &g->label_list) {
492 i = flist_entry(entry, struct graph_label, list);
494 flist_for_each(jentry, &i->value_list) {
495 j = flist_entry(jentry, struct graph_value, list);
501 answer = cmp(tmp, answer);
508 void line_graph_draw(struct graph *g, cairo_t *cr)
510 double x1, y1, x2, y2;
511 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
512 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
513 struct graph_label *i;
514 struct graph_value *j;
515 int good_data = 1, first = 1;
516 struct flist_head *entry, *lentry;
519 cairo_translate(cr, g->xoffset, g->yoffset);
520 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
522 minx = find_xy_value(g, getx, mindouble);
523 maxx = find_xy_value(g, getx, maxdouble);
524 miny = find_xy_value(g, gety, mindouble);
527 * Start graphs at zero, unless we have a value below. Otherwise
528 * it's hard to visually compare the read and write graph, since
529 * the lowest valued one will be the floor of the graph view.
534 maxy = find_xy_value(g, gety, maxdouble);
536 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
549 if (g->top_extra > 0.001)
550 top_extra = fabs(maxy - miny) * g->top_extra;
551 if (g->bottom_extra > 0.001)
552 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
553 if (g->left_extra > 0.001)
554 left_extra = fabs(maxx - minx) * g->left_extra;
555 if (g->right_extra > 0.001)
556 right_extra = fabs(maxx - minx) * g->right_extra;
558 gminx = minx - left_extra;
559 gmaxx = maxx + right_extra;
560 gminy = miny - bottom_extra;
561 gmaxy = maxy + top_extra;
563 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
564 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
569 cairo_set_line_width(cr, 1.5);
570 flist_for_each(lentry, &g->label_list) {
571 i = flist_entry(lentry, struct graph_label, list);
573 if (i->hide || i->r < 0) /* invisible data */
576 cairo_set_source_rgb(cr, i->r, i->g, i->b);
577 flist_for_each(entry, &i->value_list) {
578 j = flist_entry(entry, struct graph_value, list);
579 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
580 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
582 cairo_move_to(cr, tx, ty);
585 cairo_line_to(cr, tx, ty);
595 static void setstring(char **str, const char *value)
598 *str = strdup(value);
601 void graph_title(struct graph *bg, const char *title)
603 setstring(&bg->title, title);
606 void graph_x_title(struct graph *bg, const char *title)
608 setstring(&bg->xtitle, title);
611 void graph_y_title(struct graph *bg, const char *title)
613 setstring(&bg->ytitle, title);
616 static struct graph_label *graph_find_label(struct graph *bg,
619 struct flist_head *entry;
620 struct graph_label *i;
622 flist_for_each(entry, &bg->label_list) {
623 i = flist_entry(entry, struct graph_label, list);
625 if (strcmp(label, i->label) == 0)
632 graph_label_t graph_add_label(struct graph *bg, const char *label)
634 struct graph_label *i;
636 i = graph_find_label(bg, label);
638 return i; /* already present. */
639 i = calloc(1, sizeof(*i));
640 INIT_FLIST_HEAD(&i->value_list);
642 setstring(&i->label, label);
643 flist_add_tail(&i->list, &bg->label_list);
644 INIT_PRIO_TREE_ROOT(&i->prio_tree);
648 static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
650 flist_del_init(&v->list);
658 static void graph_value_drop(struct graph_label *l, struct graph_value *v)
660 if (v->flags & GV_F_PRIO_SKIP) {
661 __graph_value_drop(l, v);
666 * Find head, the guy that's on the prio tree
668 while (!(v->flags & GV_F_ON_PRIO)) {
669 assert(!flist_empty(&v->alias));
670 v = flist_entry(v->alias.next, struct graph_value, alias);
673 prio_tree_remove(&l->prio_tree, &v->node);
678 while (!flist_empty(&v->alias)) {
679 struct graph_value *a;
681 a = flist_entry(v->alias.next, struct graph_value, alias);
682 flist_del_init(&a->alias);
684 __graph_value_drop(l, a);
687 __graph_value_drop(l, v);
690 static void graph_label_add_value(struct graph_label *i, void *value,
693 struct graph *g = i->parent;
694 struct graph_value *x;
696 x = malloc(sizeof(*x));
697 memset(x, 0, sizeof(*x));
698 INIT_FLIST_HEAD(&x->alias);
699 INIT_FLIST_HEAD(&x->list);
700 flist_add_tail(&x->list, &i->value_list);
705 double xval = getx(x);
706 double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA);
707 double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA);
708 struct prio_tree_node *ret;
711 * use msec to avoid dropping too much precision when
712 * storing as an integer.
714 minx = minx * 1000.0;
715 maxx = maxx * 1000.0;
717 INIT_PRIO_TREE_NODE(&x->node);
718 x->node.start = minx;
720 x->tooltip = strdup(tooltip);
721 if (x->node.last == x->node.start) {
722 x->node.last += fabs(g->xtick_delta);
723 if (x->node.last == x->node.start)
728 * If ret != &x->node, we have an alias. Since the values
729 * should be identical, we can drop it
731 ret = prio_tree_insert(&i->prio_tree, &x->node);
732 if (ret != &x->node) {
733 struct graph_value *alias;
735 alias = container_of(ret, struct graph_value, node);
736 flist_add_tail(&x->alias, &alias->alias);
738 x->flags = GV_F_ON_PRIO;
740 x->flags = GV_F_PRIO_SKIP;
742 if (g->per_label_limit != -1 &&
743 i->value_count > g->per_label_limit) {
747 * If the limit was dynamically reduced, making us more
748 * than 1 entry ahead after adding this one, drop two
749 * entries. This will make us (eventually) reach the
752 if (i->value_count - g->per_label_limit >= 2)
755 while (to_drop-- && !flist_empty(&i->value_list)) {
756 x = flist_entry(i->value_list.next, struct graph_value, list);
757 graph_value_drop(i, x);
760 * If we have aliases, we could drop > 1 above.
762 if (i->value_count <= g->per_label_limit)
768 int graph_add_data(struct graph *bg, graph_label_t label, const double value)
770 struct graph_label *i = label;
773 d = malloc(sizeof(*d));
776 graph_label_add_value(i, d, NULL);
780 static int graph_nonzero_y(struct graph_label *l)
782 struct flist_head *entry;
784 flist_for_each(entry, &l->value_list) {
785 struct graph_value *v;
787 v = flist_entry(entry, struct graph_value, list);
795 int graph_add_xy_data(struct graph *bg, graph_label_t label,
796 const double x, const double y, const char *tooltip)
798 struct graph_label *i = label;
801 if (bg->dont_graph_all_zeroes && y == 0.0 && !graph_nonzero_y(i))
806 xy = malloc(sizeof(*xy));
810 graph_label_add_value(i, xy, tooltip);
814 static void graph_free_values(struct graph_label *l)
816 struct graph_value *i;
818 while (!flist_empty(&l->value_list)) {
819 i = flist_entry(l->value_list.next, struct graph_value, list);
820 graph_value_drop(l, i);
824 static void graph_free_labels(struct graph *g)
826 struct graph_label *i;
828 while (!flist_empty(&g->label_list)) {
829 i = flist_entry(g->label_list.next, struct graph_label, list);
831 graph_free_values(i);
836 void graph_set_color(struct graph *gr, graph_label_t label, double red,
837 double green, double blue)
839 struct graph_label *i = label;
842 if (red < 0.0) { /* invisible color */
864 void graph_free(struct graph *bg)
869 graph_free_labels(bg);
872 /* For each line in the line graph, up to per_label_limit segments may
873 * be added. After that, adding more data to the end of the line
874 * causes data to drop off of the front of the line.
876 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
878 g->per_label_limit = per_label_limit;
881 void graph_add_extra_space(struct graph *g, double left_percent,
882 double right_percent, double top_percent,
883 double bottom_percent)
885 g->left_extra = left_percent;
886 g->right_extra = right_percent;
887 g->top_extra = top_percent;
888 g->bottom_extra = bottom_percent;
892 * Normally values are logged in a base unit of 0, but for other purposes
893 * it makes more sense to log in higher unit. For instance for bandwidth
894 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
896 void graph_set_base_offset(struct graph *g, unsigned int base_offset)
898 g->base_offset = base_offset;
901 int graph_has_tooltips(struct graph *g)
903 struct flist_head *entry;
904 struct graph_label *i;
906 flist_for_each(entry, &g->label_list) {
907 i = flist_entry(entry, struct graph_label, list);
909 if (!prio_tree_empty(&i->prio_tree))
916 int graph_contains_xy(struct graph *g, int x, int y)
918 int first_x = g->xoffset;
919 int last_x = g->xoffset + g->xdim;
920 int first_y = g->yoffset;
921 int last_y = g->yoffset + g->ydim;
923 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
926 const char *graph_find_tooltip(struct graph *g, int ix, int iy)
928 double x = ix, y = iy;
929 struct prio_tree_iter iter;
930 struct prio_tree_node *n;
931 struct graph_value *best = NULL;
932 struct flist_head *entry;
939 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
940 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
943 maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
944 miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
945 best_delta = UINT_MAX;
946 flist_for_each(entry, &g->label_list) {
947 struct graph_label *i;
949 i = flist_entry(entry, struct graph_label, list);
953 INIT_PRIO_TREE_ITER(&iter);
954 prio_tree_iter_init(&iter, &i->prio_tree, x, x);
956 n = prio_tree_next(&iter);
961 struct graph_value *v, *rootv;
964 v = container_of(n, struct graph_value, node);
968 ydiff = fabs(yval - y);
971 * zero delta, or within or match critera, break
973 if (ydiff < best_delta) {
976 (yval >= miny && yval <= maxy)) {
981 if (!flist_empty(&v->alias))
982 v = flist_entry(v->alias.next, struct graph_value, alias);
983 } while (v != rootv);
984 } while ((n = prio_tree_next(&iter)) != NULL);
987 * If we got matches in one label, don't check others.
994 return best->tooltip;
999 void graph_set_graph_all_zeroes(struct graph *g, unsigned int set)
1001 g->dont_graph_all_zeroes = !set;