Blame graph.c

Packit 8930e1
/*
Packit 8930e1
 * gfio - gui front end for fio - the flexible io tester
Packit 8930e1
 *
Packit 8930e1
 * Copyright (C) 2012 Stephen M. Cameron <stephenmcameron@gmail.com>
Packit 8930e1
 *
Packit 8930e1
 * The license below covers all files distributed with fio unless otherwise
Packit 8930e1
 * noted in the file itself.
Packit 8930e1
 *
Packit 8930e1
 *  This program is free software; you can redistribute it and/or modify
Packit 8930e1
 *  it under the terms of the GNU General Public License version 2 as
Packit 8930e1
 *  published by the Free Software Foundation.
Packit 8930e1
 *
Packit 8930e1
 *  This program is distributed in the hope that it will be useful,
Packit 8930e1
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8930e1
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8930e1
 *  GNU General Public License for more details.
Packit 8930e1
 *
Packit 8930e1
 *  You should have received a copy of the GNU General Public License
Packit 8930e1
 *  along with this program; if not, write to the Free Software
Packit 8930e1
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 8930e1
 *
Packit 8930e1
 */
Packit 8930e1
#include <string.h>
Packit 8930e1
#include <stdlib.h>
Packit 8930e1
#include <math.h>
Packit 8930e1
#include <assert.h>
Packit 8930e1
#include <stdlib.h>
Packit 8930e1
Packit 8930e1
#include <cairo.h>
Packit 8930e1
#include <gtk/gtk.h>
Packit 8930e1
Packit 8930e1
#include "tickmarks.h"
Packit 8930e1
#include "graph.h"
Packit 8930e1
#include "flist.h"
Packit 8930e1
#include "lib/prio_tree.h"
Packit 8930e1
#include "cairo_text_helpers.h"
Packit 8930e1
Packit 8930e1
/*
Packit 8930e1
 * Allowable difference to show tooltip
Packit 8930e1
 */
Packit 8930e1
#define TOOLTIP_DELTA	0.08
Packit 8930e1
Packit 8930e1
struct xyvalue {
Packit 8930e1
	double x, y;
Packit 8930e1
};
Packit 8930e1
Packit 8930e1
enum {
Packit 8930e1
	GV_F_ON_PRIO	= 1,
Packit 8930e1
	GV_F_PRIO_SKIP	= 2,
Packit 8930e1
};
Packit 8930e1
Packit 8930e1
struct graph_value {
Packit 8930e1
	struct flist_head list;
Packit 8930e1
	struct prio_tree_node node;
Packit 8930e1
	struct flist_head alias;
Packit 8930e1
	unsigned int flags;
Packit 8930e1
	char *tooltip;
Packit 8930e1
	void *value;
Packit 8930e1
};
Packit 8930e1
Packit 8930e1
struct graph_label {
Packit 8930e1
	struct flist_head list;
Packit 8930e1
	char *label;
Packit 8930e1
	struct flist_head value_list;
Packit 8930e1
	struct prio_tree_root prio_tree;
Packit 8930e1
	double r, g, b;
Packit 8930e1
	int hide;
Packit 8930e1
	int value_count;
Packit 8930e1
	struct graph *parent;
Packit 8930e1
};
Packit 8930e1
Packit 8930e1
struct tick_value {
Packit 8930e1
	unsigned int offset;
Packit 8930e1
	double value;
Packit 8930e1
};
Packit 8930e1
Packit 8930e1
struct graph {
Packit 8930e1
	char *title;
Packit 8930e1
	char *xtitle;
Packit 8930e1
	char *ytitle;
Packit 8930e1
	unsigned int xdim, ydim;
Packit 8930e1
	double xoffset, yoffset;
Packit 8930e1
	struct flist_head label_list;
Packit 8930e1
	int per_label_limit;
Packit 8930e1
	const char *font;
Packit 8930e1
	graph_axis_unit_change_callback x_axis_unit_change_callback;
Packit 8930e1
	graph_axis_unit_change_callback y_axis_unit_change_callback;
Packit 8930e1
	unsigned int base_offset;
Packit 8930e1
	unsigned int dont_graph_all_zeroes;
Packit 8930e1
	double left_extra;
Packit 8930e1
	double right_extra;
Packit 8930e1
	double top_extra;
Packit 8930e1
	double bottom_extra;
Packit 8930e1
Packit 8930e1
	double xtick_zero;
Packit 8930e1
	double xtick_delta;
Packit 8930e1
	double xtick_zero_val;
Packit 8930e1
	double xtick_one_val;
Packit 8930e1
	double ytick_zero;
Packit 8930e1
	double ytick_delta;
Packit 8930e1
	double ytick_zero_val;
Packit 8930e1
	double ytick_one_val;
Packit 8930e1
};
Packit 8930e1
Packit 8930e1
void graph_set_size(struct graph *g, unsigned int xdim, unsigned int ydim)
Packit 8930e1
{
Packit 8930e1
	g->xdim = xdim;
Packit 8930e1
	g->ydim = ydim;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_set_position(struct graph *g, double xoffset, double yoffset)
Packit 8930e1
{
Packit 8930e1
	g->xoffset = xoffset;
Packit 8930e1
	g->yoffset = yoffset;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
struct graph *graph_new(unsigned int xdim, unsigned int ydim, const char *font)
Packit 8930e1
{
Packit 8930e1
	struct graph *g;
Packit 8930e1
Packit 8930e1
	g = calloc(1, sizeof(*g));
Packit 8930e1
	INIT_FLIST_HEAD(&g->label_list);
Packit 8930e1
	graph_set_size(g, xdim, ydim);
Packit 8930e1
	g->per_label_limit = -1;
Packit 8930e1
	g->font = font;
Packit 8930e1
	if (!g->font)
Packit 8930e1
		g->font = GRAPH_DEFAULT_FONT;
Packit 8930e1
	return g;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_set_font(struct graph *g, const char *font)
Packit 8930e1
{
Packit 8930e1
	g->font = font;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_x_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
Packit 8930e1
{
Packit 8930e1
	g->x_axis_unit_change_callback = f;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_y_axis_unit_change_notify(struct graph *g, graph_axis_unit_change_callback f)
Packit 8930e1
{
Packit 8930e1
	g->y_axis_unit_change_callback = f;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static int count_labels(struct graph *g)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	int count = 0;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &g->label_list)
Packit 8930e1
		count++;
Packit 8930e1
Packit 8930e1
	return count;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static int count_values(struct graph_label *l)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	int count = 0;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &l->value_list)
Packit 8930e1
		count++;
Packit 8930e1
Packit 8930e1
	return count;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
typedef double (*double_comparator)(double a, double b);
Packit 8930e1
Packit 8930e1
static double mindouble(double a, double b)
Packit 8930e1
{
Packit 8930e1
	return a < b ? a : b;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double maxdouble(double a, double b)
Packit 8930e1
{
Packit 8930e1
	return a < b ? b : a;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double find_double_values(struct graph_label *l, double_comparator cmp)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	double answer = 0.0, tmp;
Packit 8930e1
	int first = 1;
Packit 8930e1
Packit 8930e1
	if (flist_empty(&l->value_list))
Packit 8930e1
		return 0.0;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &l->value_list) {
Packit 8930e1
		struct graph_value *i;
Packit 8930e1
Packit 8930e1
		i = flist_entry(entry, struct graph_value, list);
Packit 8930e1
		tmp = *(double *) i->value;
Packit 8930e1
		if (first) {
Packit 8930e1
			answer = tmp;
Packit 8930e1
			first = 0;
Packit 8930e1
		} else {
Packit 8930e1
			answer = cmp(answer, tmp);
Packit 8930e1
		}
Packit 8930e1
	}
Packit 8930e1
	return answer;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double find_double_data(struct graph *g, double_comparator cmp)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
	int first = 1;
Packit 8930e1
	double answer, tmp;
Packit 8930e1
Packit 8930e1
	if (flist_empty(&g->label_list))
Packit 8930e1
		return 0.0;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &g->label_list) {
Packit 8930e1
		i = flist_entry(entry, struct graph_label, list);
Packit 8930e1
		tmp = find_double_values(i, cmp);
Packit 8930e1
		if (first) {
Packit 8930e1
			answer = tmp;
Packit 8930e1
			first = 0;
Packit 8930e1
		} else {
Packit 8930e1
			answer = cmp(tmp, answer);
Packit 8930e1
		}
Packit 8930e1
	}
Packit 8930e1
	return answer;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double find_min_data(struct graph *g)
Packit 8930e1
{
Packit 8930e1
	return find_double_data(g, mindouble);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double find_max_data(struct graph *g)
Packit 8930e1
{
Packit 8930e1
	return find_double_data(g, maxdouble);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void draw_bars(struct graph *bg, cairo_t *cr, struct graph_label *lb,
Packit 8930e1
			double label_offset, double bar_width,
Packit 8930e1
			double mindata, double maxdata)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	double x1, y1, x2, y2;
Packit 8930e1
	int bar_num = 0;
Packit 8930e1
	double domain, range, v;
Packit 8930e1
Packit 8930e1
	domain = (maxdata - mindata);
Packit 8930e1
	range = (double) bg->ydim * 0.80; /* FIXME */
Packit 8930e1
	cairo_stroke(cr);
Packit 8930e1
	flist_for_each(entry, &lb->value_list) {
Packit 8930e1
		struct graph_value *i;
Packit 8930e1
Packit 8930e1
		i = flist_entry(entry, struct graph_value, list);
Packit 8930e1
Packit 8930e1
		x1 = label_offset + (double) bar_num * bar_width + (bar_width * 0.05);
Packit 8930e1
		x2 = x1 + bar_width * 0.90;
Packit 8930e1
		y2 = bg->ydim * 0.90;
Packit 8930e1
		v = *(double *) i->value;
Packit 8930e1
		y1 = y2 - (((v - mindata) / domain) * range);
Packit 8930e1
		cairo_move_to(cr, x1, y1);
Packit 8930e1
		cairo_line_to(cr, x1, y2);
Packit 8930e1
		cairo_line_to(cr, x2, y2);
Packit 8930e1
		cairo_line_to(cr, x2, y1);
Packit 8930e1
		cairo_close_path(cr);
Packit 8930e1
		cairo_fill(cr);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
		bar_num++;
Packit 8930e1
	}
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void graph_draw_common(struct graph *g, cairo_t *cr, double *x1,
Packit 8930e1
			      double *y1, double *x2, double *y2)
Packit 8930e1
{
Packit 8930e1
	const double shade_col[3][3] = { { 0.55, 0.54, 0.54 },
Packit 8930e1
					 { 0.80, 0.78, 0.78 },
Packit 8930e1
					 { 0.93, 0.91, 0.91 } };
Packit 8930e1
	int i;
Packit 8930e1
Packit 8930e1
	*x1 = 0.10 * g->xdim;
Packit 8930e1
	*x2 = 0.95 * g->xdim;
Packit 8930e1
	*y1 = 0.10 * g->ydim;
Packit 8930e1
	*y2 = 0.90 * g->ydim;
Packit 8930e1
Packit 8930e1
	/*
Packit 8930e1
	 * Add shade
Packit 8930e1
	 */
Packit 8930e1
	cairo_set_line_width(cr, 1.0);
Packit 8930e1
	for (i = 0; i < 3; i++) {
Packit 8930e1
		float offset = i + 1.0;
Packit 8930e1
Packit 8930e1
		cairo_set_source_rgb(cr, shade_col[i][0], shade_col[i][1], shade_col[i][2]);
Packit 8930e1
		cairo_move_to(cr, offset + *x1, *y1 - offset);
Packit 8930e1
		cairo_line_to(cr, *x2 + offset, *y1 - offset);
Packit 8930e1
		cairo_line_to(cr, *x2 + offset, *y2 - offset);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	cairo_set_source_rgb(cr, 0, 0, 0);
Packit 8930e1
	cairo_set_line_width(cr, 1.2);
Packit 8930e1
Packit 8930e1
	cairo_move_to(cr, *x1, *y1);
Packit 8930e1
	cairo_line_to(cr, *x1, *y2);
Packit 8930e1
	cairo_line_to(cr, *x2, *y2);
Packit 8930e1
	cairo_line_to(cr, *x2, *y1);
Packit 8930e1
	cairo_line_to(cr, *x1, *y1);
Packit 8930e1
	cairo_stroke(cr);
Packit 8930e1
Packit 8930e1
	draw_centered_text(cr, g->font, g->xdim / 2, g->ydim / 20, 20.0, g->title);
Packit 8930e1
	draw_centered_text(cr, g->font, g->xdim / 2, g->ydim * 0.97, 14.0, g->xtitle);
Packit 8930e1
	draw_vertical_centered_text(cr, g->font, g->xdim * 0.02, g->ydim / 2, 14.0, g->ytitle);
Packit 8930e1
	cairo_stroke(cr);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void graph_draw_x_ticks(struct graph *g, cairo_t *cr,
Packit 8930e1
	double x1, double y1, double x2, double y2,
Packit 8930e1
	double minx, double maxx, int nticks, int add_tm_text)
Packit 8930e1
{
Packit 8930e1
	struct tickmark *tm;
Packit 8930e1
	double tx;
Packit 8930e1
	int i, power_of_ten;
Packit 8930e1
	static double dash[] = { 1.0, 2.0 };
Packit 8930e1
Packit 8930e1
	nticks = calc_tickmarks(minx, maxx, nticks, &tm, &power_of_ten,
Packit 8930e1
		g->x_axis_unit_change_callback == NULL, g->base_offset);
Packit 8930e1
	if (g->x_axis_unit_change_callback)
Packit 8930e1
		g->x_axis_unit_change_callback(g, power_of_ten);
Packit 8930e1
Packit 8930e1
	for (i = 0; i < nticks; i++) {
Packit 8930e1
		tx = (((tm[i].value) - minx) / (maxx - minx)) * (x2 - x1) + x1;
Packit 8930e1
Packit 8930e1
		/*
Packit 8930e1
		 * Update tick delta
Packit 8930e1
		 */
Packit 8930e1
		if (!i) {
Packit 8930e1
			g->xtick_zero = tx;
Packit 8930e1
			g->xtick_zero_val = tm[0].value;
Packit 8930e1
		} else if (i == 1) {
Packit 8930e1
			g->xtick_delta = (tm[1].value - tm[0].value) / (tx - g->xtick_zero);
Packit 8930e1
			g->xtick_one_val = tm[1].value;
Packit 8930e1
		}
Packit 8930e1
Packit 8930e1
		/* really tx < yx || tx > x2, but protect against rounding */
Packit 8930e1
		if (x1 - tx > 0.01 || tx - x2 > 0.01)
Packit 8930e1
			continue;
Packit 8930e1
Packit 8930e1
		/* Draw tick mark */
Packit 8930e1
		cairo_set_line_width(cr, 1.0);
Packit 8930e1
		cairo_move_to(cr, tx, y2);
Packit 8930e1
		cairo_line_to(cr, tx, y2 + (y2 - y1) * 0.03);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
Packit 8930e1
		/* draw grid lines */
Packit 8930e1
		cairo_save(cr);
Packit 8930e1
		cairo_set_dash(cr, dash, 2, 0.66);
Packit 8930e1
		cairo_set_line_width(cr, 0.33);
Packit 8930e1
		cairo_move_to(cr, tx, y1);
Packit 8930e1
		cairo_line_to(cr, tx, y2);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
		cairo_restore(cr);
Packit 8930e1
Packit 8930e1
		if (!add_tm_text)
Packit 8930e1
			continue;
Packit 8930e1
Packit 8930e1
		/* draw tickmark label */
Packit 8930e1
		draw_centered_text(cr, g->font, tx, y2 * 1.04, 12.0, tm[i].string);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
	}
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double graph_draw_y_ticks(struct graph *g, cairo_t *cr,
Packit 8930e1
	double x1, double y1, double x2, double y2,
Packit 8930e1
	double miny, double maxy, int nticks, int add_tm_text)
Packit 8930e1
{
Packit 8930e1
	struct tickmark *tm;
Packit 8930e1
	double ty;
Packit 8930e1
	int i, power_of_ten;
Packit 8930e1
	static double dash[] = { 1.0, 2.0 };
Packit 8930e1
Packit 8930e1
	nticks = calc_tickmarks(miny, maxy, nticks, &tm, &power_of_ten,
Packit 8930e1
		g->y_axis_unit_change_callback == NULL, g->base_offset);
Packit 8930e1
	if (g->y_axis_unit_change_callback)
Packit 8930e1
		g->y_axis_unit_change_callback(g, power_of_ten);
Packit 8930e1
Packit 8930e1
	/*
Packit 8930e1
	 * Use highest tickmark as top of graph, not highest value. Otherwise
Packit 8930e1
	 * it's impossible to see what the max value is, if the graph is
Packit 8930e1
	 * fairly flat.
Packit 8930e1
	 */
Packit 8930e1
	maxy = tm[nticks - 1].value;
Packit 8930e1
Packit 8930e1
	for (i = 0; i < nticks; i++) {
Packit 8930e1
		ty = y2 - (((tm[i].value) - miny) / (maxy - miny)) * (y2 - y1);
Packit 8930e1
Packit 8930e1
		/*
Packit 8930e1
		 * Update tick delta
Packit 8930e1
		 */
Packit 8930e1
		if (!i) {
Packit 8930e1
			g->ytick_zero = ty;
Packit 8930e1
			g->ytick_zero_val = tm[0].value;
Packit 8930e1
		} else if (i == 1) {
Packit 8930e1
			g->ytick_delta = (tm[1].value - tm[0].value) / (ty - g->ytick_zero);
Packit 8930e1
			g->ytick_one_val = tm[1].value;
Packit 8930e1
		}
Packit 8930e1
Packit 8930e1
		/* really ty < y1 || ty > y2, but protect against rounding */
Packit 8930e1
		if (y1 - ty > 0.01 || ty - y2 > 0.01)
Packit 8930e1
			continue;
Packit 8930e1
Packit 8930e1
		/* draw tick mark */
Packit 8930e1
		cairo_move_to(cr, x1, ty);
Packit 8930e1
		cairo_line_to(cr, x1 - (x2 - x1) * 0.02, ty);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
Packit 8930e1
		/* draw grid lines */
Packit 8930e1
		cairo_save(cr);
Packit 8930e1
		cairo_set_dash(cr, dash, 2, 0.66);
Packit 8930e1
		cairo_set_line_width(cr, 0.33);
Packit 8930e1
		cairo_move_to(cr, x1, ty);
Packit 8930e1
		cairo_line_to(cr, x2, ty);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
		cairo_restore(cr);
Packit 8930e1
Packit 8930e1
		if (!add_tm_text)
Packit 8930e1
			continue;
Packit 8930e1
Packit 8930e1
		/* draw tickmark label */
Packit 8930e1
		draw_right_justified_text(cr, g->font, x1 - (x2 - x1) * 0.025, ty, 12.0, tm[i].string);
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	/*
Packit 8930e1
	 * Return new max to use
Packit 8930e1
	 */
Packit 8930e1
	return maxy;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void bar_graph_draw(struct graph *bg, cairo_t *cr)
Packit 8930e1
{
Packit 8930e1
	double x1, y1, x2, y2;
Packit 8930e1
	double space_per_label, bar_width;
Packit 8930e1
	double label_offset, mindata, maxdata;
Packit 8930e1
	int i, nlabels;
Packit 8930e1
	struct graph_label *lb;
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
Packit 8930e1
	cairo_save(cr);
Packit 8930e1
	cairo_translate(cr, bg->xoffset, bg->yoffset);
Packit 8930e1
	graph_draw_common(bg, cr, &x1, &y1, &x2, &y2;;
Packit 8930e1
Packit 8930e1
	nlabels = count_labels(bg);
Packit 8930e1
	space_per_label = (x2 - x1) / (double) nlabels;
Packit 8930e1
Packit 8930e1
	/*
Packit 8930e1
	 * Start bars at 0 unless we have negative values, otherwise we
Packit 8930e1
	 * present a skewed picture comparing label X and X+1.
Packit 8930e1
	 */
Packit 8930e1
	mindata = find_min_data(bg);
Packit 8930e1
	if (mindata > 0)
Packit 8930e1
		mindata = 0;
Packit 8930e1
Packit 8930e1
	maxdata = find_max_data(bg);
Packit 8930e1
Packit 8930e1
	if (fabs(maxdata - mindata) < 1e-20) {
Packit 8930e1
		draw_centered_text(cr, bg->font,
Packit 8930e1
			x1 + (x2 - x1) / 2.0,
Packit 8930e1
			y1 + (y2 - y1) / 2.0, 20.0, "No good data");
Packit 8930e1
		return;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	maxdata = graph_draw_y_ticks(bg, cr, x1, y1, x2, y2, mindata, maxdata, 10, 1);
Packit 8930e1
	i = 0;
Packit 8930e1
	flist_for_each(entry, &bg->label_list) {
Packit 8930e1
		int nvalues;
Packit 8930e1
Packit 8930e1
		lb = flist_entry(entry, struct graph_label, list);
Packit 8930e1
		nvalues = count_values(lb);
Packit 8930e1
		bar_width = (space_per_label - space_per_label * 0.2) / (double) nvalues;
Packit 8930e1
		label_offset = bg->xdim * 0.1 + space_per_label * (double) i + space_per_label * 0.1;
Packit 8930e1
		draw_bars(bg, cr, lb, label_offset, bar_width, mindata, maxdata);
Packit 8930e1
		// draw_centered_text(cr, label_offset + (bar_width / 2.0 + bar_width * 0.1), bg->ydim * 0.93,
Packit 8930e1
		draw_centered_text(cr, bg->font, x1 + space_per_label * (i + 0.5), bg->ydim * 0.93,
Packit 8930e1
			12.0, lb->label);
Packit 8930e1
		i++;
Packit 8930e1
	}
Packit 8930e1
	cairo_stroke(cr);
Packit 8930e1
	cairo_restore(cr);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
typedef double (*xy_value_extractor)(struct graph_value *v);
Packit 8930e1
Packit 8930e1
static double getx(struct graph_value *v)
Packit 8930e1
{
Packit 8930e1
	struct xyvalue *xy = v->value;
Packit 8930e1
	return xy->x;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double gety(struct graph_value *v)
Packit 8930e1
{
Packit 8930e1
	struct xyvalue *xy = v->value;
Packit 8930e1
	return xy->y;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static double find_xy_value(struct graph *g, xy_value_extractor getvalue, double_comparator cmp)
Packit 8930e1
{
Packit 8930e1
	double tmp, answer = 0.0;
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
	struct graph_value *j;
Packit 8930e1
	struct flist_head *jentry, *entry;
Packit 8930e1
	int first = 1;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &g->label_list) {
Packit 8930e1
		i = flist_entry(entry, struct graph_label, list);
Packit 8930e1
Packit 8930e1
		flist_for_each(jentry, &i->value_list) {
Packit 8930e1
			j = flist_entry(jentry, struct graph_value, list);
Packit 8930e1
			tmp = getvalue(j);
Packit 8930e1
			if (first) {
Packit 8930e1
				first = 0;
Packit 8930e1
				answer = tmp;
Packit 8930e1
			}
Packit 8930e1
			answer = cmp(tmp, answer);
Packit 8930e1
		}
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	return answer;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void line_graph_draw(struct graph *g, cairo_t *cr)
Packit 8930e1
{
Packit 8930e1
	double x1, y1, x2, y2;
Packit 8930e1
	double minx, miny, maxx, maxy, gminx, gminy, gmaxx, gmaxy;
Packit 8930e1
	double tx, ty, top_extra, bottom_extra, left_extra, right_extra;
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
	struct graph_value *j;
Packit 8930e1
	int good_data = 1, first = 1;
Packit 8930e1
	struct flist_head *entry, *lentry;
Packit 8930e1
Packit 8930e1
	cairo_save(cr);
Packit 8930e1
	cairo_translate(cr, g->xoffset, g->yoffset);
Packit 8930e1
	graph_draw_common(g, cr, &x1, &y1, &x2, &y2;;
Packit 8930e1
Packit 8930e1
	minx = find_xy_value(g, getx, mindouble);
Packit 8930e1
	maxx = find_xy_value(g, getx, maxdouble);
Packit 8930e1
	miny = find_xy_value(g, gety, mindouble);
Packit 8930e1
Packit 8930e1
	/*
Packit 8930e1
	 * Start graphs at zero, unless we have a value below. Otherwise
Packit 8930e1
	 * it's hard to visually compare the read and write graph, since
Packit 8930e1
	 * the lowest valued one will be the floor of the graph view.
Packit 8930e1
	 */
Packit 8930e1
	if (miny > 0)
Packit 8930e1
		miny = 0;
Packit 8930e1
Packit 8930e1
	maxy = find_xy_value(g, gety, maxdouble);
Packit 8930e1
Packit 8930e1
	if (fabs(maxx - minx) < 1e-20 || fabs(maxy - miny) < 1e-20) {
Packit 8930e1
		good_data = 0;
Packit 8930e1
		minx = 0.0;
Packit 8930e1
		miny = 0.0;
Packit 8930e1
		maxx = 10.0;
Packit 8930e1
		maxy = 100.0;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	top_extra = 0.0;
Packit 8930e1
	bottom_extra = 0.0;
Packit 8930e1
	left_extra = 0.0;
Packit 8930e1
	right_extra = 0.0;
Packit 8930e1
Packit 8930e1
	if (g->top_extra > 0.001)
Packit 8930e1
		top_extra = fabs(maxy - miny) * g->top_extra;
Packit 8930e1
	if (g->bottom_extra > 0.001)
Packit 8930e1
		bottom_extra = fabs(maxy - miny) * g->bottom_extra;
Packit 8930e1
	if (g->left_extra > 0.001)
Packit 8930e1
		left_extra = fabs(maxx - minx) * g->left_extra;
Packit 8930e1
	if (g->right_extra > 0.001)
Packit 8930e1
		right_extra = fabs(maxx - minx) * g->right_extra;
Packit 8930e1
Packit 8930e1
	gminx = minx - left_extra;
Packit 8930e1
	gmaxx = maxx + right_extra;
Packit 8930e1
	gminy = miny - bottom_extra;
Packit 8930e1
	gmaxy = maxy + top_extra;
Packit 8930e1
Packit 8930e1
	graph_draw_x_ticks(g, cr, x1, y1, x2, y2, gminx, gmaxx, 10, good_data);
Packit 8930e1
	gmaxy = graph_draw_y_ticks(g, cr, x1, y1, x2, y2, gminy, gmaxy, 10, good_data);
Packit 8930e1
Packit 8930e1
	if (!good_data)
Packit 8930e1
		goto skip_data;
Packit 8930e1
Packit 8930e1
	cairo_set_line_width(cr, 1.5);
Packit 8930e1
	cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
Packit 8930e1
Packit 8930e1
	flist_for_each(lentry, &g->label_list) {
Packit 8930e1
		i = flist_entry(lentry, struct graph_label, list);
Packit 8930e1
		first = 1;
Packit 8930e1
		if (i->hide || i->r < 0) /* invisible data */
Packit 8930e1
			continue;
Packit 8930e1
Packit 8930e1
		cairo_set_source_rgb(cr, i->r, i->g, i->b);
Packit 8930e1
		flist_for_each(entry, &i->value_list) {
Packit 8930e1
			j = flist_entry(entry, struct graph_value, list);
Packit 8930e1
			tx = ((getx(j) - gminx) / (gmaxx - gminx)) * (x2 - x1) + x1;
Packit 8930e1
			ty = y2 - ((gety(j) - gminy) / (gmaxy - gminy)) * (y2 - y1);
Packit 8930e1
			if (first) {
Packit 8930e1
				cairo_move_to(cr, tx, ty);
Packit 8930e1
				first = 0;
Packit 8930e1
			} else
Packit 8930e1
				cairo_line_to(cr, tx, ty);
Packit 8930e1
		}
Packit 8930e1
		cairo_stroke(cr);
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
skip_data:
Packit 8930e1
	cairo_restore(cr);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void setstring(char **str, const char *value)
Packit 8930e1
{
Packit 8930e1
	free(*str);
Packit 8930e1
	*str = strdup(value);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_title(struct graph *bg, const char *title)
Packit 8930e1
{
Packit 8930e1
	setstring(&bg->title, title);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_x_title(struct graph *bg, const char *title)
Packit 8930e1
{
Packit 8930e1
	setstring(&bg->xtitle, title);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_y_title(struct graph *bg, const char *title)
Packit 8930e1
{
Packit 8930e1
	setstring(&bg->ytitle, title);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static struct graph_label *graph_find_label(struct graph *bg,
Packit 8930e1
				const char *label)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &bg->label_list) {
Packit 8930e1
		i = flist_entry(entry, struct graph_label, list);
Packit 8930e1
Packit 8930e1
		if (strcmp(label, i->label) == 0)
Packit 8930e1
			return i;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	return NULL;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
graph_label_t graph_add_label(struct graph *bg, const char *label)
Packit 8930e1
{
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
Packit 8930e1
	i = graph_find_label(bg, label);
Packit 8930e1
	if (i)
Packit 8930e1
		return i; /* already present. */
Packit 8930e1
	i = calloc(1, sizeof(*i));
Packit 8930e1
	INIT_FLIST_HEAD(&i->value_list);
Packit 8930e1
	i->parent = bg;
Packit 8930e1
	setstring(&i->label, label);
Packit 8930e1
	flist_add_tail(&i->list, &bg->label_list);
Packit 8930e1
	INIT_PRIO_TREE_ROOT(&i->prio_tree);
Packit 8930e1
	return i;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void __graph_value_drop(struct graph_label *l, struct graph_value *v)
Packit 8930e1
{
Packit 8930e1
	flist_del_init(&v->list);
Packit 8930e1
	if (v->tooltip)
Packit 8930e1
		free(v->tooltip);
Packit 8930e1
	free(v->value);
Packit 8930e1
	free(v);
Packit 8930e1
	l->value_count--;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void graph_value_drop(struct graph_label *l, struct graph_value *v)
Packit 8930e1
{
Packit 8930e1
	if (v->flags & GV_F_PRIO_SKIP) {
Packit 8930e1
		__graph_value_drop(l, v);
Packit 8930e1
		return;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	/*
Packit 8930e1
	 * Find head, the guy that's on the prio tree
Packit 8930e1
	 */
Packit 8930e1
	while (!(v->flags & GV_F_ON_PRIO)) {
Packit 8930e1
		assert(!flist_empty(&v->alias));
Packit 8930e1
		v = flist_first_entry(&v->alias, struct graph_value, alias);
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	prio_tree_remove(&l->prio_tree, &v->node);
Packit 8930e1
Packit 8930e1
	/*
Packit 8930e1
	 * Free aliases
Packit 8930e1
	 */
Packit 8930e1
	while (!flist_empty(&v->alias)) {
Packit 8930e1
		struct graph_value *a;
Packit 8930e1
Packit 8930e1
		a = flist_first_entry(&v->alias, struct graph_value, alias);
Packit 8930e1
		flist_del_init(&a->alias);
Packit 8930e1
Packit 8930e1
		__graph_value_drop(l, a);
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	__graph_value_drop(l, v);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void graph_label_add_value(struct graph_label *i, void *value,
Packit 8930e1
				  const char *tooltip)
Packit 8930e1
{
Packit 8930e1
	struct graph *g = i->parent;
Packit 8930e1
	struct graph_value *x;
Packit 8930e1
Packit 8930e1
	x = malloc(sizeof(*x));
Packit 8930e1
	memset(x, 0, sizeof(*x));
Packit 8930e1
	INIT_FLIST_HEAD(&x->alias);
Packit 8930e1
	INIT_FLIST_HEAD(&x->list);
Packit 8930e1
	flist_add_tail(&x->list, &i->value_list);
Packit 8930e1
	i->value_count++;
Packit 8930e1
	x->value = value;
Packit 8930e1
Packit 8930e1
	if (tooltip) {
Packit 8930e1
		double xval = getx(x);
Packit 8930e1
		double minx = xval - (g->xtick_one_val * TOOLTIP_DELTA);
Packit 8930e1
		double maxx = xval + (g->xtick_one_val * TOOLTIP_DELTA);
Packit 8930e1
		struct prio_tree_node *ret;
Packit 8930e1
Packit 8930e1
		/*
Packit 8930e1
		 * use msec to avoid dropping too much precision when
Packit 8930e1
		 * storing as an integer.
Packit 8930e1
		 */
Packit 8930e1
		minx = minx * 1000.0;
Packit 8930e1
		maxx = maxx * 1000.0;
Packit 8930e1
Packit 8930e1
		INIT_PRIO_TREE_NODE(&x->node);
Packit 8930e1
		x->node.start = minx;
Packit 8930e1
		x->node.last = maxx;
Packit 8930e1
		x->tooltip = strdup(tooltip);
Packit 8930e1
		if (x->node.last == x->node.start) {
Packit 8930e1
			x->node.last += fabs(g->xtick_delta);
Packit 8930e1
			if (x->node.last == x->node.start)
Packit 8930e1
				x->node.last++;
Packit 8930e1
		}
Packit 8930e1
Packit 8930e1
		/*
Packit 8930e1
		 * If ret != &x->node, we have an alias. Since the values
Packit 8930e1
		 * should be identical, we can drop it
Packit 8930e1
		 */
Packit 8930e1
		ret = prio_tree_insert(&i->prio_tree, &x->node);
Packit 8930e1
		if (ret != &x->node) {
Packit 8930e1
			struct graph_value *alias;
Packit 8930e1
Packit 8930e1
			alias = container_of(ret, struct graph_value, node);
Packit 8930e1
			flist_add_tail(&x->alias, &alias->alias);
Packit 8930e1
		} else
Packit 8930e1
			x->flags = GV_F_ON_PRIO;
Packit 8930e1
	} else
Packit 8930e1
		x->flags = GV_F_PRIO_SKIP;
Packit 8930e1
Packit 8930e1
	if (g->per_label_limit != -1 &&
Packit 8930e1
		i->value_count > g->per_label_limit) {
Packit 8930e1
		int to_drop = 1;
Packit 8930e1
Packit 8930e1
		/*
Packit 8930e1
		 * If the limit was dynamically reduced, making us more
Packit 8930e1
		 * than 1 entry ahead after adding this one, drop two
Packit 8930e1
		 * entries. This will make us (eventually) reach the
Packit 8930e1
		 * specified limit.
Packit 8930e1
		 */
Packit 8930e1
		if (i->value_count - g->per_label_limit >= 2)
Packit 8930e1
			to_drop = 2;
Packit 8930e1
Packit 8930e1
		while (to_drop-- && !flist_empty(&i->value_list)) {
Packit 8930e1
			x = flist_first_entry(&i->value_list, struct graph_value, list);
Packit 8930e1
			graph_value_drop(i, x);
Packit 8930e1
Packit 8930e1
			/*
Packit 8930e1
			 * If we have aliases, we could drop > 1 above.
Packit 8930e1
			 */
Packit 8930e1
			if (i->value_count <= g->per_label_limit)
Packit 8930e1
				break;
Packit 8930e1
		}
Packit 8930e1
	}
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
int graph_add_data(struct graph *bg, graph_label_t label, const double value)
Packit 8930e1
{
Packit 8930e1
	struct graph_label *i = label;
Packit 8930e1
	double *d;
Packit 8930e1
Packit 8930e1
	d = malloc(sizeof(*d));
Packit 8930e1
	*d = value;
Packit 8930e1
Packit 8930e1
	graph_label_add_value(i, d, NULL);
Packit 8930e1
	return 0;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static int graph_nonzero_y(struct graph_label *l)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &l->value_list) {
Packit 8930e1
		struct graph_value *v;
Packit 8930e1
Packit 8930e1
		v = flist_entry(entry, struct graph_value, list);
Packit 8930e1
		if (gety(v) != 0.0)
Packit 8930e1
			return 1;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	return 0;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
int graph_add_xy_data(struct graph *bg, graph_label_t label,
Packit 8930e1
		      const double x, const double y, const char *tooltip)
Packit 8930e1
{
Packit 8930e1
	struct graph_label *i = label;
Packit 8930e1
	struct xyvalue *xy;
Packit 8930e1
Packit 8930e1
	if (bg->dont_graph_all_zeroes && y == 0.0 && !graph_nonzero_y(i))
Packit 8930e1
		i->hide = 1;
Packit 8930e1
	else
Packit 8930e1
		i->hide = 0;
Packit 8930e1
Packit 8930e1
	xy = malloc(sizeof(*xy));
Packit 8930e1
	xy->x = x;
Packit 8930e1
	xy->y = y;
Packit 8930e1
Packit 8930e1
	graph_label_add_value(i, xy, tooltip);
Packit 8930e1
	return 0;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void graph_free_values(struct graph_label *l)
Packit 8930e1
{
Packit 8930e1
	struct graph_value *i;
Packit 8930e1
Packit 8930e1
	while (!flist_empty(&l->value_list)) {
Packit 8930e1
		i = flist_first_entry(&l->value_list, struct graph_value, list);
Packit 8930e1
		graph_value_drop(l, i);
Packit 8930e1
	}
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
static void graph_free_labels(struct graph *g)
Packit 8930e1
{
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
Packit 8930e1
	while (!flist_empty(&g->label_list)) {
Packit 8930e1
		i = flist_first_entry(&g->label_list, struct graph_label, list);
Packit 8930e1
		flist_del(&i->list);
Packit 8930e1
		graph_free_values(i);
Packit 8930e1
		free(i);
Packit 8930e1
	}
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_clear_values(struct graph *g)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *node;
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
Packit 8930e1
	flist_for_each(node, &g->label_list) {
Packit 8930e1
		i = flist_entry(node, struct graph_label, list);
Packit 8930e1
		graph_free_values(i);
Packit 8930e1
	}
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_set_color(struct graph *gr, graph_label_t label, double red,
Packit 8930e1
		     double green, double blue)
Packit 8930e1
{
Packit 8930e1
	struct graph_label *i = label;
Packit 8930e1
	double r, g, b;
Packit 8930e1
Packit 8930e1
	if (red < 0.0) { /* invisible color */
Packit 8930e1
		r = -1.0;
Packit 8930e1
		g = -1.0;
Packit 8930e1
		b = -1.0;
Packit 8930e1
	} else {
Packit 8930e1
		r = fabs(red);
Packit 8930e1
		g = fabs(green);
Packit 8930e1
		b = fabs(blue);
Packit 8930e1
Packit 8930e1
		if (r > 1.0)
Packit 8930e1
			r = 1.0;
Packit 8930e1
		if (g > 1.0)
Packit 8930e1
			g = 1.0;
Packit 8930e1
		if (b > 1.0)
Packit 8930e1
			b = 1.0;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	i->r = r;
Packit 8930e1
	i->g = g;
Packit 8930e1
	i->b = b;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_free(struct graph *bg)
Packit 8930e1
{
Packit 8930e1
	free(bg->title);
Packit 8930e1
	free(bg->xtitle);
Packit 8930e1
	free(bg->ytitle);
Packit 8930e1
	graph_free_labels(bg);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
/* For each line in the line graph, up to per_label_limit segments may
Packit 8930e1
 * be added.  After that, adding more data to the end of the line
Packit 8930e1
 * causes data to drop off of the front of the line.
Packit 8930e1
 */
Packit 8930e1
void line_graph_set_data_count_limit(struct graph *g, int per_label_limit)
Packit 8930e1
{
Packit 8930e1
	g->per_label_limit = per_label_limit;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_add_extra_space(struct graph *g, double left_percent,
Packit 8930e1
			   double right_percent, double top_percent,
Packit 8930e1
			   double bottom_percent)
Packit 8930e1
{
Packit 8930e1
	g->left_extra = left_percent;
Packit 8930e1
	g->right_extra = right_percent;
Packit 8930e1
	g->top_extra = top_percent;
Packit 8930e1
	g->bottom_extra = bottom_percent;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
/*
Packit 8930e1
 * Normally values are logged in a base unit of 0, but for other purposes
Packit 8930e1
 * it makes more sense to log in higher unit. For instance for bandwidth
Packit 8930e1
 * purposes, you may want to log in KB/sec (or MB/sec) rather than bytes/sec.
Packit 8930e1
 */
Packit 8930e1
void graph_set_base_offset(struct graph *g, unsigned int base_offset)
Packit 8930e1
{
Packit 8930e1
	g->base_offset = base_offset;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
int graph_has_tooltips(struct graph *g)
Packit 8930e1
{
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	struct graph_label *i;
Packit 8930e1
Packit 8930e1
	flist_for_each(entry, &g->label_list) {
Packit 8930e1
		i = flist_entry(entry, struct graph_label, list);
Packit 8930e1
Packit 8930e1
		if (!prio_tree_empty(&i->prio_tree))
Packit 8930e1
			return 1;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	return 0;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
int graph_contains_xy(struct graph *g, int x, int y)
Packit 8930e1
{
Packit 8930e1
	int first_x = g->xoffset;
Packit 8930e1
	int last_x = g->xoffset + g->xdim;
Packit 8930e1
	int first_y = g->yoffset;
Packit 8930e1
	int last_y = g->yoffset + g->ydim;
Packit 8930e1
Packit 8930e1
	return (x >= first_x && x <= last_x) && (y >= first_y && y <= last_y);
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
const char *graph_find_tooltip(struct graph *g, int ix, int iy)
Packit 8930e1
{
Packit 8930e1
	double x = ix, y = iy;
Packit 8930e1
	struct prio_tree_iter iter;
Packit 8930e1
	struct prio_tree_node *n;
Packit 8930e1
	struct graph_value *best = NULL;
Packit 8930e1
	struct flist_head *entry;
Packit 8930e1
	double best_delta;
Packit 8930e1
	double maxy, miny;
Packit 8930e1
Packit 8930e1
	x -= g->xoffset;
Packit 8930e1
	y -= g->yoffset;
Packit 8930e1
Packit 8930e1
	x = g->xtick_zero_val + ((x - g->xtick_zero) * g->xtick_delta);
Packit 8930e1
	y = g->ytick_zero_val + ((y - g->ytick_zero) * g->ytick_delta);
Packit 8930e1
Packit 8930e1
	x = x * 1000.0;
Packit 8930e1
	maxy = y + (g->ytick_one_val * TOOLTIP_DELTA);
Packit 8930e1
	miny = y - (g->ytick_one_val * TOOLTIP_DELTA);
Packit 8930e1
	best_delta = UINT_MAX;
Packit 8930e1
	flist_for_each(entry, &g->label_list) {
Packit 8930e1
		struct graph_label *i;
Packit 8930e1
Packit 8930e1
		i = flist_entry(entry, struct graph_label, list);
Packit 8930e1
		if (i->hide)
Packit 8930e1
			continue;
Packit 8930e1
Packit 8930e1
		INIT_PRIO_TREE_ITER(&iter);
Packit 8930e1
		prio_tree_iter_init(&iter, &i->prio_tree, x, x);
Packit 8930e1
Packit 8930e1
		n = prio_tree_next(&iter);
Packit 8930e1
		if (!n)
Packit 8930e1
			continue;
Packit 8930e1
Packit 8930e1
		do {
Packit 8930e1
			struct graph_value *v, *rootv;
Packit 8930e1
			double yval, ydiff;
Packit 8930e1
Packit 8930e1
			v = container_of(n, struct graph_value, node);
Packit 8930e1
			rootv = v;
Packit 8930e1
			do {
Packit 8930e1
				yval = gety(v);
Packit 8930e1
				ydiff = fabs(yval - y);
Packit 8930e1
Packit 8930e1
				/*
Packit 8930e1
				 * zero delta, or within or match critera, break
Packit 8930e1
				 */
Packit 8930e1
				if (ydiff < best_delta) {
Packit 8930e1
					best_delta = ydiff;
Packit 8930e1
					if (!best_delta ||
Packit 8930e1
					    (yval >= miny && yval <= maxy)) {
Packit 8930e1
						best = v;
Packit 8930e1
						break;
Packit 8930e1
					}
Packit 8930e1
				}
Packit 8930e1
				if (!flist_empty(&v->alias))
Packit 8930e1
					v = flist_first_entry(&v->alias, struct graph_value, alias);
Packit 8930e1
			} while (v != rootv);
Packit 8930e1
		} while ((n = prio_tree_next(&iter)) != NULL);
Packit 8930e1
Packit 8930e1
		/*
Packit 8930e1
		 * If we got matches in one label, don't check others.
Packit 8930e1
		 */
Packit 8930e1
		if (best)
Packit 8930e1
			break;
Packit 8930e1
	}
Packit 8930e1
Packit 8930e1
	if (best)
Packit 8930e1
		return best->tooltip;
Packit 8930e1
Packit 8930e1
	return NULL;
Packit 8930e1
}
Packit 8930e1
Packit 8930e1
void graph_set_graph_all_zeroes(struct graph *g, unsigned int set)
Packit 8930e1
{
Packit 8930e1
	g->dont_graph_all_zeroes = !set;
Packit 8930e1
}