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"
38 struct graph_value *next;
44 struct graph_value *tail;
45 struct graph_value *values;
46 struct graph_label *next;
56 unsigned int xdim, ydim;
57 struct graph_label *labels;
58 struct graph_label *tail;
62 struct graph *graph_new(unsigned int xdim, unsigned int ydim)
66 g = calloc(1, sizeof(*g));
69 g->per_label_limit = -1;
73 static int count_labels(struct graph_label *labels)
76 struct graph_label *i;
78 for (i = labels; i; i = i->next)
83 static int count_values(struct graph_value *values)
86 struct graph_value *i;
88 for (i = values; i; i = i->next)
93 typedef double (*double_comparator)(double a, double b);
95 static double mindouble(double a, double b)
100 static double maxdouble(double a, double b)
102 return a < b ? b : a;
105 static double find_double_values(struct graph_value *values, double_comparator cmp)
107 struct graph_value *i;
111 assert(values != NULL);
112 answer = 0.0; /* shut the compiler up, might need to think harder though. */
113 for (i = values; i; i = i->next) {
114 tmp = *(double *) i->value;
119 answer = cmp(answer, tmp);
125 static double find_double_data(struct graph_label *labels, double_comparator cmp)
127 struct graph_label *i;
131 assert(labels != NULL);
132 answer = 0.0; /* shut the compiler up, might need to think harder though. */
133 for (i = labels; i; i = i->next) {
134 tmp = find_double_values(i->values, cmp);
139 answer = cmp(tmp, answer);
145 static double find_min_data(struct graph_label *labels)
147 return find_double_data(labels, mindouble);
150 static double find_max_data(struct graph_label *labels)
152 return find_double_data(labels, maxdouble);
155 static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
156 double label_offset, double bar_width,
157 double mindata, double maxdata)
159 struct graph_value *i;
160 double x1, y1, x2, y2;
162 double domain, range, v;
164 domain = (maxdata - mindata);
165 range = (double) bg->ydim * 0.80; /* FIXME */
167 for (i = lb->values; i; i = i->next) {
169 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
170 x2 = x1 + bar_width * 0.90;
171 y2 = bg->ydim * 0.90;
172 v = *(double *) i->value;
173 y1 = y2 - (((v - mindata) / domain) * range);
174 cairo_move_to(cr, x1, y1);
175 cairo_line_to(cr, x1, y2);
176 cairo_line_to(cr, x2, y2);
177 cairo_line_to(cr, x2, y1);
178 cairo_close_path(cr);
185 static void draw_centered_text(cairo_t *cr, double x, double y,
186 double fontsize, const char *text)
188 cairo_text_extents_t extents;
190 cairo_select_font_face (cr, "Sans",
191 CAIRO_FONT_SLANT_NORMAL,
192 CAIRO_FONT_WEIGHT_NORMAL);
194 cairo_set_font_size(cr, fontsize);
195 cairo_text_extents(cr, text, &extents);
196 x = x - (extents.width / 2 + extents.x_bearing);
197 y = y - (extents.height / 2 + extents.y_bearing);
199 cairo_move_to(cr, x, y);
200 cairo_show_text(cr, text);
203 static void draw_vertical_centered_text(cairo_t *cr, double x, double y,
204 double fontsize, const char *text)
207 cairo_text_extents_t extents;
209 cairo_select_font_face (cr, "Sans",
210 CAIRO_FONT_SLANT_NORMAL,
211 CAIRO_FONT_WEIGHT_NORMAL);
213 cairo_set_font_size(cr, fontsize);
214 cairo_text_extents(cr, text, &extents);
217 y = y + (extents.width / 2.0 + extents.x_bearing);
218 x = x - (extents.height / 2.0 + extents.y_bearing);
220 cairo_move_to(cr, x, y);
222 cairo_translate(cr, -sx, -sy);
223 cairo_rotate(cr, -90.0 * M_PI / 180.0);
224 cairo_translate(cr, sx, sy);
225 cairo_show_text(cr, text);
229 static void graph_draw_common(struct graph *g, cairo_t *cr,
230 double *x1, double *y1, double *x2, double *y2)
232 cairo_set_source_rgb(cr, 0, 0, 0);
233 cairo_set_line_width (cr, 1.0);
235 cairo_move_to(cr, 0, 0);
236 cairo_line_to(cr, 0, g->ydim);
237 cairo_line_to(cr, g->xdim, g->ydim);
238 cairo_line_to(cr, g->xdim, 0);
239 cairo_line_to(cr, 0, 0);
241 /* for now just set margins at 10% of width. This is not very good. */
242 *x1 = g->xdim / 10.0;
244 *y1 = g->ydim / 10.0;
247 cairo_move_to(cr, *x1, *y1);
248 cairo_line_to(cr, *x1, *y2);
249 cairo_line_to(cr, *x2, *y2);
250 cairo_line_to(cr, *x2, *y1);
251 cairo_line_to(cr, *x1, *y1);
254 draw_centered_text(cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
255 draw_centered_text(cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
256 draw_vertical_centered_text(cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
260 static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
261 double x1, double y1, double x2, double y2,
262 double minx, double maxx, int nticks)
267 static double dash[] = { 1.0, 2.0 };
269 nticks = calc_tickmarks(minx, maxx, nticks, &tm);
271 for (i = 0; i < nticks; i++) {
272 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
273 if (tx < x1 || tx > x2)
277 cairo_set_line_width(cr, 1.0);
278 cairo_move_to(cr, tx, y2);
279 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
282 /* draw grid lines */
284 cairo_set_dash(cr, dash, 2, 2.0);
285 cairo_set_line_width(cr, 0.5);
286 cairo_move_to(cr, tx, y1);
287 cairo_line_to(cr, tx, y2);
291 /* draw tickmark label */
292 draw_centered_text(cr, tx, y2 * 1.04, 12.0, tm[i].string);
298 static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
299 double x1, double y1, double x2, double y2,
300 double miny, double maxy, int nticks)
305 static double dash[] = { 2.0, 2.0 };
307 nticks = calc_tickmarks(miny, maxy, nticks, &tm);
309 for (i = 0; i < nticks; i++) {
310 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
311 if (ty < y1 || ty > y2)
314 cairo_move_to(cr, x1, ty);
315 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
318 /* draw grid lines */
320 cairo_set_dash(cr, dash, 2, 2.0);
321 cairo_set_line_width(cr, 0.5);
322 cairo_move_to(cr, x1, ty);
323 cairo_line_to(cr, x2, ty);
327 /* draw tickmark label */
328 draw_centered_text(cr, x1 - (x2 - x1) * 0.04, ty, 12.0, tm[i].string);
333 void bar_graph_draw(struct graph *bg, cairo_t *cr)
335 double x1, y1, x2, y2;
336 double space_per_label, bar_width;
337 double label_offset, mindata, maxdata;
339 struct graph_label *lb;
342 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
344 nlabels = count_labels(bg->labels);
345 space_per_label = (x2 - x1) / (double) nlabels;
347 mindata = find_min_data(bg->labels);
348 maxdata = find_max_data(bg->labels);
350 if (fabs(maxdata - mindata) < 1e-20) {
351 draw_centered_text(cr,
352 x1 + (x2 - x1) / 2.0,
353 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
357 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
360 for (lb = bg->labels; lb; lb = lb->next) {
362 nvalues = count_values(lb->values);
363 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
364 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
365 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
366 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
367 draw_centered_text(cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
375 typedef double (*xy_value_extractor)(struct graph_value *v);
377 static double getx(struct graph_value *v)
379 struct xyvalue *xy = v->value;
383 static double gety(struct graph_value *v)
385 struct xyvalue *xy = v->value;
389 static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
391 double tmp, answer = 0.0;
392 struct graph_label *i;
393 struct graph_value *j;
395 for (i = g->labels; i; i = i->next)
396 for (j = i->values; j; j = j->next) {
398 answer = cmp(tmp, answer);
403 void line_graph_draw(struct graph *g, cairo_t *cr)
405 double x1, y1, x2, y2;
406 double minx, miny, maxx, maxy;
408 struct graph_label *i;
409 struct graph_value *j;
413 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
415 minx = find_xy_value(g, getx, mindouble);
416 maxx = find_xy_value(g, getx, maxdouble);
417 miny = find_xy_value(g, gety, mindouble);
418 maxy = find_xy_value(g, gety, maxdouble);
420 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
421 draw_centered_text(cr,
422 x1 + (x2 - x1) / 2.0,
423 y1 + (y2 - y1) / 2.0, 20.0, "No good Data");
427 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10);
428 graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10);
430 cairo_set_line_width(cr, 2.5);
431 for (i = g->labels; i; i = i->next) {
433 cairo_set_source_rgb(cr, i->r, i->g, i->b);
434 for (j = i->values; j; j = j->next) {
435 tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1;
436 ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1);
438 cairo_move_to(cr, tx, ty);
441 cairo_line_to(cr, tx, ty);
449 static void gfree(void *f)
455 static void setstring(char **str, const char *value)
458 *str = strdup(value);
461 void graph_title(struct graph *bg, const char *title)
463 setstring(&bg->title, title);
466 void graph_x_title(struct graph *bg, const char *title)
468 setstring(&bg->xtitle, title);
471 void graph_y_title(struct graph *bg, const char *title)
473 setstring(&bg->ytitle, title);
476 static struct graph_label *graph_find_label(struct graph *bg,
479 struct graph_label *i;
481 for (i = bg->labels; i; i = i->next)
482 if (strcmp(label, i->label) == 0)
487 void graph_add_label(struct graph *bg, const char *label)
489 struct graph_label *i;
491 i = graph_find_label(bg, label);
493 return; /* already present. */
494 i = calloc(1, sizeof(*i));
496 setstring(&i->label, label);
505 static void graph_label_add_value(struct graph_label *i, void *value)
507 struct graph_value *x;
509 x = malloc(sizeof(*x));
520 if (i->parent->per_label_limit != -1 &&
521 i->value_count > i->parent->per_label_limit) {
523 i->values = i->values->next;
530 int graph_add_data(struct graph *bg, const char *label, const double value)
532 struct graph_label *i;
535 d = malloc(sizeof(*d));
538 i = graph_find_label(bg, label);
541 graph_label_add_value(i, d);
545 int graph_add_xy_data(struct graph *bg, const char *label,
546 const double x, const double y)
548 struct graph_label *i;
551 xy = malloc(sizeof(*xy));
555 i = graph_find_label(bg, label);
558 graph_label_add_value(i, xy);
562 static void graph_free_values(struct graph_value *values)
564 struct graph_value *i, *next;
566 for (i = values; i; i = next) {
573 static void graph_free_labels(struct graph_label *labels)
575 struct graph_label *i, *next;
577 for (i = labels; i; i = next) {
579 graph_free_values(i->values);
584 void graph_set_color(struct graph *gr, const char *label,
585 double red, double green, double blue)
587 struct graph_label *i;
601 for (i = gr->labels; i; i = i->next)
602 if (strcmp(i->label, label) == 0) {
610 void graph_free(struct graph *bg)
615 graph_free_labels(bg->labels);
618 /* For each line in the line graph, up to per_label_limit segments may
619 * be added. After that, adding more data to the end of the line
620 * causes data to drop off of the front of the line.
622 void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
624 g->per_label_limit = per_label_limit;