Browse code

libkcore: moved core stats and mi stats to kex module

- core specific implementation for statistics api moved to kex module
- initialization of statistics collector is done while adding a
new statistic if not done before
- destroying the stats collector is done in mod_destroy from kex module

Daniel-Constantin Mierla authored on 22/04/2009 15:08:12
Showing 4 changed files
1 1
deleted file mode 100644
... ...
@@ -1,71 +0,0 @@
1
-/*
2
- * $Id$
3
- *
4
- * Copyright (C) 2006 Voice Sistem SRL
5
- *
6
- * This file is part of Kamailio, a free SIP server.
7
- *
8
- * Kamailio is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version.
12
- *
13
- * Kamailio is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
- * GNU General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU General Public License
19
- * along with this program; if not, write to the Free Software
20
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
- *
22
- * History:
23
- * ---------
24
- *  2006-01-23  first version (bogdan)
25
- *  2006-11-28  Added statistics for the number of bad URI's, methods, and 
26
- *              proxy requests (Jeffrey Magder - SOMA Networks)
27
- */
28
-
29
-/*!
30
- * \file
31
- * \brief Kamailio Core statistics
32
- */
33
-
34
-
35
-#include <string.h>
36
-
37
-#include "statistics.h"
38
-
39
-
40
-#ifdef STATISTICS
41
-
42
-stat_var* rcv_reqs;				/*!< received requests        */
43
-stat_var* rcv_rpls;				/*!< received replies         */
44
-stat_var* fwd_reqs;				/*!< forwarded requests       */
45
-stat_var* fwd_rpls;				/*!< forwarded replies        */
46
-stat_var* drp_reqs;				/*!< dropped requests         */
47
-stat_var* drp_rpls;				/*!< dropped replies          */
48
-stat_var* err_reqs;				/*!< error requests           */
49
-stat_var* err_rpls;				/*!< error replies            */
50
-stat_var* bad_URIs;				/*!< number of bad URIs       */
51
-stat_var* unsupported_methods;	/*!< unsupported methods      */
52
-stat_var* bad_msg_hdr;			/*!< messages with bad header */
53
-
54
-
55
-/*! exported core statistics */
56
-stat_export_t core_stats[] = {
57
-	{"rcv_requests" ,         0,  &rcv_reqs              },
58
-	{"rcv_replies" ,          0,  &rcv_rpls              },
59
-	{"fwd_requests" ,         0,  &fwd_reqs              },
60
-	{"fwd_replies" ,          0,  &fwd_rpls              },
61
-	{"drop_requests" ,        0,  &drp_reqs              },
62
-	{"drop_replies" ,         0,  &drp_rpls              },
63
-	{"err_requests" ,         0,  &err_reqs              },
64
-	{"err_replies" ,          0,  &err_rpls              },
65
-	{"bad_URIs_rcvd",         0,  &bad_URIs              },
66
-	{"unsupported_methods",   0,  &unsupported_methods   },
67
-	{"bad_msg_hdr",           0,  &bad_msg_hdr           },
68
-	{0,0,0}
69
-};
70
-
71
-#endif
72 1
deleted file mode 100644
... ...
@@ -1,79 +0,0 @@
1
-/*
2
- * $Id$
3
- *
4
- * Copyright (C) 2006 Voice Sistem SRL
5
- *
6
- * This file is part of Kamailio, a free SIP server.
7
- *
8
- * Kamailio is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version.
12
- *
13
- * Kamailio is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
- * GNU General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU General Public License
19
- * along with this program; if not, write to the Free Software
20
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
- *
22
- * History:
23
- * ---------
24
- *  2006-01-23  first version (bogdan)
25
- *  2006-11-28  Added statistics for the number of bad URI's, methods, and 
26
- *              proxy requests (Jeffrey Magder - SOMA Networks)
27
- */
28
-
29
-/*!
30
- * \file
31
- * \brief  Kamailio statistics
32
- */
33
-
34
-
35
-#ifndef _CORE_STATS_H_
36
-#define _CORE_STATS_H_
37
-
38
-#include "statistics.h"
39
-
40
-#ifdef STATISTICS
41
-/*! exported core statistics */
42
-extern stat_export_t core_stats[];
43
-
44
-/*! \brief received requests */
45
-extern stat_var* rcv_reqs;
46
-
47
-/*! \brief received replies */
48
-extern stat_var* rcv_rpls;
49
-
50
-/*! \brief forwarded requests */
51
-extern stat_var* fwd_reqs;
52
-
53
-/*! \brief forwarded replies */
54
-extern stat_var* fwd_rpls;
55
-
56
-/*! \brief dropped requests */
57
-extern stat_var* drp_reqs;
58
-
59
-/*! \brief dropped replies */
60
-extern stat_var* drp_rpls;
61
-
62
-/*! \brief error requests */
63
-extern stat_var* err_reqs;
64
-
65
-/*! \brief error replies */
66
-extern stat_var* err_rpls;
67
-
68
-/*! \brief Set in parse_uri() */
69
-extern stat_var* bad_URIs;
70
-
71
-/*! \brief Set in parse_method() */
72
-extern stat_var* unsupported_methods;
73
-
74
-/*! \brief Set in get_hdr_field(). */
75
-extern stat_var* bad_msg_hdr;
76
- 
77
-#endif /*STATISTICS*/
78
-
79
-#endif /*_CORE_STATS_H_*/
... ...
@@ -35,42 +35,28 @@
35 35
 
36 36
 #include <string.h>
37 37
 #include <stdio.h>
38
+#include <stdlib.h>
38 39
 
39 40
 #include "../../mem/shm_mem.h"
40
-#include "../kmi/mi.h"
41 41
 #include "../../ut.h"
42 42
 #include "../../dprint.h"
43 43
 #include "../../locking.h"
44 44
 #include "../../socket_info.h"
45
-#include "core_stats.h"
45
+#include "km_ut.h"
46
+#include "hash_func.h"
46 47
 #include "statistics.h"
47 48
 
48 49
 #ifdef STATISTICS
49 50
 
50
-#define MAX_PROC_BUFFER 256
51
-
52
-static stats_collector *collector;
53
-
54
-static struct mi_root *mi_get_stats(struct mi_root *cmd, void *param);
55
-static struct mi_root *mi_reset_stats(struct mi_root *cmd, void *param);
56
-
57
-static mi_export_t mi_stat_cmds[] = {
58
-	{ "get_statistics",    mi_get_stats,    0  ,  0,  0 },
59
-	{ "reset_statistics",  mi_reset_stats,  0  ,  0,  0 },
60
-	{ 0, 0, 0, 0, 0}
61
-};
62
-
63
-
51
+static stats_collector *collector = NULL;
64 52
 
65
-#ifdef NO_ATOMIC_OPS
66
-#warning STATISTICS: Architecture with no support for atomic operations. \
67
-         Using Locks!!
68
-gen_lock_t *stat_lock = 0;
69
-#endif
70 53
 
71 54
 #define stat_hash(_s) core_hash( _s, 0, STATS_HASH_SIZE)
72 55
 
73
-
56
+stats_collector* get_stats_collector(void)
57
+{
58
+	return collector;
59
+}
74 60
 
75 61
 /*! \brief
76 62
  * Returns the statistic associated with 'numerical_code' and 'out_codes'.
... ...
@@ -106,6 +92,8 @@ stat_var *get_stat_var_from_num_code(unsigned int numerical_code, int out_codes)
106 106
 
107 107
 int init_stats_collector(void)
108 108
 {
109
+	if(collector != NULL) /* already initialized */
110
+		return 0;
109 111
 	/* init the collector */
110 112
 	collector = (stats_collector*)shm_malloc(sizeof(stats_collector));
111 113
 	if (collector==0) {
... ...
@@ -114,31 +102,6 @@ int init_stats_collector(void)
114 114
 	}
115 115
 	memset( collector, 0 , sizeof(stats_collector));
116 116
 
117
-#ifdef NO_ATOMIC_OPS
118
-	/* init BIG (really BIG) lock */
119
-	stat_lock = lock_alloc();
120
-	if (stat_lock==0 || lock_init( stat_lock )==0 ) {
121
-		LM_ERR("failed to init the really BIG lock\n");
122
-		goto error;
123
-	}
124
-#endif
125
-
126
-	/* register MI commands */
127
-	if (register_mi_mod( "statistics", mi_stat_cmds)<0) {
128
-		LM_ERR("unable to register MI cmds\n");
129
-		goto error;
130
-	}
131
-
132
-	/* register core statistics */
133
-	if (register_module_stats( "core", core_stats)!=0 ) {
134
-		LM_ERR("failed to register core statistics\n");
135
-		goto error;
136
-	}
137
-	/* register sh_mem statistics */
138
-	if (register_module_stats( "shmem", shm_stats)!=0 ) {
139
-		LM_ERR("failed to register sh_mem statistics\n");
140
-		goto error;
141
-	}
142 117
 	LM_DBG("statistics manager successfully initialized\n");
