aboutsummaryrefslogtreecommitdiff
path: root/src/core/stats.c
blob: f01f642eed35885439589dc6c2c2290414796692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <stdio.h>
#include <string.h>

#include "core/defs.h"
#include "core/nng_impl.h"
#include "nng/nng.h"

typedef struct nng_stat nni_stat;

struct nng_stat {
	const nni_stat_info *s_info;
	const nni_stat_item *s_item; // Used during snapshot collection
	nni_list             s_children;
	nni_stat            *s_parent;
	nni_list_node        s_node;
	nni_time             s_timestamp;
	union {
		int      sv_id;
		bool     sv_bool;
		uint64_t sv_value;
		char    *sv_string;
	} s_val;
};

#ifdef NNG_ENABLE_STATS
static nni_stat_info stats_root_info = {
	.si_name = "",
	.si_desc = "all statistics",
	.si_type = NNG_STAT_SCOPE,
};

static nni_stat_item stats_root = {
	.si_children = NNI_LIST_INITIALIZER(
	    stats_root.si_children, nni_stat_item, si_node),
	.si_info = &stats_root_info,
};
static nni_mtx stats_lock     = NNI_MTX_INITIALIZER;
static nni_mtx stats_val_lock = NNI_MTX_INITIALIZER;
#endif

void
nni_stat_add(nni_stat_item *parent, nni_stat_item *child)
{
#ifdef NNG_ENABLE_STATS
	// Make sure that the lists for both children and parents
	// are correctly initialized.
	if (parent->si_children.ll_head.ln_next == NULL) {
		NNI_LIST_INIT(&parent->si_children, nni_stat_item, si_node);
	}
	if (child->si_children.ll_head.ln_next == NULL) {
		NNI_LIST_INIT(&child->si_children, nni_stat_item, si_node);
	}
	nni_list_append(&parent->si_children, child);
#else
	NNI_ARG_UNUSED(parent);
	NNI_ARG_UNUSED(child);
#endif
}

// nni_stat_register registers a stat tree, acquiring the lock
// on the stats structures before doing so.
void
nni_stat_register(nni_stat_item *child)
{
#ifdef NNG_ENABLE_STATS
	nni_mtx_lock(&stats_lock);
	nni_stat_add(&stats_root, child);
	nni_mtx_unlock(&stats_lock);
#else
	NNI_ARG_UNUSED(child);
#endif
}

#ifdef NNG_ENABLE_STATS
void
stat_unregister(nni_stat_item *item)
{
	nni_stat_item *child;
	while ((child = nni_list_first(&item->si_children)) != NULL) {
		stat_unregister(child);
	}
	if ((item->si_info->si_alloc) &&
	    (item->si_info->si_type == NNG_STAT_STRING)) {
		nni_strfree(item->si_u.sv_string);
		item->si_u.sv_string = NULL;
	}
	nni_list_node_remove(&item->si_node);
}
#endif

void
nni_stat_unregister(nni_stat_item *item)
{
#ifdef NNG_ENABLE_STATS
	nni_mtx_lock(&stats_lock);
	stat_unregister(item);
	nni_mtx_unlock(&stats_lock);
#else
	NNI_ARG_UNUSED(item);
#endif
}

void
nni_stat_init_lock(
    nni_stat_item *item, const nni_stat_info *info, nni_mtx *mtx)
{
#ifdef NNG_ENABLE_STATS
	memset(item, 0, sizeof(*item));
	NNI_LIST_INIT(&item->si_children, nni_stat_item, si_node);
	item->si_info = info;
	item->si_mtx  = mtx;
#else
	NNI_ARG_UNUSED(item);
	NNI_ARG_UNUSED(info);
	NNI_ARG_UNUSED(mtx);
#endif
}

void
nni_stat_init(nni_stat_item *item, const nni_stat_info *info)
{
	nni_stat_init_lock(item, info, NULL);
}

void
nni_stat_inc(nni_stat_item *item, uint64_t inc)
{
#ifdef NNG_ENABLE_STATS
	if (item->si_info->si_atomic) {
		nni_atomic_add64(&item->si_u.sv_atomic, inc);
	} else {
		item->si_u.sv_number += inc;
	}
#else
	NNI_ARG_UNUSED(item);
	NNI_ARG_UNUSED(inc);
#endif
}

void
nni_stat_dec(nni_stat_item *item, uint64_t inc)
{
#ifdef NNG_ENABLE_STATS

	if (item->si_info->si_atomic) {
		nni_atomic_sub64(&item->si_u.sv_atomic, inc);
	} else {
		item->si_u.sv_number -= inc;
	}
#else
	NNI_ARG_UNUSED(item);
	NNI_ARG_UNUSED(inc);
#endif
}

void
nni_stat_set_id(nni_stat_item *item, int id)
{
#ifdef NNG_ENABLE_STATS
	// IDs don't change, so just set it.
	item->si_u.sv_id = id;
#else
	NNI_ARG_UNUSED(item);
	NNI_ARG_UNUSED(id);
#endif
}

void
nni_stat_set_bool(nni_stat_item *item, bool b)
{
#ifdef NNG_ENABLE_STATS
	// bool is atomic by definitions.
	item->si_u.sv_bool = b;
#else
	NNI_ARG_UNUSED(item);
	NNI_ARG_UNUSED(b);
#endif
}

void
nni_stat_set_string(nni_stat_item *item, const char *s)
{
#ifdef NNG_ENABLE_STATS
	const nni_stat_info *info = item->si_info;
	char                *old  = item->si_u.sv_string;

	nni_mtx_lock(&stats_val_lock);
	if ((s != NULL) && (old != NULL) && (strcmp(s, old) == 0)) {
		// no change
		nni_mtx_unlock(&stats_val_lock);
		return;
	}

	if (!info->si_alloc) {
		// no allocation, just set it.
		item->si_u.sv_string = (char *) s;
		nni_mtx_unlock(&stats_val_lock);
		return;
	}

	item->si_u.sv_string = nni_strdup(s);
	nni_mtx_unlock(&stats_val_lock);

	nni_strfree(old);
#else
	NNI_ARG_UNUSED(item);
	NNI_ARG_UNUSED(s);
#endif
}

void
nni_stat_set_value(nni_stat_item *item, uint64_t v)
{
#ifdef NNG_ENABLE_STATS
	if (item->si_info->si_atomic) {
		nni_atomic_set64(&item->si_u.sv_atomic, v);
	} else {
		item->si_u.sv_number = v;
	}
#else
	NNI_ARG_UNUSED(item);
	NNI_ARG_UNUSED(v);
#endif
}

void
nng_stats_free(nni_stat *st)
{
#ifdef NNG_ENABLE_STATS
	nni_stat *child;

	while ((child = nni_list_first(&st->s_children)) != NULL) {
		nni_list_remove(&st->s_children, child);
		nng_stats_free(child);
	}
	if (st->s_info->si_alloc) {
		nni_strfree(st->s_val.sv_string);
	}
	NNI_FREE_STRUCT(st);
#else
	NNI_ARG_UNUSED(st);
#endif
}

#ifdef NNG_ENABLE_STATS
static int
stat_make_tree(nni_stat_item *item, nni_stat **sp)
{
	nni_stat      *stat;
	nni_stat_item *child;

	if ((stat = NNI_ALLOC_STRUCT(stat)) == NULL) {
		return (NNG_ENOMEM);
	}
	NNI_LIST_INIT(&stat->s_children, nni_stat, s_node);

	stat->s_info   = item->si_info;
	stat->s_item   = item;
	stat->s_parent = NULL;

	NNI_LIST_FOREACH (&item->si_children, child) {
		nni_stat *cs;
		int       rv;
		if ((rv = stat_make_tree(child, &cs)) != 0) {
			nng_stats_free(stat);
			return (rv);
		}
		nni_list_append(&stat->s_children, cs);
		cs->s_parent = stat;
	}
	*sp = stat;
	return (0);
}

static void
stat_update(nni_stat *stat, nni_mtx **mtxp)
{
	const nni_stat_item *item = stat->s_item;
	const nni_stat_info *info = item->si_info;
	char                *old;
	char                *str;

	if (info->si_lock) {
		NNI_ASSERT(item->si_mtx != NULL);
		if (*mtxp != item->si_mtx) {
			if (*mtxp) {
				nni_mtx_unlock(*mtxp);
			}
			nni_mtx_lock(item->si_mtx);
			*mtxp = item->si_mtx;
		}
	} else if (*mtxp) {
		nni_mtx_unlock(*mtxp);
		*mtxp = NULL;
	}
	switch (info->si_type) {
	case NNG_STAT_SCOPE:
	case NNG_STAT_ID:
		stat->s_val.sv_id = item->si_u.sv_id;
		break;
	case NNG_STAT_BOOLEAN:
		stat->s_val.sv_bool = item->si_u.sv_bool;
		break;
	case NNG_STAT_COUNTER:
	case NNG_STAT_LEVEL:
		if (info->si_atomic) {
			stat->s_val.sv_value = nni_atomic_get64(
			    (nni_atomic_u64 *) &item->si_u.sv_atomic);
		} else {
			stat->s_val.sv_value = item->si_u.sv_number;
		}
		break;
	case NNG_STAT_STRING:
		nni_mtx_lock(&stats_val_lock);
		old = stat->s_val.sv_string;
		str = item->si_u.sv_string;

		// If we have to allocate a new string, do so.  But
		// only do it if new string is different.
		if ((info->si_alloc) && (str != NULL) &&
		    ((old == NULL) || (strcmp(str, old) != 0))) {

			stat->s_val.sv_string = nni_strdup(str);
			nni_strfree(old);

		} else if (info->si_alloc) {
			nni_strfree(stat->s_val.sv_string);
			stat->s_val.sv_string = NULL;

		} else {
			stat->s_val.sv_string = str;
		}
		nni_mtx_unlock(&stats_val_lock);
		break;
	}
	stat->s_timestamp = nni_clock();
}

static void
stat_update_tree(nni_stat *stat, nni_mtx **mtxp)
{
	nni_stat *child;
	stat_update(stat, mtxp);
	NNI_LIST_FOREACH (&stat->s_children, child) {
		stat_update_tree(child, mtxp);
	}
}

int
nni_stat_snapshot(nni_stat **statp, nni_stat_item *item)
{
	int       rv;
	nni_stat *stat;
	nni_mtx  *mtx = NULL;

	if (item == NULL) {
		item = &stats_root;
	}
	nni_mtx_lock(&stats_lock);
	if ((rv = stat_make_tree(item, &stat)) != 0) {
		nni_mtx_unlock(&stats_lock);
		return (rv);
	}
	stat_update_tree(stat, &mtx);
	if (mtx != NULL) {
		nni_mtx_unlock(mtx);
	}
	nni_mtx_unlock(&stats_lock);
	*statp = stat;
	return (0);
}
#endif

int
nng_stats_get(nng_stat **statp)
{
#ifdef NNG_ENABLE_STATS
	return (nni_stat_snapshot(statp, &stats_root));
#else
	NNI_ARG_UNUSED(statp);
	return (NNG_ENOTSUP);
#endif
}

const nng_stat *
nng_stat_parent(const nng_stat *stat)
{
	return (stat->s_parent);
}

const nng_stat *
nng_stat_next(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	if (stat->s_parent == NULL) {
		return (NULL); // Root node, no siblings.
	}
	return (nni_list_next(&stat->s_parent->s_children, (void *) stat));
#else
	NNI_ARG_UNUSED(stat);
	return (NULL);
#endif
}

const nng_stat *
nng_stat_child(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return (nni_list_first(&stat->s_children));
#else
	NNI_ARG_UNUSED(stat);
	return (NULL);
#endif
}

const char *
nng_stat_name(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return (stat->s_info->si_name);
#else
	NNI_ARG_UNUSED(stat);
	return (NULL);
#endif
}

uint64_t
nng_stat_value(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return (stat->s_val.sv_value);
#else
	NNI_ARG_UNUSED(stat);
	return (0);
#endif
}

bool
nng_stat_bool(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return (stat->s_val.sv_bool);
#else
	NNI_ARG_UNUSED(stat);
	return (false);
#endif
}

const char *
nng_stat_string(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	if (stat->s_info->si_type != NNG_STAT_STRING) {
		return ("");
	}
	return (stat->s_val.sv_string);
#else
	NNI_ARG_UNUSED(stat);
	return ("");
#endif
}

uint64_t
nng_stat_timestamp(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return ((uint64_t) stat->s_timestamp);
#else
	NNI_ARG_UNUSED(stat);
	return (0);
#endif
}

int
nng_stat_type(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return (stat->s_info->si_type);
#else
	NNI_ARG_UNUSED(stat);
	return (NNG_STAT_ID);
#endif
}

int
nng_stat_unit(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return (stat->s_info->si_unit);
#else
	NNI_ARG_UNUSED(stat);
	return (NNG_UNIT_NONE);
#endif
}

const char *
nng_stat_desc(const nng_stat *stat)
{
#if NNG_ENABLE_STATS
	return (stat->s_info->si_desc);
#else
	NNI_ARG_UNUSED(stat);
	return ("");
#endif
}

const nng_stat *
nng_stat_find(const nng_stat *stat, const char *name)
{
#if NNG_ENABLE_STATS
	const nng_stat *child;
	if (stat == NULL) {
		return (NULL);
	}
	if (strcmp(name, stat->s_info->si_name) == 0) {
		return (stat);
	}
	NNI_LIST_FOREACH (&stat->s_children, child) {
		const nng_stat *result;
		if ((result = nng_stat_find(child, name)) != NULL) {
			return (result);
		}
	}
#else
	NNI_ARG_UNUSED(stat);
	NNI_ARG_UNUSED(name);
#endif
	return (NULL);
}

const nng_stat *
nng_stat_find_scope(const nng_stat *stat, const char *name, int id)
{
#if NNG_ENABLE_STATS
	nng_stat *child;
	if (stat == NULL || stat->s_info->si_type != NNG_STAT_SCOPE) {
		return (NULL);
	}
	if ((stat->s_val.sv_id == id) &&
	    (stat->s_info->si_type == NNG_STAT_SCOPE) &&
	    (strcmp(name, stat->s_info->si_name) == 0)) {
		return (stat);
	}
	NNI_LIST_FOREACH (&stat->s_children, child) {
		const nng_stat *result;
		if ((result = nng_stat_find_scope(child, name, id)) != NULL) {
			return (result);
		}
	}
#else
	NNI_ARG_UNUSED(stat);
	NNI_ARG_UNUSED(name);
	NNI_ARG_UNUSED(id);
#endif
	return (NULL);
}

const nng_stat *
nng_stat_find_socket(const nng_stat *stat, nng_socket s)
{
	return (nng_stat_find_scope(stat, "socket", nng_socket_id(s)));
}

const nng_stat *
nng_stat_find_dialer(const nng_stat *stat, nng_dialer d)
{
	return (nng_stat_find_scope(stat, "dialer", nng_dialer_id(d)));
}

const nng_stat *
nng_stat_find_listener(const nng_stat *stat, nng_listener l)
{
	return (nng_stat_find_scope(stat, "listener", nng_listener_id(l)));
}

#ifdef NNG_ENABLE_STATS
void
stat_sprint_scope(const nni_stat *stat, char **scope, int *lenp)
{
	if (stat->s_parent != NULL) {
		stat_sprint_scope(stat->s_parent, scope, lenp);
	}
	if (strlen(stat->s_info->si_name) > 0) {
		snprintf(*scope, *lenp, "%s#%d.", stat->s_info->si_name,
		    stat->s_val.sv_id);
	} else {
		(*scope)[0] = '\0';
	}
	*lenp -= (int) strlen(*scope);
	*scope += strlen(*scope);
}
#endif

void
nng_stats_dump(const nng_stat *stat)
{
#ifdef NNG_ENABLE_STATS
	static char        buf[128]; // to minimize recursion, not thread safe
	int                len;
	char              *scope;
	char              *indent = "        ";
	unsigned long long val;
	nni_stat          *child;

	switch (nng_stat_type(stat)) {
	case NNG_STAT_SCOPE:
		scope = buf;
		len   = sizeof(buf);
		stat_sprint_scope(stat, &scope, &len);
		len = (int) strlen(buf);
		if (len > 0) {
			if (buf[len - 1] == '.') {
				buf[--len] = '\0';
			}
		}
		if (len > 0) {
			nni_plat_printf("\n%s:\n", buf);
		}
		break;
	case NNG_STAT_STRING:
		nni_plat_printf("%s%-32s\"%s\"\n", indent, nng_stat_name(stat),
		    nng_stat_string(stat));
		break;
	case NNG_STAT_BOOLEAN:
		nni_plat_printf("%s%-32s%s\n", indent, nng_stat_name(stat),
		    nng_stat_bool(stat) ? "true" : "false");
		break;
	case NNG_STAT_LEVEL:
	case NNG_STAT_COUNTER:
		val = nng_stat_value(stat);
		nni_plat_printf(
		    "%s%-32s%llu", indent, nng_stat_name(stat), val);
		switch (nng_stat_unit(stat)) {
		case NNG_UNIT_BYTES:
			nni_plat_printf(" bytes\n");
			break;
		case NNG_UNIT_MESSAGES:
			nni_plat_printf(" msgs\n");
			break;
		case NNG_UNIT_MILLIS:
			nni_plat_printf(" ms\n");
			break;
		case NNG_UNIT_NONE:
		case NNG_UNIT_EVENTS:
		default:
			nni_plat_printf("\n");
			break;
		}
		break;
	case NNG_STAT_ID:
		val = nng_stat_value(stat);
		nni_plat_printf(
		    "%s%-32s%llu\n", indent, nng_stat_name(stat), val);
		break;
	default:
		nni_plat_printf("%s%-32s<?>\n", indent, nng_stat_name(stat));
		break;
	}

	NNI_LIST_FOREACH (&stat->s_children, child) {
		nng_stats_dump(child);
	}
#else
	NNI_ARG_UNUSED(stat);
#endif
}