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