change to int the return type of manip_stat_node() so that it can be used as a parent
[obnox/wireshark/wip.git] / epan / stats_tree.c
1 /* stats_tree.c
2  * API for a counter tree for ethereal
3  * 2004, Luis E. G. Ontanon
4  *
5  * $Id$
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
9  * Copyright 1998 Gerald Combs
10  * 
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.
15  * 
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.
20  * 
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/stats_tree_priv.h>
32 #include <string.h>
33
34 /*
35 TODO: 
36    - sort out the sorting issue
37  
38  */
39
40 /* used to contain the registered stat trees */
41 static GHashTable* registry = NULL;
42
43 /* writes into the buffers pointed by value, rate and percent
44    the string representations of a node*/
45 extern void get_strings_from_node(const stat_node* node, guint8* value, guint8* rate, guint8* percent) {
46         float f;
47         
48         if (value) g_snprintf(value,NUM_BUF_SIZE,"%u",node->counter);
49         
50         if (rate) {
51                 *rate = '\0';
52                 if (node->st->elapsed > 0.0) {
53                         f = ((float)node->counter) / node->st->elapsed;
54                         g_snprintf(rate,NUM_BUF_SIZE,"%f",f);
55                 }
56         }
57         
58         if (percent) {
59                 *percent = '\0';
60                 if (node->parent->counter > 0) {
61                         f = ((float)node->counter * 100.0) / node->parent->counter;
62                         g_snprintf(percent,NUM_BUF_SIZE,"%.2f%%",f);
63                 }
64         }
65 }
66
67
68 /* a text representation of a node
69 if buffer is NULL returns a newly allocated string */
70 extern guint8* stat_node_to_str(const stat_node* node,
71                                                                 guint8* buffer, guint len) {
72         if (buffer) {
73                 g_snprintf(buffer,len,"%s: %i",node->name, node->counter);
74                 return buffer;
75         } else {
76                 return g_strdup_printf("%s: %i",node->name, node->counter);
77         }
78 }
79
80 extern guint stats_branch_max_name_len(const stat_node* node, guint indent) {
81         stat_node* child;
82         guint maxlen = 0;
83         guint len;
84         
85         indent = indent > INDENT_MAX ? INDENT_MAX : indent;
86
87         if (node->children) {
88                 for (child = node->children; child; child = child->next ) {
89                         len = stats_branch_max_name_len(child,indent+1); 
90                         maxlen = len > maxlen ? len : maxlen;
91                 }
92         }
93         
94         len = strlen(node->name) + indent;
95         maxlen = len > maxlen ? len : maxlen;
96         
97         return maxlen;
98 }
99
100 static gchar* format;
101
102 /* populates the given GString with a tree representation of a branch given by node,
103 using indent spaces as initial indentation */
104 extern void stat_branch_to_str(const stat_node* node, GString* s, guint indent) {
105         stat_node* child;
106         static gchar indentation[INDENT_MAX+1];
107         static gchar value[NUM_BUF_SIZE];
108         static gchar rate[NUM_BUF_SIZE];
109         static gchar percent[NUM_BUF_SIZE];
110         
111         guint i = 0;
112         
113         if (indent == 0) {
114                 format = g_strdup_printf(" %%s%%-%us%%12s\t%%12s\t%%12s\n",stats_branch_max_name_len(node,0));
115         }
116         
117         get_strings_from_node(node, value, rate, percent);
118         
119         indent = indent > INDENT_MAX ? INDENT_MAX : indent;
120         
121         /* fill indentation with indent spaces */
122         if (indent > 0) {
123                 while(i<indent)
124                         indentation[i++] = ' ';
125         }
126         
127         indentation[i++] = '\0';
128         
129         g_string_sprintfa(s,format,
130                                           indentation,node->name,value,rate,percent);
131                 
132         if (node->children) {
133                 for (child = node->children; child; child = child->next ) {
134                         stat_branch_to_str(child,s,indent+1);
135                 }
136         }
137         
138         if (indent == 0) {
139                 g_free(format);
140         }
141 }
142
143
144 /* frees the resources allocated by a stat_tree node */
145 static void free_stat_node( stat_node* node ) {
146         stat_node* child;
147         
148         if (node->children) {
149                 for (child = node->children; child; child = child->next ) 
150                         free_stat_node(child);
151         }
152         
153         if(node->st->free_node_pr) node->st->free_node_pr(node);
154         
155         if (node->hash) g_hash_table_destroy(node->hash);
156         if (node->rng) g_free(node->rng);
157         
158         if (node->name) g_free(node->name);
159         
160         g_free(node);
161 }
162
163 /* destroys the whole tree */
164 extern void free_stats_tree(stats_tree* st) {
165         stat_node* child;
166         
167         g_free(st->tapname);
168         g_free(st->abbr);
169         g_free(st->filter);
170         
171         for (child = st->root.children; child; child = child->next ) 
172                 free_stat_node(child);
173         
174         if (st->free_tree_pr)
175                 st->free_tree_pr(st);
176                         
177         g_free(st);
178 }
179
180
181 /* reset a node to its original state */
182 static void reset_stat_node(stat_node* node) {
183         stat_node* child;
184         
185         if (node->children) {
186                 for (child = node->children; child; child = child->next ) 
187                         reset_stat_node(child);
188         }
189         
190         node->counter = 0;
191         
192         if(node->st->reset_node) {
193                 node->st->reset_node(node);
194         }
195         
196 }
197
198 /* reset the whole stats_tree */
199 extern void reset_stats_tree(void* p) {
200         stats_tree* st = p;
201         reset_stat_node(&st->root);
202         
203         if (st->reset_tree) {
204                 st->reset_tree(st);
205         }
206 }
207
208 extern void reinit_stats_tree(void* p) {
209         stats_tree* st = p;
210         stat_node* child;
211         
212         for (child = st->root.children; child; child = child->next) {
213                 free_stat_node(child);
214         }
215         
216         if (st->init) {
217                 st->init(st);
218         }
219 }
220
221 /* register a new stats_tree */
222 extern void register_stats_tree(guint8* tapname,
223                                                                 guint8* abbr, 
224                                                                 guint8* name,
225                                                                 stat_tree_packet_cb packet,
226                                                                 stat_tree_init_cb init ) {
227         
228         stats_tree* st = g_malloc( sizeof(stats_tree) );
229
230         /* at the very least the abbrev and the packet function should be given */ 
231         g_assert( tapname && abbr && packet );
232
233         st->tapname = g_strdup(tapname);
234         st->abbr = g_strdup(abbr);
235         st->name = name ? g_strdup(name) : g_strdup(abbr);
236         st->filter = NULL;
237         
238         st->root.counter = 0;
239         st->root.name = g_strdup(name);
240         st->root.st = st;
241         st->root.parent = NULL;
242         st->root.children = NULL;
243         st->root.next = NULL;
244         st->root.hash = NULL;
245         st->root.pr = NULL;
246         
247         st->names = g_hash_table_new(g_str_hash,g_str_equal);
248         st->parents = g_ptr_array_new();
249         
250         g_ptr_array_add(st->parents,&st->root);
251         
252         st->start = -1.0;
253         st->elapsed = 0.0;
254         
255         st->packet = packet;
256         st->init = init;
257         
258         /* these have to be filled in by implementations */
259         st->setup_node_pr = NULL;
260         st->new_tree_pr = NULL;
261         st->free_node_pr = NULL;
262         st->free_tree_pr = NULL;
263         st->draw_node = NULL;
264         st->draw_tree = NULL;
265         st->reset_node = NULL;
266         st->reset_tree = NULL;
267
268         if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
269
270         g_hash_table_insert(registry,st->abbr,st);
271         
272 }
273
274 /* will be the tap packet cb */
275 extern int stats_tree_packet(void* p, packet_info* pinfo, epan_dissect_t *edt, const void *pri) {
276         stats_tree* st = p;
277         
278         float now = (((float)pinfo->fd->rel_secs) + (((float)pinfo->fd->rel_usecs)/1000000) );
279         
280         if (st->start < 0.0) st->start = now;
281         
282         st->elapsed = now - st->start;
283         
284         if (st->packet)
285                 return st->packet(st,pinfo,edt,pri);
286         else
287                 return 0;
288 }
289
290 extern GHashTable* stat_tree_registry(void) {
291         return registry;
292 }
293
294 extern stats_tree* get_stats_tree_by_abbr(guint8* abbr) {
295         return g_hash_table_lookup(registry,abbr);
296 }
297
298
299 struct _stats_tree_pres_stuff {
300         void (*setup_node_pr)(stat_node*);
301         void (*free_node_pr)(stat_node*);
302         void (*draw_node)(stat_node*);
303         void (*reset_node)(stat_node*);
304         tree_pres* (*new_tree_pr)(stats_tree*);
305         void (*free_tree_pr)(stats_tree*);
306         void (*draw_tree)(stats_tree*);
307         void (*reset_tree)(stats_tree*);
308 };
309
310 static void setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p) {
311         stats_tree* st = v;
312         struct _stats_tree_pres_stuff *d = p;
313         
314         st->setup_node_pr = d->setup_node_pr;
315         st->new_tree_pr = d->new_tree_pr;
316         st->free_node_pr = d->free_node_pr;
317         st->free_tree_pr = d->free_tree_pr;
318         st->draw_node = d->draw_node;
319         st->draw_tree = d->draw_tree;
320         st->reset_node = d->reset_node;
321         st->reset_tree = d->reset_tree;
322         
323 }
324
325 extern void stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
326                                                                         void (*setup_node_pr)(stat_node*),
327                                                                         void (*free_node_pr)(stat_node*),
328                                                                         void (*draw_node)(stat_node*),
329                                                                         void (*reset_node)(stat_node*),
330                                                                         tree_pres* (*new_tree_pr)(stats_tree*),
331                                                                         void (*free_tree_pr)(stats_tree*),
332                                                                         void (*draw_tree)(stats_tree*),
333                                                                         void (*reset_tree)(stats_tree*),
334                                                                         void* data) {
335         struct _stats_tree_pres_stuff d = {setup_node_pr,free_node_pr,draw_node,reset_node,new_tree_pr,free_tree_pr,draw_tree,reset_tree};
336         
337         if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
338         
339         if (registry_iterator && registry)
340                 g_hash_table_foreach(registry,registry_iterator,data);
341         
342 }
343
344
345 /* creates a stat_tree node
346 *    name: the name of the stats_tree node
347 *    parent_name: the name of the ALREADY REGISTERED parent
348 *    with_hash: whether or not it should keep a hash with it's children names
349 *    as_named_node: whether or not it has to be registered in the root namespace
350 */
351 static stat_node*  new_stat_node(stats_tree* st,
352                                                                  const gchar* name,
353                                                                  int parent_id,
354                                                                  gboolean with_hash,
355                                                                  gboolean as_parent_node) {
356         
357         stat_node *node = g_malloc (sizeof(stat_node));
358         stat_node* last_chld = NULL;
359         
360         node->counter = 0;
361         node->name = g_strdup(name);
362         node->children = NULL;
363         node->next = NULL;
364         node->st = (stats_tree*) st;
365         node->hash = with_hash ? g_hash_table_new(g_str_hash,g_str_equal) : NULL;
366         node->parent = NULL;
367         
368         if (as_parent_node) {
369                 g_hash_table_insert(st->names,
370                                                         node->name,
371                                                         node);
372                 
373                 g_ptr_array_add(st->parents,node);
374                 
375                 node->id = st->parents->len - 1;
376         } else {
377                 node->id = -1;
378         }
379         
380         if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
381                 node->parent = g_ptr_array_index(st->parents,parent_id);
382         } else {
383                 /* ??? should we set the parent to be root ??? */
384                 g_assert_not_reached();
385         }
386         
387         if (node->parent->children) {
388                 /* insert as last child */
389                 
390                 for (last_chld = node->parent->children;
391                          last_chld->next;
392                          last_chld = last_chld->next ) ;
393                 
394                 last_chld->next = node;
395                 
396         } else {
397                 /* insert as first child */
398                 node->parent->children = node;
399         }
400         
401         if(node->parent->hash) {
402                 g_hash_table_insert(node->parent->hash,node->name,node);
403         }
404         
405         if (st->setup_node_pr) {
406                 st->setup_node_pr(node);
407         } else {
408                 node->pr = NULL;
409         }
410         
411         return node;
412 }
413
414
415 extern int create_node(stats_tree* st, const gchar* name, int parent_id, gboolean with_hash) {
416         stat_node* node = new_stat_node(st,name,parent_id,with_hash,TRUE);
417         
418         if (node) 
419                 return node->id;
420         else
421                 return 0;
422 }
423
424 /* XXX: should this be a macro? */
425 extern int create_node_with_parent_name(stats_tree* st,
426                                                                                    const gchar* name,
427                                                                                    const gchar* parent_name,
428                                                                                    gboolean with_children) {
429         return create_node(st,name,get_parent_id_by_name(st,parent_name),with_children);
430 }
431
432
433
434 /*
435  * Increases by delta the counter of the node whose name is given
436  * if the node does not exist yet it's created (with counter=1)
437  * using parent_name as parent node.
438  * with_hash=TRUE to indicate that the created node will have a parent
439  */
440 extern int manip_stat_node(manip_node_mode mode, stats_tree* st, const guint8* name, int parent_id, gboolean with_hash, gint value) {
441         stat_node* node = NULL;
442         stat_node* parent = NULL;
443         
444         if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
445                 parent = g_ptr_array_index(st->parents,parent_id);
446         } else {
447                 g_assert_not_reached();
448         }
449         
450         if( parent->hash ) {
451                 node = g_hash_table_lookup(parent->hash,name);
452         } else {
453                 node = g_hash_table_lookup(st->names,name);
454         }
455         
456         if ( node == NULL ) 
457                 node = new_stat_node(st,name,parent_id,with_hash,with_hash);
458         
459         switch (mode) {
460                 case MN_INCREASE: node->counter += value; break;
461                 case MN_SET: node->counter = value; break;
462         }
463         
464         if (node) 
465                 return node->id;
466         else
467                 return -1;
468 }
469
470
471 extern guint8* get_st_abbr(const guint8* optarg) {
472         guint i;
473
474         /* XXX: this fails when tethereal is given any options
475            after the -z */
476         g_assert(optarg != NULL);
477         
478         for (i=0; optarg[i] && optarg[i] != ','; i++);
479         
480         if (optarg[i] == ',') {
481                 return g_strndup(optarg,i);
482         } else {
483                 return NULL;
484         }
485 }
486
487
488 static range_pair_t* get_range(guint8* rngstr) {
489         gchar** split;
490         range_pair_t* rng = g_malloc(sizeof(range_pair_t));
491         
492         split =  g_strsplit(rngstr,"-",2);
493
494         rng->floor = strtol(split[0],NULL,10);
495         rng->ceil  = strtol(split[1],NULL,10);
496         
497         if (rng->ceil == 0) rng->ceil = G_MAXINT;
498         if (rng->floor == 0) rng->floor = G_MININT;
499
500         g_strfreev(split);
501         
502         return rng;
503 }
504
505
506 extern int create_range_node(stats_tree* st,
507                                                                 const gchar* name,
508                                                                 int parent_id,
509                                                                 ...) {
510         va_list list;
511         guint8* curr_range;
512         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
513         stat_node* range_node = NULL;
514         
515         va_start( list, parent_id );
516         while (( curr_range = va_arg(list, guint8*) )) {
517                 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
518                 range_node->rng = get_range(curr_range);
519         }
520         va_end( list );
521
522         return rng_root->id;
523 }
524
525 extern int get_parent_id_by_name(stats_tree* st, const gchar* parent_name) {
526         stat_node* node = g_hash_table_lookup(st->names,parent_name);
527         
528         if (node)
529                 return node->id;
530         else
531                 return 0; /* ??? -1 ??? */
532 }
533
534
535 extern int create_range_node_with_parent_name(stats_tree* st,
536                                                                                           const gchar* name,
537                                                                                           const gchar* parent_name,
538                                                                                           ...) {
539         va_list list;
540         guint8* curr_range;
541         stat_node* range_node = NULL;
542         int parent_id = get_parent_id_by_name(st,parent_name);
543         stat_node* rng_root = new_stat_node(st, name, parent_id, FALSE, TRUE);
544
545         va_start( list, parent_name );
546         while (( curr_range = va_arg(list, guint8*) )) {
547                 range_node = new_stat_node(st, curr_range, rng_root->id, FALSE, FALSE);
548                 range_node->rng = get_range(curr_range);
549         }
550         va_end( list );
551         
552         return rng_root->id;
553 }       
554
555
556 extern int tick_range(stats_tree* st,
557                                                  const gchar* name,
558                                                  int parent_id,
559                                                  int value_in_range) {
560         
561         stat_node* node = NULL;
562         stat_node* parent = NULL;
563         stat_node* child = NULL;
564         gint floor, ceil;
565         
566         if (parent_id >= 0 && parent_id < (int) st->parents->len) {
567                 parent = g_ptr_array_index(st->parents,parent_id);
568         } else {
569                 g_assert_not_reached();
570         }
571         
572         if( parent->hash ) {
573                 node = g_hash_table_lookup(parent->hash,name);
574         } else {
575                 node = g_hash_table_lookup(st->names,name);
576         }
577         
578         if ( node == NULL ) 
579                 return node->id;
580         
581         for ( child = node->children; child; child = child->next) {
582                 floor =  child->rng->floor;
583                 ceil = child->rng->ceil;
584                 
585                 if ( value_in_range >= floor && value_in_range <= ceil ) {
586                         child->counter++;
587                         return node->id;
588                 }
589         }
590         
591         return node->id;
592 }
593
594 extern int create_pivot_node(stats_tree* st,
595                                                          const gchar* name,
596                                                          int parent_id) {
597         stat_node* node = new_stat_node(st,name,parent_id,TRUE,TRUE);
598         
599         if (node) 
600                 return node->id;
601         else
602                 return 0;
603 }
604
605 extern int create_pivot_node_with_parent_name(stats_tree* st,
606                                                          const gchar* name,
607                                                          const gchar* parent_name) {
608         int parent_id = get_parent_id_by_name(st,parent_name);
609         stat_node* node;
610         
611         node = new_stat_node(st,name,parent_id,TRUE,TRUE);
612         
613         if (node) 
614                 return node->id;
615         else
616                 return 0;
617 }
618
619 extern int tick_pivot(stats_tree* st,
620                                           int pivot_id,
621                                           const gchar* pivot_value) {
622         
623         stat_node* parent = g_ptr_array_index(st->parents,pivot_id);
624         
625         parent->counter++;
626         manip_stat_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);
627         
628         return pivot_id;
629 }
630