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"
41 * Allowable difference to show tooltip
43 #define TOOLTIP_DELTA 1.02
50 struct graph_value *next;
51 struct prio_tree_node node;
58 struct graph_value *tail;
59 struct graph_value *values;
60 struct graph_label *next;
61 struct prio_tree_root prio_tree;
64 unsigned int tooltip_count;
77 unsigned int xdim, ydim;
78 double xoffset, yoffset;
79 struct graph_label *labels;
80 struct graph_label *tail;
83 graph_axis_unit_change_callback x_axis_unit_change_callback;
84 graph_axis_unit_change_callback y_axis_unit_change_callback;
85 unsigned int base_offset;
93 double xtick_zero_val;
96 double ytick_zero_val;
99 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
105 void graph_set_position(struct graph *g, double xoffset, double yoffset)
107 g->xoffset = xoffset;
108 g->yoffset = yoffset;
111 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
115 g = calloc(1, sizeof(*g));
116 graph_set_size(g, xdim, ydim);
117 g->per_label_limit = -1;
124 void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
126 g->x_axis_unit_change_callback = f;
129 void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
131 g->y_axis_unit_change_callback = f;
134 static int count_labels(struct graph_label *labels)
137 struct graph_label *i;
139 for (i = labels; i; i = i->next)
144 static int count_values(struct graph_value *values)
147 struct graph_value *i;
149 for (i = values; i; i = i->next)
154 typedef double (*double_comparator)(double a, double b);
156 static double mindouble(double a, double b)
158 return a < b ? a : b;
161 static double maxdouble(double a, double b)
163 return a < b ? b : a;
166 static double find_double_values(struct graph_value *values, double_comparator cmp)
168 struct graph_value *i;
172 assert(values != NULL);
173 answer = 0.0; /* shut the compiler up, might need to think harder though. */
174 for (i = values; i; i = i->next) {
175 tmp = *(double *) i->value;
180 answer = cmp(answer, tmp);
186 static double find_double_data(struct graph_label *labels, double_comparator cmp)
188 struct graph_label *i;
192 assert(labels != NULL);
193 answer = 0.0; /* shut the compiler up, might need to think harder though. */
194 for (i = labels; i; i = i->next) {
195 tmp = find_double_values(i->values, cmp);
200 answer = cmp(tmp, answer);
206 static double find_min_data(struct graph_label *labels)
208 return find_double_data(labels, mindouble);
211 static double find_max_data(struct graph_label *labels)
213 return find_double_data(labels, maxdouble);
216 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
217 double label_offset, double bar_width,
218 double mindata, double maxdata)
220 struct graph_value *i;
221 double x1, y1, x2, y2;
223 double domain, range, v;
225 domain = (maxdata - mindata);
226 range = (double) bg->ydim * 0.80; /* FIXME */
228 for (i = lb->values; i; i = i->next) {
230 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
231 x2 = x1 + bar_width * 0.90;
232 y2 = bg->ydim * 0.90;
233 v = *(double *) i->value;
234 y1 = y2 - (((v - mindata) / domain) * range);
235 cairo_move_to(cr, x1, y1);
236 cairo_line_to(cr, x1, y2);
237 cairo_line_to(cr, x2, y2);
238 cairo_line_to(cr, x2, y1);
239 cairo_close_path(cr);
246 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
247 double fontsize, const char *text, int alignment)
250 #define LEFT_JUSTIFIED 1
251 #define RIGHT_JUSTIFIED 2
253 double factor, direction;
254 cairo_text_extents_t extents;
261 case RIGHT_JUSTIFIED:
271 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
273 cairo_set_font_size(cr, fontsize);
274 cairo_text_extents(cr, text, &extents);
275 x = x + direction * (factor * extents.width + extents.x_bearing);
276 y = y - (extents.height / 2 + extents.y_bearing);
278 cairo_move_to(cr, x, y);
279 cairo_show_text(cr, text);
282 static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
283 double fontsize, const char *text)
285 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
288 static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
290 double fontsize, const char *text)
292 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
295 static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
297 double fontsize, const char *text)
299 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
302 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
303 double y, double fontsize,
307 cairo_text_extents_t extents;
309 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
311 cairo_set_font_size(cr, fontsize);
312 cairo_text_extents(cr, text, &extents);
315 y = y + (extents.width / 2.0 + extents.x_bearing);
316 x = x - (extents.height / 2.0 + extents.y_bearing);
318 cairo_move_to(cr, x, y);
320 cairo_translate(cr, -sx, -sy);
321 cairo_rotate(cr, -90.0 * M_PI / 180.0);
322 cairo_translate(cr, sx, sy);
323 cairo_show_text(cr, text);
327 static void graph_draw_common(struct graph *g, cairo_t *cr,
328 double *x1, double *y1, double *x2, double *y2)
330 cairo_set_source_rgb(cr, 0, 0, 0);
331 cairo_set_line_width (cr, 0.8);
333 *x1 = 0.10 * g->xdim;
334 *x2 = 0.95 * g->xdim;
335 *y1 = 0.10 * g->ydim;
336 *y2 = 0.90 * g->ydim;
338 cairo_move_to(cr, *x1, *y1);
339 cairo_line_to(cr, *x1, *y2);
340 cairo_line_to(cr, *x2, *y2);
341 cairo_line_to(cr, *x2, *y1);
342 cairo_line_to(cr, *x1, *y1);
345 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
346 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
347 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
351 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
352 double x1, double y1, double x2, double y2,
353 double minx, double maxx, int nticks, int add_tm_text)
358 static double dash[] = { 1.0, 2.0 };
360 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
361 g->x_axis_unit_change_callback == NULL, g->base_offset);
362 if (g->x_axis_unit_change_callback)
363 g->x_axis_unit_change_callback(g, power_of_ten);
365 for (i = 0; i < nticks; i++) {
366 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
373 g->xtick_zero_val = tm[0].value;
375 g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
377 /* really tx < yx || tx > x2, but protect against rounding */
378 if (x1 - tx > 0.01 || tx - x2 > 0.01)
382 cairo_set_line_width(cr, 0.8);
383 cairo_move_to(cr, tx, y2);
384 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
387 /* draw grid lines */
389 cairo_set_dash(cr, dash, 2, 2.0);
390 cairo_set_line_width(cr, 0.5);
391 cairo_move_to(cr, tx, y1);
392 cairo_line_to(cr, tx, y2);
399 /* draw tickmark label */
400 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
405 static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
406 double x1, double y1, double x2, double y2,
407 double miny, double maxy, int nticks, int add_tm_text)
412 static double dash[] = { 2.0, 2.0 };
414 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
415 g->y_axis_unit_change_callback == NULL, g->base_offset);
416 if (g->y_axis_unit_change_callback)
417 g->y_axis_unit_change_callback(g, power_of_ten);
420 * Use highest tickmark as top of graph, not highest value. Otherwise
421 * it's impossible to see what the max value is, if the graph is
424 maxy = tm[nticks - 1].value;
426 for (i = 0; i < nticks; i++) {
427 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
434 g->ytick_zero_val = tm[0].value;
436 g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
438 /* really ty < y1 || ty > y2, but protect against rounding */
439 if (y1 - ty > 0.01 || ty - y2 > 0.01)
443 cairo_move_to(cr, x1, ty);
444 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
447 /* draw grid lines */
449 cairo_set_dash(cr, dash, 2, 2.0);
450 cairo_set_line_width(cr, 0.5);
451 cairo_move_to(cr, x1, ty);
452 cairo_line_to(cr, x2, ty);
459 /* draw tickmark label */
460 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
465 * Return new max to use
470 void bar_graph_draw(struct graph *bg, cairo_t *cr)
472 double x1, y1, x2, y2;
473 double space_per_label, bar_width;
474 double label_offset, mindata, maxdata;
476 struct graph_label *lb;
479 cairo_translate(cr, bg->xoffset, bg->yoffset);
480 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
482 nlabels = count_labels(bg->labels);
483 space_per_label = (x2 - x1) / (double) nlabels;
486 * Start bars at 0 unless we have negative values, otherwise we
487 * present a skewed picture comparing label X and X+1.
489 mindata = find_min_data(bg->labels);
493 maxdata = find_max_data(bg->labels);
495 if (fabs(maxdata - mindata) < 1e-20) {
496 draw_centered_text(bg, cr,
497 x1 + (x2 - x1) / 2.0,
498 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
502 maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
504 for (lb = bg->labels; lb; lb = lb->next) {
506 nvalues = count_values(lb->values);
507 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
508 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
509 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
510 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
511 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
519 typedef double (*xy_value_extractor)(struct graph_value *v);
521 static double getx(struct graph_value *v)
523 struct xyvalue *xy = v->value;
527 static double gety(struct graph_value *v)
529 struct xyvalue *xy = v->value;
533 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
535 double tmp, answer = 0.0;
536 struct graph_label *i;
537 struct graph_value *j;
540 for (i = g->labels; i; i = i->next)
541 for (j = i->values; j; j = j->next) {
547 answer = cmp(tmp, answer);
552 void line_graph_draw(struct graph *g, cairo_t *cr)
554 double x1, y1, x2, y2;
555 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
556 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
557 struct graph_label *i;
558 struct graph_value *j;
559 int good_data = 1, first = 1;
562 cairo_translate(cr, g->xoffset, g->yoffset);
563 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
565 minx = find_xy_value(g, getx, mindouble);
566 maxx = find_xy_value(g, getx, maxdouble);
567 miny = find_xy_value(g, gety, mindouble);
570 * Start graphs at zero, unless we have a value below. Otherwise
571 * it's hard to visually compare the read and write graph, since
572 * the lowest valued one will be the floor of the graph view.
577 maxy = find_xy_value(g, gety, maxdouble);
579 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
592 if (g->top_extra > 0.001)
593 top_extra = fabs(maxy - miny) * g->top_extra;
594 if (g->bottom_extra > 0.001)
595 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
596 if (g->left_extra > 0.001)
597 left_extra = fabs(maxx - minx) * g->left_extra;
598 if (g->right_extra > 0.001)
599 right_extra = fabs(maxx - minx) * g->right_extra;
601 gminx = minx - left_extra;
602 gmaxx = maxx + right_extra;
603 gminy = miny - bottom_extra;
604 gmaxy = maxy + top_extra;
606 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
607 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
612 cairo_set_line_width(cr, 1.5);
613 for (i = g->labels; i; i = i->next) {
615 if (i->r < 0) /* invisible data */
618 cairo_set_source_rgb(cr, i->r, i->g, i->b);
619 for (j = i->values; j; j = j->next) {
620 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
621 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
623 cairo_move_to(cr, tx, ty);
626 cairo_line_to(cr, tx, ty);
637 static void setstring(char **str, const char *value)
640 *str = strdup(value);
643 void graph_title(struct graph *bg, const char *title)
645 setstring(&bg->title, title);
648 void graph_x_title(struct graph *bg, const char *title)
650 setstring(&bg->xtitle, title);
653 void graph_y_title(struct graph *bg, const char *title)
655 setstring(&bg->ytitle, title);
658 static struct graph_label *graph_find_label(struct graph *bg,
661 struct graph_label *i;
663 for (i = bg->labels; i; i = i->next)
664 if (strcmp(label, i->label) == 0)
669 void graph_add_label(struct graph *bg, const char *label)
671 struct graph_label *i;
673 i = graph_find_label(bg, label);
675 return; /* already present. */
676 i = calloc(1, sizeof(*i));
678 setstring(&i->label, label);
685 INIT_PRIO_TREE_ROOT(&i->prio_tree);
688 static void graph_label_add_value(struct graph_label *i, void *value,
691 struct graph_value *x;
693 x = malloc(sizeof(*x));
696 x->tooltip = strdup(tooltip);
709 double yval = gety(x);
710 double miny = yval / TOOLTIP_DELTA;
711 double maxy = yval * TOOLTIP_DELTA;
713 x->node.start = miny;
715 if (x->node.last == x->node.start)
718 prio_tree_insert(&i->prio_tree, &x->node);
719 printf("insert (x=%u,y=%u) range %lu-%lu (%s)\n", (int)getx(x), (int)gety(x), x->node.start, x->node.last, x->tooltip);
723 if (i->parent->per_label_limit != -1 &&
724 i->value_count > i->parent->per_label_limit) {
728 * If the limit was dynamically reduced, making us more
729 * than 1 entry ahead after adding this one, drop two
730 * entries. This will make us (eventually) reach the
733 if (i->value_count - i->parent->per_label_limit >= 2)
738 i->values = i->values->next;
741 prio_tree_remove(&i->prio_tree, &x->node);
751 int graph_add_data(struct graph *bg, const char *label, const double value)
753 struct graph_label *i;
756 d = malloc(sizeof(*d));
759 i = graph_find_label(bg, label);
762 graph_label_add_value(i, d, NULL);
766 int graph_add_xy_data(struct graph *bg, const char *label,
767 const double x, const double y, const char *tooltip)
769 struct graph_label *i;
772 xy = malloc(sizeof(*xy));
776 i = graph_find_label(bg, label);
780 graph_label_add_value(i, xy, tooltip);
784 static void graph_free_values(struct graph_value *values)
786 struct graph_value *i, *next;
788 for (i = values; i; i = next) {
795 static void graph_free_labels(struct graph_label *labels)
797 struct graph_label *i, *next;
799 for (i = labels; i; i = next) {
801 graph_free_values(i->values);
806 void graph_set_color(struct graph *gr, const char *label,
807 double red, double green, double blue)
809 struct graph_label *i;
812 if (red < 0.0) { /* invisible color */
829 for (i = gr->labels; i; i = i->next)
830 if (strcmp(i->label, label) == 0) {
838 void graph_free(struct graph *bg)
843 graph_free_labels(bg->labels);
846 /* For each line in the line graph, up to per_label_limit segments may
847 * be added. After that, adding more data to the end of the line
848 * causes data to drop off of the front of the line.
850 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
852 g->per_label_limit = per_label_limit;
855 void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
856 double top_percent, double bottom_percent)
858 g->left_extra = left_percent;
859 g->right_extra = right_percent;
860 g->top_extra = top_percent;
861 g->bottom_extra = bottom_percent;
865 * Normally values are logged in a base unit of 0, but for other purposes
866 * it makes more sense to log in higher unit. For instance for bandwidth
867 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
869 void graph_set_base_offset(struct graph *g, unsigned int base_offset)
871 g->base_offset = base_offset;
874 int graph_has_tooltips(struct graph *g)
876 struct graph_label *i;
878 for (i = g->labels; i; i = i->next)
879 if (i->tooltip_count)
885 int graph_contains_xy(struct graph *g, int x, int y)
887 int first_x = g->xoffset;
888 int last_x = g->xoffset + g->xdim;
889 int first_y = g->yoffset;
890 int last_y = g->yoffset + g->ydim;
892 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
895 const char *graph_find_tooltip(struct graph *g, int ix, int iy)
897 double x = ix, y = iy;
898 struct prio_tree_iter iter;
899 struct prio_tree_node *n;
900 struct graph_label *i;
901 struct graph_value *best = NULL;
908 x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
909 y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
911 maxx = x * TOOLTIP_DELTA;
912 minx = x / TOOLTIP_DELTA;
913 best_delta = UINT_MAX;
916 prio_tree_iter_init(&iter, &i->prio_tree, y, y);
918 n = prio_tree_next(&iter);
923 struct graph_value *v;
926 v = container_of(n, struct graph_value, node);
935 * zero delta, or within or match critera, break
937 if (xdiff < best_delta) {
940 (xval >= minx && xval <= maxx)) {
945 } while ((n = prio_tree_next(&iter)) != NULL);
948 * If we got matches in one label, don't check others.
951 } while ((i = i->next) != NULL);
954 return best->tooltip;