Annotation of embedaddon/bmon/src/graph.c, revision 1.1.1.1
1.1 misho 1: /*
2: * graph.c Graph creation utility
3: *
4: * Copyright (c) 2001-2005 Thomas Graf <tgraf@suug.ch>
5: *
6: * Permission is hereby granted, free of charge, to any person obtaining a
7: * copy of this software and associated documentation files (the "Software"),
8: * to deal in the Software without restriction, including without limitation
9: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22: * DEALINGS IN THE SOFTWARE.
23: */
24:
25: #include <bmon/bmon.h>
26: #include <bmon/graph.h>
27: #include <bmon/input.h>
28: #include <bmon/conf.h>
29: #include <bmon/item.h>
30: #include <bmon/node.h>
31: #include <bmon/conf.h>
32: #include <bmon/utils.h>
33:
34:
35: static void put_col(table_t *t, int data_idx, hist_data_t *src, int hist_idx,
36: double half_step)
37: {
38: int i;
39: char *col = D_AT_COL(t->t_data, data_idx);
40:
41: rate_cnt_t tot = src->hd_data[hist_idx];
42:
43: if (tot == UNK_DATA) {
44: for (i = 0; i < t->t_height; i++)
45: *(D_AT_ROW(col, i)) = get_unk_char();
46: } else if (tot) {
47: *(D_AT_ROW(col, 0)) = ':';
48:
49: for (i = 0; i < t->t_height; i++)
50: if (tot >= (t->t_y_scale[i] - half_step))
51: *(D_AT_ROW(col, i)) = get_fg_char();
52: }
53: }
54:
55: static void create_table(table_t *t, hist_data_t *src, int index, int height,
56: int unit)
57: {
58: int i, di;
59: size_t dsize = height * (HISTORY_SIZE + 1);
60: rate_cnt_t max = 0;
61: double half_step, step;
62:
63: t->t_index = index;
64: t->t_height = height;
65: t->t_y_scale = xcalloc(height, sizeof(double));
66: t->t_data = xcalloc(dsize, sizeof(char));
67:
68: memset(t->t_data, get_bg_char(), dsize);
69:
70: for (i = 0; i < height; i++)
71: *(D_AT_COL(D_AT_ROW(t->t_data, i), HISTORY_SIZE)) = '\0';
72:
73: for (i = 0; i < HISTORY_SIZE; i++)
74: if (max < src->hd_data[i] && src->hd_data[i] != UNK_DATA)
75: max = src->hd_data[i];
76:
77: step = (double) max / (double) height;
78: half_step = step / 2.0f;
79:
80: for (i = 0; i < height; i++)
81: t->t_y_scale[i] = (double) (i + 1) * step;
82:
83: for (di = 0, i = (index - 1); i >= 0; di++, i--)
84: put_col(t, di, src, i, half_step);
85:
86: for (i = (HISTORY_SIZE - 1); di < HISTORY_SIZE; di++, i--)
87: put_col(t, di, src, i, half_step);
88:
89: {
90: b_cnt_t div;
91: int h = (height / 3) * 2;
92:
93: if (h >= height)
94: h = (height - 1);
95:
96: div = get_divisor(t->t_y_scale[h], unit, &t->t_y_unit, NULL);
97:
98: for (i = 0; i < height; i++)
99: t->t_y_scale[i] /= (double) div;
100: }
101: }
102:
103: graph_t * create_graph(hist_elem_t *src, int height, int unit)
104: {
105: graph_t *g;
106:
107: g = xcalloc(1, sizeof(graph_t));
108:
109: create_table(&g->g_rx, &src->he_rx, src->he_index, height, unit);
110: create_table(&g->g_tx, &src->he_tx, src->he_index, height, unit);
111:
112: return g;
113: }
114:
115: graph_t * create_configued_graph(history_t *src, int height, int unit,
116: int x_unit)
117: {
118: graph_t *g;
119: hist_elem_t *e = NULL;
120: char *u = "s";
121: int h = 0;
122:
123: switch (x_unit) {
124: case X_SEC: u = "s"; e = &src->h_sec; break;
125: case X_MIN: u = "m"; e = &src->h_min; break;
126: case X_HOUR: u = "h"; e = &src->h_hour; break;
127: case X_DAY: u = "d"; e = &src->h_day; break;
128: case X_READ: {
129: if (get_read_interval() != 1.0f) {
130: char buf[32];
131: float ri = get_read_interval();
132:
133: snprintf(buf, sizeof(buf), "(%.2fs)", ri);
134: u = strdup(buf);
135: h = 1;
136: e = &src->h_read;
137: } else {
138: u = "s";
139: e = &src->h_sec;
140: }
141: }
142: break;
143: }
144:
145: if (NULL == e)
146: BUG();
147:
148: g = create_graph(e, height, unit);
149:
150: if (h)
151: g->g_flags |= GRAPH_HAS_FREEABLE_X_UNIT;
152:
153: g->g_rx.t_x_unit = u;
154: g->g_tx.t_x_unit = u;
155:
156: return g;
157: }
158:
159: static void free_table(table_t *t)
160: {
161: xfree(t->t_y_scale);
162: xfree(t->t_data);
163: }
164:
165: void free_graph(graph_t *g)
166: {
167: if (g->g_flags & GRAPH_HAS_FREEABLE_X_UNIT)
168: xfree(g->g_rx.t_x_unit);
169:
170: free_table(&g->g_rx);
171: free_table(&g->g_tx);
172: xfree(g);
173: }
174:
175: void new_graph(void)
176: {
177: if (get_ngraphs() >= (MAX_GRAPHS - 1))
178: return;
179: set_ngraphs(get_ngraphs() + 1);
180: }
181:
182: void del_graph(void)
183: {
184: if (get_ngraphs() <= 1)
185: return;
186: set_ngraphs(get_ngraphs() - 1);
187: }
188:
189: int next_graph(void)
190: {
191: item_t *it = get_current_item();
192: if (it == NULL)
193: return EMPTY_LIST;
194:
195: if (it->i_graph_sel >= (get_ngraphs() - 1))
196: it->i_graph_sel = 0;
197: else
198: it->i_graph_sel++;
199:
200: return 0;
201: }
202:
203: int prev_graph(void)
204: {
205: item_t *it = get_current_item();
206: if (it == NULL)
207: return EMPTY_LIST;
208:
209: if (it->i_graph_sel <= 0)
210: it->i_graph_sel = get_ngraphs() - 1;
211: else
212: it->i_graph_sel--;
213:
214: return 0;
215: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>