Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,275 +0,0 @@
1
-/*
2
- * Copyright (C) 2010 iptelorg GmbH
3
- *
4
- * Permission to use, copy, modify, and distribute this software for any
5
- * purpose with or without fee is hereby granted, provided that the above
6
- * copyright notice and this permission notice appear in all copies.
7
- *
8
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
- */
16
-/** Kamailio core :: counter/stats.
17
- * @author andrei
18
- * @file counters.h
19
- * @ingroup:  core
20
- *
21
- *  Example usage:
22
- *  1. register (must be before forking, e.g. from mod_init()):
23
- *    counter_handle_t h;
24
- *    counter_register(&h, "my_counters", "foo", 0, 0, 0, "test counter", 0);
25
- *  2. increment/add:
26
- *    counter_inc(h);
27
- *    counter_add(h, 100);
28
- *  3. get and existing counter handle, knowing its group and name
29
- *    counter_lookup(&h, "my_counters", "foo");
30
- *  4. get a counter value (the handle can be obtained like above)
31
- *    val = counter_get(h);
32
- */
33
-
34
-#ifndef __counters_h
35
-#define __counters_h
36
-
37
-#include "pt.h"
38
-
39
-/* counter flags */
40
-#define CNT_F_NO_RESET 1 /* don't reset */
41
-
42
-typedef long counter_val_t;
43
-
44
-/* use a struct. to force errors on direct access attempts */
45
-struct counter_handle_s {
46
-	unsigned short id;
47
-};
48
-
49
-
50
-struct counter_val_s {
51
-	counter_val_t v;
52
-};
53
-
54
-
55
-typedef struct counter_handle_s counter_handle_t;
56
-typedef struct counter_val_s counter_array_t;
57
-typedef counter_val_t (*counter_cbk_f)(counter_handle_t h, void* param);
58
-
59
-
60
-
61
-/* counter definition structure, used in zero term. arrays for more
62
- *  convenient registration of several counters at once
63
- *  (see counter_register_array(group, counter_array)).
64
- */
65
-struct counter_def_s {
66
-	counter_handle_t* handle; /** if non 0, will be filled with the counter
67
-							     handle */
68
-	const char* name;         /**< counter name (inside the group) */
69
-	int flags;                /**< counter flags */
70
-	counter_cbk_f get_cbk;    /**< callback function for reading */
71
-	void* get_cbk_param;      /**< callback parameter */
72
-	const char* descr;        /**< description/documentation string */
73
-};
74
-
75
-typedef struct counter_def_s counter_def_t;
76
-
77
-
78
-
79
-extern counter_array_t* _cnts_vals;
80
-extern int _cnts_row_len; /* number of elements per row */
81
-
82
-
83
-
84
-int counters_initialized(void);
85
-int init_counters(void);
86
-void destroy_counters(void);
87
-int counters_prefork_init(int max_process_no);
88
-
89
-
90
-int counter_register_array(const char* group, counter_def_t* defs);
91
-int counter_register(	counter_handle_t* handle, const char* group,
92
-						const char* name, int flags,
93
-						counter_cbk_f cbk, void* cbk_param,
94
-						const char* doc,
95
-						int reg_flags);
96
-int counter_lookup(counter_handle_t* handle,
97
-						const char* group, const char* name);
98
-int counter_lookup_str(counter_handle_t* handle, str* group, str* name);
99
-
100
-void counter_reset(counter_handle_t handle);
101
-counter_val_t counter_get_val(counter_handle_t handle);
102
-counter_val_t counter_get_raw_val(counter_handle_t handle);
103
-char* counter_get_name(counter_handle_t handle);
104
-char* counter_get_group(counter_handle_t handle);
105
-char* counter_get_doc(counter_handle_t handle);
106
-
107
-/** gets the per process value of counter h for process p_no.
108
- *  Note that if used before counter_prefork_init() process_no is 0
109
- *  and _cnts_vals will point into a temporary one "row"  array.
110
- */
111
-#define counter_pprocess_val(p_no, h) \
112
-	_cnts_vals[(p_no) * _cnts_row_len + (h).id].v
113
-
114
-
115
-
116
-/** increments a counter.
117
- * @param handle - counter handle.
118
- */
119
-inline static void counter_inc(counter_handle_t handle)
120
-{
121
-	counter_pprocess_val(process_no, handle)++;
122
-}
123
-
124
-
125
-
126
-/** adds a value to a counter.
127
- * @param handle - counter handle.
128
- * @param v - value.
129
- */
130
-inline static void counter_add(counter_handle_t handle, int v)
131
-{
132
-	counter_pprocess_val(process_no, handle)+=v;
133
-}
134
-
135
-
136
-
137
-void counter_iterate_grp_names(void (*cbk)(void* p, str* grp_name), void* p);
138
-void counter_iterate_grp_var_names(	const char* group,
139
-									void (*cbk)(void* p, str* var_name),
140
-									void* p);
141
-void counter_iterate_grp_vars(const char* group,
142
-							  void (*cbk)(void* p, str* g, str* n,
143
-								  			counter_handle_t h),
144
-							  void *p);
145
-
146
-
147
-/* k stat flags */
148
-#define STAT_NO_RESET	1  /* used in dialog(k), nat_traversal(k),
149
-							  registrar(k), statistics(k), usrloc(k) */
150
-/* #define STAT_NO_SYN	2  -- not used */
151
-#define STAT_SHM_NAME	4 /* used only from usrloc(k) */
152
-#define STAT_IS_FUNC	8
153
-
154
-
155
-
156
-#ifdef STATISTICS
157
-
158
-/* statistics support check */
159
-#define stats_support() 1
160
-
161
-/* types */
162
-
163
-typedef counter_val_t    stat_val;
164
-/* stat_var is always used as a pointer in k, we missuse
165
-   stat_var* for holding out counter id */
166
-typedef void stat_var;
167
-/* get val callback
168
- * TODO: change it to counter_cbk_f compatible callback?
169
- */
170
-typedef counter_val_t (*stat_function)(void);
171
-
172
-/* statistic module interface */
173
-struct stat_export_s {
174
-	char* name;
175
-	int flags;
176
-	stat_var** stat_pointer; /* pointer to the memory location
177
-								(where a counter handle will be stored)
178
-								Note: it's a double pointer because of
179
-								the original k version which needed it
180
-								allocated in shm. This version
181
-								will store the counter id at *stat_pointer.
182
-							  */
183
-};
184
-
185
-typedef struct stat_export_s stat_export_t;
186
-
187
-int register_stat( char *module, char *name, stat_var **pvar, int flags);
188
-int register_module_stats(char *module, stat_export_t *stats);
189
-
190
-inline static stat_var* get_stat(str *name)
191
-{
192
-	counter_handle_t h;
193
-	str grp;
194
-
195
-	grp.s = 0;
196
-	grp.len = 0;
197
-	if (counter_lookup_str(&h, &grp, name) < 0)
198
-		return 0;
199
-	return (void*)(unsigned long)h.id;
200
-}
201
-
202
-
203
-
204
-inline static unsigned long get_stat_val(stat_var *v)
205
-{
206
-	counter_handle_t h;
207
-	h.id = (unsigned short)(unsigned long)v;
208
-	return (unsigned long)counter_get_val(h);
209
-}
210
-
211
-
212
-
213
-inline static char* get_stat_name(stat_var *v)
214
-{
215
-	counter_handle_t h;
216
-	h.id = (unsigned short)(unsigned long)v;
217
-	return counter_get_name(h);
218
-}
219
-
220
-
221
-
222
-inline static char* get_stat_module(stat_var *v)
223
-{
224
-	counter_handle_t h;
225
-	h.id = (unsigned short)(unsigned long)v;
226
-	return counter_get_group(h);
227
-}
228
-
229
-
230
-
231
-inline static void update_stat(stat_var* v, int n)
232
-{
233
-	counter_handle_t h;
234
-	h.id = (unsigned short)(unsigned long)v;
235
-	counter_add(h, n);
236
-}
237
-
238
-
239
-
240
-inline static void reset_stat(stat_var* v)
241
-{
242
-	counter_handle_t h;
243
-	h.id = (unsigned short)(unsigned long)v;
244
-	counter_reset(h);
245
-}
246
-
247
-
248
-#define if_update_stat(c, var, n) \
249
-	do{ \
250
-		if ((c)) update_stat((var), (n)); \
251
-	}while(0)
252
-
253
-#define if_reset_stat(c, var) \
254
-	do{ \
255
-		if ((c)) reset_stat((var)); \
256
-	}while(0)
257
-
258
-#else /* STATISTICS */
259
-
260
-/* statistics support check */
261
-#define stats_support() 0
262
-#define register_module_stats(mod, stats) 0
263
-#define register_stat(mod, name, var, flags) 0
264
-#define get_stat(name)  0
265
-#define get_stat_val(var) 0
266
-#define update_stat(v, n)
267
-#define reset_stat(v)
268
-#define if_update_stat(c, v, n)
269
-#define if_reset_stat(c, v)
270
-
271
-#endif /* STATISTICS */
272
-
273
-#endif /*__counters_h*/
274
-
275
-/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
Browse code

core, libs, modules: removed kcore internal library

- instead of kcore stats wrapers, use directly counters.h from core
- parse of sst header moved to sst module (only use)
- string helper functions compacted in strutils.h
- statistics helper functions used only by snmpstats module moved to it

Daniel-Constantin Mierla authored on 05/12/2016 13:27:19
Showing 1 changed files
... ...
@@ -1,4 +1,4 @@
1
-/* 
1
+/*
2 2
  * Copyright (C) 2010 iptelorg GmbH
3 3
  *
4 4
  * Permission to use, copy, modify, and distribute this software for any
... ...
@@ -143,6 +143,133 @@ void counter_iterate_grp_vars(const char* group,
143 143
 								  			counter_handle_t h),
144 144
 							  void *p);
145 145
 
146
+
147
+/* k stat flags */
148
+#define STAT_NO_RESET	1  /* used in dialog(k), nat_traversal(k),
149
+							  registrar(k), statistics(k), usrloc(k) */
150
+/* #define STAT_NO_SYN	2  -- not used */
151
+#define STAT_SHM_NAME	4 /* used only from usrloc(k) */
152
+#define STAT_IS_FUNC	8
153
+
154
+
155
+
156
+#ifdef STATISTICS
157
+
158
+/* statistics support check */
159
+#define stats_support() 1
160
+
161
+/* types */
162
+
163
+typedef counter_val_t    stat_val;
164
+/* stat_var is always used as a pointer in k, we missuse
165
+   stat_var* for holding out counter id */
166
+typedef void stat_var;
167
+/* get val callback
168
+ * TODO: change it to counter_cbk_f compatible callback?
169
+ */
170
+typedef counter_val_t (*stat_function)(void);
171
+
172
+/* statistic module interface */
173
+struct stat_export_s {
174
+	char* name;
175
+	int flags;
176
+	stat_var** stat_pointer; /* pointer to the memory location
177
+								(where a counter handle will be stored)
178
+								Note: it's a double pointer because of
179
+								the original k version which needed it
180
+								allocated in shm. This version
181
+								will store the counter id at *stat_pointer.
182
+							  */
183
+};
184
+
185
+typedef struct stat_export_s stat_export_t;
186
+
187
+int register_stat( char *module, char *name, stat_var **pvar, int flags);
188
+int register_module_stats(char *module, stat_export_t *stats);
189
+
190
+inline static stat_var* get_stat(str *name)
191
+{
192
+	counter_handle_t h;
193
+	str grp;
194
+
195
+	grp.s = 0;
196
+	grp.len = 0;
197
+	if (counter_lookup_str(&h, &grp, name) < 0)
198
+		return 0;
199
+	return (void*)(unsigned long)h.id;
200
+}
201
+
202
+
203
+
204
+inline static unsigned long get_stat_val(stat_var *v)
205
+{
206
+	counter_handle_t h;
207
+	h.id = (unsigned short)(unsigned long)v;
208
+	return (unsigned long)counter_get_val(h);
209
+}
210
+
211
+
212
+
213
+inline static char* get_stat_name(stat_var *v)
214
+{
215
+	counter_handle_t h;
216
+	h.id = (unsigned short)(unsigned long)v;
217
+	return counter_get_name(h);
218
+}
219
+
220
+
221
+
222
+inline static char* get_stat_module(stat_var *v)
223
+{
224
+	counter_handle_t h;
225
+	h.id = (unsigned short)(unsigned long)v;
226
+	return counter_get_group(h);
227
+}
228
+
229
+
230
+
231
+inline static void update_stat(stat_var* v, int n)
232
+{
233
+	counter_handle_t h;
234
+	h.id = (unsigned short)(unsigned long)v;
235
+	counter_add(h, n);
236
+}
237
+
238
+
239
+
240
+inline static void reset_stat(stat_var* v)
241
+{
242
+	counter_handle_t h;
243
+	h.id = (unsigned short)(unsigned long)v;
244
+	counter_reset(h);
245
+}
246
+
247
+
248
+#define if_update_stat(c, var, n) \
249
+	do{ \
250
+		if ((c)) update_stat((var), (n)); \
251
+	}while(0)
252
+
253
+#define if_reset_stat(c, var) \
254
+	do{ \
255
+		if ((c)) reset_stat((var)); \
256
+	}while(0)
257
+
258
+#else /* STATISTICS */
259
+
260
+/* statistics support check */
261
+#define stats_support() 0
262
+#define register_module_stats(mod, stats) 0
263
+#define register_stat(mod, name, var, flags) 0
264
+#define get_stat(name)  0
265
+#define get_stat_val(var) 0
266
+#define update_stat(v, n)
267
+#define reset_stat(v)
268
+#define if_update_stat(c, v, n)
269
+#define if_reset_stat(c, v)
270
+
271
+#endif /* STATISTICS */
272
+
146 273
 #endif /*__counters_h*/
147 274
 
148 275
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */
Browse code

core : Update include files - delete IDs, update doxygen, delete history

Olle E. Johansson authored on 03/01/2015 10:55:48
Showing 1 changed files
... ...
@@ -1,6 +1,4 @@
1 1
 /* 
2
- * $Id$
3
- * 
4 2
  * Copyright (C) 2010 iptelorg GmbH
5 3
  *
6 4
  * Permission to use, copy, modify, and distribute this software for any
... ...
@@ -15,7 +13,8 @@
15 13
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 14
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 15
  */
18
-/** counter/stats.
16
+/** Kamailio core :: counter/stats.
17
+ * @author andrei
19 18
  * @file counters.h
20 19
  * @ingroup:  core
21 20
  *
... ...
@@ -31,11 +30,6 @@
31 30
  *  4. get a counter value (the handle can be obtained like above)
32 31
  *    val = counter_get(h);
33 32
  */
34
-/*
35
- * History:
36
- * --------
37
- *  2010-08-06  initial version (andrei)
38
-*/
39 33
 
40 34
 #ifndef __counters_h
41 35
 #define __counters_h
Browse code

core: added function to check if counters have been initialized

Daniel-Constantin Mierla authored on 03/12/2013 22:38:08
Showing 1 changed files
... ...
@@ -87,6 +87,7 @@ extern int _cnts_row_len; /* number of elements per row */
87 87
 
88 88
 
89 89
 
90
+int counters_initialized(void);
90 91
 int init_counters(void);
91 92
 void destroy_counters(void);
92 93
 int counters_prefork_init(int max_process_no);
Browse code

core: Added void to declarations of functions with empty argument list

In C language, a declaration in the form int f(); is equivalent to int f(...);, thus being able to accept an indefinit number of parameters. With the -Wstrict-prototypes GCC options, these declarations are reported as "function declaration isn’t a prototype".
On some cases, this may trick the compiler into generating unoptimized code (like preparing to handle variadic argument list).
In all cases having a declaration int f() and a definition inf f(int) is missleading, even if standard compliant.
This is still Work in Progress. (maybe adding the -Wstrict-prototypes option to default is desireable)

Marius Zbihlei authored on 02/04/2012 14:19:17
Showing 1 changed files
... ...
@@ -87,8 +87,8 @@ extern int _cnts_row_len; /* number of elements per row */
87 87
 
88 88
 
89 89
 
90
-int init_counters();
91
-void destroy_counters();
90
+int init_counters(void);
91
+void destroy_counters(void);
92 92
 int counters_prefork_init(int max_process_no);
93 93
 
94 94
 
Browse code

core: counters can be used before forking

Counters can now be incremented (not only registered) before
counters_prefork_init(). This enables using them also from
mod_init() or the fixup functions (not recommended but needed for
"indirect" calls).
Fixes crashes when modules call a counter using function from
mod_init() or a fixup.
E.g.: xlog(s) does a dns lookup from mod_init. If the lookup fails
the dns code will try to increment the new dns.failed_dns_request
counter.

Reported-by: Michal Matyska michal.matyska iptel org

Andrei Pelinescu-Onciul authored on 24/08/2010 19:14:50
Showing 1 changed files
... ...
@@ -109,7 +109,10 @@ char* counter_get_name(counter_handle_t handle);
109 109
 char* counter_get_group(counter_handle_t handle);
110 110
 char* counter_get_doc(counter_handle_t handle);
111 111
 
112
-/** gets the per process value of counter h for process p_no. */
112
+/** gets the per process value of counter h for process p_no.
113
+ *  Note that if used before counter_prefork_init() process_no is 0
114
+ *  and _cnts_vals will point into a temporary one "row"  array.
115
+ */
113 116
 #define counter_pprocess_val(p_no, h) \
114 117
 	_cnts_vals[(p_no) * _cnts_row_len + (h).id].v
115 118
 
Browse code

core: counters arrays and counters descriptions

- each counter must have a description / help string.
- allow null handles in counter_register().
- counters arrays can be used to conveniently describe and
register several counters in the same time.
E.g.:
counter_def_t my_counters_defs[] = {
{&h1, "counter1", 0, 0, 0, "test counter1"},
{&h2, "counter2", 0, 0, 0, "test counter2"},
{0, "counter3", 0, get_cbk, 0, "counter with callback"},
{0, 0, 0, 0, 0, 0}
};

counter_register_array("my_counters", my_counters_defs);

Andrei Pelinescu-Onciul authored on 09/08/2010 14:51:36
Showing 1 changed files
... ...
@@ -22,7 +22,7 @@
22 22
  *  Example usage:
23 23
  *  1. register (must be before forking, e.g. from mod_init()):
24 24
  *    counter_handle_t h;
25
- *    counter_register(&h, "my_counters", "foo", 0, 0, 0, 0);
25
+ *    counter_register(&h, "my_counters", "foo", 0, 0, 0, "test counter", 0);
26 26
  *  2. increment/add:
27 27
  *    counter_inc(h);
28 28
  *    counter_add(h, 100);
... ...
@@ -62,6 +62,26 @@ typedef struct counter_handle_s counter_handle_t;
62 62
 typedef struct counter_val_s counter_array_t;
63 63
 typedef counter_val_t (*counter_cbk_f)(counter_handle_t h, void* param);
64 64
 
65
+
66
+
67
+/* counter definition structure, used in zero term. arrays for more
68
+ *  convenient registration of several counters at once
69
+ *  (see counter_register_array(group, counter_array)).
70
+ */
71
+struct counter_def_s {
72
+	counter_handle_t* handle; /** if non 0, will be filled with the counter
73
+							     handle */
74
+	const char* name;         /**< counter name (inside the group) */
75
+	int flags;                /**< counter flags */
76
+	counter_cbk_f get_cbk;    /**< callback function for reading */
77
+	void* get_cbk_param;      /**< callback parameter */
78
+	const char* descr;        /**< description/documentation string */
79
+};
80
+
81
+typedef struct counter_def_s counter_def_t;
82
+
83
+
84
+
65 85
 extern counter_array_t* _cnts_vals;
66 86
 extern int _cnts_row_len; /* number of elements per row */
67 87
 
... ...
@@ -71,10 +91,15 @@ int init_counters();
71 91
 void destroy_counters();
72 92
 int counters_prefork_init(int max_process_no);
73 93
 
74
-int counter_register(	counter_handle_t* handle, char* group, char* name,
75
-						int flags, counter_cbk_f cbk, void* cbk_param,
94
+
95
+int counter_register_array(const char* group, counter_def_t* defs);
96
+int counter_register(	counter_handle_t* handle, const char* group,
97
+						const char* name, int flags,
98
+						counter_cbk_f cbk, void* cbk_param,
99
+						const char* doc,
76 100
 						int reg_flags);
77
-int counter_lookup(counter_handle_t* handle, char* group, char* name);
101
+int counter_lookup(counter_handle_t* handle,
102
+						const char* group, const char* name);
78 103
 int counter_lookup_str(counter_handle_t* handle, str* group, str* name);
79 104
 
80 105
 void counter_reset(counter_handle_t handle);
... ...
@@ -82,6 +107,7 @@ counter_val_t counter_get_val(counter_handle_t handle);
82 107
 counter_val_t counter_get_raw_val(counter_handle_t handle);
83 108
 char* counter_get_name(counter_handle_t handle);
84 109
 char* counter_get_group(counter_handle_t handle);
110
+char* counter_get_doc(counter_handle_t handle);
85 111
 
86 112
 /** gets the per process value of counter h for process p_no. */
87 113
 #define counter_pprocess_val(p_no, h) \
... ...
@@ -111,10 +137,10 @@ inline static void counter_add(counter_handle_t handle, int v)
111 137
 
112 138
 
113 139
 void counter_iterate_grp_names(void (*cbk)(void* p, str* grp_name), void* p);
114
-void counter_iterate_grp_var_names(	char* group,
140
+void counter_iterate_grp_var_names(	const char* group,
115 141
 									void (*cbk)(void* p, str* var_name),
116 142
 									void* p);
117
-void counter_iterate_grp_vars(char* group,
143
+void counter_iterate_grp_vars(const char* group,
118 144
 							  void (*cbk)(void* p, str* g, str* n,
119 145
 								  			counter_handle_t h),
120 146
 							  void *p);
Browse code

core: counters / statistics support

Efficient counters support (api).
Incrementing or adding a value to a defined counter is extremely
fast at the cost of slower reading (each process has its own
set of counters => no cacheline ping-pong and no expensive atomic
operations).
All counters must be defined before forking (in a module this
means from mod_init() or init_child(rank==PROC_INIT)).
A counter is uniquely identified by its group and its name
(and not only by its name like in kamailio stats).

Example (error checking skipped):
/* before forking, e.g. mod_init() */
counter_handle_t h;
/* declare the counter my_grp.foo */
counter_register(&h, "my_grp", "foo", 0, 0, 0, 0);

/* after forking */
counter_inc(h);

/* getting a counter value, knowing its group and name*/
counter_lookup(&h, "my_grp", "foo");
val = counter_get(h);

Andrei Pelinescu-Onciul authored on 08/08/2010 16:41:30
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,124 @@
1
+/* 
2
+ * $Id$
3
+ * 
4
+ * Copyright (C) 2010 iptelorg GmbH
5
+ *
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
9
+ *
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
+ */
18
+/** counter/stats.
19
+ * @file counters.h
20
+ * @ingroup:  core
21
+ *
22
+ *  Example usage:
23
+ *  1. register (must be before forking, e.g. from mod_init()):
24
+ *    counter_handle_t h;
25
+ *    counter_register(&h, "my_counters", "foo", 0, 0, 0, 0);
26
+ *  2. increment/add:
27
+ *    counter_inc(h);
28
+ *    counter_add(h, 100);
29
+ *  3. get and existing counter handle, knowing its group and name
30
+ *    counter_lookup(&h, "my_counters", "foo");
31
+ *  4. get a counter value (the handle can be obtained like above)
32
+ *    val = counter_get(h);
33
+ */
34
+/*
35
+ * History:
36
+ * --------
37
+ *  2010-08-06  initial version (andrei)
38
+*/
39
+
40
+#ifndef __counters_h
41
+#define __counters_h
42
+
43
+#include "pt.h"
44
+
45
+/* counter flags */
46
+#define CNT_F_NO_RESET 1 /* don't reset */
47
+
48
+typedef long counter_val_t;
49
+
50
+/* use a struct. to force errors on direct access attempts */
51
+struct counter_handle_s {
52
+	unsigned short id;
53
+};
54
+
55
+
56
+struct counter_val_s {
57
+	counter_val_t v;
58
+};
59
+
60
+
61
+typedef struct counter_handle_s counter_handle_t;
62
+typedef struct counter_val_s counter_array_t;
63
+typedef counter_val_t (*counter_cbk_f)(counter_handle_t h, void* param);
64
+
65
+extern counter_array_t* _cnts_vals;
66
+extern int _cnts_row_len; /* number of elements per row */
67
+
68
+
69
+
70
+int init_counters();
71
+void destroy_counters();
72
+int counters_prefork_init(int max_process_no);
73
+
74
+int counter_register(	counter_handle_t* handle, char* group, char* name,
75
+						int flags, counter_cbk_f cbk, void* cbk_param,
76
+						int reg_flags);
77
+int counter_lookup(counter_handle_t* handle, char* group, char* name);
78
+int counter_lookup_str(counter_handle_t* handle, str* group, str* name);
79
+
80
+void counter_reset(counter_handle_t handle);
81
+counter_val_t counter_get_val(counter_handle_t handle);
82
+counter_val_t counter_get_raw_val(counter_handle_t handle);
83
+char* counter_get_name(counter_handle_t handle);
84
+char* counter_get_group(counter_handle_t handle);
85
+
86
+/** gets the per process value of counter h for process p_no. */
87
+#define counter_pprocess_val(p_no, h) \
88
+	_cnts_vals[(p_no) * _cnts_row_len + (h).id].v
89
+
90
+
91
+
92
+/** increments a counter.
93
+ * @param handle - counter handle.
94
+ */
95
+inline static void counter_inc(counter_handle_t handle)
96
+{
97
+	counter_pprocess_val(process_no, handle)++;
98
+}
99
+
100
+
101
+
102
+/** adds a value to a counter.
103
+ * @param handle - counter handle.
104
+ * @param v - value.
105
+ */
106
+inline static void counter_add(counter_handle_t handle, int v)
107
+{
108
+	counter_pprocess_val(process_no, handle)+=v;
109
+}
110
+
111
+
112
+
113
+void counter_iterate_grp_names(void (*cbk)(void* p, str* grp_name), void* p);
114
+void counter_iterate_grp_var_names(	char* group,
115
+									void (*cbk)(void* p, str* var_name),
116
+									void* p);
117
+void counter_iterate_grp_vars(char* group,
118
+							  void (*cbk)(void* p, str* g, str* n,
119
+								  			counter_handle_t h),
120
+							  void *p);
121
+
122
+#endif /*__counters_h*/
123
+
124
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */