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.
26 /* stats_tree modifications by Deon van der Westhuysen, November 2013
28 * - sorting by column,
29 * - calculation of average values
30 * - calculation of burst rate
31 * - export to text, CSV or XML file
37 #include <glib/gprintf.h>
41 #include <epan/stats_tree_priv.h>
42 #include <epan/prefs.h>
47 #include "stats_tree.h"
49 enum _stat_tree_columns {
62 /* used to contain the registered stat trees */
63 static GHashTable *registry = NULL;
65 /* a text representation of a node
66 if buffer is NULL returns a newly allocated string */
68 stats_tree_node_to_str(const stat_node *node, gchar *buffer, guint len)
71 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
74 return g_strdup_printf("%s: %i",node->name, node->counter);
79 stats_tree_branch_max_namelen(const stat_node *node, guint indent)
85 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
88 for (child = node->children; child; child = child->next ) {
89 len = stats_tree_branch_max_namelen(child,indent+1);
90 maxlen = len > maxlen ? len : maxlen;
94 if (node->st_flags&ST_FLG_ROOTCHILD) {
95 gchar *display_name= stats_tree_get_displayname(node->name);
96 len = (guint) strlen(display_name) + indent;
100 len = (guint) strlen(node->name) + indent;
102 maxlen = len > maxlen ? len : maxlen;
107 /* frees the resources allocated by a stat_tree node */
109 free_stat_node(stat_node *node)
113 burst_bucket *bucket;
115 if (node->children) {
116 for (child = node->children; child; child = next ) {
117 /* child->next will be gone after free_stat_node, so cache it here */
119 free_stat_node(child);
123 if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
125 if (node->hash) g_hash_table_destroy(node->hash);
129 node->bh = bucket->next;
138 /* destroys the whole tree instance */
140 stats_tree_free(stats_tree *st)
146 g_hash_table_destroy(st->names);
147 g_ptr_array_free(st->parents,TRUE);
148 g_free(st->display_name);
150 for (child = st->root.children; child; child = next ) {
151 /* child->next will be gone after free_stat_node, so cache it here */
153 free_stat_node(child);
156 if (st->cfg->free_tree_pr)
157 st->cfg->free_tree_pr(st);
159 if (st->cfg->cleanup)
160 st->cfg->cleanup(st);
166 /* reset a node to its original state */
168 reset_stat_node(stat_node *node)
171 burst_bucket *bucket;
175 node->minvalue = G_MAXINT;
176 node->maxvalue = G_MININT;
181 node->bh = bucket->next;
184 node->bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
188 node->burst_time = -1.0;
190 if (node->children) {
191 for (child = node->children; child; child = child->next )
192 reset_stat_node(child);
195 if(node->st->cfg->reset_node) {
196 node->st->cfg->reset_node(node);
201 /* reset the whole stats_tree */
203 stats_tree_reset(void *p)
205 stats_tree *st = (stats_tree *)p;
211 reset_stat_node(&st->root);
213 if (st->cfg->reset_tree) {
214 st->cfg->reset_tree(st);
219 stats_tree_reinit(void *p)
221 stats_tree *st = (stats_tree *)p;
225 for (child = st->root.children; child; child = next) {
226 /* child->next will be gone after free_stat_node, so cache it here */
228 free_stat_node(child);
231 st->root.children = NULL;
232 st->root.counter = 0;
234 st->root.minvalue = G_MAXINT;
235 st->root.maxvalue = G_MININT;
236 st->root.st_flags = 0;
238 st->root.bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
239 st->root.bt = st->root.bh;
241 st->root.max_burst = 0;
242 st->root.burst_time = -1.0;
244 /* No more stat_nodes left in tree - clean out hash, array */
245 g_hash_table_remove_all(st->names);
246 if (st->parents->len>1) {
247 g_ptr_array_remove_range(st->parents, 1, st->parents->len-1);
250 /* Do not update st_flags for the tree (sorting) - leave as was */
251 st->num_columns = N_COLUMNS;
252 g_free(st->display_name);
253 st->display_name= stats_tree_get_displayname(st->cfg->name);
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)
267 stats_tree_cfg *cfg = (stats_tree_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 );
273 cfg->tapname = g_strdup(tapname);
274 cfg->abbr = g_strdup(abbr);
275 cfg->name = name ? g_strdup(name) : g_strdup(abbr);
276 cfg->stat_group = stat_group;
278 cfg->packet = packet;
280 cfg->cleanup = cleanup;
282 cfg->flags = flags&~ST_FLG_MASK;
283 cfg->st_flags = flags&ST_FLG_MASK;
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(abbr);
331 stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, const char *filter)
333 stats_tree *st = (stats_tree *)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;
347 st->root.minvalue = G_MAXINT;
348 st->root.maxvalue = G_MININT;
349 st->root.st_flags = 0;
351 st->root.bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
352 st->root.bt = st->root.bh;
354 st->root.max_burst = 0;
355 st->root.burst_time = -1.0;
357 st->root.name = stats_tree_get_displayname(cfg->name);
359 st->root.parent = NULL;
360 st->root.children = NULL;
361 st->root.next = NULL;
362 st->root.hash = NULL;
365 st->st_flags = st->cfg->st_flags;
367 if (!(st->st_flags&ST_FLG_SRTCOL_MASK)) {
368 /* No default sort specified - use preferences */
369 st->st_flags |= prefs.st_sort_defcolflag<<ST_FLG_SRTCOL_SHIFT;
370 if (prefs.st_sort_defdescending) {
371 st->st_flags |= ST_FLG_SORT_DESC;
374 st->num_columns = N_COLUMNS;
375 st->display_name= stats_tree_get_displayname(st->cfg->name);
377 g_ptr_array_add(st->parents,&st->root);
382 /* will be the tap packet cb */
384 stats_tree_packet(void *p, packet_info *pinfo, epan_dissect_t *edt, const void *pri)
386 stats_tree *st = (stats_tree *)p;
388 st->now = nstime_to_msec(&pinfo->rel_ts);
389 if (st->start < 0.0) st->start = st->now;
391 st->elapsed = st->now - st->start;
394 return st->cfg->packet(st,pinfo,edt,pri);
399 extern stats_tree_cfg*
400 stats_tree_get_cfg_by_abbr(const char *abbr)
402 return (stats_tree_cfg *)g_hash_table_lookup(registry,abbr);
406 stats_tree_get_cfg_list(void)
408 return g_hash_table_get_values(registry);
411 struct _stats_tree_pres_cbs {
412 void (*setup_node_pr)(stat_node*);
413 void (*free_node_pr)(stat_node*);
414 void (*draw_node)(stat_node*);
415 void (*reset_node)(stat_node*);
416 tree_pres *(*new_tree_pr)(stats_tree*);
417 void (*free_tree_pr)(stats_tree*);
418 void (*draw_tree)(stats_tree*);
419 void (*reset_tree)(stats_tree*);
423 setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p)
425 stats_tree_cfg *cfg = (stats_tree_cfg *)v;
426 struct _stats_tree_pres_cbs *d = (struct _stats_tree_pres_cbs *)p;
429 cfg->setup_node_pr = d->setup_node_pr;
430 cfg->new_tree_pr = d->new_tree_pr;
431 cfg->free_node_pr = d->free_node_pr;
432 cfg->free_tree_pr = d->free_tree_pr;
433 cfg->draw_node = d->draw_node;
434 cfg->draw_tree = d->draw_tree;
435 cfg->reset_node = d->reset_node;
436 cfg->reset_tree = d->reset_tree;
441 stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
442 void (*setup_node_pr)(stat_node*),
443 void (*free_node_pr)(stat_node*),
444 void (*draw_node)(stat_node*),
445 void (*reset_node)(stat_node*),
446 tree_pres *(*new_tree_pr)(stats_tree*),
447 void (*free_tree_pr)(stats_tree*),
448 void (*draw_tree)(stats_tree*),
449 void (*reset_tree)(stats_tree*),
452 static struct _stats_tree_pres_cbs d;
454 d.setup_node_pr = setup_node_pr;
455 d.new_tree_pr = new_tree_pr;
456 d.free_node_pr = free_node_pr;
457 d.free_tree_pr = free_tree_pr;
458 d.draw_node = draw_node;
459 d.draw_tree = draw_tree;
460 d.reset_node = reset_node;
461 d.reset_tree = reset_tree;
463 if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
465 if (registry_iterator && registry)
466 g_hash_table_foreach(registry,registry_iterator,data);
471 /* creates a stat_tree node
472 * name: the name of the stats_tree node
473 * parent_name: the name of the ALREADY REGISTERED parent
474 * with_hash: whether or not it should keep a hash with its children names
475 * as_named_node: whether or not it has to be registered in the root namespace
478 new_stat_node(stats_tree *st, const gchar *name, int parent_id,
479 gboolean with_hash, gboolean as_parent_node)
482 stat_node *node = (stat_node *)g_malloc (sizeof(stat_node));
483 stat_node *last_chld = NULL;
487 node->minvalue = G_MAXINT;
488 node->maxvalue = G_MININT;
489 node->st_flags = parent_id?0:ST_FLG_ROOTCHILD;
491 node->bh = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
495 node->burst_time = -1.0;
497 node->name = g_strdup(name);
498 node->children = NULL;
500 node->st = (stats_tree*) st;
501 node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
505 if (as_parent_node) {
506 g_hash_table_insert(st->names,
510 g_ptr_array_add(st->parents,node);
512 node->id = st->parents->len - 1;
517 if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
518 node->parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
520 /* ??? should we set the parent to be root ??? */
521 g_assert_not_reached();
524 if (node->parent->children) {
525 /* insert as last child */
527 for (last_chld = node->parent->children;
529 last_chld = last_chld->next ) ;
531 last_chld->next = node;
534 /* insert as first child */
535 node->parent->children = node;
538 if(node->parent->hash) {
539 g_hash_table_insert(node->parent->hash,node->name,node);
542 if (st->cfg->setup_node_pr) {
543 st->cfg->setup_node_pr(node);
553 stats_tree_create_node(stats_tree *st, const gchar *name, int parent_id, gboolean with_hash)
555 stat_node *node = new_stat_node(st,name,parent_id,with_hash,TRUE);
563 /* XXX: should this be a macro? */
565 stats_tree_create_node_by_pname(stats_tree *st, const gchar *name,
566 const gchar *parent_name, gboolean with_children)
568 return stats_tree_create_node(st,name,stats_tree_parent_id_by_name(st,parent_name),with_children);
571 /* Internal function to update the burst calculation data - add entry to bucket */
573 update_burst_calc(stat_node *node, gint value)
575 double current_bucket;
580 if (!prefs.st_enable_burstinfo) {
584 /* NB thebucket list should always contain at least one node - even if it is */
585 /* the dummy created at init time. Head and tail should never be NULL! */
586 current_bucket= floor(node->st->now/prefs.st_burst_resolution);
587 burstwin= prefs.st_burst_windowlen/prefs.st_burst_resolution;
588 if (current_bucket>node->bt->bucket_no) {
589 /* Must add a new bucket at the burst list tail */
590 bn = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
592 bn->bucket_no = current_bucket;
593 bn->start_time = node->st->now;
597 /* And add value to the current burst count for node */
598 node->bcount += value;
599 /* Check if bucket list head is now too old and must be removed */
600 while (current_bucket>=(node->bh->bucket_no+burstwin)) {
601 /* off with its head! */
604 node->bh->prev = NULL;
605 node->bcount -= bn->count;
609 else if (current_bucket<node->bh->bucket_no) {
610 /* Packet must be added at head of burst list - check if not too old */
611 if ((current_bucket+burstwin)>node->bt->bucket_no) {
612 /* packet still within the window */
613 bn = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
615 bn->bucket_no = current_bucket;
616 bn->start_time = node->st->now;
620 /* And add value to the current burst count for node */
621 node->bcount += value;
626 /* Somewhere in the middle... */
627 burst_bucket *search = node->bt;
628 while (current_bucket<search->bucket_no) {
629 search = search->prev;
631 if (current_bucket==search->bucket_no) {
632 /* found existing bucket, increase value */
633 search->count += value;
634 if (search->start_time>node->st->now) {
635 search->start_time = node->st->now;
639 /* must add a new bucket after bn. */
640 bn = (burst_bucket*)g_malloc0(sizeof(burst_bucket));
642 bn->bucket_no = current_bucket;
643 bn->start_time = node->st->now;
645 bn->next = search->next;
649 node->bcount += value;
651 if (node->bcount>node->max_burst) {
652 /* new record burst */
653 node->max_burst = node->bcount;
654 node->burst_time = node->bh->start_time;
659 * Increases by delta the counter of the node whose name is given
660 * if the node does not exist yet it's created (with counter=1)
661 * using parent_name as parent node.
662 * with_hash=TRUE to indicate that the created node will have a parent
665 stats_tree_manip_node(manip_node_mode mode, stats_tree *st, const char *name,
666 int parent_id, gboolean with_hash, gint value)
668 stat_node *node = NULL;
669 stat_node *parent = NULL;
671 g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
673 parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
676 node = (stat_node *)g_hash_table_lookup(parent->hash,name);
678 node = (stat_node *)g_hash_table_lookup(st->names,name);
682 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
686 node->counter += value;
687 update_burst_calc(node, value);
689 case MN_SET: node->counter = value; break;
692 update_burst_calc(node, 1);
693 /* fall through to average code */
694 case MN_AVERAGE_NOTICK:
695 node->total += value;
696 if (node->minvalue > value) {
697 node->minvalue = value;
699 if (node->maxvalue < value) {
700 node->maxvalue = value;
702 node->st_flags |= ST_FLG_AVERAGE;
705 node->st_flags |= value;
708 node->st_flags &= ~value;
720 stats_tree_get_abbr(const char *opt_arg)
724 /* XXX: this fails when tshark is given any options
726 g_assert(opt_arg != NULL);
728 for (i=0; opt_arg[i] && opt_arg[i] != ','; i++);
730 if (opt_arg[i] == ',') {
731 return g_strndup(opt_arg,i);
739 * This function accepts an input string which should define a long integer range.
740 * The normal result is a struct containing the floor and ceil value of this
743 * It is allowed to define a range string in the following ways :
745 * "0-10" -> { 0, 10 }
746 * "-0" -> { G_MININT, 0 }
747 * "0-" -> { 0, G_MAXINT }
748 * "-" -> { G_MININT, G_MAXINT }
750 * Note that this function is robust to buggy input string. If in some cases it
751 * returns NULL, it but may also return a pair with undefined values.
755 get_range(char *rngstr)
760 split = g_strsplit((gchar*)rngstr,"-",2);
763 if (split[0] == NULL) {
768 rng = (range_pair_t *)g_malloc(sizeof(range_pair_t));
770 if (split[1] == NULL) {
771 /* means we have a non empty string with no delimiter
772 * so it must be a single number */
773 rng->floor = (gint)strtol(split[0],NULL,10);
774 rng->ceil = rng->floor;
776 /* string == "X-?" */
777 if (*(split[0]) != '\0') {
778 rng->floor = (gint)strtol(split[0],NULL,10);
781 rng->floor = G_MININT;
784 if (*(split[1]) != '\0') {
785 rng->ceil = (gint)strtol(split[1],NULL,10);
788 rng->ceil = G_MAXINT;
797 stats_tree_create_range_node(stats_tree *st, const gchar *name, int parent_id, ...)
801 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
802 stat_node *range_node = NULL;
804 va_start( list, parent_id );
805 while (( curr_range = va_arg(list, gchar*) )) {
806 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
807 range_node->rng = get_range(curr_range);
815 stats_tree_create_range_node_string(stats_tree *st, const gchar *name,
816 int parent_id, int num_str_ranges,
820 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
821 stat_node *range_node = NULL;
823 for (i = 0; i < num_str_ranges; i++) {
824 range_node = new_stat_node(st, str_ranges[i], rng_root->id, FALSE, FALSE);
825 range_node->rng = get_range(str_ranges[i]);
833 stats_tree_parent_id_by_name(stats_tree *st, const gchar *parent_name)
835 stat_node *node = (stat_node *)g_hash_table_lookup(st->names,parent_name);
840 return 0; /* XXX: this is the root shoud we return -1 instead?*/
845 stats_tree_range_node_with_pname(stats_tree *st, const gchar *name,
846 const gchar *parent_name, ...)
850 stat_node *range_node = NULL;
851 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
852 stat_node *rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
854 va_start( list, parent_name );
855 while (( curr_range = va_arg(list, gchar*) )) {
856 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
857 range_node->rng = get_range(curr_range);
866 stats_tree_tick_range(stats_tree *st, const gchar *name, int parent_id,
870 stat_node *node = NULL;
871 stat_node *parent = NULL;
872 stat_node *child = NULL;
873 gint stat_floor, stat_ceil;
875 if (parent_id >= 0 && parent_id < (int) st->parents->len) {
876 parent = (stat_node *)g_ptr_array_index(st->parents,parent_id);
878 g_assert_not_reached();
882 node = (stat_node *)g_hash_table_lookup(parent->hash,name);
884 node = (stat_node *)g_hash_table_lookup(st->names,name);
888 g_assert_not_reached();
890 /* update stats for container node. counter should already be ticked so we only update total and min/max */
891 node->total += value_in_range;
892 if (node->minvalue > value_in_range) {
893 node->minvalue = value_in_range;
895 if (node->maxvalue < value_in_range) {
896 node->maxvalue = value_in_range;
898 node->st_flags |= ST_FLG_AVERAGE;
900 for ( child = node->children; child; child = child->next) {
901 stat_floor = child->rng->floor;
902 stat_ceil = child->rng->ceil;
904 if ( value_in_range >= stat_floor && value_in_range <= stat_ceil ) {
906 child->total += value_in_range;
907 if (child->minvalue > value_in_range) {
908 child->minvalue = value_in_range;
910 if (child->maxvalue < value_in_range) {
911 child->maxvalue = value_in_range;
913 child->st_flags |= ST_FLG_AVERAGE;
914 update_burst_calc(child, 1);
923 stats_tree_create_pivot(stats_tree *st, const gchar *name, int parent_id)
925 stat_node *node = new_stat_node(st,name,parent_id,TRUE,TRUE);
934 stats_tree_create_pivot_by_pname(stats_tree *st, const gchar *name,
935 const gchar *parent_name)
937 int parent_id = stats_tree_parent_id_by_name(st,parent_name);
940 node = new_stat_node(st,name,parent_id,TRUE,TRUE);
949 stats_tree_tick_pivot(stats_tree *st, int pivot_id, const gchar *pivot_value)
951 stat_node *parent = (stat_node *)g_ptr_array_index(st->parents,pivot_id);
954 update_burst_calc(parent, 1);
955 stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);
961 stats_tree_get_displayname (gchar* fullname)
963 gchar *buf = g_strdup(fullname);
966 if (prefs.st_sort_showfullname) {
967 return buf; /* unmodifed */
971 while ((sep = strchr(sep,'/')) != NULL) {
972 if (*(++sep)=='/') { /* escapeded slash - two slash characters after each other */
973 memmove(sep,sep+1,strlen(sep));
976 /* we got a new path separator */
977 memmove(buf,sep,strlen(sep)+1);
986 stats_tree_get_default_sort_col (stats_tree *st)
988 switch ((st->st_flags&ST_FLG_SRTCOL_MASK)>>ST_FLG_SRTCOL_SHIFT) {
989 case ST_SORT_COL_NAME: return COL_NAME;
990 case ST_SORT_COL_COUNT: return COL_COUNT;
991 case ST_SORT_COL_AVG: return COL_AVERAGE;
992 case ST_SORT_COL_MIN: return COL_MIN;
993 case ST_SORT_COL_MAX: return COL_MAX;
994 case ST_SORT_COL_BURSTRATE: return COL_BURSTRATE;
996 return COL_COUNT; /* nothing specific set */
1000 stats_tree_is_default_sort_DESC (stats_tree *st)
1002 return st->st_flags&ST_FLG_SORT_DESC;
1006 stats_tree_get_column_name (gint col_index)
1008 switch (col_index) {
1009 case COL_NAME: return "Topic / Item";
1010 case COL_COUNT: return "Count";
1011 case COL_AVERAGE: return "Average";
1012 case COL_MIN: return "Min val";
1013 case COL_MAX: return "Max val";
1014 case COL_RATE: return "Rate (ms)";
1015 case COL_PERCENT: return "Percent";
1016 case COL_BURSTRATE: return prefs.st_burst_showcount?"Burst count":"Burst rate";
1017 case COL_BURSTTIME: return "Burst start";
1018 default: return "(Unknown)";
1023 stats_tree_get_column_size (gint col_index)
1025 if (col_index==COL_NAME) {
1026 return 36; /* but caller should really call stats_tree_branch_max_namelen() */
1028 if (col_index<N_COLUMNS) {
1029 return 12; /* all numerical values are this size */
1031 return 0; /* invalid column */
1035 stats_tree_get_values_from_node (const stat_node* node)
1037 gchar **values = (gchar**) g_malloc0(sizeof(gchar*)*(node->st->num_columns));
1039 values[COL_NAME]= (node->st_flags&ST_FLG_ROOTCHILD)?stats_tree_get_displayname(node->name):g_strdup(node->name);
1040 values[COL_COUNT]= g_strdup_printf("%u",node->counter);
1041 values[COL_AVERAGE]= ((node->st_flags&ST_FLG_AVERAGE)||node->rng)?
1042 (node->counter?g_strdup_printf("%.2f",((float)node->total)/node->counter):g_strdup("-")):
1044 values[COL_MIN]= ((node->st_flags&ST_FLG_AVERAGE)||node->rng)?
1045 (node->counter?g_strdup_printf("%u",node->minvalue):g_strdup("-")):
1047 values[COL_MAX]= ((node->st_flags&ST_FLG_AVERAGE)||node->rng)?
1048 (node->counter?g_strdup_printf("%u",node->maxvalue):g_strdup("-")):
1050 values[COL_RATE]= (node->st->elapsed)?g_strdup_printf("%.4f",((float)node->counter)/node->st->elapsed):g_strdup("");
1051 values[COL_PERCENT]= ((node->parent)&&(node->parent->counter))?
1052 g_strdup_printf("%.2f%%",(node->counter*100.0)/node->parent->counter):
1053 (node->parent==&(node->st->root)?g_strdup("100%"):g_strdup(""));
1054 if (node->st->num_columns>COL_BURSTTIME) {
1055 values[COL_BURSTRATE]= (!prefs.st_enable_burstinfo)?g_strdup(""):
1056 (node->max_burst?(prefs.st_burst_showcount?
1057 g_strdup_printf("%d",node->max_burst):
1058 g_strdup_printf("%.4f",((double)node->max_burst)/prefs.st_burst_windowlen)):
1060 values[COL_BURSTTIME]= (!prefs.st_enable_burstinfo)?g_strdup(""):
1061 (node->max_burst?g_strdup_printf("%.3f",((double)node->burst_time/1000.0)):g_strdup("-"));
1067 stats_tree_sort_compare (const stat_node *a, const stat_node *b, gint sort_column,
1068 gboolean sort_descending)
1073 if (prefs.st_sort_rng_nameonly&&(a->rng&&b->rng)) {
1074 /* always sort ranges by range name */
1075 result = a->rng->floor - b->rng->floor;
1076 if (sort_descending&&(!prefs.st_sort_rng_fixorder)) {
1082 switch (sort_column)
1084 case COL_NAME: if (a->rng&&b->rng) {
1085 result = a->rng->floor - b->rng->floor;
1087 else if (prefs.st_sort_casesensitve) {
1088 result = strcmp(a->name,b->name);
1091 result = g_ascii_strcasecmp(a->name,b->name);
1097 case COL_COUNT: result = a->counter - b->counter;
1100 case COL_AVERAGE: if (a->counter) {
1101 result= 1; /* assume a>b */
1103 avg_a= ((float)a->total)/a->counter;
1104 avg_b= ((float)b->total)/b->counter;
1105 result= (avg_a>avg_b)?1:((avg_a<avg_b)?-1:0);
1109 result= -1; /* let b>a */
1113 case COL_MIN: result = a->minvalue - b->minvalue;
1116 case COL_MAX: result = a->maxvalue - b->maxvalue;
1119 case COL_BURSTRATE: result = a->max_burst - b->max_burst;
1122 case COL_BURSTTIME: result = (a->burst_time>b->burst_time)?1:((a->burst_time<b->burst_time)?-1:0);
1126 /* no sort comparison found for column - must update this switch statement */
1127 g_assert_not_reached();
1130 /* break tie between items with same primary search result */
1132 if (sort_column==COL_NAME) {
1133 result = a->counter - b->counter;
1136 if (a->rng&&b->rng) {
1137 result = a->rng->floor - b->rng->floor;
1139 else if (prefs.st_sort_casesensitve) {
1140 result = strcmp(a->name,b->name);
1143 result = g_ascii_strcasecmp(a->name,b->name);
1148 /* take into account sort order */
1149 if (sort_descending) {
1153 if ((a->st_flags&ST_FLG_SORT_TOP)!=(b->st_flags&ST_FLG_SORT_TOP)) {
1154 /* different sort groups top vs non-top */
1155 result= (a->st_flags&ST_FLG_SORT_TOP)?-1:1;
1161 stats_tree_format_as_str(const stats_tree* st, st_format_type format_type,
1162 gint sort_column, gboolean sort_descending)
1164 int maxnamelen= stats_tree_branch_max_namelen(&st->root,0);
1168 gchar *separator = NULL;
1172 case ST_FORMAT_YAML:
1173 s = g_string_new("---\n");
1176 s = g_string_new("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
1179 s = g_string_new("\"level\",\"parent\",");
1180 for (count = 0; count<st->num_columns; count++) {
1181 g_string_append_printf(s,"\"%s\",",stats_tree_get_column_name(count));
1183 g_string_append (s,"\n");
1185 case ST_FORMAT_PLAIN:
1190 sep_length= maxnamelen;
1191 for (count = 1; count<st->num_columns; count++) {
1192 sep_length += stats_tree_get_column_size(count)+2;
1194 separator = (gchar *)g_malloc(sep_length+1);
1195 memset (separator, '=', sep_length);
1196 separator[sep_length] = 0;
1198 s = g_string_new("\n");
1199 g_string_append(s,separator);
1200 g_string_append_printf(s,"\n%s:\n",st->cfg->name);
1201 g_sprintf (fmt,"%%-%us",maxnamelen);
1202 g_string_append_printf(s,fmt,stats_tree_get_column_name(0));
1203 for (count = 1; count<st->num_columns; count++) {
1204 g_sprintf (fmt," %%-%us",stats_tree_get_column_size(count)+1);
1205 g_string_append_printf(s,fmt,stats_tree_get_column_name(count));
1207 memset (separator, '-', sep_length);
1208 g_string_append_printf(s,"\n%s\n",separator);
1212 return g_string_new("unknown format for stats_tree\n");
1215 for (child = st->root.children; child; child = child->next ) {
1216 stats_tree_format_node_as_str(child,s,format_type,0,"",maxnamelen,sort_column,sort_descending);
1220 if (format_type==ST_FORMAT_PLAIN) {
1221 g_string_append_printf(s,"\n%s\n",separator);
1230 gboolean sort_descending;
1233 /* Function to compare elements for child array sort. a and b are children, user_data
1234 points to a st_flags value */
1236 stat_node_array_sortcmp (gconstpointer a, gconstpointer b, gpointer user_data)
1238 /* user_data is *guint value to st_flags */
1239 return stats_tree_sort_compare (*(const stat_node**)a,*(const stat_node**)b,
1240 ((sortinfo*)user_data)->sort_column,((sortinfo*)user_data)->sort_descending);
1244 clean_for_xml_tag (gchar *str)
1247 while ((s=strpbrk(s,"!\"#$%%&'()*+,/;<=>?@[\\]^`{|}~ ")) != NULL) {
1253 /** helper funcation to add note to formatted stats_tree */
1254 WS_DLL_PUBLIC void stats_tree_format_node_as_str(const stat_node *node,
1256 st_format_type format_type,
1261 gboolean sort_descending)
1264 int num_columns= node->st->num_columns;
1265 gchar **values= stats_tree_get_values_from_node(node);
1273 case ST_FORMAT_YAML:
1275 g_sprintf(fmt, "%%%ds%%s%%s", indent*4-2);
1278 strcpy(fmt, "%s%s%s");
1280 g_string_append_printf(s, fmt, "", indent?"- ":"", "Description");
1281 g_string_append_printf(s, ": \"%s\"\n", values[0]);
1283 for (count = 1; count<num_columns; count++) {
1284 if (*values[count]) {
1285 g_string_append_printf(s, fmt, "", indent?" ":"",
1286 stats_tree_get_column_name(count));
1287 g_string_append_printf(s, ": %s\n", values[count]);
1290 if (node->children) {
1291 g_string_append_printf(s, fmt, "", indent?" ":"", "Items:\n");
1296 char *itemname = xml_escape(values[0]);
1297 g_string_append_printf(s,"<stat-node name=\"%s\"%s>\n",itemname,
1298 node->rng?" isrange=\"true\"":"");
1300 for (count = 1; count<num_columns; count++) {
1301 gchar *colname= g_strdup(stats_tree_get_column_name(count));
1302 g_string_append_printf(s,"<%s>",clean_for_xml_tag(colname));
1303 g_string_append_printf(s,"%s</%s>\n",values[count],colname);
1309 g_string_append_printf(s,"%d,\"%s\",\"%s\"",indent,path,values[0]);
1310 for (count = 1; count<num_columns; count++) {
1311 g_string_append_printf(s,",%s",values[count]);
1313 g_string_append (s,"\n");
1315 case ST_FORMAT_PLAIN:
1316 g_sprintf (fmt,"%%%ds%%-%us",indent,maxnamelen-indent);
1317 g_string_append_printf(s,fmt,"",values[0]);
1318 for (count = 1; count<num_columns; count++) {
1319 g_sprintf (fmt," %%-%us",stats_tree_get_column_size(count)+1);
1320 g_string_append_printf(s,fmt,values[count]);
1322 g_string_append (s,"\n");
1327 indent = indent > INDENT_MAX ? INDENT_MAX : indent;
1328 full_path= g_strdup_printf ("%s/%s",path,values[0]);
1330 for (count = 0; count<num_columns; count++) {
1331 g_free(values[count]);
1335 if (node->children) {
1336 GArray *Children= g_array_new(FALSE,FALSE,sizeof(child));
1337 for (child = node->children; child; child = child->next ) {
1338 g_array_append_val(Children,child);
1340 si.sort_column = sort_column;
1341 si.sort_descending = sort_descending;
1342 g_array_sort_with_data(Children,stat_node_array_sortcmp,&si);
1343 for (count = 0; count<((int)Children->len); count++) {
1344 stats_tree_format_node_as_str(g_array_index(Children,stat_node*,count), s, format_type,
1345 indent, full_path, maxnamelen, sort_column, sort_descending);
1347 g_array_free(Children,FALSE);
1351 if (format_type==ST_FORMAT_XML) {
1352 g_string_append(s,"</stat-node>\n");
1362 * indent-tabs-mode: t
1365 * vi: ex: set shiftwidth=4 tabstop=8 noexpandtab:
1366 * :indentSize=4:tabSize=8:noTabs=false: