2 * Netgrind -- Histograms
4 * (c) 2003 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU General Public License.
11 #include "netgrind/pkt.h"
12 #include "netgrind/netgrind.h"
18 static FILE *hist_file;
19 static list hist_list;
20 static uns hist_bucket_start, hist_threshold;
21 static uns hist_bucket_size = 60;
32 struct pkt_stats *var;
33 struct pkt_stats last_state;
42 void histogram_init(byte *name)
44 hist_file = fopen(name, "w");
46 die("Unable to open %s: %m", name);
47 list_init(&hist_list);
50 void histogram_add_stat(byte *name, struct pkt_stats *stat)
52 struct hist_entry *e = xmalloc_zero(sizeof(*e));
53 list_add_tail(&hist_list, &e->n);
59 void histogram_add_int(byte *name, int *var)
61 struct hist_entry *e = xmalloc_zero(sizeof(*e));
62 list_add_tail(&hist_list, &e->n);
68 static void hist_header(void)
71 fprintf(hist_file, "# time");
72 WALK_LIST(e, hist_list)
73 fprintf(hist_file, "\t%s", e->name);
74 fputc('\n', hist_file);
77 static void hist_print(void)
80 fprintf(hist_file, "%d", hist_bucket_start);
81 WALK_LIST(e, hist_list)
85 fprintf(hist_file, "\t%Ld(%Ld)",
86 e->u.st.var->packets - e->u.st.last_state.packets,
87 e->u.st.var->bytes - e->u.st.last_state.bytes);
90 fprintf(hist_file, "\t%d", *e->u.in.var - e->u.in.last_state);
95 fputc('\n', hist_file);
98 void histogram_step(uns time)
100 if (!hist_file || time < hist_threshold)
102 if (!hist_bucket_start)
106 hist_bucket_start = time - (time % hist_bucket_size);
107 hist_threshold = hist_bucket_start + hist_bucket_size;
109 struct hist_entry *e;
110 WALK_LIST(e, hist_list)
114 e->u.st.last_state = *e->u.st.var;
117 e->u.in.last_state = *e->u.in.var;
124 void histogram_cleanup(void)