pkg/kamailio/fedora/16: Updated rel in .spec to dev2
[sip-router] / counters.h
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, "test counter", 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
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
85 extern counter_array_t* _cnts_vals;
86 extern int _cnts_row_len; /* number of elements per row */
87
88
89
90 int init_counters(void);
91 void destroy_counters(void);
92 int counters_prefork_init(int max_process_no);
93
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,
100                                                 int reg_flags);
101 int counter_lookup(counter_handle_t* handle,
102                                                 const char* group, const char* name);
103 int counter_lookup_str(counter_handle_t* handle, str* group, str* name);
104
105 void counter_reset(counter_handle_t handle);
106 counter_val_t counter_get_val(counter_handle_t handle);
107 counter_val_t counter_get_raw_val(counter_handle_t handle);
108 char* counter_get_name(counter_handle_t handle);
109 char* counter_get_group(counter_handle_t handle);
110 char* counter_get_doc(counter_handle_t handle);
111
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  */
116 #define counter_pprocess_val(p_no, h) \
117         _cnts_vals[(p_no) * _cnts_row_len + (h).id].v
118
119
120
121 /** increments a counter.
122  * @param handle - counter handle.
123  */
124 inline static void counter_inc(counter_handle_t handle)
125 {
126         counter_pprocess_val(process_no, handle)++;
127 }
128
129
130
131 /** adds a value to a counter.
132  * @param handle - counter handle.
133  * @param v - value.
134  */
135 inline static void counter_add(counter_handle_t handle, int v)
136 {
137         counter_pprocess_val(process_no, handle)+=v;
138 }
139
140
141
142 void counter_iterate_grp_names(void (*cbk)(void* p, str* grp_name), void* p);
143 void counter_iterate_grp_var_names(     const char* group,
144                                                                         void (*cbk)(void* p, str* var_name),
145                                                                         void* p);
146 void counter_iterate_grp_vars(const char* group,
147                                                           void (*cbk)(void* p, str* g, str* n,
148                                                                                         counter_handle_t h),
149                                                           void *p);
150
151 #endif /*__counters_h*/
152
153 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */