| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | * gfio - gui front end for fio - the flexible io tester | 
|  | 3 | * | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 4 | * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com> | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 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> | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 27 | #include <stdlib.h> | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 28 |  | 
|  | 29 | #include <cairo.h> | 
|  | 30 | #include <gtk/gtk.h> | 
|  | 31 |  | 
|  | 32 | #include "tickmarks.h" | 
| Stephen M. Cameron | 09ad20f | 2012-03-11 11:34:38 +0100 | [diff] [blame] | 33 | #include "graph.h" | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 34 | #include "flist.h" | 
|  | 35 | #include "lib/prio_tree.h" | 
| Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 36 | #include "cairo_text_helpers.h" | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 37 |  | 
|  | 38 | /* | 
|  | 39 | * Allowable difference to show tooltip | 
|  | 40 | */ | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 41 | #define TOOLTIP_DELTA	0.08 | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 42 |  | 
|  | 43 | struct xyvalue { | 
|  | 44 | double x, y; | 
|  | 45 | }; | 
|  | 46 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 47 | enum { | 
|  | 48 | GV_F_ON_PRIO	= 1, | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 49 | GV_F_PRIO_SKIP	= 2, | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 50 | }; | 
|  | 51 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 52 | struct graph_value { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 53 | struct flist_head list; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 54 | struct prio_tree_node node; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 55 | struct flist_head alias; | 
|  | 56 | unsigned int flags; | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 57 | char *tooltip; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 58 | void *value; | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | struct graph_label { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 62 | struct flist_head list; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 63 | char *label; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 64 | struct flist_head value_list; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 65 | struct prio_tree_root prio_tree; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 66 | double r, g, b; | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 67 | int hide; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 68 | int value_count; | 
|  | 69 | struct graph *parent; | 
|  | 70 | }; | 
|  | 71 |  | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 72 | struct tick_value { | 
|  | 73 | unsigned int offset; | 
|  | 74 | double value; | 
|  | 75 | }; | 
|  | 76 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 77 | struct graph { | 
|  | 78 | char *title; | 
|  | 79 | char *xtitle; | 
|  | 80 | char *ytitle; | 
| Jens Axboe | 87d5f27 | 2012-03-07 12:31:40 +0100 | [diff] [blame] | 81 | unsigned int xdim, ydim; | 
| Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 82 | double xoffset, yoffset; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 83 | struct flist_head label_list; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 84 | int per_label_limit; | 
| Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 85 | const char *font; | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 86 | graph_axis_unit_change_callback x_axis_unit_change_callback; | 
|  | 87 | graph_axis_unit_change_callback y_axis_unit_change_callback; | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 88 | unsigned int base_offset; | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 89 | unsigned int dont_graph_all_zeroes; | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 90 | double left_extra; | 
|  | 91 | double right_extra; | 
|  | 92 | double top_extra; | 
|  | 93 | double bottom_extra; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 94 |  | 
|  | 95 | double xtick_zero; | 
|  | 96 | double xtick_delta; | 
|  | 97 | double xtick_zero_val; | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 98 | double xtick_one_val; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 99 | double ytick_zero; | 
|  | 100 | double ytick_delta; | 
|  | 101 | double ytick_zero_val; | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 102 | double ytick_one_val; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 103 | }; | 
|  | 104 |  | 
| Stephen M. Cameron | 3ea48b8 | 2012-03-07 19:40:58 +0100 | [diff] [blame] | 105 | void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim) | 
|  | 106 | { | 
|  | 107 | g->xdim = xdim; | 
|  | 108 | g->ydim = ydim; | 
|  | 109 | } | 
|  | 110 |  | 
| Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 111 | void graph_set_position(struct graph *g, double xoffset, double yoffset) | 
|  | 112 | { | 
|  | 113 | g->xoffset = xoffset; | 
|  | 114 | g->yoffset = yoffset; | 
|  | 115 | } | 
|  | 116 |  | 
| Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 117 | struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 118 | { | 
|  | 119 | struct graph *g; | 
|  | 120 |  | 
|  | 121 | g = calloc(1, sizeof(*g)); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 122 | INIT_FLIST_HEAD(&g->label_list); | 
| Stephen M. Cameron | 3ea48b8 | 2012-03-07 19:40:58 +0100 | [diff] [blame] | 123 | graph_set_size(g, xdim, ydim); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 124 | g->per_label_limit = -1; | 
| Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 125 | g->font = font; | 
|  | 126 | if (!g->font) | 
| Jens Axboe | a1e7972 | 2012-03-23 10:52:25 +0100 | [diff] [blame] | 127 | g->font = GRAPH_DEFAULT_FONT; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 128 | return g; | 
|  | 129 | } | 
|  | 130 |  | 
| Jens Axboe | a1e7972 | 2012-03-23 10:52:25 +0100 | [diff] [blame] | 131 | void graph_set_font(struct graph *g, const char *font) | 
|  | 132 | { | 
|  | 133 | g->font = font; | 
|  | 134 | } | 
|  | 135 |  | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 136 | void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) | 
|  | 137 | { | 
|  | 138 | g->x_axis_unit_change_callback = f; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f) | 
|  | 142 | { | 
|  | 143 | g->y_axis_unit_change_callback = f; | 
|  | 144 | } | 
|  | 145 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 146 | static int count_labels(struct graph *g) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 147 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 148 | struct flist_head *entry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 149 | int count = 0; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 150 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 151 | flist_for_each(entry, &g->label_list) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 152 | count++; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 153 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 154 | return count; | 
|  | 155 | } | 
|  | 156 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 157 | static int count_values(struct graph_label *l) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 158 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 159 | struct flist_head *entry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 160 | int count = 0; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 161 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 162 | flist_for_each(entry, &l->value_list) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 163 | count++; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 164 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 165 | return count; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | typedef double (*double_comparator)(double a, double b); | 
|  | 169 |  | 
|  | 170 | static double mindouble(double a, double b) | 
|  | 171 | { | 
|  | 172 | return a < b ? a : b; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | static double maxdouble(double a, double b) | 
|  | 176 | { | 
|  | 177 | return a < b ? b : a; | 
|  | 178 | } | 
|  | 179 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 180 | static double find_double_values(struct graph_label *l, double_comparator cmp) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 181 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 182 | struct flist_head *entry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 183 | double answer, tmp; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 184 | int first = 1; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 185 |  | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 186 | if (flist_empty(&l->value_list)) | 
|  | 187 | return 0.0; | 
|  | 188 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 189 | flist_for_each(entry, &l->value_list) { | 
|  | 190 | struct graph_value *i; | 
|  | 191 |  | 
|  | 192 | i = flist_entry(entry, struct graph_value, list); | 
|  | 193 | tmp = *(double *) i->value; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 194 | if (first) { | 
|  | 195 | answer = tmp; | 
|  | 196 | first = 0; | 
|  | 197 | } else { | 
|  | 198 | answer = cmp(answer, tmp); | 
|  | 199 | } | 
|  | 200 | } | 
|  | 201 | return answer; | 
|  | 202 | } | 
|  | 203 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 204 | static double find_double_data(struct graph *g, double_comparator cmp) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 205 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 206 | struct flist_head *entry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 207 | struct graph_label *i; | 
|  | 208 | int first = 1; | 
|  | 209 | double answer, tmp; | 
|  | 210 |  | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 211 | if (flist_empty(&g->label_list)) | 
|  | 212 | return 0.0; | 
|  | 213 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 214 | flist_for_each(entry, &g->label_list) { | 
|  | 215 | i = flist_entry(entry, struct graph_label, list); | 
|  | 216 | tmp = find_double_values(i, cmp); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 217 | if (first) { | 
|  | 218 | answer = tmp; | 
|  | 219 | first = 0; | 
|  | 220 | } else { | 
|  | 221 | answer = cmp(tmp, answer); | 
|  | 222 | } | 
|  | 223 | } | 
|  | 224 | return answer; | 
|  | 225 | } | 
|  | 226 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 227 | static double find_min_data(struct graph *g) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 228 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 229 | return find_double_data(g, mindouble); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 230 | } | 
|  | 231 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 232 | static double find_max_data(struct graph *g) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 233 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 234 | return find_double_data(g, maxdouble); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 235 | } | 
|  | 236 |  | 
|  | 237 | static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb, | 
|  | 238 | double label_offset, double bar_width, | 
|  | 239 | double mindata, double maxdata) | 
|  | 240 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 241 | struct flist_head *entry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 242 | double x1, y1, x2, y2; | 
|  | 243 | int bar_num = 0; | 
|  | 244 | double domain, range, v; | 
|  | 245 |  | 
|  | 246 | domain = (maxdata - mindata); | 
|  | 247 | range = (double) bg->ydim * 0.80; /* FIXME */ | 
|  | 248 | cairo_stroke(cr); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 249 | flist_for_each(entry, &lb->value_list) { | 
|  | 250 | struct graph_value *i; | 
|  | 251 |  | 
|  | 252 | i = flist_entry(entry, struct graph_value, list); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 253 |  | 
|  | 254 | x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05); | 
|  | 255 | x2 = x1 + bar_width * 0.90; | 
|  | 256 | y2 = bg->ydim * 0.90; | 
|  | 257 | v = *(double *) i->value; | 
|  | 258 | y1 = y2 - (((v - mindata) / domain) * range); | 
|  | 259 | cairo_move_to(cr, x1, y1); | 
|  | 260 | cairo_line_to(cr, x1, y2); | 
|  | 261 | cairo_line_to(cr, x2, y2); | 
|  | 262 | cairo_line_to(cr, x2, y1); | 
|  | 263 | cairo_close_path(cr); | 
|  | 264 | cairo_fill(cr); | 
|  | 265 | cairo_stroke(cr); | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 266 | bar_num++; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 267 | } | 
|  | 268 | } | 
|  | 269 |  | 
| Jens Axboe | f3a3176 | 2012-04-12 11:16:17 +0200 | [diff] [blame^] | 270 | static void graph_draw_common(struct graph *g, cairo_t *cr, double *x1, | 
|  | 271 | double *y1, double *x2, double *y2) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 272 | { | 
| Jens Axboe | f3a3176 | 2012-04-12 11:16:17 +0200 | [diff] [blame^] | 273 | const double shade_col[3][3] = { { 0.55, 0.54, 0.54 }, | 
|  | 274 | { 0.80, 0.78, 0.78 }, | 
|  | 275 | { 0.93, 0.91, 0.91 } }; | 
|  | 276 | int i; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 277 |  | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 278 | *x1 = 0.10 * g->xdim; | 
| Stephen M. Cameron | 6bf8600 | 2012-03-08 16:58:50 +0100 | [diff] [blame] | 279 | *x2 = 0.95 * g->xdim; | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 280 | *y1 = 0.10 * g->ydim; | 
| Stephen M. Cameron | 6bf8600 | 2012-03-08 16:58:50 +0100 | [diff] [blame] | 281 | *y2 = 0.90 * g->ydim; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 282 |  | 
| Jens Axboe | f3a3176 | 2012-04-12 11:16:17 +0200 | [diff] [blame^] | 283 | /* | 
|  | 284 | * Add shade | 
|  | 285 | */ | 
|  | 286 | cairo_set_line_width(cr, 1.0); | 
|  | 287 | for (i = 0; i < 3; i++) { | 
|  | 288 | float offset = i + 1.0; | 
|  | 289 |  | 
|  | 290 | cairo_set_source_rgb(cr, shade_col[i][0], shade_col[i][1], shade_col[i][2]); | 
|  | 291 | cairo_move_to(cr, offset + *x1, *y1 - offset); | 
|  | 292 | cairo_line_to(cr, *x2 + offset, *y1 - offset); | 
|  | 293 | cairo_line_to(cr, *x2 + offset, *y2 - offset); | 
|  | 294 | cairo_stroke(cr); | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | cairo_set_source_rgb(cr, 0, 0, 0); | 
|  | 298 | cairo_set_line_width(cr, 1.2); | 
|  | 299 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 300 | cairo_move_to(cr, *x1, *y1); | 
|  | 301 | cairo_line_to(cr, *x1, *y2); | 
|  | 302 | cairo_line_to(cr, *x2, *y2); | 
|  | 303 | cairo_line_to(cr, *x2, *y1); | 
|  | 304 | cairo_line_to(cr, *x1, *y1); | 
|  | 305 | cairo_stroke(cr); | 
|  | 306 |  | 
| Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 307 | draw_centered_text(cr, g->font, g->xdim / 2, g->ydim / 20, 20.0, g->title); | 
|  | 308 | draw_centered_text(cr, g->font, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle); | 
|  | 309 | draw_vertical_centered_text(cr, g->font, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 310 | cairo_stroke(cr); | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | static void graph_draw_x_ticks(struct graph *g, cairo_t *cr, | 
|  | 314 | double x1, double y1, double x2, double y2, | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 315 | double minx, double maxx, int nticks, int add_tm_text) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 316 | { | 
|  | 317 | struct tickmark *tm; | 
|  | 318 | double tx; | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 319 | int i, power_of_ten; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 320 | static double dash[] = { 1.0, 2.0 }; | 
|  | 321 |  | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 322 | nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten, | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 323 | g->x_axis_unit_change_callback == NULL, g->base_offset); | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 324 | if (g->x_axis_unit_change_callback) | 
|  | 325 | g->x_axis_unit_change_callback(g, power_of_ten); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 326 |  | 
|  | 327 | for (i = 0; i < nticks; i++) { | 
|  | 328 | tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1; | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 329 |  | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 330 | /* | 
|  | 331 | * Update tick delta | 
|  | 332 | */ | 
|  | 333 | if (!i) { | 
|  | 334 | g->xtick_zero = tx; | 
|  | 335 | g->xtick_zero_val = tm[0].value; | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 336 | } else if (i == 1) { | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 337 | g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero); | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 338 | g->xtick_one_val = tm[1].value; | 
|  | 339 | } | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 340 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 341 | /* really tx < yx || tx > x2, but protect against rounding */ | 
|  | 342 | if (x1 - tx > 0.01 || tx - x2 > 0.01) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 343 | continue; | 
|  | 344 |  | 
|  | 345 | /* Draw tick mark */ | 
| Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 346 | cairo_set_line_width(cr, 0.8); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 347 | cairo_move_to(cr, tx, y2); | 
|  | 348 | cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03); | 
|  | 349 | cairo_stroke(cr); | 
|  | 350 |  | 
|  | 351 | /* draw grid lines */ | 
|  | 352 | cairo_save(cr); | 
|  | 353 | cairo_set_dash(cr, dash, 2, 2.0); | 
|  | 354 | cairo_set_line_width(cr, 0.5); | 
|  | 355 | cairo_move_to(cr, tx, y1); | 
|  | 356 | cairo_line_to(cr, tx, y2); | 
|  | 357 | cairo_stroke(cr); | 
|  | 358 | cairo_restore(cr); | 
|  | 359 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 360 | if (!add_tm_text) | 
|  | 361 | continue; | 
|  | 362 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 363 | /* draw tickmark label */ | 
| Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 364 | draw_centered_text(cr, g->font, tx, y2 * 1.04, 12.0, tm[i].string); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 365 | cairo_stroke(cr); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 366 | } | 
|  | 367 | } | 
|  | 368 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 369 | static double graph_draw_y_ticks(struct graph *g, cairo_t *cr, | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 370 | double x1, double y1, double x2, double y2, | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 371 | double miny, double maxy, int nticks, int add_tm_text) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 372 | { | 
|  | 373 | struct tickmark *tm; | 
|  | 374 | double ty; | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 375 | int i, power_of_ten; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 376 | static double dash[] = { 2.0, 2.0 }; | 
|  | 377 |  | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 378 | nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten, | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 379 | g->y_axis_unit_change_callback == NULL, g->base_offset); | 
| Stephen M. Cameron | 7175d91 | 2012-03-11 11:35:10 +0100 | [diff] [blame] | 380 | if (g->y_axis_unit_change_callback) | 
|  | 381 | g->y_axis_unit_change_callback(g, power_of_ten); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 382 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 383 | /* | 
|  | 384 | * Use highest tickmark as top of graph, not highest value. Otherwise | 
|  | 385 | * it's impossible to see what the max value is, if the graph is | 
|  | 386 | * fairly flat. | 
|  | 387 | */ | 
|  | 388 | maxy = tm[nticks - 1].value; | 
|  | 389 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 390 | for (i = 0; i < nticks; i++) { | 
|  | 391 | ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1); | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 392 |  | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 393 | /* | 
|  | 394 | * Update tick delta | 
|  | 395 | */ | 
|  | 396 | if (!i) { | 
|  | 397 | g->ytick_zero = ty; | 
|  | 398 | g->ytick_zero_val = tm[0].value; | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 399 | } else if (i == 1) { | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 400 | g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero); | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 401 | g->ytick_one_val = tm[1].value; | 
|  | 402 | } | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 403 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 404 | /* really ty < y1 || ty > y2, but protect against rounding */ | 
|  | 405 | if (y1 - ty > 0.01 || ty - y2 > 0.01) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 406 | continue; | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 407 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 408 | /* draw tick mark */ | 
|  | 409 | cairo_move_to(cr, x1, ty); | 
|  | 410 | cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty); | 
|  | 411 | cairo_stroke(cr); | 
|  | 412 |  | 
|  | 413 | /* draw grid lines */ | 
|  | 414 | cairo_save(cr); | 
|  | 415 | cairo_set_dash(cr, dash, 2, 2.0); | 
|  | 416 | cairo_set_line_width(cr, 0.5); | 
|  | 417 | cairo_move_to(cr, x1, ty); | 
|  | 418 | cairo_line_to(cr, x2, ty); | 
|  | 419 | cairo_stroke(cr); | 
|  | 420 | cairo_restore(cr); | 
|  | 421 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 422 | if (!add_tm_text) | 
|  | 423 | continue; | 
|  | 424 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 425 | /* draw tickmark label */ | 
| Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 426 | draw_right_justified_text(cr, g->font, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 427 | cairo_stroke(cr); | 
|  | 428 | } | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 429 |  | 
|  | 430 | /* | 
|  | 431 | * Return new max to use | 
|  | 432 | */ | 
|  | 433 | return maxy; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
|  | 436 | void bar_graph_draw(struct graph *bg, cairo_t *cr) | 
|  | 437 | { | 
|  | 438 | double x1, y1, x2, y2; | 
|  | 439 | double space_per_label, bar_width; | 
|  | 440 | double label_offset, mindata, maxdata; | 
|  | 441 | int i, nlabels; | 
|  | 442 | struct graph_label *lb; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 443 | struct flist_head *entry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 444 |  | 
|  | 445 | cairo_save(cr); | 
| Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 446 | cairo_translate(cr, bg->xoffset, bg->yoffset); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 447 | graph_draw_common(bg, cr, &x1, &y1, &x2, &y2); | 
|  | 448 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 449 | nlabels = count_labels(bg); | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 450 | space_per_label = (x2 - x1) / (double) nlabels; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 451 |  | 
| Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 452 | /* | 
|  | 453 | * Start bars at 0 unless we have negative values, otherwise we | 
|  | 454 | * present a skewed picture comparing label X and X+1. | 
|  | 455 | */ | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 456 | mindata = find_min_data(bg); | 
| Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 457 | if (mindata > 0) | 
|  | 458 | mindata = 0; | 
|  | 459 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 460 | maxdata = find_max_data(bg); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 461 |  | 
|  | 462 | if (fabs(maxdata - mindata) < 1e-20) { | 
| Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 463 | draw_centered_text(cr, bg->font, | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 464 | x1 + (x2 - x1) / 2.0, | 
|  | 465 | y1 + (y2 - y1) / 2.0, 20.0, "No good data"); | 
|  | 466 | return; | 
|  | 467 | } | 
|  | 468 |  | 
| Jens Axboe | bb39379 | 2012-03-15 10:52:38 +0100 | [diff] [blame] | 469 | maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 470 | i = 0; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 471 | flist_for_each(entry, &bg->label_list) { | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 472 | int nvalues; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 473 |  | 
|  | 474 | lb = flist_entry(entry, struct graph_label, list); | 
|  | 475 | nvalues = count_values(lb); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 476 | bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues; | 
|  | 477 | label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1; | 
|  | 478 | draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata); | 
|  | 479 | // draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93, | 
| Stephen M. Cameron | ee2f55b | 2012-03-27 08:14:09 +0200 | [diff] [blame] | 480 | draw_centered_text(cr, bg->font, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93, | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 481 | 12.0, lb->label); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 482 | i++; | 
|  | 483 | } | 
|  | 484 | cairo_stroke(cr); | 
|  | 485 | cairo_restore(cr); | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | typedef double (*xy_value_extractor)(struct graph_value *v); | 
|  | 489 |  | 
|  | 490 | static double getx(struct graph_value *v) | 
|  | 491 | { | 
|  | 492 | struct xyvalue *xy = v->value; | 
|  | 493 | return xy->x; | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | static double gety(struct graph_value *v) | 
|  | 497 | { | 
|  | 498 | struct xyvalue *xy = v->value; | 
|  | 499 | return xy->y; | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp) | 
|  | 503 | { | 
| Stephen M. Cameron | 10e54cd | 2012-03-07 19:39:55 +0100 | [diff] [blame] | 504 | double tmp, answer = 0.0; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 505 | struct graph_label *i; | 
|  | 506 | struct graph_value *j; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 507 | struct flist_head *jentry, *entry; | 
| Stephen M. Cameron | d582bf7 | 2012-03-07 19:37:57 +0100 | [diff] [blame] | 508 | int first = 1; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 509 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 510 | flist_for_each(entry, &g->label_list) { | 
|  | 511 | i = flist_entry(entry, struct graph_label, list); | 
|  | 512 |  | 
|  | 513 | flist_for_each(jentry, &i->value_list) { | 
|  | 514 | j = flist_entry(jentry, struct graph_value, list); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 515 | tmp = getvalue(j); | 
| Stephen M. Cameron | d582bf7 | 2012-03-07 19:37:57 +0100 | [diff] [blame] | 516 | if (first) { | 
|  | 517 | first = 0; | 
|  | 518 | answer = tmp; | 
|  | 519 | } | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 520 | answer = cmp(tmp, answer); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 521 | } | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 522 | } | 
|  | 523 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 524 | return answer; | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 525 | } | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 526 |  | 
|  | 527 | void line_graph_draw(struct graph *g, cairo_t *cr) | 
|  | 528 | { | 
|  | 529 | double x1, y1, x2, y2; | 
| Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 530 | double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy; | 
|  | 531 | double tx, ty, top_extra, bottom_extra, left_extra, right_extra; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 532 | struct graph_label *i; | 
|  | 533 | struct graph_value *j; | 
| Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 534 | int good_data = 1, first = 1; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 535 | struct flist_head *entry, *lentry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 536 |  | 
|  | 537 | cairo_save(cr); | 
| Stephen M. Cameron | 57f9d28 | 2012-03-11 11:36:51 +0100 | [diff] [blame] | 538 | cairo_translate(cr, g->xoffset, g->yoffset); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 539 | graph_draw_common(g, cr, &x1, &y1, &x2, &y2); | 
|  | 540 |  | 
|  | 541 | minx = find_xy_value(g, getx, mindouble); | 
|  | 542 | maxx = find_xy_value(g, getx, maxdouble); | 
|  | 543 | miny = find_xy_value(g, gety, mindouble); | 
| Jens Axboe | 5aec668 | 2012-03-16 10:17:08 +0100 | [diff] [blame] | 544 |  | 
|  | 545 | /* | 
|  | 546 | * Start graphs at zero, unless we have a value below. Otherwise | 
|  | 547 | * it's hard to visually compare the read and write graph, since | 
|  | 548 | * the lowest valued one will be the floor of the graph view. | 
|  | 549 | */ | 
|  | 550 | if (miny > 0) | 
|  | 551 | miny = 0; | 
|  | 552 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 553 | maxy = find_xy_value(g, gety, maxdouble); | 
|  | 554 |  | 
|  | 555 | if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) { | 
| Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 556 | good_data = 0; | 
|  | 557 | minx = 0.0; | 
|  | 558 | miny = 0.0; | 
|  | 559 | maxx = 10.0; | 
|  | 560 | maxy = 100.0; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 561 | } | 
|  | 562 |  | 
| Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 563 | top_extra = 0.0; | 
|  | 564 | bottom_extra = 0.0; | 
|  | 565 | left_extra = 0.0; | 
|  | 566 | right_extra = 0.0; | 
|  | 567 |  | 
|  | 568 | if (g->top_extra > 0.001) | 
|  | 569 | top_extra = fabs(maxy - miny) * g->top_extra; | 
|  | 570 | if (g->bottom_extra > 0.001) | 
|  | 571 | bottom_extra = fabs(maxy - miny) * g->bottom_extra; | 
|  | 572 | if (g->left_extra > 0.001) | 
|  | 573 | left_extra = fabs(maxx - minx) * g->left_extra; | 
|  | 574 | if (g->right_extra > 0.001) | 
|  | 575 | right_extra = fabs(maxx - minx) * g->right_extra; | 
|  | 576 |  | 
|  | 577 | gminx = minx - left_extra; | 
|  | 578 | gmaxx = maxx + right_extra; | 
|  | 579 | gminy = miny - bottom_extra; | 
|  | 580 | gmaxy = maxy + top_extra; | 
|  | 581 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 582 | graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data); | 
|  | 583 | gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 584 |  | 
| Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 585 | if (!good_data) | 
|  | 586 | goto skip_data; | 
|  | 587 |  | 
| Jens Axboe | f3e8440 | 2012-03-07 13:14:32 +0100 | [diff] [blame] | 588 | cairo_set_line_width(cr, 1.5); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 589 | flist_for_each(lentry, &g->label_list) { | 
|  | 590 | i = flist_entry(lentry, struct graph_label, list); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 591 | first = 1; | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 592 | if (i->hide || i->r < 0) /* invisible data */ | 
| Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 593 | continue; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 594 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 595 | cairo_set_source_rgb(cr, i->r, i->g, i->b); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 596 | flist_for_each(entry, &i->value_list) { | 
|  | 597 | j = flist_entry(entry, struct graph_value, list); | 
| Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 598 | tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1; | 
|  | 599 | ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 600 | if (first) { | 
|  | 601 | cairo_move_to(cr, tx, ty); | 
|  | 602 | first = 0; | 
|  | 603 | } else { | 
|  | 604 | cairo_line_to(cr, tx, ty); | 
|  | 605 | } | 
|  | 606 | } | 
|  | 607 | cairo_stroke(cr); | 
|  | 608 | } | 
| Stephen M. Cameron | 9ce9cfb | 2012-03-07 19:37:25 +0100 | [diff] [blame] | 609 |  | 
|  | 610 | skip_data: | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 611 | cairo_restore(cr); | 
|  | 612 | } | 
|  | 613 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 614 | static void setstring(char **str, const char *value) | 
|  | 615 | { | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 616 | free(*str); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 617 | *str = strdup(value); | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | void graph_title(struct graph *bg, const char *title) | 
|  | 621 | { | 
|  | 622 | setstring(&bg->title, title); | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | void graph_x_title(struct graph *bg, const char *title) | 
|  | 626 | { | 
|  | 627 | setstring(&bg->xtitle, title); | 
|  | 628 | } | 
|  | 629 |  | 
|  | 630 | void graph_y_title(struct graph *bg, const char *title) | 
|  | 631 | { | 
|  | 632 | setstring(&bg->ytitle, title); | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | static struct graph_label *graph_find_label(struct graph *bg, | 
|  | 636 | const char *label) | 
|  | 637 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 638 | struct flist_head *entry; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 639 | struct graph_label *i; | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 640 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 641 | flist_for_each(entry, &bg->label_list) { | 
|  | 642 | i = flist_entry(entry, struct graph_label, list); | 
|  | 643 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 644 | if (strcmp(label, i->label) == 0) | 
|  | 645 | return i; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 648 | return NULL; | 
|  | 649 | } | 
|  | 650 |  | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 651 | graph_label_t graph_add_label(struct graph *bg, const char *label) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 652 | { | 
|  | 653 | struct graph_label *i; | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 654 |  | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 655 | i = graph_find_label(bg, label); | 
|  | 656 | if (i) | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 657 | return i; /* already present. */ | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 658 | i = calloc(1, sizeof(*i)); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 659 | INIT_FLIST_HEAD(&i->value_list); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 660 | i->parent = bg; | 
|  | 661 | setstring(&i->label, label); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 662 | flist_add_tail(&i->list, &bg->label_list); | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 663 | INIT_PRIO_TREE_ROOT(&i->prio_tree); | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 664 | return i; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 665 | } | 
|  | 666 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 667 | static void __graph_value_drop(struct graph_label *l, struct graph_value *v) | 
|  | 668 | { | 
| Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 669 | flist_del_init(&v->list); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 670 | if (v->tooltip) | 
|  | 671 | free(v->tooltip); | 
|  | 672 | free(v->value); | 
|  | 673 | free(v); | 
|  | 674 | l->value_count--; | 
|  | 675 | } | 
|  | 676 |  | 
|  | 677 | static void graph_value_drop(struct graph_label *l, struct graph_value *v) | 
|  | 678 | { | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 679 | if (v->flags & GV_F_PRIO_SKIP) { | 
|  | 680 | __graph_value_drop(l, v); | 
|  | 681 | return; | 
|  | 682 | } | 
|  | 683 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 684 | /* | 
|  | 685 | * Find head, the guy that's on the prio tree | 
|  | 686 | */ | 
|  | 687 | while (!(v->flags & GV_F_ON_PRIO)) { | 
|  | 688 | assert(!flist_empty(&v->alias)); | 
|  | 689 | v = flist_entry(v->alias.next, struct graph_value, alias); | 
|  | 690 | } | 
|  | 691 |  | 
|  | 692 | prio_tree_remove(&l->prio_tree, &v->node); | 
|  | 693 |  | 
|  | 694 | /* | 
|  | 695 | * Free aliases | 
|  | 696 | */ | 
| Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 697 | while (!flist_empty(&v->alias)) { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 698 | struct graph_value *a; | 
|  | 699 |  | 
| Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 700 | a = flist_entry(v->alias.next, struct graph_value, alias); | 
|  | 701 | flist_del_init(&a->alias); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 702 |  | 
|  | 703 | __graph_value_drop(l, a); | 
|  | 704 | } | 
|  | 705 |  | 
|  | 706 | __graph_value_drop(l, v); | 
|  | 707 | } | 
|  | 708 |  | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 709 | static void graph_label_add_value(struct graph_label *i, void *value, | 
|  | 710 | const char *tooltip) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 711 | { | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 712 | struct graph *g = i->parent; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 713 | struct graph_value *x; | 
|  | 714 |  | 
|  | 715 | x = malloc(sizeof(*x)); | 
| Jens Axboe | 3ca30e6 | 2012-03-21 18:54:30 +0100 | [diff] [blame] | 716 | memset(x, 0, sizeof(*x)); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 717 | INIT_FLIST_HEAD(&x->alias); | 
|  | 718 | INIT_FLIST_HEAD(&x->list); | 
|  | 719 | flist_add_tail(&x->list, &i->value_list); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 720 | i->value_count++; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 721 | x->value = value; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 722 |  | 
| Jens Axboe | f00e43f | 2012-03-21 19:53:32 +0100 | [diff] [blame] | 723 | if (tooltip) { | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 724 | double xval = getx(x); | 
|  | 725 | double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA); | 
|  | 726 | double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA); | 
| Jens Axboe | 4c0cd53 | 2012-03-21 19:48:32 +0100 | [diff] [blame] | 727 | struct prio_tree_node *ret; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 728 |  | 
| Jens Axboe | 5fb8d79 | 2012-03-21 22:00:43 +0100 | [diff] [blame] | 729 | /* | 
|  | 730 | * use msec to avoid dropping too much precision when | 
|  | 731 | * storing as an integer. | 
|  | 732 | */ | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 733 | minx = minx * 1000.0; | 
|  | 734 | maxx = maxx * 1000.0; | 
|  | 735 |  | 
| Jens Axboe | 3ca30e6 | 2012-03-21 18:54:30 +0100 | [diff] [blame] | 736 | INIT_PRIO_TREE_NODE(&x->node); | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 737 | x->node.start = minx; | 
|  | 738 | x->node.last = maxx; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 739 | x->tooltip = strdup(tooltip); | 
| Jens Axboe | b552682 | 2012-03-21 20:20:35 +0100 | [diff] [blame] | 740 | if (x->node.last == x->node.start) { | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 741 | x->node.last += fabs(g->xtick_delta); | 
|  | 742 | if (x->node.last == x->node.start) | 
|  | 743 | x->node.last++; | 
| Jens Axboe | b552682 | 2012-03-21 20:20:35 +0100 | [diff] [blame] | 744 | } | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 745 |  | 
| Jens Axboe | 4c0cd53 | 2012-03-21 19:48:32 +0100 | [diff] [blame] | 746 | /* | 
|  | 747 | * If ret != &x->node, we have an alias. Since the values | 
|  | 748 | * should be identical, we can drop it | 
|  | 749 | */ | 
|  | 750 | ret = prio_tree_insert(&i->prio_tree, &x->node); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 751 | if (ret != &x->node) { | 
|  | 752 | struct graph_value *alias; | 
|  | 753 |  | 
|  | 754 | alias = container_of(ret, struct graph_value, node); | 
|  | 755 | flist_add_tail(&x->alias, &alias->alias); | 
| Jens Axboe | bd10a06 | 2012-03-22 09:33:33 +0100 | [diff] [blame] | 756 | } else | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 757 | x->flags = GV_F_ON_PRIO; | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 758 | } else | 
|  | 759 | x->flags = GV_F_PRIO_SKIP; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 760 |  | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 761 | if (g->per_label_limit != -1 && | 
|  | 762 | i->value_count > g->per_label_limit) { | 
| Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 763 | int to_drop = 1; | 
|  | 764 |  | 
|  | 765 | /* | 
|  | 766 | * If the limit was dynamically reduced, making us more | 
|  | 767 | * than 1 entry ahead after adding this one, drop two | 
|  | 768 | * entries. This will make us (eventually) reach the | 
|  | 769 | * specified limit. | 
|  | 770 | */ | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 771 | if (i->value_count - g->per_label_limit >= 2) | 
| Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 772 | to_drop = 2; | 
|  | 773 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 774 | while (to_drop-- && !flist_empty(&i->value_list)) { | 
|  | 775 | x = flist_entry(i->value_list.next, struct graph_value, list); | 
|  | 776 | graph_value_drop(i, x); | 
|  | 777 |  | 
|  | 778 | /* | 
|  | 779 | * If we have aliases, we could drop > 1 above. | 
|  | 780 | */ | 
|  | 781 | if (i->value_count <= g->per_label_limit) | 
|  | 782 | break; | 
| Jens Axboe | c148dae | 2012-03-11 21:35:47 +0100 | [diff] [blame] | 783 | } | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 784 | } | 
|  | 785 | } | 
|  | 786 |  | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 787 | int graph_add_data(struct graph *bg, graph_label_t label, const double value) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 788 | { | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 789 | struct graph_label *i = label; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 790 | double *d; | 
|  | 791 |  | 
|  | 792 | d = malloc(sizeof(*d)); | 
|  | 793 | *d = value; | 
|  | 794 |  | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 795 | graph_label_add_value(i, d, NULL); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 796 | return 0; | 
|  | 797 | } | 
|  | 798 |  | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 799 | static int graph_nonzero_y(struct graph_label *l) | 
|  | 800 | { | 
|  | 801 | struct flist_head *entry; | 
|  | 802 |  | 
|  | 803 | flist_for_each(entry, &l->value_list) { | 
|  | 804 | struct graph_value *v; | 
|  | 805 |  | 
|  | 806 | v = flist_entry(entry, struct graph_value, list); | 
|  | 807 | if (gety(v) != 0.0) | 
|  | 808 | return 1; | 
|  | 809 | } | 
|  | 810 |  | 
|  | 811 | return 0; | 
|  | 812 | } | 
|  | 813 |  | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 814 | int graph_add_xy_data(struct graph *bg, graph_label_t label, | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 815 | const double x, const double y, const char *tooltip) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 816 | { | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 817 | struct graph_label *i = label; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 818 | struct xyvalue *xy; | 
|  | 819 |  | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 820 | if (bg->dont_graph_all_zeroes && y == 0.0 && !graph_nonzero_y(i)) | 
|  | 821 | i->hide = 1; | 
|  | 822 | else | 
|  | 823 | i->hide = 0; | 
|  | 824 |  | 
|  | 825 | xy = malloc(sizeof(*xy)); | 
|  | 826 | xy->x = x; | 
|  | 827 | xy->y = y; | 
|  | 828 |  | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 829 | graph_label_add_value(i, xy, tooltip); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 830 | return 0; | 
|  | 831 | } | 
|  | 832 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 833 | static void graph_free_values(struct graph_label *l) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 834 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 835 | struct graph_value *i; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 836 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 837 | while (!flist_empty(&l->value_list)) { | 
|  | 838 | i = flist_entry(l->value_list.next, struct graph_value, list); | 
|  | 839 | graph_value_drop(l, i); | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 840 | } | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 841 | } | 
|  | 842 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 843 | static void graph_free_labels(struct graph *g) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 844 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 845 | struct graph_label *i; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 846 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 847 | while (!flist_empty(&g->label_list)) { | 
|  | 848 | i = flist_entry(g->label_list.next, struct graph_label, list); | 
|  | 849 | flist_del(&i->list); | 
|  | 850 | graph_free_values(i); | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 851 | free(i); | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 852 | } | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 853 | } | 
|  | 854 |  | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 855 | void graph_set_color(struct graph *gr, graph_label_t label, double red, | 
|  | 856 | double green, double blue) | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 857 | { | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 858 | struct graph_label *i = label; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 859 | double r, g, b; | 
|  | 860 |  | 
| Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 861 | if (red < 0.0) { /* invisible color */ | 
|  | 862 | r = -1.0; | 
|  | 863 | g = -1.0; | 
|  | 864 | b = -1.0; | 
|  | 865 | } else { | 
|  | 866 | r = fabs(red); | 
|  | 867 | g = fabs(green); | 
|  | 868 | b = fabs(blue); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 869 |  | 
| Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 870 | if (r > 1.0) | 
|  | 871 | r = 1.0; | 
|  | 872 | if (g > 1.0) | 
|  | 873 | g = 1.0; | 
|  | 874 | if (b > 1.0) | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 875 | b = 1.0; | 
| Stephen M. Cameron | cae0872 | 2012-03-07 14:47:38 +0100 | [diff] [blame] | 876 | } | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 877 |  | 
| Jens Axboe | 8dfd607 | 2012-03-22 22:10:37 +0100 | [diff] [blame] | 878 | i->r = r; | 
|  | 879 | i->g = g; | 
|  | 880 | i->b = b; | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 881 | } | 
|  | 882 |  | 
|  | 883 | void graph_free(struct graph *bg) | 
|  | 884 | { | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 885 | free(bg->title); | 
|  | 886 | free(bg->xtitle); | 
|  | 887 | free(bg->ytitle); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 888 | graph_free_labels(bg); | 
| Stephen M. Cameron | af58ef3 | 2012-03-07 07:56:16 +0100 | [diff] [blame] | 889 | } | 
|  | 890 |  | 
|  | 891 | /* For each line in the line graph, up to per_label_limit segments may | 
|  | 892 | * be added.  After that, adding more data to the end of the line | 
|  | 893 | * causes data to drop off of the front of the line. | 
|  | 894 | */ | 
|  | 895 | void line_graph_set_data_count_limit(struct graph *g, int per_label_limit) | 
|  | 896 | { | 
|  | 897 | g->per_label_limit = per_label_limit; | 
|  | 898 | } | 
|  | 899 |  | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 900 | void graph_add_extra_space(struct graph *g, double left_percent, | 
|  | 901 | double right_percent, double top_percent, | 
|  | 902 | double bottom_percent) | 
| Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 903 | { | 
| Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 904 | g->left_extra = left_percent; | 
|  | 905 | g->right_extra = right_percent; | 
|  | 906 | g->top_extra = top_percent; | 
|  | 907 | g->bottom_extra = bottom_percent; | 
| Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 908 | } | 
|  | 909 |  | 
| Jens Axboe | d8fbeef | 2012-03-14 10:25:44 +0100 | [diff] [blame] | 910 | /* | 
|  | 911 | * Normally values are logged in a base unit of 0, but for other purposes | 
|  | 912 | * it makes more sense to log in higher unit. For instance for bandwidth | 
|  | 913 | * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec. | 
|  | 914 | */ | 
|  | 915 | void graph_set_base_offset(struct graph *g, unsigned int base_offset) | 
|  | 916 | { | 
|  | 917 | g->base_offset = base_offset; | 
|  | 918 | } | 
|  | 919 |  | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 920 | int graph_has_tooltips(struct graph *g) | 
|  | 921 | { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 922 | struct flist_head *entry; | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 923 | struct graph_label *i; | 
| Stephen M. Cameron | def0ac2 | 2012-03-12 07:32:57 +0100 | [diff] [blame] | 924 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 925 | flist_for_each(entry, &g->label_list) { | 
|  | 926 | i = flist_entry(entry, struct graph_label, list); | 
|  | 927 |  | 
|  | 928 | if (!prio_tree_empty(&i->prio_tree)) | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 929 | return 1; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 930 | } | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 931 |  | 
|  | 932 | return 0; | 
|  | 933 | } | 
|  | 934 |  | 
|  | 935 | int graph_contains_xy(struct graph *g, int x, int y) | 
|  | 936 | { | 
|  | 937 | int first_x = g->xoffset; | 
|  | 938 | int last_x = g->xoffset + g->xdim; | 
|  | 939 | int first_y = g->yoffset; | 
|  | 940 | int last_y = g->yoffset + g->ydim; | 
|  | 941 |  | 
|  | 942 | return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y); | 
|  | 943 | } | 
|  | 944 |  | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 945 | const char *graph_find_tooltip(struct graph *g, int ix, int iy) | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 946 | { | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 947 | double x = ix, y = iy; | 
|  | 948 | struct prio_tree_iter iter; | 
|  | 949 | struct prio_tree_node *n; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 950 | struct graph_value *best = NULL; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 951 | struct flist_head *entry; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 952 | double best_delta; | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 953 | double maxy, miny; | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 954 |  | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 955 | x -= g->xoffset; | 
|  | 956 | y -= g->yoffset; | 
|  | 957 |  | 
|  | 958 | x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta); | 
|  | 959 | y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta); | 
|  | 960 |  | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 961 | x = x * 1000.0; | 
|  | 962 | maxy = y + (g->ytick_one_val * TOOLTIP_DELTA); | 
|  | 963 | miny = y - (g->ytick_one_val * TOOLTIP_DELTA); | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 964 | best_delta = UINT_MAX; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 965 | flist_for_each(entry, &g->label_list) { | 
|  | 966 | struct graph_label *i; | 
|  | 967 |  | 
|  | 968 | i = flist_entry(entry, struct graph_label, list); | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 969 | if (i->hide) | 
|  | 970 | continue; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 971 |  | 
| Jens Axboe | 08d7d5a | 2012-03-21 19:33:32 +0100 | [diff] [blame] | 972 | INIT_PRIO_TREE_ITER(&iter); | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 973 | prio_tree_iter_init(&iter, &i->prio_tree, x, x); | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 974 |  | 
|  | 975 | n = prio_tree_next(&iter); | 
|  | 976 | if (!n) | 
|  | 977 | continue; | 
|  | 978 |  | 
|  | 979 | do { | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 980 | struct graph_value *v, *rootv; | 
| Jens Axboe | b7a69ad | 2012-03-21 21:55:21 +0100 | [diff] [blame] | 981 | double yval, ydiff; | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 982 |  | 
|  | 983 | v = container_of(n, struct graph_value, node); | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 984 | rootv = v; | 
|  | 985 | do { | 
|  | 986 | yval = gety(v); | 
|  | 987 | ydiff = fabs(yval - y); | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 988 |  | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 989 | /* | 
|  | 990 | * zero delta, or within or match critera, break | 
|  | 991 | */ | 
|  | 992 | if (ydiff < best_delta) { | 
|  | 993 | best_delta = ydiff; | 
|  | 994 | if (!best_delta || | 
|  | 995 | (yval >= miny && yval <= maxy)) { | 
|  | 996 | best = v; | 
|  | 997 | break; | 
|  | 998 | } | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 999 | } | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 1000 | if (!flist_empty(&v->alias)) | 
|  | 1001 | v = flist_entry(v->alias.next, struct graph_value, alias); | 
|  | 1002 | } while (v != rootv); | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 1003 | } while ((n = prio_tree_next(&iter)) != NULL); | 
|  | 1004 |  | 
|  | 1005 | /* | 
|  | 1006 | * If we got matches in one label, don't check others. | 
|  | 1007 | */ | 
| Jens Axboe | d0db819 | 2012-03-22 20:48:02 +0100 | [diff] [blame] | 1008 | if (best) | 
|  | 1009 | break; | 
| Jens Axboe | cdae5ff | 2012-03-22 09:24:05 +0100 | [diff] [blame] | 1010 | } | 
| Jens Axboe | b65c7ec | 2012-03-21 17:17:45 +0100 | [diff] [blame] | 1011 |  | 
|  | 1012 | if (best) | 
|  | 1013 | return best->tooltip; | 
| Jens Axboe | 93e2db2 | 2012-03-13 09:45:22 +0100 | [diff] [blame] | 1014 |  | 
|  | 1015 | return NULL; | 
|  | 1016 | } | 
| Jens Axboe | 01a947f | 2012-03-22 21:21:00 +0100 | [diff] [blame] | 1017 |  | 
|  | 1018 | void graph_set_graph_all_zeroes(struct graph *g, unsigned int set) | 
|  | 1019 | { | 
|  | 1020 | g->dont_graph_all_zeroes = !set; | 
|  | 1021 | } |