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 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,696 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2010 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/** counters/stats.
18
+ * @file counters.c
19
+ * @ingroup: core
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2010-08-06  initial version (andrei)
25
+*/
26
+
27
+#include "counters.h"
28
+#include "str_hash.h"
29
+#include "str.h"
30
+#include "compiler_opt.h"
31
+#include "mem/mem.h"
32
+#include "mem/shm_mem.h"
33
+
34
+
35
+#define CNT_HASH_SIZE	64
36
+/* group hash size (rpc use) */
37
+#define GRP_HASH_SIZE	16
38
+/* initial sorted groups array size (rpc use) */
39
+#define GRP_SORTED_SIZE	16
40
+/* intial counter id 2 record array size */
41
+#define CNT_ID2RECORD_SIZE	64
42
+
43
+#define CACHELINE_PAD 128
44
+
45
+
46
+
47
+/* leave space for one flag */
48
+#define MAX_COUNTER_ID 32767
49
+
50
+struct counter_record {
51
+	str group;
52
+	str name;
53
+	counter_handle_t h;
54
+	unsigned short flags;
55
+	void* cbk_param;
56
+	counter_cbk_f cbk;
57
+	struct counter_record* grp_next; /* next in group */
58
+};
59
+
60
+
61
+struct grp_record {
62
+	str group;
63
+	struct counter_record* first;
64
+};
65
+
66
+
67
+
68
+/** hash table mapping a counter name to an id */
69
+static struct str_hash_table cnts_hash_table;
70
+/** array maping id 2 record */
71
+struct counter_record** cnt_id2record;
72
+static int cnt_id2record_size;
73
+/** hash table for groups (maps a group name to a counter list) */
74
+static struct str_hash_table grp_hash_table;
75
+/** array of groups, sorted */
76
+static struct grp_record** grp_sorted;
77
+static int grp_sorted_max_size;
78
+static int grp_sorted_crt_size;
79
+static int grp_no; /* number of groups */
80
+
81
+/** counters array. a[proc_no][counter_id] =>
82
+  _cnst_vals[proc_no*cnts_no+counter_id] */
83
+counter_array_t* _cnts_vals;
84
+int _cnts_row_len; /* number of elements per row */
85
+static int cnts_no; /* number of registered counters */
86
+static int cnts_max_rows; /* set to process number */
87
+
88
+
89
+
90
+/** init the coutner hash table(s).
91
+ * @return 0 on success, -1 on error.
92
+ */
93
+int init_counters()
94
+{
95
+	if (str_hash_alloc(&cnts_hash_table, CNT_HASH_SIZE) < 0)
96
+		goto error;
97
+	str_hash_init(&cnts_hash_table);
98
+	if (str_hash_alloc(&grp_hash_table, GRP_HASH_SIZE) < 0)
99
+		goto error;
100
+	str_hash_init(&grp_hash_table);
101
+	cnts_no = 1; /* start at 1 (0 used only for invalid counters) */
102
+	grp_no = 0;
103
+	cnt_id2record_size = CNT_ID2RECORD_SIZE;
104
+	cnt_id2record = pkg_malloc(sizeof(*cnt_id2record) * cnt_id2record_size);
105
+	if (cnt_id2record == 0)
106
+		goto error;
107
+	memset(cnt_id2record, 0, sizeof(*cnt_id2record) * cnt_id2record_size);
108
+	grp_sorted_max_size = GRP_SORTED_SIZE;
109
+	grp_sorted_crt_size = 0;
110
+	grp_sorted = pkg_malloc(sizeof(*grp_sorted) * grp_sorted_max_size);
111
+	if (grp_sorted == 0)
112
+		goto error;
113
+	memset(grp_sorted, 0, sizeof(*grp_sorted) * grp_sorted_max_size);
114
+	return 0;
115
+error:
116
+	destroy_counters();
117
+	return -1;
118
+}
119
+
120
+
121
+
122
+void destroy_counters()
123
+{
124
+	int r;
125
+	struct str_hash_entry* e;
126
+	struct str_hash_entry* bak;
127
+	if (_cnts_vals) {
128
+		shm_free(_cnts_vals);
129
+		_cnts_vals = 0;
130
+	}
131
+	if (cnts_hash_table.table) {
132
+		for (r=0; r< cnts_hash_table.size; r++) {
133
+			clist_foreach_safe(&cnts_hash_table.table[r], e, bak, next) {
134
+				pkg_free(e);
135
+			}
136
+		}
137
+		pkg_free(cnts_hash_table.table);
138
+	}
139
+	if (grp_hash_table.table) {
140
+		for (r=0; r< grp_hash_table.size; r++) {
141
+			clist_foreach_safe(&grp_hash_table.table[r], e, bak, next) {
142
+				pkg_free(e);
143
+			}
144
+		}
145
+		pkg_free(grp_hash_table.table);
146
+	}
147
+	if (cnt_id2record)
148
+		pkg_free(cnt_id2record);
149
+	if (grp_sorted)
150
+		pkg_free(grp_sorted);
151
+	cnts_hash_table.table = 0;
152
+	cnts_hash_table.size = 0;
153
+	cnt_id2record = 0;
154
+	grp_sorted = 0;
155
+	grp_hash_table.table = 0;
156
+	grp_hash_table.size = 0;
157
+	grp_sorted_crt_size = 0;
158
+	grp_sorted_max_size = 0;
159
+	cnts_no = 0;
160
+	_cnts_row_len = 0;
161
+	grp_no = 0;
162
+}
163
+
164
+
165
+
166
+/** complete counter intialization, when the number of processes is known.
167
+ * shm must be available.
168
+ * @return 0 on success, < 0 on error
169
+ */
170
+int counters_prefork_init(int max_process_no)
171
+{
172
+	int size, row_size;
173
+	/* round cnts_no so that cnts_no * sizeof(counter) it's a CACHELINE_PAD
174
+	   multiple */
175
+	/* round-up row_size to a CACHELINE_PAD multiple  if needed */
176
+	row_size = ((sizeof(*_cnts_vals) * cnts_no - 1) / CACHELINE_PAD + 1) *
177
+		CACHELINE_PAD;
178
+	/* round-up the resulted row_siue to a sizeof(*_cnts_vals) multiple */
179
+	row_size = ((row_size -1) / sizeof(*_cnts_vals) + 1) *
180
+				sizeof(*_cnts_vals);
181
+	/* get updated cnts_no (row length) */
182
+	_cnts_row_len = row_size / sizeof(*_cnts_vals);
183
+	size = max_process_no * row_size;
184
+	_cnts_vals = shm_malloc(max_process_no * row_size);
185
+	if (_cnts_vals == 0)
186
+		return -1;
187
+	memset(_cnts_vals, 0, max_process_no * row_size);
188
+	cnts_max_rows = max_process_no;
189
+	return 0;
190
+}
191
+
192
+
193
+
194
+/** adds new group to the group hash table (no checks, internal version).
195
+ * @return pointer to new group record on success, 0 on error.
196
+ */
197
+static struct grp_record* grp_hash_add(str* group)
198
+{
199
+	struct str_hash_entry* g;
200
+	struct grp_record* grp_rec;
201
+	struct grp_record** r;
202
+
203
+	/* grp_rec copied at &g->u.data */
204
+	g = pkg_malloc(sizeof(struct str_hash_entry) - sizeof(g->u.data) +
205
+					sizeof(*grp_rec) + group->len + 1);
206
+	if (g == 0)
207
+		goto error;
208
+	grp_rec = (struct grp_record*)&g->u.data[0];
209
+	grp_rec->group.s = (char*)(grp_rec + 1);
210
+	grp_rec->group.len = group->len;
211
+	grp_rec->first = 0;
212
+	memcpy(grp_rec->group.s, group->s, group->len + 1);
213
+	g->key = grp_rec->group;
214
+	g->flags = 0;
215
+	/* insert group into the sorted group array */
216
+	if (grp_sorted_max_size <= grp_sorted_crt_size) {
217
+		/* must increase the array */
218
+		r = pkg_realloc(grp_sorted, 2 * grp_sorted_max_size *
219
+						sizeof(*grp_sorted));
220
+		if (r == 0)
221
+			goto error;
222
+		grp_sorted= r;
223
+		grp_sorted_max_size *= 2;
224
+		memset(&grp_sorted[grp_sorted_crt_size], 0,
225
+				(grp_sorted_max_size - grp_sorted_crt_size) *
226
+				sizeof(*grp_sorted));
227
+	}
228
+	for (r = grp_sorted; r < (grp_sorted + grp_sorted_crt_size); r++)
229
+		if (strcmp(grp_rec->group.s, (*r)->group.s) < 0)
230
+			break;
231
+	if (r != (grp_sorted + grp_sorted_crt_size))
232
+		memmove(r+1, r, (int)(long)((char*)(grp_sorted + grp_sorted_crt_size) -
233
+						(char*)r));
234
+	grp_sorted_crt_size++;
235
+	*r = grp_rec;
236
+	/* insert into the hash only on success */
237
+	str_hash_add(&grp_hash_table, g);
238
+	return grp_rec;
239
+error:
240
+	if (g)
241
+		pkg_free(g);
242
+	return 0;
243
+}
244
+
245
+
246
+
247
+/** lookup a group into the group hash (internal version).
248
+ * @return pointer to grp_record on success, 0 on failure (not found).
249
+ */
250
+static struct grp_record* grp_hash_lookup(str* group)
251
+{
252
+	struct str_hash_entry* e;
253
+	e = str_hash_get(&grp_hash_table, group->s, group->len);
254
+	return e?(struct grp_record*)&e->u.data[0]:0;
255
+}
256
+
257
+
258
+
259
+/** lookup a group and if not found create a new group record.
260
+ * @return pointer to grp_record on succes, 0 on failure ( not found and
261
+ *  failed to create new group record).
262
+ */
263
+static struct grp_record* grp_hash_get_create(str* group)
264
+{
265
+	struct grp_record* ret;
266
+
267
+	ret = grp_hash_lookup(group);
268
+	if (ret)
269
+		return ret;
270
+	return grp_hash_add(group);
271
+}
272
+
273
+
274
+
275
+/** adds new counter to the hash table (no checks, internal version).
276
+ * @return pointer to new record on success, 0 on error.
277
+ */
278
+static struct counter_record* cnt_hash_add(
279
+							str* group, str* name,
280
+							int flags, counter_cbk_f cbk,
281
+							void* param)
282
+{
283
+	struct str_hash_entry* e;
284
+	struct counter_record* cnt_rec;
285
+	struct grp_record* grp_rec;
286
+	struct counter_record** p;
287
+	
288
+	e = 0;
289
+	if (cnts_no >= MAX_COUNTER_ID)
290
+		/* too many counters */
291
+		goto error;
292
+	grp_rec = grp_hash_get_create(group);
293
+	if (grp_rec == 0)
294
+		/* non existing group an no new one could be created */
295
+		goto error;
296
+	/* cnt_rec copied at &e->u.data[0] */
297
+	e = pkg_malloc(sizeof(struct str_hash_entry) - sizeof(e->u.data) +
298
+					sizeof(*cnt_rec) + name->len + 1 + group->len + 1);
299
+	if (e == 0)
300
+		goto error;
301
+	cnt_rec = (struct counter_record*)&e->u.data[0];
302
+	cnt_rec->group.s = (char*)(cnt_rec + 1);
303
+	cnt_rec->group.len = group->len;
304
+	cnt_rec->name.s = cnt_rec->group.s + group->len + 1;
305
+	cnt_rec->name.len = name->len;
306
+	cnt_rec->h.id = cnts_no++;
307
+	cnt_rec->flags = flags;
308
+	cnt_rec->cbk_param = param;
309
+	cnt_rec->cbk = cbk;
310
+	cnt_rec->grp_next = 0;
311
+	memcpy(cnt_rec->group.s, group->s, group->len + 1);
312
+	memcpy(cnt_rec->name.s, name->s, name->len + 1);
313
+	e->key = cnt_rec->name;
314
+	e->flags = 0;
315
+	/* add it a pointer to it in the records array */
316
+	if (cnt_id2record_size <= cnt_rec->h.id) {
317
+		/* must increase the array */
318
+		p = pkg_realloc(cnt_id2record,
319
+						2 * cnt_id2record_size * sizeof(*cnt_id2record));
320
+		if (p == 0)
321
+			goto error;
322
+		cnt_id2record = p;
323
+		cnt_id2record_size *= 2;
324
+		memset(&cnt_id2record[cnt_rec->h.id], 0,
325
+				(cnt_id2record_size - cnt_rec->h.id) * sizeof(*cnt_id2record));
326
+	}
327
+	cnt_id2record[cnt_rec->h.id] = cnt_rec;
328
+	/* add into the hash */
329
+	str_hash_add(&cnts_hash_table, e);
330
+	/* insert it sorted in the per group list */
331
+	for (p = &grp_rec->first; *p; p = &((*p)->grp_next))
332
+		if (strcmp(cnt_rec->name.s, (*p)->name.s) < 0)
333
+			break;
334
+	cnt_rec->grp_next = *p;
335
+	*p = cnt_rec;
336
+	return cnt_rec;
337
+error:
338
+	if (e)
339
+		pkg_free(e);
340
+	return 0;
341
+}
342
+
343
+
344
+
345
+/** lookup a (group, name) pair into the cnts hash (internal version).
346
+ * @param group - counter group name. If "" the first matching counter with
347
+ *                the given name will be returned (k compat).
348
+ * @param name
349
+ * @return pointer to counter_record on success, 0 on failure (not found).
350
+ */
351
+static struct counter_record* cnt_hash_lookup(str* group, str* name)
352
+{
353
+	struct str_hash_entry* e;
354
+	struct str_hash_entry* first;
355
+	struct counter_record* cnt_rec;
356
+	e = str_hash_get(&cnts_hash_table, name->s, name->len);
357
+	/* fast path */
358
+	if (likely(e)) {
359
+		cnt_rec = (struct counter_record*)&e->u.data[0];
360
+		if (likely( group->len == 0 ||
361
+				(cnt_rec->group.len == group->len &&
362
+				memcmp(cnt_rec->group.s, group->s, group->len) == 0)))
363
+		return cnt_rec;
364
+	} else
365
+		return 0;
366
+	/* search between records with same name, but different groups */
367
+	first = e;
368
+	do {
369
+		cnt_rec = (struct counter_record*)&e->u.data[0];
370
+		if (cnt_rec->group.len == group->len &&
371
+			cnt_rec->name.len  == name->len &&
372
+			memcmp(cnt_rec->group.s, group->s, group->len) == 0 &&
373
+			memcmp(cnt_rec->name.s, name->s, name->len) == 0)
374
+			/* found */
375
+			return cnt_rec;
376
+		e = e->next;
377
+	} while(e != first);
378
+	return 0;
379
+}
380
+
381
+
382
+
383
+/** lookup a counter and if not found create a new counter record.
384
+ * @return pointer to counter_record on succes, 0 on failure ( not found and
385
+ *  failed to create new group record).
386
+ */
387
+static struct counter_record* cnt_hash_get_create(
388
+								str* group, str* name,
389
+								int flags,
390
+								counter_cbk_f cbk,
391
+								void* param)
392
+{
393
+	struct counter_record* ret;
394
+
395
+	ret = cnt_hash_lookup(group, name);
396
+	if (ret)
397
+		return ret;
398
+	return cnt_hash_add(group, name, flags, cbk, param);
399
+}
400
+
401
+
402
+
403
+/** register a new counter.
404
+ * Can be called only before forking (e.g. from mod_init() or
405
+ * init_child(PROC_INIT)).
406
+ * @param handle - result parameter, it will be filled with the counter
407
+ *                  handle on success.
408
+ * @param group - group name
409
+ * @param name  - counter name (group.name must be unique).
410
+ * @param flags  - counter flags: one of CNT_F_*.
411
+ * @param cbk   - read callback function (if set it will be called each time
412
+ *                  someone will call counter_get()).
413
+ * @param cbk_param - callback param.
414
+ * @param reg_flags - register flags: 1 - don't fail if counter already
415
+ *                    registered (act like counter_lookup(handle, group, name).
416
+ * @return 0 on succes, < 0 on error (-1 not init or malloc error, -2 already
417
+ *         registered (and register_flags & 1 == 0).
418
+ */
419
+int counter_register(	counter_handle_t* handle, char* group, char* name,
420
+						int flags,
421
+						counter_cbk_f cbk, void* cbk_param,
422
+						int reg_flags)
423
+{
424
+	str grp;
425
+	str n;
426
+	struct counter_record* cnt_rec;
427
+
428
+	if (unlikely(_cnts_vals)) {
429
+		/* too late */
430
+		BUG("late attempt to register counter: %s.%s\n", group, name);
431
+		goto error;
432
+	}
433
+	n.s = name;
434
+	n.len = strlen(name);
435
+	if (unlikely(group == 0 || *group == 0)) {
436
+		BUG("attempt to register counter %s without a group\n", name);
437
+		goto error;
438
+	}
439
+	grp.s = group;
440
+	grp.len = strlen(group);
441
+	cnt_rec = cnt_hash_lookup(&grp, &n);
442
+	if (cnt_rec) {
443
+		if (reg_flags & 1)
444
+			goto found;
445
+		else {
446
+			handle->id = 0;
447
+			return -2;
448
+		}
449
+	} else
450
+		cnt_rec = cnt_hash_get_create(&grp, &n, flags, cbk, cbk_param);
451
+	if (unlikely(cnt_rec == 0))
452
+		goto error;
453
+found:
454
+	*handle = cnt_rec->h;
455
+	return 0;
456
+error:
457
+	handle->id = 0;
458
+	return -1;
459
+}
460
+
461
+
462
+
463
+/** fill in the handle of an existing counter (str parameters).
464
+  * @param handle - filled with the corresp. handle on success.
465
+  * @param group - counter group name. If "" the first matching
466
+  *                counter with the given name will be returned
467
+  *                (k compat).
468
+  * @param name - counter name.
469
+ * @return 0 on success, < 0 on error
470
+ */
471
+int counter_lookup_str(counter_handle_t* handle, str* group, str* name)
472
+{
473
+	struct counter_record* cnt_rec;
474
+
475
+	cnt_rec = cnt_hash_lookup(group, name);
476
+	if (likely(cnt_rec)) {
477
+		*handle = cnt_rec->h;
478
+		return 0;
479
+	}
480
+	handle->id = 0;
481
+	return -1;
482
+}
483
+
484
+
485
+
486
+/** fill in the handle of an existing counter (asciiz parameters).
487
+  * @param handle - filled with the corresp. handle on success.
488
+  * @param group - counter group name. If 0 or "" the first matching
489
+  *                counter with the given name will be returned
490
+  *                (k compat).
491
+  * @param name - counter name.
492
+ * @return 0 on success, < 0 on error
493
+ */
494
+int counter_lookup(counter_handle_t* handle, char* group, char* name)
495
+{
496
+	str grp;
497
+	str n;
498
+
499
+	n.s = name;
500
+	n.len = strlen(name);
501
+	grp.s = group;
502
+	grp.len = group?strlen(group):0;
503
+	return counter_lookup_str(handle, &grp, &n);
504
+}
505
+
506
+
507
+
508
+/** get the value of the counter, bypassing callbacks.
509
+ * @param handle - counter handle obtained using counter_lookup() or
510
+ *                 counter_register().
511
+ * @return counter value.
512
+ */
513
+counter_val_t counter_get_raw_val(counter_handle_t handle)
514
+{
515
+	int r;
516
+	counter_val_t ret;
517
+
518
+	if (unlikely(_cnts_vals == 0)) {
519
+		/* not init yet */
520
+		BUG("counters not fully initialized yet\n");
521
+		return 0;
522
+	}
523
+	if (unlikely(handle.id >= cnts_no || handle.id < 0)) {
524
+		BUG("invalid counter id %d (max %d)\n", handle.id, cnts_no - 1);
525
+		return 0;
526
+	}
527
+	ret = 0;
528
+	for (r = 0; r < cnts_max_rows; r++)
529
+		ret += counter_pprocess_val(r, handle);
530
+	return ret;
531
+}
532
+
533
+
534
+
535
+/** get the value of the counter, using the callbacks (if defined).
536
+ * @param handle - counter handle obtained using counter_lookup() or
537
+ *                 counter_register().
538
+ * @return counter value. */
539
+counter_val_t counter_get_val(counter_handle_t handle)
540
+{
541
+	struct counter_record* cnt_rec;
542
+
543
+	if (unlikely(_cnts_vals == 0 || cnt_id2record == 0)) {
544
+		/* not init yet */
545
+		BUG("counters not fully initialized yet\n");
546
+		return 0;
547
+	}
548
+	cnt_rec = cnt_id2record[handle.id];
549
+	if (unlikely(cnt_rec->cbk))
550
+		return cnt_rec->cbk(handle, cnt_rec->cbk_param);
551
+	return counter_get_raw_val(handle);
552
+}
553
+
554
+
555
+
556
+/** reset the  counter.
557
+ * Reset a counter, unless it has the CNT_F_NO_RESET flag set.
558
+ * @param handle - counter handle obtained using counter_lookup() or
559
+ *                 counter_register().
560
+ * Note: it's racy.
561
+ */
562
+void counter_reset(counter_handle_t handle)
563
+{
564
+	int r;
565
+
566
+	if (unlikely(_cnts_vals == 0)) {
567
+		/* not init yet */
568
+		BUG("counters not fully initialized yet\n");
569
+		return;
570
+	}
571
+	if (unlikely(handle.id >= cnts_no)) {
572
+		BUG("invalid counter id %d (max %d)\n", handle.id, cnts_no - 1);
573
+		return;
574
+	}
575
+	if (unlikely(cnt_id2record[handle.id]->flags & CNT_F_NO_RESET))
576
+		return;
577
+	for (r=0; r < cnts_max_rows; r++)
578
+		counter_pprocess_val(r, handle) = 0;
579
+	return;
580
+}
581
+
582
+
583
+
584
+/** return the name for counter handle.
585
+ * @param handle - counter handle obtained using counter_lookup() or
586
+ *                 counter_register().
587
+ * @return asciiz pointer on success, 0 on error.
588
+ */
589
+char* counter_get_name(counter_handle_t handle)
590
+{
591
+	if (unlikely(_cnts_vals == 0)) {
592
+		/* not init yet */
593
+		BUG("counters not fully initialized yet\n");
594
+		goto error;
595
+	}
596
+	if (unlikely(handle.id >= cnts_no)) {
597
+		BUG("invalid counter id %d (max %d)\n", handle.id, cnts_no - 1);
598
+		goto error;
599
+	}
600
+	return cnt_id2record[handle.id]->name.s;
601
+error:
602
+	return 0;
603
+}
604
+
605
+
606
+
607
+/** return the group name for counter handle.
608
+ * @param handle - counter handle obtained using counter_lookup() or
609
+ *                 counter_register().
610
+ * @return asciiz pointer on success, 0 on error.
611
+ */
612
+char* counter_get_group(counter_handle_t handle)
613
+{
614
+	if (unlikely(_cnts_vals == 0)) {
615
+		/* not init yet */
616
+		BUG("counters not fully initialized yet\n");
617
+		goto error;
618
+	}
619
+	if (unlikely(handle.id >= cnts_no)) {
620
+		BUG("invalid counter id %d (max %d)\n", handle.id, cnts_no - 1);
621
+		goto error;
622
+	}
623
+	return cnt_id2record[handle.id]->group.s;
624
+error:
625
+	return 0;
626
+}
627
+
628
+
629
+
630
+/** iterate on all the counter group names.
631
+ * @param cbk - pointer to a callback function that will be called for each
632
+ *              group name.
633
+ * @param p   - parameter that will be passed to the callback function
634
+ *              (along the group name).
635
+ */
636
+void counter_iterate_grp_names(void (*cbk)(void* p, str* grp_name), void* p)
637
+{
638
+	int r;
639
+
640
+	for (r=0; r < grp_sorted_crt_size; r++)
641
+		cbk(p, &grp_sorted[r]->group);
642
+}
643
+
644
+
645
+
646
+/** iterate on all the variable names in a specified group.
647
+ * @param group - group name.
648
+ * @param cbk - pointer to a callback function that will be called for each
649
+ *              variable name.
650
+ * @param p   - parameter that will be passed to the callback function
651
+ *              (along the variable name).
652
+ */
653
+void counter_iterate_grp_var_names(	char* group,
654
+									void (*cbk)(void* p, str* var_name),
655
+									void* p)
656
+{
657
+	struct counter_record* r;
658
+	struct grp_record* g;
659
+	str grp;
660
+	
661
+	grp.s = group;
662
+	grp.len = strlen(group);
663
+	g = grp_hash_lookup(&grp);
664
+	if (g)
665
+		for (r = g->first; r; r = r->grp_next)
666
+			cbk(p, &r->name);
667
+}
668
+
669
+
670
+
671
+/** iterate on all the variable names and handles in a specified group.
672
+ * @param group - group name.
673
+ * @param cbk - pointer to a callback function that will be called for each
674
+ *              [variable name, variable handle] pair.
675
+ * @param p   - parameter that will be passed to the callback function
676
+ *              (along the group name, variable name and variable handle).
677
+ */
678
+void counter_iterate_grp_vars(char* group,
679
+							  void (*cbk)(void* p, str* g, str* n,
680
+								  			counter_handle_t h),
681
+							  void *p)
682
+{
683
+	struct counter_record* r;
684
+	struct grp_record* g;
685
+	str grp;
686
+	
687
+	grp.s = group;
688
+	grp.len = strlen(group);
689
+	g = grp_hash_lookup(&grp);
690
+	if (g)
691
+		for (r = g->first; r; r = r->grp_next)
692
+			cbk(p, &r->group, &r->name, r->h);
693
+}
694
+
695
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
0 696
new file mode 100644
... ...
@@ -0,0 +1,124 @@
0
+/* 
1
+ * $Id$
2
+ * 
3
+ * Copyright (C) 2010 iptelorg GmbH
4
+ *
5
+ * Permission to use, copy, modify, and distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+/** counter/stats.
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, 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
+ * History:
35
+ * --------
36
+ *  2010-08-06  initial version (andrei)
37
+*/
38
+
39
+#ifndef __counters_h
40
+#define __counters_h
41
+
42
+#include "pt.h"
43
+
44
+/* counter flags */
45
+#define CNT_F_NO_RESET 1 /* don't reset */
46
+
47
+typedef long counter_val_t;
48
+
49
+/* use a struct. to force errors on direct access attempts */
50
+struct counter_handle_s {
51
+	unsigned short id;
52
+};
53
+
54
+
55
+struct counter_val_s {
56
+	counter_val_t v;
57
+};
58
+
59
+
60
+typedef struct counter_handle_s counter_handle_t;
61
+typedef struct counter_val_s counter_array_t;
62
+typedef counter_val_t (*counter_cbk_f)(counter_handle_t h, void* param);
63
+
64
+extern counter_array_t* _cnts_vals;
65
+extern int _cnts_row_len; /* number of elements per row */
66
+
67
+
68
+
69
+int init_counters();
70
+void destroy_counters();
71
+int counters_prefork_init(int max_process_no);
72
+
73
+int counter_register(	counter_handle_t* handle, char* group, char* name,
74
+						int flags, counter_cbk_f cbk, void* cbk_param,
75
+						int reg_flags);
76
+int counter_lookup(counter_handle_t* handle, char* group, char* name);
77
+int counter_lookup_str(counter_handle_t* handle, str* group, str* name);
78
+
79
+void counter_reset(counter_handle_t handle);
80
+counter_val_t counter_get_val(counter_handle_t handle);
81
+counter_val_t counter_get_raw_val(counter_handle_t handle);
82
+char* counter_get_name(counter_handle_t handle);
83
+char* counter_get_group(counter_handle_t handle);
84
+
85
+/** gets the per process value of counter h for process p_no. */
86
+#define counter_pprocess_val(p_no, h) \
87
+	_cnts_vals[(p_no) * _cnts_row_len + (h).id].v
88
+
89
+
90
+
91
+/** increments a counter.
92
+ * @param handle - counter handle.
93
+ */
94
+inline static void counter_inc(counter_handle_t handle)
95
+{
96
+	counter_pprocess_val(process_no, handle)++;
97
+}
98
+
99
+
100
+
101
+/** adds a value to a counter.
102
+ * @param handle - counter handle.
103
+ * @param v - value.
104
+ */
105
+inline static void counter_add(counter_handle_t handle, int v)
106
+{
107
+	counter_pprocess_val(process_no, handle)+=v;
108
+}
109
+
110
+
111
+
112
+void counter_iterate_grp_names(void (*cbk)(void* p, str* grp_name), void* p);
113
+void counter_iterate_grp_var_names(	char* group,
114
+									void (*cbk)(void* p, str* var_name),
115
+									void* p);
116
+void counter_iterate_grp_vars(char* group,
117
+							  void (*cbk)(void* p, str* g, str* n,
118
+								  			counter_handle_t h),
119
+							  void *p);
120
+
121
+#endif /*__counters_h*/
122
+
123
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */
... ...
@@ -176,6 +176,7 @@
176 176
 #include "rand/fastrand.h" /* seed */
177 177
 
178 178
 #include "stats.h"
179
+#include "counters.h"
179 180
 #include "cfg/cfg.h"
180 181
 #include "cfg/cfg_struct.h"
181 182
 #include "cfg_core.h"
... ...
@@ -546,6 +547,7 @@ void cleanup(show_status)
546 546
 	destroy_nonsip_hooks();
547 547
 	destroy_routes();
548 548
 	destroy_atomic_ops();
549
+	destroy_counters();
549 550
 	memlog=cfg_get(core, core_cfg, memlog);
550 551
 #ifdef PKG_MALLOC
551 552
 	if (show_status && memlog <= cfg_get(core, core_cfg, debug)){
... ...
@@ -1264,6 +1266,7 @@ int main_loop()
1264 1264
 						" exiting\n");
1265 1265
 			goto error;
1266 1266
 		}
1267
+		if (counters_prefork_init(get_max_procs()) == -1) goto error;
1267 1268
 
1268 1269
 #ifdef USE_SLOW_TIMER
1269 1270
 		/* we need another process to act as the "slow" timer*/
... ...
@@ -1432,6 +1435,7 @@ int main_loop()
1432 1432
 					" exiting\n");
1433 1433
 			goto error;
1434 1434
 		}
1435
+		if (counters_prefork_init(get_max_procs()) == -1) goto error;
1435 1436
 
1436 1437
 
1437 1438
 		/* udp processes */
... ...
@@ -1669,7 +1673,9 @@ int main(int argc, char** argv)
1669 1669
 		"s:"
1670 1670
 #endif
1671 1671
 	;
1672
-	
1672
+	/* init counters / stats */
1673
+	if (init_counters() == -1)
1674
+		goto error;
1673 1675
 #ifdef USE_TCP
1674 1676
 	init_tcp_options(); /* set the defaults before the config */
1675 1677
 #endif
... ...
@@ -2155,7 +2161,7 @@ try_again:
2155 2155
 		goto error;
2156 2156
 	}
2157 2157
 #ifdef USE_DST_BLACKLIST_STATS
2158
-	/* preinitializing before the nubmer of processes is determined */
2158
+	/* preinitializing before the number of processes is determined */
2159 2159
 	if (init_dst_blacklist_stats(1)<0){
2160 2160
 		LOG(L_CRIT, "could not initialize the dst blacklist measurement\n");
2161 2161
 		goto error;