2 * API for a counter tree for Wireshark
3 * 2004, Luis E. G. Ontanon
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 /* stats_tree modifications by Deon van der Westhuysen, November 2013
14 * - sorting by column,
15 * - calculation of average values
16 * - calculation of burst rate
17 * - export to text, CSV or XML file
26 #include <epan/stats_tree_priv.h>
27 #include <epan/prefs.h>
32 #include "stats_tree.h"
34 enum _stat_tree_columns {
47 /* used to contain the registered stat trees */
48 static GHashTable *registry = NULL;
50 /* a text representation of a node
51 if buffer is NULL returns a newly allocated string */
53 stats_tree_node_to_str(const stat_node *node, gchar *buffer, guint len)
56 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
59 return g_strdup_printf("%s: %i",node->name, node->counter);
64 stats_tree_branch_max_namelen(const stat_node *node, guint indent)
70 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
73 for (child = node->children; child; child = child->next ) {
74 len = stats_tree_branch_max_namelen(child,indent+1);
75 maxlen = len > maxlen ? len : maxlen;
79 if (node->st_flags&ST_FLG_ROOTCHILD) {
80 gchar *display_name = stats_tree_get_displayname(node->name);
81 len = (guint) strlen(display_name) + indent;
85 len = (guint) strlen(node->name) + indent;
87 maxlen = len > maxlen ? len : maxlen;
92 /* frees the resources allocated by a stat_tree node */
94 free_stat_node(stat_node *node)
100 if (node->children) {
101 for (child = node->children; child; child = next ) {
102 /* child->next will be gone after free_stat_node, so cache it here */
104 free_stat_node(child);
108 if (node->hash) g_hash_table_destroy(node->hash);
112 node->bh = bucket->next;
121 /* destroys the whole tree instance */
123 stats_tree_free(stats_tree *st)
131 g_hash_table_destroy(st->names);
132 g_ptr_array_free(st->parents,TRUE);
133 g_free(st->display_name);
135 for (child = st->root.children; child; child = next ) {
136 /* child->next will be gone after free_stat_node, so cache it here */
138 free_stat_node(child);
141 if (st->cfg->free_tree_pr)
142 st->cfg->free_tree_pr(st);
144 if (st->cfg->cleanup)
145 st->cfg->cleanup(st);
151 /* reset a node to its original state */
153 reset_stat_node(stat_node *node)
156 burst_bucket *bucket;
160 node->minvalue = G_MAXINT;
161 node->maxvalue = G_MININT;
166 node->bh = bucket->next;
169 node->bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
173 node->burst_time = -1.0;
175 if (node->children) {
176 for (child = node->children; child; child = child->next )
177 reset_stat_node(child);
181 /* reset the whole stats_tree */
183 stats_tree_reset(void *p)
185 stats_tree *st = (stats_tree *)p;
191 reset_stat_node(&st->root);
195 stats_tree_reinit(void *p)
197 stats_tree *st = (stats_tree *)p;
201 for (child = st->root.children; child; child = next) {
202 /* child->next will be gone after free_stat_node, so cache it here */
204 free_stat_node(child);
207 st->root.children = NULL;
208 st->root.counter = 0;
210 st->root.minvalue = G_MAXINT;
211 st->root.maxvalue = G_MININT;
212 st->root.st_flags = 0;
214 st->root.bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
215 st->root.bt = st->root.bh;
217 st->root.max_burst = 0;
218 st->root.burst_time = -1.0;
220 /* No more stat_nodes left in tree - clean out hash, array */
221 g_hash_table_remove_all(st->names);
222 if (st->parents->len>1) {
223 g_ptr_array_remove_range(st->parents, 1, st->parents->len-1);
226 /* Do not update st_flags for the tree (sorting) - leave as was */
227 st->num_columns = N_COLUMNS;
228 g_free(st->display_name);
229 st->display_name = stats_tree_get_displayname(st->cfg->name);
237 stats_tree_cfg_free(gpointer p)
239 stats_tree_cfg* cfg = (stats_tree_cfg*)p;
240 g_free(cfg->tapname);
246 /* register a new stats_tree */
248 stats_tree_register_with_group(const char *tapname, const char *abbr, const char *name,
250 stat_tree_packet_cb packet, stat_tree_init_cb init,
251 stat_tree_cleanup_cb cleanup, register_stat_group_t stat_group)
253 stats_tree_cfg *cfg = (stats_tree_cfg *)g_malloc0( 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);
261 cfg->stat_group = stat_group;
263 cfg->packet = packet;
265 cfg->cleanup = cleanup;
267 cfg->flags = flags&~ST_FLG_MASK;
268 cfg->st_flags = flags&ST_FLG_MASK;
270 if (!registry) registry = g_hash_table_new_full(g_str_hash,g_str_equal,NULL,stats_tree_cfg_free);
272 g_hash_table_insert(registry,cfg->abbr,cfg);
275 /* register a new stats_tree with default group REGISTER_STAT_GROUP_UNSORTED */
277 stats_tree_register(const char *tapname, const char *abbr, const char *name,
279 stat_tree_packet_cb packet, stat_tree_init_cb init,
280 stat_tree_cleanup_cb cleanup)
282 stats_tree_register_with_group(tapname, abbr, name,
285 cleanup, REGISTER_STAT_GROUP_UNSORTED);
288 /* register a new stat_tree with default group REGISTER_STAT_GROUP_UNSORTED from a plugin */
290 stats_tree_register_plugin(const char *tapname, const char *abbr, const char *name,
292 stat_tree_packet_cb packet, stat_tree_init_cb init,
293 stat_tree_cleanup_cb cleanup)
297 stats_tree_register(tapname, abbr, name,
301 cfg = stats_tree_get_cfg_by_abbr(abbr);
306 stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, const char *filter)
308 stats_tree *st = (stats_tree *)g_malloc0(sizeof(stats_tree));
313 st->names = g_hash_table_new(g_str_hash,g_str_equal);
314 st->parents = g_ptr_array_new();
315 st->filter = g_strdup(filter);
320 st->root.minvalue = G_MAXINT;
321 st->root.maxvalue = G_MININT;
323 st->root.bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
324 st->root.bt = st->root.bh;
325 st->root.burst_time = -1.0;
327 st->root.name = stats_tree_get_displayname(cfg->name);
330 st->st_flags = st->cfg->st_flags;
332 if (!(st->st_flags&ST_FLG_SRTCOL_MASK)) {
333 /* No default sort specified - use preferences */
334 st->st_flags |= prefs.st_sort_defcolflag<<ST_FLG_SRTCOL_SHIFT;
335 if (prefs.st_sort_defdescending) {
336 st->st_flags |= ST_FLG_SORT_DESC;
339 st->num_columns = N_COLUMNS;
340 st->display_name = stats_tree_get_displayname(st->cfg->name);
342 g_ptr_array_add(st->parents,&st->root);
347 /* will be the tap packet cb */
349 stats_tree_packet(void *p, packet_info *pinfo, epan_dissect_t *edt, const void *pri)
351 stats_tree *st = (stats_tree *)p;
353 st->now = nstime_to_msec(&pinfo->rel_ts);
354 if (st->start < 0.0) st->start = st->now;
356 st->elapsed = st->now - st->start;
359 return st->cfg->packet(st,pinfo,edt,pri);
364 extern stats_tree_cfg*
365 stats_tree_get_cfg_by_abbr(const char *abbr)
367 if (!abbr) return NULL;
368 return (stats_tree_cfg *)g_hash_table_lookup(registry,abbr);
372 compare_stat_menu_item(gconstpointer stat_a, gconstpointer stat_b)
374 const stats_tree_cfg* stat_cfg_a = (const stats_tree_cfg*)stat_a;
375 const stats_tree_cfg* stat_cfg_b = (const stats_tree_cfg*)stat_b;
377 return strcmp(stat_cfg_a->name, stat_cfg_b->name);
381 stats_tree_get_cfg_list(void)
383 GList* registry_list = g_hash_table_get_values(registry);
384 /* Now sort the list so they can show up in the
385 menu alphabetically */
386 return g_list_sort(registry_list, compare_stat_menu_item);
390 struct _stats_tree_pres_cbs {
391 void (*setup_node_pr)(stat_node*);
392 void (*free_tree_pr)(stats_tree*);
396 setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p)
398 stats_tree_cfg *cfg = (stats_tree_cfg *)v;
399 struct _stats_tree_pres_cbs *d = (struct _stats_tree_pres_cbs *)p;
401 cfg->setup_node_pr = d->setup_node_pr;
402 cfg->free_tree_pr = d->free_tree_pr;
407 stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
408 void (*setup_node_pr)(stat_node*),
409 void (*free_tree_pr)(stats_tree*),
412 static struct _stats_tree_pres_cbs d;
414 d.setup_node_pr = setup_node_pr;
415 d.free_tree_pr = free_tree_pr;
417 if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
419 if (registry_iterator && registry)
420 g_hash_table_foreach(registry,registry_iterator,data);
425 /* creates a stat_tree node
426 * name: the name of the stats_tree node
427 * parent_name: the name of the ALREADY REGISTERED parent
428 * with_hash: whether or not it should keep a hash with its children names
429 * as_named_node: whether or not it has to be registered in the root namespace
432 new_stat_node(stats_tree *st, const gchar *name, int parent_id,
433 gboolean with_hash, gboolean as_parent_node)
436 stat_node *node = (stat_node *)g_malloc0(sizeof(stat_node));
437 stat_node *last_chld = NULL;
439 node->minvalue = G_MAXINT;
440 node->maxvalue = G_MININT;
441 node->st_flags = parent_id?0:ST_FLG_ROOTCHILD;
443 node->bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
445 node->burst_time = -1.0;
447 node->name = g_strdup(name);
448 node->st = (stats_tree*) st;
449 node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
451 if (as_parent_node) {
452 g_hash_table_insert(st->names,
456 g_ptr_array_add(st->parents,node);
458 node->id = st->parents->len - 1;
463 if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
464 node->parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
466 /* ??? should we set the parent to be root ??? */
467 g_assert_not_reached();
470 if (node->parent->children) {
471 /* insert as last child */
473 for (last_chld = node->parent->children;
475 last_chld = last_chld->next ) ;
477 last_chld->next = node;
480 /* insert as first child */
481 node->parent->children = node;
484 if(node->parent->hash) {
485 g_hash_table_insert(node->parent->hash,node->name,node);
488 if (st->cfg->setup_node_pr) {
489 st->cfg->setup_node_pr(node);
499 stats_tree_create_node(stats_tree *st, const gchar *name, int parent_id, gboolean with_hash)
501 stat_node *node = new_stat_node(st,name,parent_id,with_hash,TRUE);
509 /* XXX: should this be a macro? */
511 stats_tree_create_node_by_pname(stats_tree *st, const gchar *name,
512 const gchar *parent_name, gboolean with_children)
514 return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
517 /* Internal function to update the burst calculation data - add entry to bucket */
519 update_burst_calc(stat_node *node, gint value)
521 double current_bucket;
526 if (!prefs.st_enable_burstinfo) {
530 /* NB thebucket list should always contain at least one node - even if it is */
531 /* the dummy created at init time. Head and tail should never be NULL! */
532 current_bucket = floor(node->st->now/prefs.st_burst_resolution);
533 burstwin = prefs.st_burst_windowlen/prefs.st_burst_resolution;
534 if (current_bucket>node->bt->bucket_no) {
535 /* Must add a new bucket at the burst list tail */
536 bn = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
538 bn->bucket_no = current_bucket;
539 bn->start_time = node->st->now;
543 /* And add value to the current burst count for node */
544 node->bcount += value;
545 /* Check if bucket list head is now too old and must be removed */
546 while (current_bucket>=(node->bh->bucket_no+burstwin)) {
547 /* off with its head! */
550 node->bh->prev = NULL;
551 node->bcount -= bn->count;
555 else if (current_bucket<node->bh->bucket_no) {
556 /* Packet must be added at head of burst list - check if not too old */
557 if ((current_bucket+burstwin)>node->bt->bucket_no) {
558 /* packet still within the window */
559 bn = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
561 bn->bucket_no = current_bucket;
562 bn->start_time = node->st->now;
566 /* And add value to the current burst count for node */
567 node->bcount += value;
572 /* Somewhere in the middle... */
573 burst_bucket *search = node->bt;
574 while (current_bucket<search->bucket_no) {
575 search = search->prev;
577 if (current_bucket==search->bucket_no) {
578 /* found existing bucket, increase value */
579 search->count += value;
580 if (search->start_time>node->st->now) {
581 search->start_time = node->st->now;
585 /* must add a new bucket after bn. */
586 bn = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
588 bn->bucket_no = current_bucket;
589 bn->start_time = node->st->now;
591 bn->next = search->next;
595 node->bcount += value;
597 if (node->bcount>node->max_burst) {
598 /* new record burst */
599 node->max_burst = node->bcount;
600 node->burst_time = node->bh->start_time;
605 * Increases by delta the counter of the node whose name is given
606 * if the node does not exist yet it's created (with counter=1)
607 * using parent_name as parent node.
608 * with_hash=TRUE to indicate that the created node will have a parent
611 stats_tree_manip_node(manip_node_mode mode, stats_tree *st, const char *name,
612 int parent_id, gboolean with_hash, gint value)
614 stat_node *node = NULL;
615 stat_node *parent = NULL;
617 g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
619 parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
622 node = (stat_node *)g_hash_table_lookup(parent->hash,name);
624 node = (stat_node *)g_hash_table_lookup(st->names,name);
628 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
632 node->counter += value;
633 update_burst_calc(node, value);
635 case MN_SET: node->counter = value; break;
638 update_burst_calc(node, 1);
639 /* fall through */ /*to average code */
640 case MN_AVERAGE_NOTICK:
641 node->total += value;
642 if (node->minvalue > value) {
643 node->minvalue = value;
645 if (node->maxvalue < value) {
646 node->maxvalue = value;
648 node->st_flags |= ST_FLG_AVERAGE;
651 node->st_flags |= value;
654 node->st_flags &= ~value;
666 stats_tree_get_abbr(const char *opt_arg)
670 /* XXX: this fails when tshark is given any options
672 g_assert(opt_arg != NULL);
674 for (i=0; opt_arg[i] && opt_arg[i] != ','; i++);
676 if (opt_arg[i] == ',') {
677 return g_strndup(opt_arg,i);
685 * This function accepts an input string which should define a long integer range.
686 * The normal result is a struct containing the floor and ceil value of this
689 * It is allowed to define a range string in the following ways :
691 * "0-10" -> { 0, 10 }
692 * "-0" -> { G_MININT, 0 }
693 * "0-" -> { 0, G_MAXINT }
694 * "-" -> { G_MININT, G_MAXINT }
696 * Note that this function is robust to buggy input string. If in some cases it
697 * returns NULL, it but may also return a pair with undefined values.
701 get_range(char *rngstr)
706 split = g_strsplit((gchar*)rngstr,"-",2);
709 if (split[0] == NULL) {
714 rng = (range_pair_t *)g_malloc(sizeof(range_pair_t));
716 if (split[1] == NULL) {
717 /* means we have a non empty string with no delimiter
718 * so it must be a single number */
719 rng->floor = (gint)strtol(split[0],NULL,10);
720 rng->ceil = rng->floor;
722 /* string == "X-?" */
723 if (*(split[0]) != '\0') {
724 rng->floor = (gint)strtol(split[0],NULL,10);
727 rng->floor = G_MININT;
731 if (*(split[1]) != '\0') {
732 rng->ceil = (gint)strtol(split[1],NULL,10);
735 rng->ceil = G_MAXINT;
745 stats_tree_create_range_node(stats_tree *st, const gchar *name, int parent_id, ...)
749 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
750 stat_node *range_node = NULL;
752 va_start( list, parent_id );
753 while (( curr_range = va_arg(list, gchar*) )) {
754 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
755 range_node->rng = get_range(curr_range);
763 stats_tree_create_range_node_string(stats_tree *st, const gchar *name,
764 int parent_id, int num_str_ranges,
768 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
769 stat_node *range_node = NULL;
771 for (i = 0; i < num_str_ranges - 1; i++) {
772 range_node = new_stat_node(st, str_ranges[i], rng_root->id, FALSE, FALSE);
773 range_node->rng = get_range(str_ranges[i]);
775 range_node = new_stat_node(st, str_ranges[i], rng_root->id, FALSE, FALSE);
776 range_node->rng = get_range(str_ranges[i]);
777 if (range_node->rng->floor == range_node->rng->ceil) {
778 range_node->rng->ceil = G_MAXINT;
786 stats_tree_parent_id_by_name(stats_tree *st, const gchar *parent_name)
788 stat_node *node = (stat_node *)g_hash_table_lookup(st->names,parent_name);
793 return 0; /* XXX: this is the root shoud we return -1 instead?*/
798 stats_tree_range_node_with_pname(stats_tree *st, const gchar *name,
799 const gchar *parent_name, ...)
803 stat_node *range_node = NULL;
804 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
805 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
807 va_start( list, parent_name );
808 while (( curr_range = va_arg(list, gchar*) )) {
809 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
810 range_node->rng = get_range(curr_range);
819 stats_tree_tick_range(stats_tree *st, const gchar *name, int parent_id,
823 stat_node *node = NULL;
824 stat_node *parent = NULL;
825 stat_node *child = NULL;
826 gint stat_floor, stat_ceil;
828 if (parent_id >= 0 && parent_id < (int) st->parents->len) {
829 parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
831 g_assert_not_reached();
835 node = (stat_node *)g_hash_table_lookup(parent->hash,name);
837 node = (stat_node *)g_hash_table_lookup(st->names,name);
841 g_assert_not_reached();
843 /* update stats for container node. counter should already be ticked so we only update total and min/max */
844 node->total += value_in_range;
845 if (node->minvalue > value_in_range) {
846 node->minvalue = value_in_range;
848 if (node->maxvalue < value_in_range) {
849 node->maxvalue = value_in_range;
851 node->st_flags |= ST_FLG_AVERAGE;
853 for ( child = node->children; child; child = child->next) {
854 stat_floor = child->rng->floor;
855 stat_ceil = child->rng->ceil;
857 if ( value_in_range >= stat_floor && value_in_range <= stat_ceil ) {
859 child->total += value_in_range;
860 if (child->minvalue > value_in_range) {
861 child->minvalue = value_in_range;
863 if (child->maxvalue < value_in_range) {
864 child->maxvalue = value_in_range;
866 child->st_flags |= ST_FLG_AVERAGE;
867 update_burst_calc(child, 1);
876 stats_tree_create_pivot(stats_tree *st, const gchar *name, int parent_id)
878 stat_node *node = new_stat_node(st,name,parent_id,TRUE,TRUE);
887 stats_tree_create_pivot_by_pname(stats_tree *st, const gchar *name,
888 const gchar *parent_name)
890 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
893 node = new_stat_node(st,name,parent_id,TRUE,TRUE);
902 stats_tree_tick_pivot(stats_tree *st, int pivot_id, const gchar *pivot_value)
904 stat_node *parent = (stat_node *)g_ptr_array_index(st->parents,pivot_id);
907 update_burst_calc(parent, 1);
908 stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);
914 stats_tree_get_displayname (gchar* fullname)
916 gchar *buf = g_strdup(fullname);
919 if (prefs.st_sort_showfullname) {
920 return buf; /* unmodifed */
924 while ((sep = strchr(sep,'/')) != NULL) {
925 if (*(++sep)=='/') { /* escapeded slash - two slash characters after each other */
926 memmove(sep,sep+1,strlen(sep));
929 /* we got a new path separator */
930 memmove(buf,sep,strlen(sep)+1);
939 stats_tree_get_default_sort_col (stats_tree *st)
941 switch ((st->st_flags&ST_FLG_SRTCOL_MASK)>>ST_FLG_SRTCOL_SHIFT) {
942 case ST_SORT_COL_NAME:
944 case ST_SORT_COL_COUNT:
946 case ST_SORT_COL_AVG:
948 case ST_SORT_COL_MIN:
950 case ST_SORT_COL_MAX:
952 case ST_SORT_COL_BURSTRATE:
953 return COL_BURSTRATE;
955 return COL_COUNT; /* nothing specific set */
959 stats_tree_is_default_sort_DESC (stats_tree *st)
961 return st->st_flags&ST_FLG_SORT_DESC;
965 stats_tree_get_column_name (gint col_index)
969 return "Topic / Item";
983 return prefs.st_burst_showcount?"Burst count":"Burst rate";
985 return "Burst start";
992 stats_tree_get_column_size (gint col_index)
994 if (col_index==COL_NAME) {
995 return 36; /* but caller should really call stats_tree_branch_max_namelen() */
997 if (col_index<N_COLUMNS) {
998 return 12; /* all numerical values are this size */
1000 return 0; /* invalid column */
1004 stats_tree_get_values_from_node (const stat_node* node)
1006 gchar **values = (gchar**) g_malloc0(sizeof(gchar*)*(node->st->num_columns));
1008 values[COL_NAME] = (node->st_flags&ST_FLG_ROOTCHILD)?stats_tree_get_displayname(node->name):g_strdup(node->name);
1009 values[COL_COUNT] = g_strdup_printf("%u",node->counter);
1010 values[COL_AVERAGE] = ((node->st_flags&ST_FLG_AVERAGE)||node->rng)?
1011 (node->counter?g_strdup_printf("%.2f",((float)node->total)/node->counter):g_strdup("-")):
1013 values[COL_MIN] = ((node->st_flags&ST_FLG_AVERAGE)||node->rng)?
1014 (node->counter?g_strdup_printf("%u",node->minvalue):g_strdup("-")):
1016 values[COL_MAX] = ((node->st_flags&ST_FLG_AVERAGE)||node->rng)?
1017 (node->counter?g_strdup_printf("%u",node->maxvalue):g_strdup("-")):
1019 values[COL_RATE] = (node->st->elapsed)?g_strdup_printf("%.4f",((float)node->counter)/node->st->elapsed):g_strdup("");
1020 values[COL_PERCENT] = ((node->parent)&&(node->parent->counter))?
1021 g_strdup_printf("%.2f%%",(node->counter*100.0)/node->parent->counter):
1022 (node->parent==&(node->st->root)?g_strdup("100%"):g_strdup(""));
1023 if (node->st->num_columns>COL_BURSTTIME) {
1024 values[COL_BURSTRATE] = (!prefs.st_enable_burstinfo)?g_strdup(""):
1025 (node->max_burst?(prefs.st_burst_showcount?
1026 g_strdup_printf("%d",node->max_burst):
1027 g_strdup_printf("%.4f",((double)node->max_burst)/prefs.st_burst_windowlen)):
1029 values[COL_BURSTTIME] = (!prefs.st_enable_burstinfo)?g_strdup(""):
1030 (node->max_burst?g_strdup_printf("%.3f",((double)node->burst_time/1000.0)):g_strdup("-"));
1036 stats_tree_sort_compare (const stat_node *a, const stat_node *b, gint sort_column,
1037 gboolean sort_descending)
1042 if (prefs.st_sort_rng_nameonly&&(a->rng&&b->rng)) {
1043 /* always sort ranges by range name */
1044 result = a->rng->floor - b->rng->floor;
1045 if (sort_descending&&(!prefs.st_sort_rng_fixorder)) {
1051 switch (sort_column) {
1053 if (a->rng&&b->rng) {
1054 result = a->rng->floor - b->rng->floor;
1056 else if (prefs.st_sort_casesensitve) {
1057 result = strcmp(a->name,b->name);
1060 result = g_ascii_strcasecmp(a->name,b->name);
1067 result = a->counter - b->counter;
1071 avg_a = a->counter ? ((float)a->total)/a->counter : 0;
1072 avg_b = b->counter ? ((float)b->total)/b->counter : 0;
1073 result = (avg_a>avg_b) ? 1 : ( (avg_a<avg_b) ? -1 : 0);
1077 result = a->minvalue - b->minvalue;
1081 result = a->maxvalue - b->maxvalue;
1085 result = a->max_burst - b->max_burst;
1089 result = (a->burst_time>b->burst_time)?1:((a->burst_time<b->burst_time)?-1:0);
1093 /* no sort comparison found for column - must update this switch statement */
1094 g_assert_not_reached();
1097 /* break tie between items with same primary search result */
1099 if (sort_column==COL_NAME) {
1100 result = a->counter - b->counter;
1103 if (a->rng&&b->rng) {
1104 result = a->rng->floor - b->rng->floor;
1106 else if (prefs.st_sort_casesensitve) {
1107 result = strcmp(a->name,b->name);
1110 result = g_ascii_strcasecmp(a->name,b->name);
1115 /* take into account sort order */
1116 if (sort_descending) {
1120 if ((a->st_flags&ST_FLG_SORT_TOP)!=(b->st_flags&ST_FLG_SORT_TOP)) {
1121 /* different sort groups top vs non-top */
1122 result = (a->st_flags&ST_FLG_SORT_TOP)?-1:1;
1128 stats_tree_format_as_str(const stats_tree* st, st_format_type format_type,
1129 gint sort_column, gboolean sort_descending)
1131 int maxnamelen = stats_tree_branch_max_namelen(&st->root,0);
1135 gchar *separator = NULL;
1137 switch(format_type) {
1138 case ST_FORMAT_YAML:
1139 s = g_string_new("---\n");
1142 s = g_string_new("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1145 s = g_string_new("\"level\",\"parent\",");
1146 for (count = 0; count<st->num_columns; count++) {
1147 g_string_append_printf(s,"\"%s\",",stats_tree_get_column_name(count));
1149 g_string_append (s,"\n");
1151 case ST_FORMAT_PLAIN:
1156 sep_length = maxnamelen;
1157 for (count = 1; count<st->num_columns; count++) {
1158 sep_length += stats_tree_get_column_size(count)+2;
1160 separator = (gchar *)g_malloc(sep_length+1);
1161 memset (separator, '=', sep_length);
1162 separator[sep_length] = 0;
1164 s = g_string_new("\n");
1165 g_string_append(s,separator);
1166 g_string_append_printf(s,"\n%s:\n",st->cfg->name);
1167 g_snprintf (fmt,(gulong)sizeof(fmt),"%%-%us",maxnamelen);
1168 g_string_append_printf(s,fmt,stats_tree_get_column_name(0));
1169 for (count = 1; count<st->num_columns; count++) {
1170 g_snprintf (fmt,(gulong)sizeof(fmt)," %%-%us",stats_tree_get_column_size(count)+1);
1171 g_string_append_printf(s,fmt,stats_tree_get_column_name(count));
1173 memset (separator, '-', sep_length);
1174 g_string_append_printf(s,"\n%s\n",separator);
1178 return g_string_new("unknown format for stats_tree\n");
1181 for (child = st->root.children; child; child = child->next ) {
1182 stats_tree_format_node_as_str(child,s,format_type,0,"",maxnamelen,sort_column,sort_descending);
1186 if (format_type==ST_FORMAT_PLAIN) {
1187 g_string_append_printf(s,"\n%s\n",separator);
1196 gboolean sort_descending;
1199 /* Function to compare elements for child array sort. a and b are children, user_data
1200 points to a st_flags value */
1202 stat_node_array_sortcmp (gconstpointer a, gconstpointer b, gpointer user_data)
1204 /* user_data is *guint value to st_flags */
1205 return stats_tree_sort_compare (*(const stat_node*const*)a,*(const stat_node*const*)b,
1206 ((sortinfo*)user_data)->sort_column,((sortinfo*)user_data)->sort_descending);
1210 clean_for_xml_tag (gchar *str)
1213 while ((s=strpbrk(s,"!\"#$%%&'()*+,/;<=>?@[\\]^`{|}~ ")) != NULL) {
1219 /** helper funcation to add note to formatted stats_tree */
1220 WS_DLL_PUBLIC void stats_tree_format_node_as_str(const stat_node *node,
1222 st_format_type format_type,
1227 gboolean sort_descending)
1230 int num_columns = node->st->num_columns;
1231 gchar **values = stats_tree_get_values_from_node(node);
1235 char fmt[16] = "%s%s%s";
1237 switch(format_type) {
1238 case ST_FORMAT_YAML:
1240 g_snprintf(fmt, (gulong)sizeof(fmt), "%%%ds%%s%%s", indent*4-2);
1242 g_string_append_printf(s, fmt, "", indent?"- ":"", "Description");
1243 g_string_append_printf(s, ": \"%s\"\n", values[0]);
1245 for (count = 1; count<num_columns; count++) {
1246 if (*values[count]) {
1247 g_string_append_printf(s, fmt, "", indent?" ":"",
1248 stats_tree_get_column_name(count));
1249 g_string_append_printf(s, ": %s\n", values[count]);
1252 if (node->children) {
1253 g_string_append_printf(s, fmt, "", indent?" ":"", "Items:\n");
1258 char *itemname = xml_escape(values[0]);
1259 g_string_append_printf(s,"<stat-node name=\"%s\"%s>\n",itemname,
1260 node->rng?" isrange=\"true\"":"");
1262 for (count = 1; count<num_columns; count++) {
1263 gchar *colname = g_strdup(stats_tree_get_column_name(count));
1264 g_string_append_printf(s,"<%s>",clean_for_xml_tag(colname));
1265 g_string_append_printf(s,"%s</%s>\n",values[count],colname);
1271 g_string_append_printf(s,"%d,\"%s\",\"%s\"",indent,path,values[0]);
1272 for (count = 1; count<num_columns; count++) {
1273 g_string_append_printf(s,",%s",values[count]);
1275 g_string_append (s,"\n");
1277 case ST_FORMAT_PLAIN:
1278 g_snprintf (fmt,(gulong)sizeof(fmt),"%%%ds%%-%us",indent,maxnamelen-indent);
1279 g_string_append_printf(s,fmt,"",values[0]);
1280 for (count = 1; count<num_columns; count++) {
1281 g_snprintf (fmt,(gulong)sizeof(fmt)," %%-%us",stats_tree_get_column_size(count)+1);
1282 g_string_append_printf(s,fmt,values[count]);
1284 g_string_append (s,"\n");
1289 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
1290 full_path = g_strdup_printf ("%s/%s",path,values[0]);
1292 for (count = 0; count<num_columns; count++) {
1293 g_free(values[count]);
1297 if (node->children) {
1298 GArray *Children = g_array_new(FALSE,FALSE,sizeof(child));
1299 for (child = node->children; child; child = child->next ) {
1300 g_array_append_val(Children,child);
1302 si.sort_column = sort_column;
1303 si.sort_descending = sort_descending;
1304 g_array_sort_with_data(Children,stat_node_array_sortcmp,&si);
1305 for (count = 0; count<((int)Children->len); count++) {
1306 stats_tree_format_node_as_str(g_array_index(Children,stat_node*,count), s, format_type,
1307 indent, full_path, maxnamelen, sort_column, sort_descending);
1309 g_array_free(Children, TRUE);
1313 if (format_type==ST_FORMAT_XML) {
1314 g_string_append(s,"</stat-node>\n");
1318 void stats_tree_cleanup(void)
1320 g_hash_table_destroy(registry);
1324 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1329 * indent-tabs-mode: nil
1332 * vi: set shiftwidth=4 tabstop=8 expandtab:
1333 * :indentSize=4:tabSize=8:noTabs=true: