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>
32 #include <epan/ws_strsplit.h>
35 #include "stats_tree.h"
39 - sort out the sorting issue
43 /* used to contain the registered stat trees */
44 static GHashTable* registry = NULL;
46 /* writes into the buffers pointed by value, rate and percent
47 the string representations of a node*/
48 extern void stats_tree_get_strs_from_node(const stat_node* node, guint8* value, guint8* rate, guint8* percent) {
51 if (value) g_snprintf(value,NUM_BUF_SIZE,"%u",node->counter);
55 if (node->st->elapsed > 0.0) {
56 f = ((float)node->counter) / (float)node->st->elapsed;
57 g_snprintf(rate,NUM_BUF_SIZE,"%f",f);
63 if (node->parent->counter > 0) {
64 f = (float)(((float)node->counter * 100.0) / node->parent->counter);
65 g_snprintf(percent,NUM_BUF_SIZE,"%.2f%%",f);
71 /* a text representation of a node
72 if buffer is NULL returns a newly allocated string */
73 extern guint8* stats_tree_node_to_str(const stat_node* node,
74 guint8* 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);
83 extern guint stats_tree_branch_max_namelen(const stat_node* node, guint indent) {
88 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
91 for (child = node->children; child; child = child->next ) {
92 len = stats_tree_branch_max_namelen(child,indent+1);
93 maxlen = len > maxlen ? len : maxlen;
97 len = strlen(node->name) + indent;
98 maxlen = len > maxlen ? len : maxlen;
103 static gchar* format;
105 /* populates the given GString with a tree representation of a branch given by node,
106 using indent spaces as initial indentation */
107 extern void stats_tree_branch_to_str(const stat_node* node, GString* s, guint indent) {
109 static gchar indentation[INDENT_MAX+1];
110 static gchar value[NUM_BUF_SIZE];
111 static gchar rate[NUM_BUF_SIZE];
112 static gchar percent[NUM_BUF_SIZE];
117 format = g_strdup_printf(" %%s%%-%us%%12s %%12s %%12s\n",stats_tree_branch_max_namelen(node,0));
120 stats_tree_get_strs_from_node(node, value, rate, percent);
122 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
124 /* fill indentation with indent spaces */
127 indentation[i++] = ' ';
130 indentation[i++] = '\0';
132 g_string_sprintfa(s,format,
133 indentation,node->name,value,rate,percent);
135 if (node->children) {
136 for (child = node->children; child; child = child->next ) {
137 stats_tree_branch_to_str(child,s,indent+1);
147 /* frees the resources allocated by a stat_tree node */
148 static void free_stat_node( stat_node* node ) {
152 if (node->children) {
153 for (child = node->children; child; child = next ) {
154 /* child->next will be gone after free_stat_node, so cache it here */
156 free_stat_node(child);
160 if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
162 if (node->hash) g_hash_table_destroy(node->hash);
164 if (node->rng) g_free(node->rng);
166 if (node->name) g_free(node->name);
171 /* destroys the whole tree instance */
172 extern void stats_tree_free(stats_tree* st) {
177 g_hash_table_destroy(st->names);
178 g_ptr_array_free(st->parents,TRUE);
180 for (child = st->root.children; child; child = next ) {
181 /* child->next will be gone after free_stat_node, so cache it here */
183 free_stat_node(child);
186 if (st->cfg->free_tree_pr)
187 st->cfg->free_tree_pr(st);
189 if (st->cfg->cleanup)
190 st->cfg->cleanup(st);
196 /* reset a node to its original state */
197 static void reset_stat_node(stat_node* node) {
200 if (node->children) {
201 for (child = node->children; child; child = child->next )
202 reset_stat_node(child);
207 if(node->st->cfg->reset_node) {
208 node->st->cfg->reset_node(node);
213 /* reset the whole stats_tree */
214 extern void stats_tree_reset(void* p) {
220 reset_stat_node(&st->root);
222 if (st->cfg->reset_tree) {
223 st->cfg->reset_tree(st);
227 extern void stats_tree_reinit(void* p) {
232 for (child = st->root.children; child; child = next) {
233 /* child->next will be gone after free_stat_node, so cache it here */
235 free_stat_node(child);
238 st->root.children = NULL;
239 st->root.counter = 0;
246 /* register a new stats_tree */
247 extern void stats_tree_register(const guint8* tapname,
250 stat_tree_packet_cb packet,
251 stat_tree_init_cb init,
252 stat_tree_cleanup_cb cleanup) {
254 stats_tree_cfg* cfg = g_malloc( sizeof(stats_tree_cfg) );
256 /* at the very least the abbrev and the packet function should be given */
257 g_assert( tapname && abbr && packet );
259 cfg->tapname = g_strdup(tapname);
260 cfg->abbr = g_strdup(abbr);
261 cfg->name = name ? g_strdup(name) : g_strdup(abbr);
263 cfg->packet = packet;
265 cfg->cleanup = cleanup;
267 /* these have to be filled in by implementations */
268 cfg->setup_node_pr = NULL;
269 cfg->new_tree_pr = NULL;
270 cfg->free_node_pr = NULL;
271 cfg->free_tree_pr = NULL;
272 cfg->draw_node = NULL;
273 cfg->draw_tree = NULL;
274 cfg->reset_node = NULL;
275 cfg->reset_tree = NULL;
277 if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
279 g_hash_table_insert(registry,cfg->abbr,cfg);
283 extern stats_tree* stats_tree_new(stats_tree_cfg* cfg, tree_pres* pr,char* filter) {
284 stats_tree* st = g_malloc(sizeof(stats_tree));
289 st->names = g_hash_table_new(g_str_hash,g_str_equal);
290 st->parents = g_ptr_array_new();
291 st->filter = g_strdup(filter);
296 st->root.counter = 0;
297 st->root.name = g_strdup(cfg->name);
299 st->root.parent = NULL;
300 st->root.children = NULL;
301 st->root.next = NULL;
302 st->root.hash = NULL;
305 g_ptr_array_add(st->parents,&st->root);
310 /* will be the tap packet cb */
311 extern int stats_tree_packet(void* p, packet_info* pinfo, epan_dissect_t *edt, const void *pri) {
313 double now = nstime_to_msec(&pinfo->fd->rel_ts);
315 if (st->start < 0.0) st->start = now;
317 st->elapsed = now - st->start;
320 return st->cfg->packet(st,pinfo,edt,pri);
325 extern stats_tree_cfg* stats_tree_get_cfg_by_abbr(guint8* abbr) {
326 return g_hash_table_lookup(registry,abbr);
330 struct _stats_tree_pres_cbs {
331 void (*setup_node_pr)(stat_node*);
332 void (*free_node_pr)(stat_node*);
333 void (*draw_node)(stat_node*);
334 void (*reset_node)(stat_node*);
335 tree_pres* (*new_tree_pr)(stats_tree*);
336 void (*free_tree_pr)(stats_tree*);
337 void (*draw_tree)(stats_tree*);
338 void (*reset_tree)(stats_tree*);
341 static void setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p) {
342 stats_tree_cfg* cfg = v;
343 struct _stats_tree_pres_cbs *d = p;
346 cfg->setup_node_pr = d->setup_node_pr;
347 cfg->new_tree_pr = d->new_tree_pr;
348 cfg->free_node_pr = d->free_node_pr;
349 cfg->free_tree_pr = d->free_tree_pr;
350 cfg->draw_node = d->draw_node;
351 cfg->draw_tree = d->draw_tree;
352 cfg->reset_node = d->reset_node;
353 cfg->reset_tree = d->reset_tree;
357 extern void stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
358 void (*setup_node_pr)(stat_node*),
359 void (*free_node_pr)(stat_node*),
360 void (*draw_node)(stat_node*),
361 void (*reset_node)(stat_node*),
362 tree_pres* (*new_tree_pr)(stats_tree*),
363 void (*free_tree_pr)(stats_tree*),
364 void (*draw_tree)(stats_tree*),
365 void (*reset_tree)(stats_tree*),
367 static struct _stats_tree_pres_cbs d;
369 d.setup_node_pr = setup_node_pr;
370 d.new_tree_pr = new_tree_pr;
371 d.free_node_pr = free_node_pr;
372 d.free_tree_pr = free_tree_pr;
373 d.draw_node = draw_node;
374 d.draw_tree = draw_tree;
375 d.reset_node = reset_node;
376 d.reset_tree = reset_tree;
378 if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
380 if (registry_iterator && registry)
381 g_hash_table_foreach(registry,registry_iterator,data);
386 /* creates a stat_tree node
387 * name: the name of the stats_tree node
388 * parent_name: the name of the ALREADY REGISTERED parent
389 * with_hash: whether or not it should keep a hash with it's children names
390 * as_named_node: whether or not it has to be registered in the root namespace
392 static stat_node* new_stat_node(stats_tree* st,
396 gboolean as_parent_node) {
398 stat_node *node = g_malloc (sizeof(stat_node));
399 stat_node* last_chld = NULL;
402 node->name = g_strdup(name);
403 node->children = NULL;
405 node->st = (stats_tree*) st;
406 node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
410 if (as_parent_node) {
411 g_hash_table_insert(st->names,
415 g_ptr_array_add(st->parents,node);
417 node->id = st->parents->len - 1;
422 if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
423 node->parent = g_ptr_array_index(st->parents,parent_id);
425 /* ??? should we set the parent to be root ??? */
426 g_assert_not_reached();
429 if (node->parent->children) {
430 /* insert as last child */
432 for (last_chld = node->parent->children;
434 last_chld = last_chld->next ) ;
436 last_chld->next = node;
439 /* insert as first child */
440 node->parent->children = node;
443 if(node->parent->hash) {
444 g_hash_table_insert(node->parent->hash,node->name,node);
447 if (st->cfg->setup_node_pr) {
448 st->cfg->setup_node_pr(node);
457 extern int stats_tree_create_node(stats_tree* st, const gchar* name, int parent_id, gboolean with_hash) {
458 stat_node* node = new_stat_node(st,name,parent_id,with_hash,TRUE);
466 /* XXX: should this be a macro? */
467 extern int stats_tree_create_node_by_pname(stats_tree* st,
469 const gchar* parent_name,
470 gboolean with_children) {
471 return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
477 * Increases by delta the counter of the node whose name is given
478 * if the node does not exist yet it's created (with counter=1)
479 * using parent_name as parent node.
480 * with_hash=TRUE to indicate that the created node will have a parent
482 extern int stats_tree_manip_node(manip_node_mode mode, stats_tree* st, const guint8* name, int parent_id, gboolean with_hash, gint value) {
483 stat_node* node = NULL;
484 stat_node* parent = NULL;
486 g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
488 parent = g_ptr_array_index(st->parents,parent_id);
491 node = g_hash_table_lookup(parent->hash,name);
493 node = g_hash_table_lookup(st->names,name);
497 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
500 case MN_INCREASE: node->counter += value; break;
501 case MN_SET: node->counter = value; break;
511 extern guint8* stats_tree_get_abbr(const guint8* optarg) {
514 /* XXX: this fails when tshark is given any options
516 g_assert(optarg != NULL);
518 for (i=0; optarg[i] && optarg[i] != ','; i++);
520 if (optarg[i] == ',') {
521 return g_strndup(optarg,i);
529 * This function accepts an input string which should define a long integer range.
530 * The normal result is a struct containing the floor and ceil value of this
533 * It is allowed to define a range string in the following ways :
535 * "0-10" -> { 0, 10 }
536 * "-0" -> { G_MININT, 0 }
537 * "0-" -> { 0, G_MAXINT }
538 * "-" -> { G_MININT, G_MAXINT }
540 * Note that this function is robust to buggy input string. If in some cases it
541 * returns NULL, it but may also return a pair with undefined values.
544 static range_pair_t* get_range(guint8* rngstr) {
548 split = g_strsplit((gchar*)rngstr,"-",2);
551 if (split[0] == NULL) {
556 /* means we have a non empty string
557 * which does not contain a delimiter */
558 if (split[1] == NULL) {
563 rng = g_malloc(sizeof(range_pair_t));
565 /* string == "X-?" */
566 if (*(split[0]) != '\0') {
567 rng->floor = strtol(split[0],NULL,10);
570 rng->floor = G_MININT;
573 if (*(split[1]) != '\0') {
574 rng->ceil = strtol(split[1],NULL,10);
577 rng->ceil = G_MAXINT;
585 extern int stats_tree_create_range_node(stats_tree* st,
591 stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
592 stat_node* range_node = NULL;
594 va_start( list, parent_id );
595 while (( curr_range = va_arg(list, guint8*) )) {
596 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
597 range_node->rng = get_range(curr_range);
605 extern int stats_tree_parent_id_by_name(stats_tree* st, const gchar* parent_name) {
606 stat_node* node = g_hash_table_lookup(st->names,parent_name);
611 return 0; /* XXX: this is the root shoud we return -1 instead?*/
615 extern int stats_tree_range_node_with_pname(stats_tree* st,
617 const gchar* parent_name,
621 stat_node* range_node = NULL;
622 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
623 stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
625 va_start( list, parent_name );
626 while (( curr_range = va_arg(list, guint8*) )) {
627 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
628 range_node->rng = get_range(curr_range);
636 extern int stats_tree_tick_range(stats_tree* st,
639 int value_in_range) {
641 stat_node* node = NULL;
642 stat_node* parent = NULL;
643 stat_node* child = NULL;
646 if (parent_id >= 0 && parent_id < (int) st->parents->len) {
647 parent = g_ptr_array_index(st->parents,parent_id);
649 g_assert_not_reached();
653 node = g_hash_table_lookup(parent->hash,name);
655 node = g_hash_table_lookup(st->names,name);
659 g_assert_not_reached();
661 for ( child = node->children; child; child = child->next) {
662 floor = child->rng->floor;
663 ceil = child->rng->ceil;
665 if ( value_in_range >= floor && value_in_range <= ceil ) {
674 extern int stats_tree_create_pivot(stats_tree* st,
677 stat_node* node = new_stat_node(st,name,parent_id,TRUE,TRUE);
685 extern int stats_tree_create_pivot_by_pname(stats_tree* st,
687 const gchar* parent_name) {
688 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
691 node = new_stat_node(st,name,parent_id,TRUE,TRUE);
699 extern int stats_tree_tick_pivot(stats_tree* st,
701 const gchar* pivot_value) {
703 stat_node* parent = g_ptr_array_index(st->parents,pivot_id);
706 stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);