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*/
48 stats_tree_get_strs_from_node(const stat_node *node, gchar *value, gchar *rate, gchar *percent)
52 if (value) g_snprintf(value,NUM_BUF_SIZE,"%u",node->counter);
56 if (node->st->elapsed > 0.0) {
57 f = ((float)node->counter) / (float)node->st->elapsed;
58 g_snprintf(rate,NUM_BUF_SIZE,"%f",f);
64 if (node->parent->counter > 0) {
65 f = (float)(((float)node->counter * 100.0) / node->parent->counter);
66 g_snprintf(percent,NUM_BUF_SIZE,"%.2f%%",f);
72 /* a text representation of a node
73 if buffer is NULL returns a newly allocated string */
75 stats_tree_node_to_str(const stat_node *node, gchar *buffer, guint len)
78 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
81 return g_strdup_printf("%s: %i",node->name, node->counter);
86 stats_tree_branch_max_namelen(const stat_node *node, guint indent)
92 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
95 for (child = node->children; child; child = child->next ) {
96 len = stats_tree_branch_max_namelen(child,indent+1);
97 maxlen = len > maxlen ? len : maxlen;
101 len = (guint) strlen(node->name) + indent;
102 maxlen = len > maxlen ? len : maxlen;
107 static gchar *format;
109 /* populates the given GString with a tree representation of a branch given by node,
110 using indent spaces as initial indentation */
112 stats_tree_branch_to_str(const stat_node *node, GString *s, guint indent)
115 static gchar indentation[INDENT_MAX+1];
116 static gchar value[NUM_BUF_SIZE];
117 static gchar rate[NUM_BUF_SIZE];
118 static gchar percent[NUM_BUF_SIZE];
123 format = g_strdup_printf(" %%s%%-%us%%12s %%12s %%12s\n",stats_tree_branch_max_namelen(node,0));
126 stats_tree_get_strs_from_node(node, value, rate, percent);
128 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
130 /* fill indentation with indent spaces */
133 indentation[i++] = ' ';
136 indentation[i++] = '\0';
138 g_string_append_printf(s,format,
139 indentation,node->name,value,rate,percent);
141 if (node->children) {
142 for (child = node->children; child; child = child->next ) {
143 stats_tree_branch_to_str(child,s,indent+1);
153 /* frees the resources allocated by a stat_tree node */
155 free_stat_node(stat_node *node)
160 if (node->children) {
161 for (child = node->children; child; child = next ) {
162 /* child->next will be gone after free_stat_node, so cache it here */
164 free_stat_node(child);
168 if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
170 if (node->hash) g_hash_table_destroy(node->hash);
177 /* destroys the whole tree instance */
179 stats_tree_free(stats_tree *st)
185 g_hash_table_destroy(st->names);
186 g_ptr_array_free(st->parents,TRUE);
188 for (child = st->root.children; child; child = next ) {
189 /* child->next will be gone after free_stat_node, so cache it here */
191 free_stat_node(child);
194 if (st->cfg->free_tree_pr)
195 st->cfg->free_tree_pr(st);
197 if (st->cfg->cleanup)
198 st->cfg->cleanup(st);
204 /* reset a node to its original state */
206 reset_stat_node(stat_node *node)
210 if (node->children) {
211 for (child = node->children; child; child = child->next )
212 reset_stat_node(child);
217 if(node->st->cfg->reset_node) {
218 node->st->cfg->reset_node(node);
223 /* reset the whole stats_tree */
225 stats_tree_reset(void *p)
232 reset_stat_node(&st->root);
234 if (st->cfg->reset_tree) {
235 st->cfg->reset_tree(st);
240 stats_tree_reinit(void *p)
246 for (child = st->root.children; child; child = next) {
247 /* child->next will be gone after free_stat_node, so cache it here */
249 free_stat_node(child);
252 st->root.children = NULL;
253 st->root.counter = 0;
260 /* register a new stats_tree */
262 stats_tree_register_with_group(const char *tapname, const char *abbr, const char *name,
263 stat_tree_packet_cb packet, stat_tree_init_cb init,
264 stat_tree_cleanup_cb cleanup, register_stat_group_t stat_group)
267 stats_tree_cfg *cfg = g_malloc( sizeof(stats_tree_cfg) );
269 /* at the very least the abbrev and the packet function should be given */
270 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;
281 /* these have to be filled in by implementations */
282 cfg->setup_node_pr = NULL;
283 cfg->new_tree_pr = NULL;
284 cfg->free_node_pr = NULL;
285 cfg->free_tree_pr = NULL;
286 cfg->draw_node = NULL;
287 cfg->draw_tree = NULL;
288 cfg->reset_node = NULL;
289 cfg->reset_tree = NULL;
291 if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
293 g_hash_table_insert(registry,cfg->abbr,cfg);
297 /* register a new stats_tree with default group REGISTER_STAT_GROUP_UNSORTED */
299 stats_tree_register(const char *tapname, const char *abbr, const char *name,
300 stat_tree_packet_cb packet, stat_tree_init_cb init,
301 stat_tree_cleanup_cb cleanup)
303 stats_tree_register_with_group(tapname, abbr, name,
305 cleanup, REGISTER_STAT_GROUP_UNSORTED);
309 stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, char *filter)
311 stats_tree *st = g_malloc(sizeof(stats_tree));
316 st->names = g_hash_table_new(g_str_hash,g_str_equal);
317 st->parents = g_ptr_array_new();
318 st->filter = g_strdup(filter);
323 st->root.counter = 0;
324 st->root.name = g_strdup(cfg->name);
326 st->root.parent = NULL;
327 st->root.children = NULL;
328 st->root.next = NULL;
329 st->root.hash = NULL;
332 g_ptr_array_add(st->parents,&st->root);
337 /* will be the tap packet cb */
339 stats_tree_packet(void *p, packet_info *pinfo, epan_dissect_t *edt, const void *pri)
342 double now = nstime_to_msec(&pinfo->fd->rel_ts);
344 if (st->start < 0.0) st->start = now;
346 st->elapsed = now - st->start;
349 return st->cfg->packet(st,pinfo,edt,pri);
354 extern stats_tree_cfg*
355 stats_tree_get_cfg_by_abbr(char *abbr)
357 return g_hash_table_lookup(registry,abbr);
361 struct _stats_tree_pres_cbs {
362 void (*setup_node_pr)(stat_node*);
363 void (*free_node_pr)(stat_node*);
364 void (*draw_node)(stat_node*);
365 void (*reset_node)(stat_node*);
366 tree_pres *(*new_tree_pr)(stats_tree*);
367 void (*free_tree_pr)(stats_tree*);
368 void (*draw_tree)(stats_tree*);
369 void (*reset_tree)(stats_tree*);
373 setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p)
375 stats_tree_cfg *cfg = v;
376 struct _stats_tree_pres_cbs *d = p;
379 cfg->setup_node_pr = d->setup_node_pr;
380 cfg->new_tree_pr = d->new_tree_pr;
381 cfg->free_node_pr = d->free_node_pr;
382 cfg->free_tree_pr = d->free_tree_pr;
383 cfg->draw_node = d->draw_node;
384 cfg->draw_tree = d->draw_tree;
385 cfg->reset_node = d->reset_node;
386 cfg->reset_tree = d->reset_tree;
391 stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
392 void (*setup_node_pr)(stat_node*),
393 void (*free_node_pr)(stat_node*),
394 void (*draw_node)(stat_node*),
395 void (*reset_node)(stat_node*),
396 tree_pres *(*new_tree_pr)(stats_tree*),
397 void (*free_tree_pr)(stats_tree*),
398 void (*draw_tree)(stats_tree*),
399 void (*reset_tree)(stats_tree*),
402 static struct _stats_tree_pres_cbs d;
404 d.setup_node_pr = setup_node_pr;
405 d.new_tree_pr = new_tree_pr;
406 d.free_node_pr = free_node_pr;
407 d.free_tree_pr = free_tree_pr;
408 d.draw_node = draw_node;
409 d.draw_tree = draw_tree;
410 d.reset_node = reset_node;
411 d.reset_tree = reset_tree;
413 if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
415 if (registry_iterator && registry)
416 g_hash_table_foreach(registry,registry_iterator,data);
421 /* creates a stat_tree node
422 * name: the name of the stats_tree node
423 * parent_name: the name of the ALREADY REGISTERED parent
424 * with_hash: whether or not it should keep a hash with it's children names
425 * as_named_node: whether or not it has to be registered in the root namespace
428 new_stat_node(stats_tree *st, const gchar *name, int parent_id,
429 gboolean with_hash, gboolean as_parent_node)
432 stat_node *node = g_malloc (sizeof(stat_node));
433 stat_node *last_chld = NULL;
436 node->name = g_strdup(name);
437 node->children = NULL;
439 node->st = (stats_tree*) st;
440 node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
444 if (as_parent_node) {
445 g_hash_table_insert(st->names,
449 g_ptr_array_add(st->parents,node);
451 node->id = st->parents->len - 1;
456 if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
457 node->parent = g_ptr_array_index(st->parents,parent_id);
459 /* ??? should we set the parent to be root ??? */
460 g_assert_not_reached();
463 if (node->parent->children) {
464 /* insert as last child */
466 for (last_chld = node->parent->children;
468 last_chld = last_chld->next ) ;
470 last_chld->next = node;
473 /* insert as first child */
474 node->parent->children = node;
477 if(node->parent->hash) {
478 g_hash_table_insert(node->parent->hash,node->name,node);
481 if (st->cfg->setup_node_pr) {
482 st->cfg->setup_node_pr(node);
492 stats_tree_create_node(stats_tree *st, const gchar *name, int parent_id, gboolean with_hash)
494 stat_node *node = new_stat_node(st,name,parent_id,with_hash,TRUE);
502 /* XXX: should this be a macro? */
504 stats_tree_create_node_by_pname(stats_tree *st, const gchar *name,
505 const gchar *parent_name, gboolean with_children)
507 return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
513 * Increases by delta the counter of the node whose name is given
514 * if the node does not exist yet it's created (with counter=1)
515 * using parent_name as parent node.
516 * with_hash=TRUE to indicate that the created node will have a parent
519 stats_tree_manip_node(manip_node_mode mode, stats_tree *st, const char *name,
520 int parent_id, gboolean with_hash, gint value)
522 stat_node *node = NULL;
523 stat_node *parent = NULL;
525 g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
527 parent = g_ptr_array_index(st->parents,parent_id);
530 node = g_hash_table_lookup(parent->hash,name);
532 node = g_hash_table_lookup(st->names,name);
536 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
539 case MN_INCREASE: node->counter += value; break;
540 case MN_SET: node->counter = value; break;
551 stats_tree_get_abbr(const char *optarg)
555 /* XXX: this fails when tshark is given any options
557 g_assert(optarg != NULL);
559 for (i=0; optarg[i] && optarg[i] != ','; i++);
561 if (optarg[i] == ',') {
562 return g_strndup(optarg,i);
570 * This function accepts an input string which should define a long integer range.
571 * The normal result is a struct containing the floor and ceil value of this
574 * It is allowed to define a range string in the following ways :
576 * "0-10" -> { 0, 10 }
577 * "-0" -> { G_MININT, 0 }
578 * "0-" -> { 0, G_MAXINT }
579 * "-" -> { G_MININT, G_MAXINT }
581 * Note that this function is robust to buggy input string. If in some cases it
582 * returns NULL, it but may also return a pair with undefined values.
586 get_range(char *rngstr)
591 split = g_strsplit((gchar*)rngstr,"-",2);
594 if (split[0] == NULL) {
599 /* means we have a non empty string
600 * which does not contain a delimiter */
601 if (split[1] == NULL) {
606 rng = g_malloc(sizeof(range_pair_t));
608 /* string == "X-?" */
609 if (*(split[0]) != '\0') {
610 rng->floor = strtol(split[0],NULL,10);
613 rng->floor = G_MININT;
616 if (*(split[1]) != '\0') {
617 rng->ceil = strtol(split[1],NULL,10);
620 rng->ceil = G_MAXINT;
629 stats_tree_create_range_node(stats_tree *st, const gchar *name, int parent_id, ...)
633 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
634 stat_node *range_node = NULL;
636 va_start( list, parent_id );
637 while (( curr_range = va_arg(list, gchar*) )) {
638 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
639 range_node->rng = get_range(curr_range);
648 stats_tree_parent_id_by_name(stats_tree *st, const gchar *parent_name)
650 stat_node *node = g_hash_table_lookup(st->names,parent_name);
655 return 0; /* XXX: this is the root shoud we return -1 instead?*/
660 stats_tree_range_node_with_pname(stats_tree *st, const gchar *name,
661 const gchar *parent_name, ...)
665 stat_node *range_node = NULL;
666 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
667 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
669 va_start( list, parent_name );
670 while (( curr_range = va_arg(list, gchar*) )) {
671 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
672 range_node->rng = get_range(curr_range);
681 stats_tree_tick_range(stats_tree *st, const gchar *name, int parent_id,
685 stat_node *node = NULL;
686 stat_node *parent = NULL;
687 stat_node *child = NULL;
690 if (parent_id >= 0 && parent_id < (int) st->parents->len) {
691 parent = g_ptr_array_index(st->parents,parent_id);
693 g_assert_not_reached();
697 node = g_hash_table_lookup(parent->hash,name);
699 node = g_hash_table_lookup(st->names,name);
703 g_assert_not_reached();
705 for ( child = node->children; child; child = child->next) {
706 floor = child->rng->floor;
707 ceil = child->rng->ceil;
709 if ( value_in_range >= floor && value_in_range <= ceil ) {
719 stats_tree_create_pivot(stats_tree *st, const gchar *name, int parent_id)
721 stat_node *node = new_stat_node(st,name,parent_id,TRUE,TRUE);
730 stats_tree_create_pivot_by_pname(stats_tree *st, const gchar *name,
731 const gchar *parent_name)
733 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
736 node = new_stat_node(st,name,parent_id,TRUE,TRUE);
745 stats_tree_tick_pivot(stats_tree *st, int pivot_id, const gchar *pivot_value)
748 stat_node *parent = g_ptr_array_index(st->parents,pivot_id);
751 stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);