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
31 #include "tickmarks.h"
39 struct graph_value *next;
45 struct graph_value *tail;
46 struct graph_value *values;
47 struct graph_label *next;
57 unsigned int xdim, ydim;
58 double xoffset, yoffset;
59 struct graph_label *labels;
60 struct graph_label *tail;
63 graph_axis_unit_change_callback x_axis_unit_change_callback;
64 graph_axis_unit_change_callback y_axis_unit_change_callback;
71 void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
77 void graph_set_position(struct graph *g, double xoffset, double yoffset)
83 struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
87 g = calloc(1, sizeof(*g));
88 graph_set_size(g, xdim, ydim);
89 g->per_label_limit = -1;
96 void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
98 g->x_axis_unit_change_callback = f;
101 void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
103 g->y_axis_unit_change_callback = f;
106 static int count_labels(struct graph_label *labels)
109 struct graph_label *i;
111 for (i = labels; i; i = i->next)
116 static int count_values(struct graph_value *values)
119 struct graph_value *i;
121 for (i = values; i; i = i->next)
126 typedef double (*double_comparator)(double a, double b);
128 static double mindouble(double a, double b)
130 return a < b ? a : b;
133 static double maxdouble(double a, double b)
135 return a < b ? b : a;
138 static double find_double_values(struct graph_value *values, double_comparator cmp)
140 struct graph_value *i;
144 assert(values != NULL);
145 answer = 0.0; /* shut the compiler up, might need to think harder though. */
146 for (i = values; i; i = i->next) {
147 tmp = *(double *) i->value;
152 answer = cmp(answer, tmp);
158 static double find_double_data(struct graph_label *labels, double_comparator cmp)
160 struct graph_label *i;
164 assert(labels != NULL);
165 answer = 0.0; /* shut the compiler up, might need to think harder though. */
166 for (i = labels; i; i = i->next) {
167 tmp = find_double_values(i->values, cmp);
172 answer = cmp(tmp, answer);
178 static double find_min_data(struct graph_label *labels)
180 return find_double_data(labels, mindouble);
183 static double find_max_data(struct graph_label *labels)
185 return find_double_data(labels, maxdouble);
188 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
189 double label_offset, double bar_width,
190 double mindata, double maxdata)
192 struct graph_value *i;
193 double x1, y1, x2, y2;
195 double domain, range, v;
197 domain = (maxdata - mindata);
198 range = (double) bg->ydim * 0.80; /* FIXME */
200 for (i = lb->values; i; i = i->next) {
202 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
203 x2 = x1 + bar_width * 0.90;
204 y2 = bg->ydim * 0.90;
205 v = *(double *) i->value;
206 y1 = y2 - (((v - mindata) / domain) * range);
207 cairo_move_to(cr, x1, y1);
208 cairo_line_to(cr, x1, y2);
209 cairo_line_to(cr, x2, y2);
210 cairo_line_to(cr, x2, y1);
211 cairo_close_path(cr);
218 static void draw_aligned_text(struct graph *g, cairo_t *cr, double x, double y,
219 double fontsize, const char *text, int alignment)
222 #define LEFT_JUSTIFIED 1
223 #define RIGHT_JUSTIFIED 2
225 double factor, direction;
226 cairo_text_extents_t extents;
233 case RIGHT_JUSTIFIED:
243 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
245 cairo_set_font_size(cr, fontsize);
246 cairo_text_extents(cr, text, &extents);
247 x = x + direction * (factor * extents.width + extents.x_bearing);
248 y = y - (extents.height / 2 + extents.y_bearing);
250 cairo_move_to(cr, x, y);
251 cairo_show_text(cr, text);
254 static inline void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
255 double fontsize, const char *text)
257 draw_aligned_text(g, cr, x, y, fontsize, text, CENTERED);
260 static inline void draw_right_justified_text(struct graph *g, cairo_t *cr,
262 double fontsize, const char *text)
264 draw_aligned_text(g, cr, x, y, fontsize, text, RIGHT_JUSTIFIED);
267 static inline void draw_left_justified_text(struct graph *g, cairo_t *cr,
269 double fontsize, const char *text)
271 draw_aligned_text(g, cr, x, y, fontsize, text, LEFT_JUSTIFIED);
274 static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
275 double y, double fontsize,
279 cairo_text_extents_t extents;
281 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
283 cairo_set_font_size(cr, fontsize);
284 cairo_text_extents(cr, text, &extents);
287 y = y + (extents.width / 2.0 + extents.x_bearing);
288 x = x - (extents.height / 2.0 + extents.y_bearing);
290 cairo_move_to(cr, x, y);
292 cairo_translate(cr, -sx, -sy);
293 cairo_rotate(cr, -90.0 * M_PI / 180.0);
294 cairo_translate(cr, sx, sy);
295 cairo_show_text(cr, text);
299 static void graph_draw_common(struct graph *g, cairo_t *cr,
300 double *x1, double *y1, double *x2, double *y2)
302 cairo_set_source_rgb(cr, 0, 0, 0);
303 cairo_set_line_width (cr, 0.8);
305 *x1 = 0.15 * g->xdim;
306 *x2 = 0.95 * g->xdim;
307 *y1 = 0.10 * g->ydim;
308 *y2 = 0.90 * g->ydim;
310 cairo_move_to(cr, *x1, *y1);
311 cairo_line_to(cr, *x1, *y2);
312 cairo_line_to(cr, *x2, *y2);
313 cairo_line_to(cr, *x2, *y1);
314 cairo_line_to(cr, *x1, *y1);
317 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
318 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
319 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
323 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
324 double x1, double y1, double x2, double y2,
325 double minx, double maxx, int nticks)
330 static double dash[] = { 1.0, 2.0 };
332 nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
333 g->x_axis_unit_change_callback == NULL);
334 if (g->x_axis_unit_change_callback)
335 g->x_axis_unit_change_callback(g, power_of_ten);
337 for (i = 0; i < nticks; i++) {
338 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
339 if (tx < x1 || tx > x2)
343 cairo_set_line_width(cr, 0.8);
344 cairo_move_to(cr, tx, y2);
345 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
348 /* draw grid lines */
350 cairo_set_dash(cr, dash, 2, 2.0);
351 cairo_set_line_width(cr, 0.5);
352 cairo_move_to(cr, tx, y1);
353 cairo_line_to(cr, tx, y2);
357 /* draw tickmark label */
358 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
364 static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
365 double x1, double y1, double x2, double y2,
366 double miny, double maxy, int nticks)
371 static double dash[] = { 2.0, 2.0 };
373 nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
374 g->y_axis_unit_change_callback == NULL);
375 if (g->y_axis_unit_change_callback)
376 g->y_axis_unit_change_callback(g, power_of_ten);
378 for (i = 0; i < nticks; i++) {
379 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
380 if (ty < y1 || ty > y2)
383 cairo_move_to(cr, x1, ty);
384 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
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, x1, ty);
392 cairo_line_to(cr, x2, ty);
396 /* draw tickmark label */
397 draw_right_justified_text(g, cr, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
402 void bar_graph_draw(struct graph *bg, cairo_t *cr)
404 double x1, y1, x2, y2;
405 double space_per_label, bar_width;
406 double label_offset, mindata, maxdata;
408 struct graph_label *lb;
411 cairo_translate(cr, bg->xoffset, bg->yoffset);
412 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
414 nlabels = count_labels(bg->labels);
415 space_per_label = (x2 - x1) / (double) nlabels;
417 mindata = find_min_data(bg->labels);
418 maxdata = find_max_data(bg->labels);
420 if (fabs(maxdata - mindata) < 1e-20) {
421 draw_centered_text(bg, cr,
422 x1 + (x2 - x1) / 2.0,
423 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
427 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
430 for (lb = bg->labels; lb; lb = lb->next) {
432 nvalues = count_values(lb->values);
433 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
434 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
435 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
436 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
437 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
445 typedef double (*xy_value_extractor)(struct graph_value *v);
447 static double getx(struct graph_value *v)
449 struct xyvalue *xy = v->value;
453 static double gety(struct graph_value *v)
455 struct xyvalue *xy = v->value;
459 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
461 double tmp, answer = 0.0;
462 struct graph_label *i;
463 struct graph_value *j;
466 for (i = g->labels; i; i = i->next)
467 for (j = i->values; j; j = j->next) {
473 answer = cmp(tmp, answer);
478 void line_graph_draw(struct graph *g, cairo_t *cr)
480 double x1, y1, x2, y2;
481 double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
482 double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
483 struct graph_label *i;
484 struct graph_value *j;
485 int good_data = 1, first = 1;
488 cairo_translate(cr, g->xoffset, g->yoffset);
489 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
491 minx = find_xy_value(g, getx, mindouble);
492 maxx = find_xy_value(g, getx, maxdouble);
493 miny = find_xy_value(g, gety, mindouble);
494 maxy = find_xy_value(g, gety, maxdouble);
496 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
509 if (g->top_extra > 0.001)
510 top_extra = fabs(maxy - miny) * g->top_extra;
511 if (g->bottom_extra > 0.001)
512 bottom_extra = fabs(maxy - miny) * g->bottom_extra;
513 if (g->left_extra > 0.001)
514 left_extra = fabs(maxx - minx) * g->left_extra;
515 if (g->right_extra > 0.001)
516 right_extra = fabs(maxx - minx) * g->right_extra;
518 gminx = minx - left_extra;
519 gmaxx = maxx + right_extra;
520 gminy = miny - bottom_extra;
521 gmaxy = maxy + top_extra;
523 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10);
524 graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10);
529 cairo_set_line_width(cr, 1.5);
530 for (i = g->labels; i; i = i->next) {
532 if (i->r < 0) /* invisible data */
534 cairo_set_source_rgb(cr, i->r, i->g, i->b);
535 for (j = i->values; j; j = j->next) {
536 tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
537 ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
539 cairo_move_to(cr, tx, ty);
542 cairo_line_to(cr, tx, ty);
553 static void gfree(void *f)
559 static void setstring(char **str, const char *value)
562 *str = strdup(value);
565 void graph_title(struct graph *bg, const char *title)
567 setstring(&bg->title, title);
570 void graph_x_title(struct graph *bg, const char *title)
572 setstring(&bg->xtitle, title);
575 void graph_y_title(struct graph *bg, const char *title)
577 setstring(&bg->ytitle, title);
580 static struct graph_label *graph_find_label(struct graph *bg,
583 struct graph_label *i;
585 for (i = bg->labels; i; i = i->next)
586 if (strcmp(label, i->label) == 0)
591 void graph_add_label(struct graph *bg, const char *label)
593 struct graph_label *i;
595 i = graph_find_label(bg, label);
597 return; /* already present. */
598 i = calloc(1, sizeof(*i));
600 setstring(&i->label, label);
609 static void graph_label_add_value(struct graph_label *i, void *value)
611 struct graph_value *x;
613 x = malloc(sizeof(*x));
624 if (i->parent->per_label_limit != -1 &&
625 i->value_count > i->parent->per_label_limit) {
629 * If the limit was dynamically reduced, making us more
630 * than 1 entry ahead after adding this one, drop two
631 * entries. This will make us (eventually) reach the
634 if (i->value_count - i->parent->per_label_limit >= 2)
639 i->values = i->values->next;
647 int graph_add_data(struct graph *bg, const char *label, const double value)
649 struct graph_label *i;
652 d = malloc(sizeof(*d));
655 i = graph_find_label(bg, label);
658 graph_label_add_value(i, d);
662 int graph_add_xy_data(struct graph *bg, const char *label,
663 const double x, const double y)
665 struct graph_label *i;
668 xy = malloc(sizeof(*xy));
672 i = graph_find_label(bg, label);
675 graph_label_add_value(i, xy);
679 static void graph_free_values(struct graph_value *values)
681 struct graph_value *i, *next;
683 for (i = values; i; i = next) {
690 static void graph_free_labels(struct graph_label *labels)
692 struct graph_label *i, *next;
694 for (i = labels; i; i = next) {
696 graph_free_values(i->values);
701 void graph_set_color(struct graph *gr, const char *label,
702 double red, double green, double blue)
704 struct graph_label *i;
707 if (red < 0.0) { /* invisible color */
724 for (i = gr->labels; i; i = i->next)
725 if (strcmp(i->label, label) == 0) {
733 void graph_free(struct graph *bg)
738 graph_free_labels(bg->labels);
741 /* For each line in the line graph, up to per_label_limit segments may
742 * be added. After that, adding more data to the end of the line
743 * causes data to drop off of the front of the line.
745 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
747 g->per_label_limit = per_label_limit;
750 void graph_add_extra_space(struct graph *g, double left_percent, double right_percent,
751 double top_percent, double bottom_percent)
753 g->left_extra = left_percent;
754 g->right_extra = right_percent;
755 g->top_extra = top_percent;
756 g->bottom_extra = bottom_percent;