143 118
 
144 119
 	return 0;
... ...
@@ -153,12 +116,6 @@ void destroy_stats_collector(void)
153 153
 	stat_var *tmp_stat;
154 154
 	int i;
155 155
 
156
-#ifdef NO_ATOMIC_OPS
157
-	/* destroy big lock */
158
-	if (stat_lock)
159
-		lock_destroy( stat_lock );
160
-#endif
161
-
162 156
 	if (collector) {
163 157
 		/* destroy hash table */
164 158
 		for( i=0 ; i<STATS_HASH_SIZE ; i++ ) {
... ...
@@ -179,13 +136,14 @@ void destroy_stats_collector(void)
179 179
 
180 180
 		/* destroy the collector */
181 181
 		shm_free(collector);
182
+		collector = NULL;
182 183
 	}
183 184
 
184 185
 	return;
185 186
 }
186 187
 
187 188
 
188
-static inline module_stats* get_stat_module( str *module)
189
+module_stats* get_stat_module(str *module)
189 190
 {
190 191
 	int i;
191 192
 
... ...
@@ -210,6 +168,8 @@ static inline module_stats* add_stat_module( char *module)
210 210
 
211 211
 	if ( (module==0) || ((len = strlen(module))==0 ) )
212 212
 		return 0;
213
+	if(init_stats_collector()!=0)
214
+		return 0;
213 215
 
214 216
 	amods = (module_stats*)shm_realloc( collector->amodules,
215 217
 			(collector->mod_no+1)*sizeof(module_stats) );
... ...
@@ -245,6 +205,9 @@ int register_stat( char *module, char *name, stat_var **pvar, int flags)
245 245
 		goto error;
246 246
 	}
247 247
 
248
+	if(init_stats_collector()!=0)
249
+		return -1;
250
+
248 251
 	stat = (stat_var*)shm_malloc(sizeof(stat_var));
249 252
 	if (stat==0) {
250 253
 		LM_ERR("no more shm memory\n");
... ...
@@ -258,11 +221,7 @@ int register_stat( char *module, char *name, stat_var **pvar, int flags)
258 258
 			LM_ERR("no more shm memory\n");
259 259
 			goto error1;
260 260
 		}
261
-#ifdef NO_ATOMIC_OPS
262
-		*(stat->u.val) = 0;
263
-#else
264 261
 		atomic_set(stat->u.val,0);
265
-#endif
266 262
 		*pvar = stat;
267 263
 	} else {
268 264
 		stat->u.f = (stat_function)(pvar);
... ...
@@ -368,6 +327,10 @@ stat_var* get_stat( str *name )
368 368
 	return 0;
369 369
 }
370 370
 
371
+#endif /*STATISTICS*/
372
+
373
+#define MAX_PROC_BUFFER 256
374
+
371 375
 /*!
372 376
  * This function will retrieve a list of all ip addresses and ports that OpenSER
373 377
  * is listening on, with respect to the transport protocol specified with
... ...
@@ -724,138 +687,3 @@ int get_total_bytes_waiting(void)
724 724
 }
725 725
 
726 726
 
727
-
728
-/***************************** MI STUFF ********************************/
729
-
730
-inline static int mi_add_stat(struct mi_node *rpl, stat_var *stat)
731
-{
732
-	struct mi_node *node;
733
-
734
-	node = addf_mi_node_child(rpl, 0, 0, 0, "%.*s:%.*s = %lu",
735
-		collector->amodules[stat->mod_idx].name.len,
736
-		collector->amodules[stat->mod_idx].name.s,
737
-		stat->name.len, stat->name.s,
738
-		get_stat_val(stat) );
739
-
740
-	if (node==0)
741
-		return -1;
742
-	return 0;
743
-}
744
-
745
-inline static int mi_add_module_stats(struct mi_node *rpl,
746
-													module_stats *mods)
747
-{
748
-	struct mi_node *node;
749
-	stat_var *stat;
750
-
751
-	for( stat=mods->head ; stat ; stat=stat->lnext) {
752
-		node = addf_mi_node_child(rpl, 0, 0, 0, "%.*s:%.*s = %lu",
753
-			mods->name.len, mods->name.s,
754
-			stat->name.len, stat->name.s,
755
-			get_stat_val(stat) );
756
-		if (node==0)
757
-			return -1;
758
-	}
759
-	return 0;
760
-}
761
-
762
-
763
-static struct mi_root *mi_get_stats(struct mi_root *cmd, void *param)
764
-{
765
-	struct mi_root *rpl_tree;
766
-	struct mi_node *rpl;
767
-	struct mi_node *arg;
768
-	module_stats   *mods;
769
-	stat_var       *stat;
770
-	str val;
771
-	int i;
772
-
773
-	if (cmd->node.kids==NULL)
774
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
775
-
776
-	rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
777
-	if (rpl_tree==0)
778
-		return 0;
779
-	rpl = &rpl_tree->node;
780
-
781
-	for( arg=cmd->node.kids ; arg ; arg=arg->next) {
782
-		if (arg->value.len==0)
783
-			continue;
784
-
785
-		val = arg->value;
786
-
787
-		if ( val.len==3 && memcmp(val.s,"all",3)==0) {
788
-			/* add all statistic variables */
789
-			for( i=0 ; i<collector->mod_no ;i++ ) {
790
-				if (mi_add_module_stats( rpl, &collector->amodules[i] )!=0)
791
-					goto error;
792
-			}
793
-		} else if ( val.len>1 && val.s[val.len-1]==':') {
794
-			/* add module statistics */
795
-			val.len--;
796
-			mods = get_stat_module( &val );
797
-			if (mods==0)
798
-				continue;
799
-			if (mi_add_module_stats( rpl, mods )!=0)
800
-				goto error;
801
-		} else {
802
-			/* add only one statistic */
803
-			stat = get_stat( &val );
804
-			if (stat==0)
805
-				continue;
806
-			if (mi_add_stat(rpl,stat)!=0)
807
-				goto error;
808
-		}
809
-	}
810
-
811
-	if (rpl->kids==0) {
812
-		free_mi_tree(rpl_tree);
813
-		return init_mi_tree( 404, "Statistics Not Found", 20);
814
-	}
815
-
816
-	return rpl_tree;
817
-error:
818
-	free_mi_tree(rpl_tree);
819
-	return 0;
820
-}
821
-
822
-
823
-
824
-static struct mi_root *mi_reset_stats(struct mi_root *cmd, void *param)
825
-{
826
-	struct mi_root *rpl_tree;
827
-	struct mi_node *arg;
828
-	stat_var       *stat;
829
-	int found;
830
-
831
-	if (cmd->node.kids==NULL)
832
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
833
-
834
-	rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
835
-	if (rpl_tree==0)
836
-		return 0;
837
-	found = 0;
838
-
839
-	for( arg=cmd->node.kids ; arg ; arg=arg->next) {
840
-		if (arg->value.len==0)
841
-			continue;
842
-
843
-		stat = get_stat( &arg->value );
844
-		if (stat==0)
845
-			continue;
846
-
847
-		reset_stat( stat );
848
-		found = 1;
849
-	}
850
-
851
-	if (!found) {
852
-		free_mi_tree(rpl_tree);
853
-		return init_mi_tree( 404, "Statistics Not Found", 20);
854
-	}
855
-
856
-	return rpl_tree;
857
-}
858
-
859
-
860
-#endif /*STATISTICS*/
861
-
... ...
@@ -32,10 +32,11 @@
32 32
  */
33 33
 
34 34
 
35
-#ifndef _STATISTICS_H_
36
-#define _STATISTICS_H_
35
+#ifndef _KSTATISTICS_H_
36
+#define _KSTATISTICS_H_
37 37
 
38
-#include "hash_func.h"
38
+#include <string.h>
39
+#include "../../str.h"
39 40
 #include "../../atomic_ops.h"
40 41
 
41 42
 #define STATS_HASH_POWER   8
... ...
@@ -48,11 +49,7 @@
48 48
 #define STAT_SHM_NAME  (1<<2)
49 49
 #define STAT_IS_FUNC   (1<<3)
50 50
 
51
-#ifdef NO_ATOMIC_OPS
52
-typedef unsigned int stat_val;
53
-#else
54 51
 typedef atomic_t stat_val;
55
-#endif
56 52
 
57 53
 typedef unsigned long (*stat_function)(void);
58 54
 
