2 * API for a counter tree for Wireshark
3 * 2004, Luis E. G. Ontanon
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <epan/stats_tree_priv.h>
34 #include "stats_tree.h"
38 - sort out the sorting issue
42 /* used to contain the registered stat trees */
43 static GHashTable* registry = NULL;
45 /* writes into the buffers pointed by value, rate and percent
46 the string representations of a node*/
47 extern void stats_tree_get_strs_from_node(const stat_node* node, guint8* value, guint8* rate, guint8* percent) {
50 if (value) g_snprintf(value,NUM_BUF_SIZE,"%u",node->counter);
54 if (node->st->elapsed > 0.0) {
55 f = ((float)node->counter) / (float)node->st->elapsed;
56 g_snprintf(rate,NUM_BUF_SIZE,"%f",f);
62 if (node->parent->counter > 0) {
63 f = (float)(((float)node->counter * 100.0) / node->parent->counter);
64 g_snprintf(percent,NUM_BUF_SIZE,"%.2f%%",f);
70 /* a text representation of a node
71 if buffer is NULL returns a newly allocated string */
72 extern guint8* stats_tree_node_to_str(const stat_node* node,
73 guint8* buffer, guint len) {
75 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
78 return g_strdup_printf("%s: %i",node->name, node->counter);
82 extern guint stats_tree_branch_max_namelen(const stat_node* node, guint indent) {
87 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
90 for (child = node->children; child; child = child->next ) {
91 len = stats_tree_branch_max_namelen(child,indent+1);
92 maxlen = len > maxlen ? len : maxlen;
96 len = strlen(node->name) + indent;
97 maxlen = len > maxlen ? len : maxlen;
102 static gchar* format;
104 /* populates the given GString with a tree representation of a branch given by node,
105 using indent spaces as initial indentation */
106 extern void stats_tree_branch_to_str(const stat_node* node, GString* s, guint indent) {
108 static gchar indentation[INDENT_MAX+1];
109 static gchar value[NUM_BUF_SIZE];
110 static gchar rate[NUM_BUF_SIZE];
111 static gchar percent[NUM_BUF_SIZE];
116 format = g_strdup_printf(" %%s%%-%us%%12s %%12s %%12s\n",stats_tree_branch_max_namelen(node,0));
119 stats_tree_get_strs_from_node(node, value, rate, percent);
121 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
123 /* fill indentation with indent spaces */
126 indentation[i++] = ' ';
129 indentation[i++] = '\0';
131 g_string_sprintfa(s,format,
132 indentation,node->name,value,rate,percent);
134 if (node->children) {
135 for (child = node->children; child; child = child->next ) {
136 stats_tree_branch_to_str(child,s,indent+1);
146 /* frees the resources allocated by a stat_tree node */
147 static void free_stat_node( stat_node* node ) {
151 if (node->children) {
152 for (child = node->children; child; child = next ) {
153 /* child->next will be gone after free_stat_node, so cache it here */
155 free_stat_node(child);
159 if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
161 if (node->hash) g_hash_table_destroy(node->hash);
163 if (node->rng) g_free(node->rng);
165 if (node->name) g_free(node->name);
170 /* destroys the whole tree instance */
171 extern void stats_tree_free(stats_tree* st) {
176 g_hash_table_destroy(st->names);
177 g_ptr_array_free(st->parents,TRUE);
179 for (child = st->root.children; child; child = next ) {
180 /* child->next will be gone after free_stat_node, so cache it here */
182 free_stat_node(child);
185 if (st->cfg->free_tree_pr)
186 st->cfg->free_tree_pr(st);
188 if (st->cfg->cleanup)
189 st->cfg->cleanup(st);
195 /* reset a node to its original state */
196 static void reset_stat_node(stat_node* node) {
199 if (node->children) {
200 for (child = node->children; child; child = child->next )
201 reset_stat_node(child);
206 if(node->st->cfg->reset_node) {
207 node->st->cfg->reset_node(node);
212 /* reset the whole stats_tree */
213 extern void stats_tree_reset(void* p) {
219 reset_stat_node(&st->root);
221 if (st->cfg->reset_tree) {
222 st->cfg->reset_tree(st);
226 extern void stats_tree_reinit(void* p) {
231 for (child = st->root.children; child; child = next) {
232 /* child->next will be gone after free_stat_node, so cache it here */
234 free_stat_node(child);
237 st->root.children = NULL;
238 st->root.counter = 0;
245 /* register a new stats_tree */
246 extern void stats_tree_register(const guint8* tapname,
249 stat_tree_packet_cb packet,
250 stat_tree_init_cb init,
251 stat_tree_cleanup_cb cleanup) {
253 stats_tree_cfg* cfg = g_malloc( sizeof(stats_tree_cfg) );
255 /* at the very least the abbrev and the packet function should be given */
256 g_assert( tapname && abbr && packet );
258 cfg->tapname = g_strdup(tapname);
259 cfg->abbr = g_strdup(abbr);
260 cfg->name = name ? g_strdup(name) : g_strdup(abbr);
262 cfg->packet = packet;
264 cfg->cleanup = cleanup;
266 /* these have to be filled in by implementations */
267 cfg->setup_node_pr = NULL;
268 cfg->new_tree_pr = NULL;
269 cfg->free_node_pr = NULL;
270 cfg->free_tree_pr = NULL;
271 cfg->draw_node = NULL;
272 cfg->draw_tree = NULL;
273 cfg->reset_node = NULL;
274 cfg->reset_tree = NULL;
276 if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
278 g_hash_table_insert(registry,cfg->abbr,cfg);
282 extern stats_tree* stats_tree_new(stats_tree_cfg* cfg, tree_pres* pr,char* filter) {
283 stats_tree* st = g_malloc(sizeof(stats_tree));
288 st->names = g_hash_table_new(g_str_hash,g_str_equal);
289 st->parents = g_ptr_array_new();
290 st->filter = g_strdup(filter);
295 st->root.counter = 0;
296 st->root.name = g_strdup(cfg->name);
298 st->root.parent = NULL;
299 st->root.children = NULL;
300 st->root.next = NULL;
301 st->root.hash = NULL;
304 g_ptr_array_add(st->parents,&st->root);
309 /* will be the tap packet cb */
310 extern int stats_tree_packet(void* p, packet_info* pinfo, epan_dissect_t *edt, const void *pri) {
312 double now = nstime_to_msec(&pinfo->fd->rel_ts);
314 if (st->start < 0.0) st->start = now;
316 st->elapsed = now - st->start;
319 return st->cfg->packet(st,pinfo,edt,pri);
324 extern stats_tree_cfg* stats_tree_get_cfg_by_abbr(guint8* abbr) {
325 return g_hash_table_lookup(registry,abbr);
329 struct _stats_tree_pres_cbs {
330 void (*setup_node_pr)(stat_node*);
331 void (*free_node_pr)(stat_node*);
332 void (*draw_node)(stat_node*);
333 void (*reset_node)(stat_node*);
334 tree_pres* (*new_tree_pr)(stats_tree*);
335 void (*free_tree_pr)(stats_tree*);
336 void (*draw_tree)(stats_tree*);
337 void (*reset_tree)(stats_tree*);
340 static void setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p) {
341 stats_tree_cfg* cfg = v;
342 struct _stats_tree_pres_cbs *d = p;
345 cfg->setup_node_pr = d->setup_node_pr;
346 cfg->new_tree_pr = d->new_tree_pr;
347 cfg->free_node_pr = d->free_node_pr;
348 cfg->free_tree_pr = d->free_tree_pr;
349 cfg->draw_node = d->draw_node;
350 cfg->draw_tree = d->draw_tree;
351 cfg->reset_node = d->reset_node;
352 cfg->reset_tree = d->reset_tree;
356 extern void stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
357 void (*setup_node_pr)(stat_node*),
358 void (*free_node_pr)(stat_node*),
359 void (*draw_node)(stat_node*),
360 void (*reset_node)(stat_node*),
361 tree_pres* (*new_tree_pr)(stats_tree*),
362 void (*free_tree_pr)(stats_tree*),
363 void (*draw_tree)(stats_tree*),
364 void (*reset_tree)(stats_tree*),
366 static struct _stats_tree_pres_cbs d;
368 d.setup_node_pr = setup_node_pr;
369 d.new_tree_pr = new_tree_pr;
370 d.free_node_pr = free_node_pr;
371 d.free_tree_pr = free_tree_pr;
372 d.draw_node = draw_node;
373 d.draw_tree = draw_tree;
374 d.reset_node = reset_node;
375 d.reset_tree = reset_tree;
377 if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
379 if (registry_iterator && registry)
380 g_hash_table_foreach(registry,registry_iterator,data);
385 /* creates a stat_tree node
386 * name: the name of the stats_tree node
387 * parent_name: the name of the ALREADY REGISTERED parent
388 * with_hash: whether or not it should keep a hash with it's children names
389 * as_named_node: whether or not it has to be registered in the root namespace
391 static stat_node* new_stat_node(stats_tree* st,
395 gboolean as_parent_node) {
397 stat_node *node = g_malloc (sizeof(stat_node));
398 stat_node* last_chld = NULL;
401 node->name = g_strdup(name);
402 node->children = NULL;
404 node->st = (stats_tree*) st;
405 node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
409 if (as_parent_node) {
410 g_hash_table_insert(st->names,
414 g_ptr_array_add(st->parents,node);
416 node->id = st->parents->len - 1;
421 if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
422 node->parent = g_ptr_array_index(st->parents,parent_id);
424 /* ??? should we set the parent to be root ??? */
425 g_assert_not_reached();
428 if (node->parent->children) {
429 /* insert as last child */
431 for (last_chld = node->parent->children;
433 last_chld = last_chld->next ) ;
435 last_chld->next = node;
438 /* insert as first child */
439 node->parent->children = node;
442 if(node->parent->hash) {
443 g_hash_table_insert(node->parent->hash,node->name,node);
446 if (st->cfg->setup_node_pr) {
447 st->cfg->setup_node_pr(node);
456 extern int stats_tree_create_node(stats_tree* st, const gchar* name, int parent_id, gboolean with_hash) {
457 stat_node* node = new_stat_node(st,name,parent_id,with_hash,TRUE);
465 /* XXX: should this be a macro? */
466 extern int stats_tree_create_node_by_pname(stats_tree* st,
468 const gchar* parent_name,
469 gboolean with_children) {
470 return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
476 * Increases by delta the counter of the node whose name is given
477 * if the node does not exist yet it's created (with counter=1)
478 * using parent_name as parent node.
479 * with_hash=TRUE to indicate that the created node will have a parent
481 extern int stats_tree_manip_node(manip_node_mode mode, stats_tree* st, const guint8* name, int parent_id, gboolean with_hash, gint value) {
482 stat_node* node = NULL;
483 stat_node* parent = NULL;
485 g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
487 parent = g_ptr_array_index(st->parents,parent_id);
490 node = g_hash_table_lookup(parent->hash,name);
492 node = g_hash_table_lookup(st->names,name);
496 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
499 case MN_INCREASE: node->counter += value; break;
500 case MN_SET: node->counter = value; break;
510 extern guint8* stats_tree_get_abbr(const guint8* optarg) {
513 /* XXX: this fails when tshark is given any options
515 g_assert(optarg != NULL);
517 for (i=0; optarg[i] && optarg[i] != ','; i++);
519 if (optarg[i] == ',') {
520 return g_strndup(optarg,i);
527 static range_pair_t* get_range(guint8* rngstr) {
529 range_pair_t* rng = g_malloc(sizeof(range_pair_t));
531 split = g_strsplit(rngstr,"-",2);
533 rng->floor = strtol(split[0],NULL,10);
534 rng->ceil = strtol(split[1],NULL,10);
536 if (rng->ceil == 0) rng->ceil = G_MAXINT;
537 if (rng->floor == 0) rng->floor = G_MININT;
545 extern int stats_tree_create_range_node(stats_tree* st,
551 stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
552 stat_node* range_node = NULL;
554 va_start( list, parent_id );
555 while (( curr_range = va_arg(list, guint8*) )) {
556 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
557 range_node->rng = get_range(curr_range);
565 extern int stats_tree_parent_id_by_name(stats_tree* st, const gchar* parent_name) {
566 stat_node* node = g_hash_table_lookup(st->names,parent_name);
571 return 0; /* XXX: this is the root shoud we return -1 instead?*/
575 extern int stats_tree_range_node_with_pname(stats_tree* st,
577 const gchar* parent_name,
581 stat_node* range_node = NULL;
582 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
583 stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
585 va_start( list, parent_name );
586 while (( curr_range = va_arg(list, guint8*) )) {
587 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
588 range_node->rng = get_range(curr_range);
596 extern int stats_tree_tick_range(stats_tree* st,
599 int value_in_range) {
601 stat_node* node = NULL;
602 stat_node* parent = NULL;
603 stat_node* child = NULL;
606 if (parent_id >= 0 && parent_id < (int) st->parents->len) {
607 parent = g_ptr_array_index(st->parents,parent_id);
609 g_assert_not_reached();
613 node = g_hash_table_lookup(parent->hash,name);
615 node = g_hash_table_lookup(st->names,name);
619 g_assert_not_reached();
621 for ( child = node->children; child; child = child->next) {
622 floor = child->rng->floor;
623 ceil = child->rng->ceil;
625 if ( value_in_range >= floor && value_in_range <= ceil ) {
634 extern int stats_tree_create_pivot(stats_tree* st,
637 stat_node* node = new_stat_node(st,name,parent_id,TRUE,TRUE);
645 extern int stats_tree_create_pivot_by_pname(stats_tree* st,
647 const gchar* parent_name) {
648 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
651 node = new_stat_node(st,name,parent_id,TRUE,TRUE);
659 extern int stats_tree_tick_pivot(stats_tree* st,
661 const gchar* pivot_value) {
663 stat_node* parent = g_ptr_array_index(st->parents,pivot_id);
666 stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);