gfio: fix problem with graph finding data range.
[fio.git] / graph.c
CommitLineData
af58ef32
SC
1/*
2 * gfio - gui front end for fio - the flexible io tester
3 *
4 * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com>
5 *
6 * The license below covers all files distributed with fio unless otherwise
7 * noted in the file itself.
8 *
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.
12 *
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.
17 *
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
21 *
22 */
23#include <string.h>
24#include <malloc.h>
25#include <math.h>
26#include <assert.h>
27
28#include <cairo.h>
29#include <gtk/gtk.h>
30
31#include "tickmarks.h"
32
33struct xyvalue {
34 double x, y;
35};
36
37struct graph_value {
38 struct graph_value *next;
39 void *value;
40};
41
42struct graph_label {
43 char *label;
44 struct graph_value *tail;
45 struct graph_value *values;
46 struct graph_label *next;
47 double r, g, b;
48 int value_count;
49 struct graph *parent;
50};
51
52struct graph {
53 char *title;
54 char *xtitle;
55 char *ytitle;
87d5f276 56 unsigned int xdim, ydim;
af58ef32
SC
57 struct graph_label *labels;
58 struct graph_label *tail;
59 int per_label_limit;
f3e8440f 60 const char *font;
af58ef32
SC
61};
62
f3e8440f 63struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
af58ef32
SC
64{
65 struct graph *g;
66
67 g = calloc(1, sizeof(*g));
68 g->xdim = xdim;
69 g->ydim = ydim;
70 g->per_label_limit = -1;
f3e8440f
JA
71 g->font = font;
72 if (!g->font)
73 g->font = "Sans";
af58ef32
SC
74 return g;
75}
76
77static int count_labels(struct graph_label *labels)
78{
79 int count = 0;
80 struct graph_label *i;
81
82 for (i = labels; i; i = i->next)
83 count++;
84 return count;
85}
86
87static int count_values(struct graph_value *values)
88{
89 int count = 0;
90 struct graph_value *i;
91
92 for (i = values; i; i = i->next)
93 count++;
94 return count;
95}
96
97typedef double (*double_comparator)(double a, double b);
98
99static double mindouble(double a, double b)
100{
101 return a < b ? a : b;
102}
103
104static double maxdouble(double a, double b)
105{
106 return a < b ? b : a;
107}
108
109static double find_double_values(struct graph_value *values, double_comparator cmp)
110{
111 struct graph_value *i;
112 int first = 1;
113 double answer, tmp;
114
115 assert(values != NULL);
116 answer = 0.0; /* shut the compiler up, might need to think harder though. */
117 for (i = values; i; i = i->next) {
118 tmp = *(double *) i->value;
119 if (first) {
120 answer = tmp;
121 first = 0;
122 } else {
123 answer = cmp(answer, tmp);
124 }
125 }
126 return answer;
127}
128
129static double find_double_data(struct graph_label *labels, double_comparator cmp)
130{
131 struct graph_label *i;
132 int first = 1;
133 double answer, tmp;
134
135 assert(labels != NULL);
136 answer = 0.0; /* shut the compiler up, might need to think harder though. */
137 for (i = labels; i; i = i->next) {
138 tmp = find_double_values(i->values, cmp);
139 if (first) {
140 answer = tmp;
141 first = 0;
142 } else {
143 answer = cmp(tmp, answer);
144 }
145 }
146 return answer;
147}
148
149static double find_min_data(struct graph_label *labels)
150{
151 return find_double_data(labels, mindouble);
152}
153
154static double find_max_data(struct graph_label *labels)
155{
156 return find_double_data(labels, maxdouble);
157}
158
159static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
160 double label_offset, double bar_width,
161 double mindata, double maxdata)
162{
163 struct graph_value *i;
164 double x1, y1, x2, y2;
165 int bar_num = 0;
166 double domain, range, v;
167
168 domain = (maxdata - mindata);
169 range = (double) bg->ydim * 0.80; /* FIXME */
170 cairo_stroke(cr);
171 for (i = lb->values; i; i = i->next) {
172
173 x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
174 x2 = x1 + bar_width * 0.90;
175 y2 = bg->ydim * 0.90;
176 v = *(double *) i->value;
177 y1 = y2 - (((v - mindata) / domain) * range);
178 cairo_move_to(cr, x1, y1);
179 cairo_line_to(cr, x1, y2);
180 cairo_line_to(cr, x2, y2);
181 cairo_line_to(cr, x2, y1);
182 cairo_close_path(cr);
183 cairo_fill(cr);
184 cairo_stroke(cr);
185 bar_num++;
186 }
187}
188
f3e8440f
JA
189static void draw_centered_text(struct graph *g, cairo_t *cr, double x, double y,
190 double fontsize, const char *text)
af58ef32
SC
191{
192 cairo_text_extents_t extents;
193
f3e8440f 194 cairo_select_font_face (cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
af58ef32
SC
195
196 cairo_set_font_size(cr, fontsize);
197 cairo_text_extents(cr, text, &extents);
198 x = x - (extents.width / 2 + extents.x_bearing);
199 y = y - (extents.height / 2 + extents.y_bearing);
200
201 cairo_move_to(cr, x, y);
202 cairo_show_text(cr, text);
203}
204
f3e8440f
JA
205static void draw_vertical_centered_text(struct graph *g, cairo_t *cr, double x,
206 double y, double fontsize,
207 const char *text)
af58ef32
SC
208{
209 double sx, sy;
210 cairo_text_extents_t extents;
211
f3e8440f 212 cairo_select_font_face(cr, g->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
af58ef32
SC
213
214 cairo_set_font_size(cr, fontsize);
215 cairo_text_extents(cr, text, &extents);
216 sx = x;
217 sy = y;
218 y = y + (extents.width / 2.0 + extents.x_bearing);
219 x = x - (extents.height / 2.0 + extents.y_bearing);
220
221 cairo_move_to(cr, x, y);
222 cairo_save(cr);
223 cairo_translate(cr, -sx, -sy);
224 cairo_rotate(cr, -90.0 * M_PI / 180.0);
225 cairo_translate(cr, sx, sy);
226 cairo_show_text(cr, text);
227 cairo_restore(cr);
228}
229
230static void graph_draw_common(struct graph *g, cairo_t *cr,
231 double *x1, double *y1, double *x2, double *y2)
232{
233 cairo_set_source_rgb(cr, 0, 0, 0);
f3e8440f 234 cairo_set_line_width (cr, 0.8);
af58ef32 235
af58ef32
SC
236 /* for now just set margins at 10% of width. This is not very good. */
237 *x1 = g->xdim / 10.0;
f3e8440f 238 *x2 = 9.0 * *x1;
af58ef32 239 *y1 = g->ydim / 10.0;
f3e8440f 240 *y2 = 9.0 * *y1;
af58ef32
SC
241
242 cairo_move_to(cr, *x1, *y1);
243 cairo_line_to(cr, *x1, *y2);
244 cairo_line_to(cr, *x2, *y2);
245 cairo_line_to(cr, *x2, *y1);
246 cairo_line_to(cr, *x1, *y1);
247 cairo_stroke(cr);
248
f3e8440f
JA
249 draw_centered_text(g, cr, g->xdim / 2, g->ydim / 20, 20.0, g->title);
250 draw_centered_text(g, cr, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
251 draw_vertical_centered_text(g, cr, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
af58ef32
SC
252 cairo_stroke(cr);
253}
254
255static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
256 double x1, double y1, double x2, double y2,
257 double minx, double maxx, int nticks)
258{
259 struct tickmark *tm;
260 double tx;
261 int i;
262 static double dash[] = { 1.0, 2.0 };
263
264 nticks = calc_tickmarks(minx, maxx, nticks, &tm);
265
266 for (i = 0; i < nticks; i++) {
267 tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
268 if (tx < x1 || tx > x2)
269 continue;
270
271 /* Draw tick mark */
f3e8440f 272 cairo_set_line_width(cr, 0.8);
af58ef32
SC
273 cairo_move_to(cr, tx, y2);
274 cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
275 cairo_stroke(cr);
276
277 /* draw grid lines */
278 cairo_save(cr);
279 cairo_set_dash(cr, dash, 2, 2.0);
280 cairo_set_line_width(cr, 0.5);
281 cairo_move_to(cr, tx, y1);
282 cairo_line_to(cr, tx, y2);
283 cairo_stroke(cr);
284 cairo_restore(cr);
285
286 /* draw tickmark label */
f3e8440f 287 draw_centered_text(g, cr, tx, y2 * 1.04, 12.0, tm[i].string);
af58ef32
SC
288 cairo_stroke(cr);
289
290 }
291}
292
293static void graph_draw_y_ticks(struct graph *g, cairo_t *cr,
294 double x1, double y1, double x2, double y2,
295 double miny, double maxy, int nticks)
296{
297 struct tickmark *tm;
298 double ty;
299 int i;
300 static double dash[] = { 2.0, 2.0 };
301
302 nticks = calc_tickmarks(miny, maxy, nticks, &tm);
303
304 for (i = 0; i < nticks; i++) {
305 ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
306 if (ty < y1 || ty > y2)
307 continue;
308 /* draw tick mark */
309 cairo_move_to(cr, x1, ty);
310 cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
311 cairo_stroke(cr);
312
313 /* draw grid lines */
314 cairo_save(cr);
315 cairo_set_dash(cr, dash, 2, 2.0);
316 cairo_set_line_width(cr, 0.5);
317 cairo_move_to(cr, x1, ty);
318 cairo_line_to(cr, x2, ty);
319 cairo_stroke(cr);
320 cairo_restore(cr);
321
322 /* draw tickmark label */
f3e8440f 323 draw_centered_text(g, cr, x1 - (x2 - x1) * 0.04, ty, 12.0, tm[i].string);
af58ef32
SC
324 cairo_stroke(cr);
325 }
326}
327
328void bar_graph_draw(struct graph *bg, cairo_t *cr)
329{
330 double x1, y1, x2, y2;
331 double space_per_label, bar_width;
332 double label_offset, mindata, maxdata;
333 int i, nlabels;
334 struct graph_label *lb;
335
336 cairo_save(cr);
337 graph_draw_common(bg, cr, &x1, &y1, &x2, &y2);
338
339 nlabels = count_labels(bg->labels);
340 space_per_label = (x2 - x1) / (double) nlabels;
341
342 mindata = find_min_data(bg->labels);
343 maxdata = find_max_data(bg->labels);
344
345 if (fabs(maxdata - mindata) < 1e-20) {
f3e8440f 346 draw_centered_text(bg, cr,
af58ef32
SC
347 x1 + (x2 - x1) / 2.0,
348 y1 + (y2 - y1) / 2.0, 20.0, "No good data");
349 return;
350 }
351
352 graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10);
353
354 i = 0;
355 for (lb = bg->labels; lb; lb = lb->next) {
356 int nvalues;
357 nvalues = count_values(lb->values);
358 bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
359 label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
360 draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
361 // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
f3e8440f 362 draw_centered_text(bg, cr, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
af58ef32
SC
363 12.0, lb->label);
364 i++;
365 }
366 cairo_stroke(cr);
367 cairo_restore(cr);
368}
369
370typedef double (*xy_value_extractor)(struct graph_value *v);
371
372static double getx(struct graph_value *v)
373{
374 struct xyvalue *xy = v->value;
375 return xy->x;
376}
377
378static double gety(struct graph_value *v)
379{
380 struct xyvalue *xy = v->value;
381 return xy->y;
382}
383
384static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
385{
d582bf70 386 double tmp, answer;
af58ef32
SC
387 struct graph_label *i;
388 struct graph_value *j;
d582bf70 389 int first = 1;
af58ef32
SC
390
391 for (i = g->labels; i; i = i->next)
392 for (j = i->values; j; j = j->next) {
393 tmp = getvalue(j);
d582bf70
SC
394 if (first) {
395 first = 0;
396 answer = tmp;
397 }
af58ef32
SC
398 answer = cmp(tmp, answer);
399 }
400 return answer;
401}
402
403void line_graph_draw(struct graph *g, cairo_t *cr)
404{
405 double x1, y1, x2, y2;
406 double minx, miny, maxx, maxy;
407 double tx, ty;
408 struct graph_label *i;
409 struct graph_value *j;
9ce9cfbd 410 int good_data = 1, first = 1;
af58ef32
SC
411
412 cairo_save(cr);
413 graph_draw_common(g, cr, &x1, &y1, &x2, &y2);
414
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);
419
420 if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
9ce9cfbd
SC
421 good_data = 0;
422 minx = 0.0;
423 miny = 0.0;
424 maxx = 10.0;
425 maxy = 100.0;
af58ef32
SC
426 }
427
428 graph_draw_x_ticks(g, cr, x1, y1, x2, y2, minx, maxx, 10);
429 graph_draw_y_ticks(g, cr, x1, y1, x2, y2, miny, maxy, 10);
430
9ce9cfbd
SC
431 if (!good_data)
432 goto skip_data;
433
f3e8440f 434 cairo_set_line_width(cr, 1.5);
af58ef32
SC
435 for (i = g->labels; i; i = i->next) {
436 first = 1;
cae08727
SC
437 if (i->r < 0) /* invisible data */
438 continue;
af58ef32
SC
439 cairo_set_source_rgb(cr, i->r, i->g, i->b);
440 for (j = i->values; j; j = j->next) {
441 tx = ((getx(j) - minx) / (maxx - minx)) * (x2 - x1) + x1;
442 ty = y2 - ((gety(j) - miny) / (maxy - miny)) * (y2 - y1);
443 if (first) {
444 cairo_move_to(cr, tx, ty);
445 first = 0;
446 } else {
447 cairo_line_to(cr, tx, ty);
448 }
449 }
450 cairo_stroke(cr);
451 }
9ce9cfbd
SC
452
453skip_data:
af58ef32 454 cairo_restore(cr);
9ce9cfbd 455
af58ef32
SC
456}
457
458static void gfree(void *f)
459{
460 if (f)
461 free(f);
462}
463
464static void setstring(char **str, const char *value)
465{
466 gfree(*str);
467 *str = strdup(value);
468}
469
470void graph_title(struct graph *bg, const char *title)
471{
472 setstring(&bg->title, title);
473}
474
475void graph_x_title(struct graph *bg, const char *title)
476{
477 setstring(&bg->xtitle, title);
478}
479
480void graph_y_title(struct graph *bg, const char *title)
481{
482 setstring(&bg->ytitle, title);
483}
484
485static struct graph_label *graph_find_label(struct graph *bg,
486 const char *label)
487{
488 struct graph_label *i;
489
490 for (i = bg->labels; i; i = i->next)
491 if (strcmp(label, i->label) == 0)
492 return i;
493 return NULL;
494}
495
496void graph_add_label(struct graph *bg, const char *label)
497{
498 struct graph_label *i;
499
500 i = graph_find_label(bg, label);
501 if (i)
502 return; /* already present. */
503 i = calloc(1, sizeof(*i));
504 i->parent = bg;
505 setstring(&i->label, label);
506 i->next = NULL;
507 if (!bg->tail)
508 bg->labels = i;
509 else
510 bg->tail->next = i;
511 bg->tail = i;
512}
513
514static void graph_label_add_value(struct graph_label *i, void *value)
515{
516 struct graph_value *x;
517
518 x = malloc(sizeof(*x));
519 x->value = value;
520 x->next = NULL;
521 if (!i->tail) {
522 i->values = x;
523 } else {
524 i->tail->next = x;
525 }
526 i->tail = x;
527 i->value_count++;
528
529 if (i->parent->per_label_limit != -1 &&
530 i->value_count > i->parent->per_label_limit) {
531 x = i->values;
532 i->values = i->values->next;
533 free(x->value);
534 free(x);
535 i->value_count--;
536 }
537}
538
539int graph_add_data(struct graph *bg, const char *label, const double value)
540{
541 struct graph_label *i;
542 double *d;
543
544 d = malloc(sizeof(*d));
545 *d = value;
546
547 i = graph_find_label(bg, label);
548 if (!i)
549 return -1;
550 graph_label_add_value(i, d);
551 return 0;
552}
553
554int graph_add_xy_data(struct graph *bg, const char *label,
555 const double x, const double y)
556{
557 struct graph_label *i;
558 struct xyvalue *xy;
559
560 xy = malloc(sizeof(*xy));
561 xy->x = x;
562 xy->y = y;
563
564 i = graph_find_label(bg, label);
565 if (!i)
566 return -1;
567 graph_label_add_value(i, xy);
568 return 0;
569}
570
571static void graph_free_values(struct graph_value *values)
572{
573 struct graph_value *i, *next;
574
575 for (i = values; i; i = next) {
576 next = i->next;
577 gfree(i->value);
578 gfree(i);
579 }
580}
581
582static void graph_free_labels(struct graph_label *labels)
583{
584 struct graph_label *i, *next;
585
586 for (i = labels; i; i = next) {
587 next = i->next;
588 graph_free_values(i->values);
589 gfree(i);
590 }
591}
592
593void graph_set_color(struct graph *gr, const char *label,
594 double red, double green, double blue)
595{
596 struct graph_label *i;
597 double r, g, b;
598
cae08727
SC
599 if (red < 0.0) { /* invisible color */
600 r = -1.0;
601 g = -1.0;
602 b = -1.0;
603 } else {
604 r = fabs(red);
605 g = fabs(green);
606 b = fabs(blue);
607
608 if (r > 1.0)
609 r = 1.0;
610 if (g > 1.0)
611 g = 1.0;
612 if (b > 1.0)
613 b =1.0;
614 }
af58ef32
SC
615
616 for (i = gr->labels; i; i = i->next)
617 if (strcmp(i->label, label) == 0) {
618 i->r = r;
619 i->g = g;
620 i->b = b;
621 break;
622 }
623}
624
625void graph_free(struct graph *bg)
626{
627 gfree(bg->title);
628 gfree(bg->xtitle);
629 gfree(bg->ytitle);
630 graph_free_labels(bg->labels);
631}
632
633/* For each line in the line graph, up to per_label_limit segments may
634 * be added. After that, adding more data to the end of the line
635 * causes data to drop off of the front of the line.
636 */
637void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
638{
639 g->per_label_limit = per_label_limit;
640}
641