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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <epan/stats_tree_priv.h>
32 #include "stats_tree.h"
36 - sort out the sorting issue
40 /* used to contain the registered stat trees */
41 static GHashTable *registry = NULL;
43 /* writes into the buffers pointed by value, rate and percent
44 the string representations of a node*/
46 stats_tree_get_strs_from_node(const stat_node *node, gchar *value, gchar *rate, gchar *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 */
73 stats_tree_node_to_str(const stat_node *node, gchar *buffer, guint len)
76 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
79 return g_strdup_printf("%s: %i",node->name, node->counter);
84 stats_tree_branch_max_namelen(const stat_node *node, guint indent)
90 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
93 for (child = node->children; child; child = child->next ) {
94 len = stats_tree_branch_max_namelen(child,indent+1);
95 maxlen = len > maxlen ? len : maxlen;
99 len = (guint) strlen(node->name) + indent;
100 maxlen = len > maxlen ? len : maxlen;
105 static gchar *format;
107 /* populates the given GString with a tree representation of a branch given by node,
108 using indent spaces as initial indentation */
110 stats_tree_branch_to_str(const stat_node *node, GString *s, guint indent)
113 static gchar indentation[INDENT_MAX+1];
114 static gchar value[NUM_BUF_SIZE];
115 static gchar rate[NUM_BUF_SIZE];
116 static gchar percent[NUM_BUF_SIZE];
121 format = g_strdup_printf(" %%s%%-%us%%12s %%12s %%12s\n",stats_tree_branch_max_namelen(node,0));
124 stats_tree_get_strs_from_node(node, value, rate, percent);
126 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
128 /* fill indentation with indent spaces */
131 indentation[i++] = ' ';
134 indentation[i] = '\0';
136 g_string_append_printf(s,format,
137 indentation,node->name,value,rate,percent);
139 if (node->children) {
140 for (child = node->children; child; child = child->next ) {
141 stats_tree_branch_to_str(child,s,indent+1);
151 /* frees the resources allocated by a stat_tree node */
153 free_stat_node(stat_node *node)
158 if (node->children) {
159 for (child = node->children; child; child = next ) {
160 /* child->next will be gone after free_stat_node, so cache it here */
162 free_stat_node(child);
166 if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
168 if (node->hash) g_hash_table_destroy(node->hash);
175 /* destroys the whole tree instance */
177 stats_tree_free(stats_tree *st)
183 g_hash_table_destroy(st->names);
184 g_ptr_array_free(st->parents,TRUE);
186 for (child = st->root.children; child; child = next ) {
187 /* child->next will be gone after free_stat_node, so cache it here */
189 free_stat_node(child);
192 if (st->cfg->free_tree_pr)
193 st->cfg->free_tree_pr(st);
195 if (st->cfg->cleanup)
196 st->cfg->cleanup(st);
202 /* reset a node to its original state */
204 reset_stat_node(stat_node *node)
208 if (node->children) {
209 for (child = node->children; child; child = child->next )
210 reset_stat_node(child);
215 if(node->st->cfg->reset_node) {
216 node->st->cfg->reset_node(node);
221 /* reset the whole stats_tree */
223 stats_tree_reset(void *p)
225 stats_tree *st = (stats_tree *)p;
230 reset_stat_node(&st->root);
232 if (st->cfg->reset_tree) {
233 st->cfg->reset_tree(st);
238 stats_tree_reinit(void *p)
240 stats_tree *st = (stats_tree *)p;
244 for (child = st->root.children; child; child = next) {
245 /* child->next will be gone after free_stat_node, so cache it here */
247 free_stat_node(child);
250 st->root.children = NULL;
251 st->root.counter = 0;
258 /* register a new stats_tree */
260 stats_tree_register_with_group(const char *tapname, const char *abbr, const char *name,
262 stat_tree_packet_cb packet, stat_tree_init_cb init,
263 stat_tree_cleanup_cb cleanup, register_stat_group_t stat_group)
266 stats_tree_cfg *cfg = (stats_tree_cfg *)g_malloc( sizeof(stats_tree_cfg) );
268 /* at the very least the abbrev and the packet function should be given */
269 g_assert( tapname && abbr && packet );
272 cfg->tapname = g_strdup(tapname);
273 cfg->abbr = g_strdup(abbr);
274 cfg->name = name ? g_strdup(name) : g_strdup(abbr);
275 cfg->stat_group = stat_group;
277 cfg->packet = packet;
279 cfg->cleanup = cleanup;
283 /* these have to be filled in by implementations */
284 cfg->setup_node_pr = NULL;
285 cfg->new_tree_pr = NULL;
286 cfg->free_node_pr = NULL;
287 cfg->free_tree_pr = NULL;
288 cfg->draw_node = NULL;
289 cfg->draw_tree = NULL;
290 cfg->reset_node = NULL;
291 cfg->reset_tree = NULL;
293 if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
295 g_hash_table_insert(registry,cfg->abbr,cfg);
298 /* register a new stats_tree with default group REGISTER_STAT_GROUP_UNSORTED */
300 stats_tree_register(const char *tapname, const char *abbr, const char *name,
302 stat_tree_packet_cb packet, stat_tree_init_cb init,
303 stat_tree_cleanup_cb cleanup)
305 stats_tree_register_with_group(tapname, abbr, name,
308 cleanup, REGISTER_STAT_GROUP_UNSORTED);
311 /* register a new stat_tree with default group REGISTER_STAT_GROUP_UNSORTED from a plugin */
313 stats_tree_register_plugin(const char *tapname, const char *abbr, const char *name,
315 stat_tree_packet_cb packet, stat_tree_init_cb init,
316 stat_tree_cleanup_cb cleanup)
320 stats_tree_register(tapname, abbr, name,
324 cfg = stats_tree_get_cfg_by_abbr((char*)abbr);
329 stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, const char *filter)
331 stats_tree *st = (stats_tree *)g_malloc(sizeof(stats_tree));
336 st->names = g_hash_table_new(g_str_hash,g_str_equal);
337 st->parents = g_ptr_array_new();
338 st->filter = g_strdup(filter);
343 st->root.counter = 0;
344 st->root.name = g_strdup(cfg->name);
346 st->root.parent = NULL;
347 st->root.children = NULL;
348 st->root.next = NULL;
349 st->root.hash = NULL;
352 g_ptr_array_add(st->parents,&st->root);
357 /* will be the tap packet cb */
359 stats_tree_packet(void *p, packet_info *pinfo, epan_dissect_t *edt, const void *pri)
361 stats_tree *st = (stats_tree *)p;
362 double now = nstime_to_msec(&pinfo->rel_ts);
364 if (st->start < 0.0) st->start = now;
366 st->elapsed = now - st->start;
369 return st->cfg->packet(st,pinfo,edt,pri);
374 extern stats_tree_cfg*
375 stats_tree_get_cfg_by_abbr(char *abbr)
377 return (stats_tree_cfg *)g_hash_table_lookup(registry,abbr);
381 stats_tree_get_cfg_list(void)
383 return g_hash_table_get_values(registry);
386 struct _stats_tree_pres_cbs {
387 void (*setup_node_pr)(stat_node*);
388 void (*free_node_pr)(stat_node*);
389 void (*draw_node)(stat_node*);
390 void (*reset_node)(stat_node*);
391 tree_pres *(*new_tree_pr)(stats_tree*);
392 void (*free_tree_pr)(stats_tree*);
393 void (*draw_tree)(stats_tree*);
394 void (*reset_tree)(stats_tree*);
398 setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p)
400 stats_tree_cfg *cfg = (stats_tree_cfg *)v;
401 struct _stats_tree_pres_cbs *d = (struct _stats_tree_pres_cbs *)p;
404 cfg->setup_node_pr = d->setup_node_pr;
405 cfg->new_tree_pr = d->new_tree_pr;
406 cfg->free_node_pr = d->free_node_pr;
407 cfg->free_tree_pr = d->free_tree_pr;
408 cfg->draw_node = d->draw_node;
409 cfg->draw_tree = d->draw_tree;
410 cfg->reset_node = d->reset_node;
411 cfg->reset_tree = d->reset_tree;
416 stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
417 void (*setup_node_pr)(stat_node*),
418 void (*free_node_pr)(stat_node*),
419 void (*draw_node)(stat_node*),
420 void (*reset_node)(stat_node*),
421 tree_pres *(*new_tree_pr)(stats_tree*),
422 void (*free_tree_pr)(stats_tree*),
423 void (*draw_tree)(stats_tree*),
424 void (*reset_tree)(stats_tree*),
427 static struct _stats_tree_pres_cbs d;
429 d.setup_node_pr = setup_node_pr;
430 d.new_tree_pr = new_tree_pr;
431 d.free_node_pr = free_node_pr;
432 d.free_tree_pr = free_tree_pr;
433 d.draw_node = draw_node;
434 d.draw_tree = draw_tree;
435 d.reset_node = reset_node;
436 d.reset_tree = reset_tree;
438 if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
440 if (registry_iterator && registry)
441 g_hash_table_foreach(registry,registry_iterator,data);
446 /* creates a stat_tree node
447 * name: the name of the stats_tree node
448 * parent_name: the name of the ALREADY REGISTERED parent
449 * with_hash: whether or not it should keep a hash with its children names
450 * as_named_node: whether or not it has to be registered in the root namespace
453 new_stat_node(stats_tree *st, const gchar *name, int parent_id,
454 gboolean with_hash, gboolean as_parent_node)
457 stat_node *node = (stat_node *)g_malloc (sizeof(stat_node));
458 stat_node *last_chld = NULL;
461 node->name = g_strdup(name);
462 node->children = NULL;
464 node->st = (stats_tree*) st;
465 node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
469 if (as_parent_node) {
470 g_hash_table_insert(st->names,
474 g_ptr_array_add(st->parents,node);
476 node->id = st->parents->len - 1;
481 if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
482 node->parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
484 /* ??? should we set the parent to be root ??? */
485 g_assert_not_reached();
488 if (node->parent->children) {
489 /* insert as last child */
491 for (last_chld = node->parent->children;
493 last_chld = last_chld->next ) ;
495 last_chld->next = node;
498 /* insert as first child */
499 node->parent->children = node;
502 if(node->parent->hash) {
503 g_hash_table_insert(node->parent->hash,node->name,node);
506 if (st->cfg->setup_node_pr) {
507 st->cfg->setup_node_pr(node);
517 stats_tree_create_node(stats_tree *st, const gchar *name, int parent_id, gboolean with_hash)
519 stat_node *node = new_stat_node(st,name,parent_id,with_hash,TRUE);
527 /* XXX: should this be a macro? */
529 stats_tree_create_node_by_pname(stats_tree *st, const gchar *name,
530 const gchar *parent_name, gboolean with_children)
532 return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
538 * Increases by delta the counter of the node whose name is given
539 * if the node does not exist yet it's created (with counter=1)
540 * using parent_name as parent node.
541 * with_hash=TRUE to indicate that the created node will have a parent
544 stats_tree_manip_node(manip_node_mode mode, stats_tree *st, const char *name,
545 int parent_id, gboolean with_hash, gint value)
547 stat_node *node = NULL;
548 stat_node *parent = NULL;
550 g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
552 parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
555 node = (stat_node *)g_hash_table_lookup(parent->hash,name);
557 node = (stat_node *)g_hash_table_lookup(st->names,name);
561 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
564 case MN_INCREASE: node->counter += value; break;
565 case MN_SET: node->counter = value; break;
576 stats_tree_get_abbr(const char *opt_arg)
580 /* XXX: this fails when tshark is given any options
582 g_assert(opt_arg != NULL);
584 for (i=0; opt_arg[i] && opt_arg[i] != ','; i++);
586 if (opt_arg[i] == ',') {
587 return g_strndup(opt_arg,i);
595 * This function accepts an input string which should define a long integer range.
596 * The normal result is a struct containing the floor and ceil value of this
599 * It is allowed to define a range string in the following ways :
601 * "0-10" -> { 0, 10 }
602 * "-0" -> { G_MININT, 0 }
603 * "0-" -> { 0, G_MAXINT }
604 * "-" -> { G_MININT, G_MAXINT }
606 * Note that this function is robust to buggy input string. If in some cases it
607 * returns NULL, it but may also return a pair with undefined values.
611 get_range(char *rngstr)
616 split = g_strsplit((gchar*)rngstr,"-",2);
619 if (split[0] == NULL) {
624 /* means we have a non empty string
625 * which does not contain a delimiter */
626 if (split[1] == NULL) {
631 rng = (range_pair_t *)g_malloc(sizeof(range_pair_t));
633 /* string == "X-?" */
634 if (*(split[0]) != '\0') {
635 rng->floor = (gint)strtol(split[0],NULL,10);
638 rng->floor = G_MININT;
641 if (*(split[1]) != '\0') {
642 rng->ceil = (gint)strtol(split[1],NULL,10);
645 rng->ceil = G_MAXINT;
654 stats_tree_create_range_node(stats_tree *st, const gchar *name, int parent_id, ...)
658 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
659 stat_node *range_node = NULL;
661 va_start( list, parent_id );
662 while (( curr_range = va_arg(list, gchar*) )) {
663 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
664 range_node->rng = get_range(curr_range);
672 stats_tree_create_range_node_string(stats_tree *st, const gchar *name,
673 int parent_id, int num_str_ranges,
677 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
678 stat_node *range_node = NULL;
680 for (i = 0; i < num_str_ranges; i++) {
681 range_node = new_stat_node(st, str_ranges[i], rng_root->id, FALSE, FALSE);
682 range_node->rng = get_range(str_ranges[i]);
690 stats_tree_parent_id_by_name(stats_tree *st, const gchar *parent_name)
692 stat_node *node = (stat_node *)g_hash_table_lookup(st->names,parent_name);
697 return 0; /* XXX: this is the root shoud we return -1 instead?*/
702 stats_tree_range_node_with_pname(stats_tree *st, const gchar *name,
703 const gchar *parent_name, ...)
707 stat_node *range_node = NULL;
708 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
709 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
711 va_start( list, parent_name );
712 while (( curr_range = va_arg(list, gchar*) )) {
713 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
714 range_node->rng = get_range(curr_range);
723 stats_tree_tick_range(stats_tree *st, const gchar *name, int parent_id,
727 stat_node *node = NULL;
728 stat_node *parent = NULL;
729 stat_node *child = NULL;
732 if (parent_id >= 0 && parent_id < (int) st->parents->len) {
733 parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
735 g_assert_not_reached();
739 node = (stat_node *)g_hash_table_lookup(parent->hash,name);
741 node = (stat_node *)g_hash_table_lookup(st->names,name);
745 g_assert_not_reached();
747 for ( child = node->children; child; child = child->next) {
748 floor = child->rng->floor;
749 ceil = child->rng->ceil;
751 if ( value_in_range >= floor && value_in_range <= ceil ) {
761 stats_tree_create_pivot(stats_tree *st, const gchar *name, int parent_id)
763 stat_node *node = new_stat_node(st,name,parent_id,TRUE,TRUE);
772 stats_tree_create_pivot_by_pname(stats_tree *st, const gchar *name,
773 const gchar *parent_name)
775 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
778 node = new_stat_node(st,name,parent_id,TRUE,TRUE);
787 stats_tree_tick_pivot(stats_tree *st, int pivot_id, const gchar *pivot_value)
790 stat_node *parent = (stat_node *)g_ptr_array_index(st->parents,pivot_id);
793 stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);