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"
41 struct graph_value *next;
48 struct graph_value *tail;
49 struct graph_value *values;
50 struct graph_label *next;
53 unsigned int tooltip_count;
61 unsigned int xdim, ydim;
62 double xoffset, yoffset;
63 struct graph_label *labels;
64 struct graph_label *tail;
67 graph_axis_unit_change_callback x_axis_unit_change_callback;
68 graph_axis_unit_change_callback y_axis_unit_change_callback;
69 unsigned int base_offset;
76 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
82 void graph_set_position(struct graph *g, double xoffset, double yoffset)
88 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
92 g = calloc(1, sizeof(*g));
93 graph_set_size(g, xdim, ydim);
94 g->per_label_limit = -1;
101 void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
103 g->x_axis_unit_change_callback = f;
106 void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
108 g->y_axis_unit_change_callback = f;
111 static int count_labels(struct graph_label *labels)
114 struct graph_label *i;
116 for (i = labels; i; i = i->next)
121 static int count_values(struct graph_value *values)
124 struct graph_value *i;
126 for (i = values; i; i = i->next)
131 typedef double (*double_comparator)(double a, double b);
133 static double mindouble(double a, double b)
135 return a < b ? a : b;
138 static double maxdouble(double a, double b)
140 return a < b ? b : a;
143 static double find_double_values(struct graph_value *values, double_comparator cmp)
145 struct graph_value *i;
149 assert(values != NULL);
150 answer = 0.0; /* shut the compiler up, might need to think harder though. */
151 for (i = values; i; i = i->next) {
152 tmp = *(double *) i->value;
157 answer = cmp(answer, tmp);
163 static double find_double_data(struct graph_label *labels, double_comparator cmp)
165 struct graph_label *i;
169 assert(labels != NULL);
170 answer = 0.0; /* shut the compiler up, might need to think harder though. */
171 for (i = labels; i; i = i->next) {
172 tmp = find_double_values(i->values, cmp);
177 answer = cmp(tmp, answer);
183 static double find_min_data(struct graph_label *labels)
185 return find_double_data(labels, mindouble);
188 static double find_max_data(struct graph_label *labels)
190 return find_double_data(labels, maxdouble);
193 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
194 double label_offset, double bar_width,
195 double mindata, double maxdata)
197 struct graph_value *i;
198 double x1, y1, x2, y2;
200 double domain, range, v;
202 domain = (maxdata - mindata);
203 range = (double) bg->ydim * 0.80; /* FIXME */
205 for (i = lb->values; i; i = i->next) {
207 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
208 x2 = x1 + bar_width * 0.90;
209 y2 = bg->ydim * 0.90;
210 v = *(double *) i->value;
211 y1 = y2 - (((v - mindata) / domain) * range);
212 cairo_move_to(cr, x1, y1);
213 cairo_line_to(cr, x1, y2);
214 cairo_line_to(cr, x2, y2);
215 cairo_line_to(cr, x2, y1);
216 cairo_close_path(cr);
223 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
224 double fontsize, const char *text, int alignment)
227 #define LEFT_JUSTIFIED 1
228 #define RIGHT_JUSTIFIED 2
230 double factor, direction;
231 cairo_text_extents_t extents;
238 case RIGHT_JUSTIFIED:
248 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
250 cairo_set_font_size(cr, fontsize);
251 cairo_text_extents(cr, text, &extents);
252 x = x + direction * (factor * extents.width + extents.x_bearing);
253 y = y - (extents.height / 2 + extents.y_bearing);
255 cairo_move_to(cr, x, y);
256 cairo_show_text(cr, text);
259 static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
260 double fontsize, const char *text)
262 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
265 static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
267 double fontsize, const char *text)
269 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
272 static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
274 double fontsize, const char *text)
276 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
279 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
280 double y, double fontsize,
284 cairo_text_extents_t extents;
286 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
288 cairo_set_font_size(cr, fontsize);
289 cairo_text_extents(cr, text, &extents);
292 y = y + (extents.width / 2.0 + extents.x_bearing);
293 x = x - (extents.height / 2.0 + extents.y_bearing);
295 cairo_move_to(cr, x, y);
297 cairo_translate(cr, -sx, -sy);
298 cairo_rotate(cr, -90.0 * M_PI / 180.0);
299 cairo_translate(cr, sx, sy);
300 cairo_show_text(cr, text);
304 static void graph_draw_common(struct graph *g, cairo_t *cr,
305 double *x1, double *y1, double *x2, double *y2)
307 cairo_set_source_rgb(cr, 0, 0, 0);
308 cairo_set_line_width (cr, 0.8);
310 *x1 = 0.10 * g->xdim;
311 *x2 = 0.95 * g->xdim;
312 *y1 = 0.10 * g->ydim;
313 *y2 = 0.90 * g->ydim;
315 cairo_move_to(cr, *x1, *y1);
316 cairo_line_to(cr, *x1, *y2);
317 cairo_line_to(cr, *x2, *y2);
318 cairo_line_to(cr, *x2, *y1);
319 cairo_line_to(cr, *x1, *y1);
322 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
323 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
324 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
328 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
329 double x1, double y1, double x2, double y2,
330 double minx, double maxx, int nticks, int add_tm_text)
335 static double dash[] = { 1.0, 2.0 };
337 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
338 g->x_axis_unit_change_callback == NULL, g->base_offset);
339 if (g->x_axis_unit_change_callback)
340 g->x_axis_unit_change_callback(g, power_of_ten);
342 for (i = 0; i < nticks; i++) {
343 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
345 /* really tx < yx || tx > x2, but protect against rounding */
346 if (x1 - tx > 0.01 || tx - x2 > 0.01)
350 cairo_set_line_width(cr, 0.8);
351 cairo_move_to(cr, tx, y2);
352 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
355 /* draw grid lines */
357 cairo_set_dash(cr, dash, 2, 2.0);
358 cairo_set_line_width(cr, 0.5);
359 cairo_move_to(cr, tx, y1);
360 cairo_line_to(cr, tx, y2);
367 /* draw tickmark label */
368 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
374 static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
375 double x1, double y1, double x2, double y2,
376 double miny, double maxy, int nticks, int add_tm_text)
381 static double dash[] = { 2.0, 2.0 };
383 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
384 g->y_axis_unit_change_callback == NULL, g->base_offset);
385 if (g->y_axis_unit_change_callback)
386 g->y_axis_unit_change_callback(g, power_of_ten);
389 * Use highest tickmark as top of graph, not highest value. Otherwise
390 * it's impossible to see what the max value is, if the graph is
393 maxy = tm[nticks - 1].value;
395 for (i = 0; i < nticks; i++) {
396 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
398 /* really ty < y1 || ty > y2, but protect against rounding */
399 if (y1 - ty > 0.01 || ty - y2 > 0.01)
403 cairo_move_to(cr, x1, ty);
404 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
407 /* draw grid lines */
409 cairo_set_dash(cr, dash, 2, 2.0);
410 cairo_set_line_width(cr, 0.5);
411 cairo_move_to(cr, x1, ty);
412 cairo_line_to(cr, x2, ty);
419 /* draw tickmark label */
420 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
425 * Return new max to use
430 void bar_graph_draw(struct graph *bg, cairo_t *cr)
432 double x1, y1, x2, y2;
433 double space_per_label, bar_width;
434 double label_offset, mindata, maxdata;
436 struct graph_label *lb;
439 cairo_translate(cr, bg->xoffset, bg->yoffset);
440 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
442 nlabels = count_labels(bg->labels);
443 space_per_label = (x2 - x1) / (double) nlabels;
445 mindata = find_min_data(bg->labels);
446 maxdata = find_max_data(bg->labels);
448 if (fabs(maxdata - mindata) < 1e-20) {
449 draw_centered_text(bg, cr,
450 x1 + (x2 - x1) / 2.0,
451 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
455 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
458 for (lb = bg->labels; lb; lb = lb->next) {
460 nvalues = count_values(lb->values);
461 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
462 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
463 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
464 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
465 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
473 typedef double (*xy_value_extractor)(struct graph_value *v);
475 static double getx(struct graph_value *v)
477 struct xyvalue *xy = v->value;
481 static double gety(struct graph_value *v)
483 struct xyvalue *xy = v->value;
487 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
489 double tmp, answer = 0.0;
490 struct graph_label *i;
491 struct graph_value *j;
494 for (i = g->labels; i; i = i->next)
495 for (j = i->values; j; j = j->next) {
501 answer = cmp(tmp, answer);
506 void line_graph_draw(struct graph *g, cairo_t *cr)
508 double x1, y1, x2, y2;
509 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
510 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
511 struct graph_label *i;
512 struct graph_value *j;
513 int good_data = 1, first = 1;
516 cairo_translate(cr, g->xoffset, g->yoffset);
517 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
519 minx = find_xy_value(g, getx, mindouble);
520 maxx = find_xy_value(g, getx, maxdouble);
521 miny = find_xy_value(g, gety, mindouble);
522 maxy = find_xy_value(g, gety, maxdouble);
524 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
537 if (g->top_extra > 0.001)
538 top_extra = fabs(maxy - miny) * g->top_extra;
539 if (g->bottom_extra > 0.001)
540 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
541 if (g->left_extra > 0.001)
542 left_extra = fabs(maxx - minx) * g->left_extra;
543 if (g->right_extra > 0.001)
544 right_extra = fabs(maxx - minx) * g->right_extra;
546 gminx = minx - left_extra;
547 gmaxx = maxx + right_extra;
548 gminy = miny - bottom_extra;
549 gmaxy = maxy + top_extra;
551 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
552 gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
557 cairo_set_line_width(cr, 1.5);
558 for (i = g->labels; i; i = i->next) {
560 if (i->r < 0) /* invisible data */
562 cairo_set_source_rgb(cr, i->r, i->g, i->b);
563 for (j = i->values; j; j = j->next) {
564 struct xyvalue *xy = j->value;
566 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
567 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
569 cairo_move_to(cr, tx, ty);
572 cairo_line_to(cr, tx, ty);
585 static void gfree(void *f)
591 static void setstring(char **str, const char *value)
594 *str = strdup(value);
597 void graph_title(struct graph *bg, const char *title)
599 setstring(&bg->title, title);
602 void graph_x_title(struct graph *bg, const char *title)
604 setstring(&bg->xtitle, title);
607 void graph_y_title(struct graph *bg, const char *title)
609 setstring(&bg->ytitle, title);
612 static struct graph_label *graph_find_label(struct graph *bg,
615 struct graph_label *i;
617 for (i = bg->labels; i; i = i->next)
618 if (strcmp(label, i->label) == 0)
623 void graph_add_label(struct graph *bg, const char *label)
625 struct graph_label *i;
627 i = graph_find_label(bg, label);
629 return; /* already present. */
630 i = calloc(1, sizeof(*i));
632 setstring(&i->label, label);
641 static void graph_label_add_value(struct graph_label *i, void *value,
644 struct graph_value *x;
646 x = malloc(sizeof(*x));
649 x->tooltip = strdup(tooltip);
663 if (i->parent->per_label_limit != -1 &&
664 i->value_count > i->parent->per_label_limit) {
668 * If the limit was dynamically reduced, making us more
669 * than 1 entry ahead after adding this one, drop two
670 * entries. This will make us (eventually) reach the
673 if (i->value_count - i->parent->per_label_limit >= 2)
678 i->values = i->values->next;
690 int graph_add_data(struct graph *bg, const char *label, const double value)
692 struct graph_label *i;
695 d = malloc(sizeof(*d));
698 i = graph_find_label(bg, label);
701 graph_label_add_value(i, d, NULL);
705 int graph_add_xy_data(struct graph *bg, const char *label,
706 const double x, const double y, const char *tooltip)
708 struct graph_label *i;
711 xy = malloc(sizeof(*xy));
715 i = graph_find_label(bg, label);
719 graph_label_add_value(i, xy, tooltip);
723 static void graph_free_values(struct graph_value *values)
725 struct graph_value *i, *next;
727 for (i = values; i; i = next) {
734 static void graph_free_labels(struct graph_label *labels)
736 struct graph_label *i, *next;
738 for (i = labels; i; i = next) {
740 graph_free_values(i->values);
745 void graph_set_color(struct graph *gr, const char *label,
746 double red, double green, double blue)
748 struct graph_label *i;
751 if (red < 0.0) { /* invisible color */
768 for (i = gr->labels; i; i = i->next)
769 if (strcmp(i->label, label) == 0) {
777 void graph_free(struct graph *bg)
782 graph_free_labels(bg->labels);
785 /* For each line in the line graph, up to per_label_limit segments may
786 * be added. After that, adding more data to the end of the line
787 * causes data to drop off of the front of the line.
789 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
791 g->per_label_limit = per_label_limit;
794 void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
795 double top_percent, double bottom_percent)
797 g->left_extra = left_percent;
798 g->right_extra = right_percent;
799 g->top_extra = top_percent;
800 g->bottom_extra = bottom_percent;
804 * Normally values are logged in a base unit of 0, but for other purposes
805 * it makes more sense to log in higher unit. For instance for bandwidth
806 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
808 void graph_set_base_offset(struct graph *g, unsigned int base_offset)
810 g->base_offset = base_offset;
813 int graph_has_tooltips(struct graph *g)
815 struct graph_label *i;
817 for (i = g->labels; i; i = i->next)
818 if (i->tooltip_count)
824 int graph_contains_xy(struct graph *g, int x, int y)
826 int first_x = g->xoffset;
827 int last_x = g->xoffset + g->xdim;
828 int first_y = g->yoffset;
829 int last_y = g->yoffset + g->ydim;
831 return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
834 static int xy_match(struct xyvalue *xy, int x, int y)
836 int xdiff = abs(xy->gx - x);
837 int ydiff = abs(xy->gy - y);
839 return xdiff <= 20 && ydiff <= 10;
842 const char *graph_find_tooltip(struct graph *g, int x, int y)
844 struct graph_label *i;
845 struct graph_value *j;
847 for (i = g->labels; i; i = i->next) {
848 for (j = i->values; j; j = j->next) {
849 struct xyvalue *xy = j->value;
851 if (xy_match(xy, x - g->xoffset, y))