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.
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,
264 stat_tree_packet_cb packet, stat_tree_init_cb init,
265 stat_tree_cleanup_cb cleanup, register_stat_group_t stat_group)
268 stats_tree_cfg *cfg = g_malloc( sizeof(stats_tree_cfg) );
270 /* at the very least the abbrev and the packet function should be given */
271 g_assert( tapname && abbr && packet );
274 cfg->tapname = g_strdup(tapname);
275 cfg->abbr = g_strdup(abbr);
276 cfg->name = name ? g_strdup(name) : g_strdup(abbr);
277 cfg->stat_group = stat_group;
279 cfg->packet = packet;
281 cfg->cleanup = cleanup;
285 /* these have to be filled in by implementations */
286 cfg->setup_node_pr = NULL;
287 cfg->new_tree_pr = NULL;
288 cfg->free_node_pr = NULL;
289 cfg->free_tree_pr = NULL;
290 cfg->draw_node = NULL;
291 cfg->draw_tree = NULL;
292 cfg->reset_node = NULL;
293 cfg->reset_tree = NULL;
295 if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
297 g_hash_table_insert(registry,cfg->abbr,cfg);
300 /* register a new stats_tree with default group REGISTER_STAT_GROUP_UNSORTED */
302 stats_tree_register(const char *tapname, const char *abbr, const char *name,
304 stat_tree_packet_cb packet, stat_tree_init_cb init,
305 stat_tree_cleanup_cb cleanup)
307 stats_tree_register_with_group(tapname, abbr, name,
310 cleanup, REGISTER_STAT_GROUP_UNSORTED);
313 /* register a new stat_tree with default group REGISTER_STAT_GROUP_UNSORTED from a plugin */
315 stats_tree_register_plugin(const char *tapname, const char *abbr, const char *name,
317 stat_tree_packet_cb packet, stat_tree_init_cb init,
318 stat_tree_cleanup_cb cleanup)
322 stats_tree_register(tapname, abbr, name,
326 cfg = stats_tree_get_cfg_by_abbr((char*)abbr);
331 stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, const char *filter)
333 stats_tree *st = g_malloc(sizeof(stats_tree));
338 st->names = g_hash_table_new(g_str_hash,g_str_equal);
339 st->parents = g_ptr_array_new();
340 st->filter = g_strdup(filter);
345 st->root.counter = 0;
346 st->root.name = g_strdup(cfg->name);
348 st->root.parent = NULL;
349 st->root.children = NULL;
350 st->root.next = NULL;
351 st->root.hash = NULL;
354 g_ptr_array_add(st->parents,&st->root);
359 /* will be the tap packet cb */
361 stats_tree_packet(void *p, packet_info *pinfo, epan_dissect_t *edt, const void *pri)
364 double now = nstime_to_msec(&pinfo->fd->rel_ts);
366 if (st->start < 0.0) st->start = now;
368 st->elapsed = now - st->start;
371 return st->cfg->packet(st,pinfo,edt,pri);
376 extern stats_tree_cfg*
377 stats_tree_get_cfg_by_abbr(char *abbr)
379 return g_hash_table_lookup(registry,abbr);
383 stats_tree_get_cfg_list(void)
385 return g_hash_table_get_values(registry);
388 struct _stats_tree_pres_cbs {
389 void (*setup_node_pr)(stat_node*);
390 void (*free_node_pr)(stat_node*);
391 void (*draw_node)(stat_node*);
392 void (*reset_node)(stat_node*);
393 tree_pres *(*new_tree_pr)(stats_tree*);
394 void (*free_tree_pr)(stats_tree*);
395 void (*draw_tree)(stats_tree*);
396 void (*reset_tree)(stats_tree*);
400 setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p)
402 stats_tree_cfg *cfg = v;
403 struct _stats_tree_pres_cbs *d = p;
406 cfg->setup_node_pr = d->setup_node_pr;
407 cfg->new_tree_pr = d->new_tree_pr;
408 cfg->free_node_pr = d->free_node_pr;
409 cfg->free_tree_pr = d->free_tree_pr;
410 cfg->draw_node = d->draw_node;
411 cfg->draw_tree = d->draw_tree;
412 cfg->reset_node = d->reset_node;
413 cfg->reset_tree = d->reset_tree;
418 stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
419 void (*setup_node_pr)(stat_node*),
420 void (*free_node_pr)(stat_node*),
421 void (*draw_node)(stat_node*),
422 void (*reset_node)(stat_node*),
423 tree_pres *(*new_tree_pr)(stats_tree*),
424 void (*free_tree_pr)(stats_tree*),
425 void (*draw_tree)(stats_tree*),
426 void (*reset_tree)(stats_tree*),
429 static struct _stats_tree_pres_cbs d;
431 d.setup_node_pr = setup_node_pr;
432 d.new_tree_pr = new_tree_pr;
433 d.free_node_pr = free_node_pr;
434 d.free_tree_pr = free_tree_pr;
435 d.draw_node = draw_node;
436 d.draw_tree = draw_tree;
437 d.reset_node = reset_node;
438 d.reset_tree = reset_tree;
440 if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
442 if (registry_iterator && registry)
443 g_hash_table_foreach(registry,registry_iterator,data);
448 /* creates a stat_tree node
449 * name: the name of the stats_tree node
450 * parent_name: the name of the ALREADY REGISTERED parent
451 * with_hash: whether or not it should keep a hash with it's children names
452 * as_named_node: whether or not it has to be registered in the root namespace
455 new_stat_node(stats_tree *st, const gchar *name, int parent_id,
456 gboolean with_hash, gboolean as_parent_node)
459 stat_node *node = g_malloc (sizeof(stat_node));
460 stat_node *last_chld = NULL;
463 node->name = g_strdup(name);
464 node->children = NULL;
466 node->st = (stats_tree*) st;
467 node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
471 if (as_parent_node) {
472 g_hash_table_insert(st->names,
476 g_ptr_array_add(st->parents,node);
478 node->id = st->parents->len - 1;
483 if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
484 node->parent = g_ptr_array_index(st->parents,parent_id);
486 /* ??? should we set the parent to be root ??? */
487 g_assert_not_reached();
490 if (node->parent->children) {
491 /* insert as last child */
493 for (last_chld = node->parent->children;
495 last_chld = last_chld->next ) ;
497 last_chld->next = node;
500 /* insert as first child */
501 node->parent->children = node;
504 if(node->parent->hash) {
505 g_hash_table_insert(node->parent->hash,node->name,node);
508 if (st->cfg->setup_node_pr) {
509 st->cfg->setup_node_pr(node);
519 stats_tree_create_node(stats_tree *st, const gchar *name, int parent_id, gboolean with_hash)
521 stat_node *node = new_stat_node(st,name,parent_id,with_hash,TRUE);
529 /* XXX: should this be a macro? */
531 stats_tree_create_node_by_pname(stats_tree *st, const gchar *name,
532 const gchar *parent_name, gboolean with_children)
534 return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
540 * Increases by delta the counter of the node whose name is given
541 * if the node does not exist yet it's created (with counter=1)
542 * using parent_name as parent node.
543 * with_hash=TRUE to indicate that the created node will have a parent
546 stats_tree_manip_node(manip_node_mode mode, stats_tree *st, const char *name,
547 int parent_id, gboolean with_hash, gint value)
549 stat_node *node = NULL;
550 stat_node *parent = NULL;
552 g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
554 parent = g_ptr_array_index(st->parents,parent_id);
557 node = g_hash_table_lookup(parent->hash,name);
559 node = g_hash_table_lookup(st->names,name);
563 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
566 case MN_INCREASE: node->counter += value; break;
567 case MN_SET: node->counter = value; break;
578 stats_tree_get_abbr(const char *optarg)
582 /* XXX: this fails when tshark is given any options
584 g_assert(optarg != NULL);
586 for (i=0; optarg[i] && optarg[i] != ','; i++);
588 if (optarg[i] == ',') {
589 return g_strndup(optarg,i);
597 * This function accepts an input string which should define a long integer range.
598 * The normal result is a struct containing the floor and ceil value of this
601 * It is allowed to define a range string in the following ways :
603 * "0-10" -> { 0, 10 }
604 * "-0" -> { G_MININT, 0 }
605 * "0-" -> { 0, G_MAXINT }
606 * "-" -> { G_MININT, G_MAXINT }
608 * Note that this function is robust to buggy input string. If in some cases it
609 * returns NULL, it but may also return a pair with undefined values.
613 get_range(char *rngstr)
618 split = g_strsplit((gchar*)rngstr,"-",2);
621 if (split[0] == NULL) {
626 /* means we have a non empty string
627 * which does not contain a delimiter */
628 if (split[1] == NULL) {
633 rng = g_malloc(sizeof(range_pair_t));
635 /* string == "X-?" */
636 if (*(split[0]) != '\0') {
637 rng->floor = strtol(split[0],NULL,10);
640 rng->floor = G_MININT;
643 if (*(split[1]) != '\0') {
644 rng->ceil = strtol(split[1],NULL,10);
647 rng->ceil = G_MAXINT;
656 stats_tree_create_range_node(stats_tree *st, const gchar *name, int parent_id, ...)
660 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
661 stat_node *range_node = NULL;
663 va_start( list, parent_id );
664 while (( curr_range = va_arg(list, gchar*) )) {
665 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
666 range_node->rng = get_range(curr_range);
674 stats_tree_create_range_node_string(stats_tree *st, const gchar *name,
675 int parent_id, int num_str_ranges,
679 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
680 stat_node *range_node = NULL;
682 for (i = 0; i < num_str_ranges; i++) {
683 range_node = new_stat_node(st, str_ranges[i], rng_root->id, FALSE, FALSE);
684 range_node->rng = get_range(str_ranges[i]);
692 stats_tree_parent_id_by_name(stats_tree *st, const gchar *parent_name)
694 stat_node *node = g_hash_table_lookup(st->names,parent_name);
699 return 0; /* XXX: this is the root shoud we return -1 instead?*/
704 stats_tree_range_node_with_pname(stats_tree *st, const gchar *name,
705 const gchar *parent_name, ...)
709 stat_node *range_node = NULL;
710 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
711 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
713 va_start( list, parent_name );
714 while (( curr_range = va_arg(list, gchar*) )) {
715 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
716 range_node->rng = get_range(curr_range);
725 stats_tree_tick_range(stats_tree *st, const gchar *name, int parent_id,
729 stat_node *node = NULL;
730 stat_node *parent = NULL;
731 stat_node *child = NULL;
734 if (parent_id >= 0 && parent_id < (int) st->parents->len) {
735 parent = g_ptr_array_index(st->parents,parent_id);
737 g_assert_not_reached();
741 node = g_hash_table_lookup(parent->hash,name);
743 node = g_hash_table_lookup(st->names,name);
747 g_assert_not_reached();
749 for ( child = node->children; child; child = child->next) {
750 floor = child->rng->floor;
751 ceil = child->rng->ceil;
753 if ( value_in_range >= floor && value_in_range <= ceil ) {
763 stats_tree_create_pivot(stats_tree *st, const gchar *name, int parent_id)
765 stat_node *node = new_stat_node(st,name,parent_id,TRUE,TRUE);
774 stats_tree_create_pivot_by_pname(stats_tree *st, const gchar *name,
775 const gchar *parent_name)
777 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
780 node = new_stat_node(st,name,parent_id,TRUE,TRUE);
789 stats_tree_tick_pivot(stats_tree *st, int pivot_id, const gchar *pivot_value)
792 stat_node *parent = g_ptr_array_index(st->parents,pivot_id);
795 stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);