... ...
@@ -116,11 +113,8 @@ unsigned int get_stat_val( stat_var *var );
116 116
  */
117 117
 stat_var *get_stat_var_from_num_code(unsigned int numerical_code, int in_codes);
118 118
 
119
-
120
-#ifdef NO_ATOMIC_OPS
121
-#include "locking.h"
122
-extern gen_lock_t *stat_lock;
123
-#endif
119
+stats_collector* get_stats_collector(void);
120
+module_stats* get_stat_module(str *module);
124 121
 
125 122
 #else
126 123
 	#define init_stats_collector()  0
... ...
@@ -130,56 +124,29 @@ extern gen_lock_t *stat_lock;
130 130
 	#define get_stat( _name )  0
131 131
 	#define get_stat_val( _var ) 0
132 132
 	#define get_stat_var_from_num_code( _n_code, _in_code) NULL
133
+	#define get_stats_collector() NULL
134
+	#define get_stat_module(_module) NULL
133 135
 #endif
134 136
 
135 137
 
136 138
 #ifdef STATISTICS
137
-	#ifdef NO_ATOMIC_OPS
138
-		#define update_stat( _var, _n) \
139
-			do { \
140
-				if ( !((_var)->flags&STAT_IS_FUNC) ) {\
141
-					if ((_var)->flags&STAT_NO_SYNC) {\
142
-						*((_var)->u.val) += _n;\
143
-					} else {\
144
-						lock_get(stat_lock);\
145
-						*((_var)->u.val) += _n;\
146
-						lock_release(stat_lock);\
147
-					}\
148
-				}\
149
-			}while(0)
150
-		#define reset_stat( _var) \
151
-			do { \
152
-				if ( ((_var)->flags&(STAT_NO_RESET|STAT_IS_FUNC))==0 ) {\
153
-					if ((_var)->flags&STAT_NO_SYNC) {\
154
-						*((_var)->u.val) = 0;\
155
-					} else {\
156
-						lock_get(stat_lock);\
157
-						*((_var)->u.val) = 0;\
158
-						lock_release(stat_lock);\
159
-					}\
160
-				}\
161
-			}while(0)
162
-		#define get_stat_val( _var ) ((unsigned long)\
163
-			((_var)->flags&STAT_IS_FUNC)?(_var)->u.f():*((_var)->u.val))
164
-	#else
165
-		#define update_stat( _var, _n) \
166
-			do { \
167
-				if ( !((_var)->flags&STAT_IS_FUNC) ) {\
168
-					if (_n>=0) \
169
-						atomic_add( _n, (_var)->u.val);\
170
-					else \
171
-						atomic_sub( -(_n), (_var)->u.val);\
172
-				}\
173
-			}while(0)
174
-		#define reset_stat( _var) \
175
-			do { \
176
-				if ( ((_var)->flags&(STAT_NO_RESET|STAT_IS_FUNC))==0 ) {\
177
-					atomic_set( (_var)->u.val, 0);\
178
-				}\
179
-			}while(0)
180
-		#define get_stat_val( _var ) ((unsigned long)\
181
-			((_var)->flags&STAT_IS_FUNC)?(_var)->u.f():(_var)->u.val->counter)
182
-	#endif /* NO_ATOMIC_OPS */
139
+	#define update_stat( _var, _n) \
140
+		do { \
141
+			if ( !((_var)->flags&STAT_IS_FUNC) ) {\
142
+				if (_n>=0) \
143
+					atomic_add( _n, (_var)->u.val);\
144
+				else \
145
+					atomic_sub( -(_n), (_var)->u.val);\
146
+			}\
147
+		}while(0)
148
+	#define reset_stat( _var) \
149
+		do { \
150
+			if ( ((_var)->flags&(STAT_NO_RESET|STAT_IS_FUNC))==0 ) {\
151
+				atomic_set( (_var)->u.val, 0);\
152
+			}\
153
+		}while(0)
154
+	#define get_stat_val( _var ) ((unsigned long)\
155
+		((_var)->flags&STAT_IS_FUNC)?(_var)->u.f():(_var)->u.val->val)
183 156
 
184 157
 	#define if_update_stat(_c, _var, _n) \
185 158
 		do { \
... ...
@@ -241,5 +208,4 @@ int get_socket_list_from_proto(int **ipList, int protocol);
241 241
 int get_total_bytes_waiting(void);
242 242
 
243 243
 
244
-
245 244
 #endif