xref: /netbsd-src/external/mpl/bind/dist/lib/isc/stats.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /*	$NetBSD: stats.c,v 1.7 2020/05/24 19:46:26 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 /*! \file */
15 
16 #include <inttypes.h>
17 #include <string.h>
18 
19 #include <isc/atomic.h>
20 #include <isc/buffer.h>
21 #include <isc/magic.h>
22 #include <isc/mem.h>
23 #include <isc/platform.h>
24 #include <isc/print.h>
25 #include <isc/refcount.h>
26 #include <isc/stats.h>
27 #include <isc/util.h>
28 
29 #define ISC_STATS_MAGIC	   ISC_MAGIC('S', 't', 'a', 't')
30 #define ISC_STATS_VALID(x) ISC_MAGIC_VALID(x, ISC_STATS_MAGIC)
31 
32 #if (defined(_WIN32) && !defined(_WIN64)) || !defined(_LP64)
33 typedef atomic_int_fast32_t isc__atomic_statcounter_t;
34 #else  /* if defined(_WIN32) && !defined(_WIN64) */
35 typedef atomic_int_fast64_t isc__atomic_statcounter_t;
36 #endif /* if defined(_WIN32) && !defined(_WIN64) */
37 
38 struct isc_stats {
39 	unsigned int magic;
40 	isc_mem_t *mctx;
41 	isc_refcount_t references;
42 	int ncounters;
43 	isc__atomic_statcounter_t *counters;
44 };
45 
46 static isc_result_t
47 create_stats(isc_mem_t *mctx, int ncounters, isc_stats_t **statsp) {
48 	isc_stats_t *stats;
49 	size_t counters_alloc_size;
50 
51 	REQUIRE(statsp != NULL && *statsp == NULL);
52 
53 	stats = isc_mem_get(mctx, sizeof(*stats));
54 	counters_alloc_size = sizeof(isc__atomic_statcounter_t) * ncounters;
55 	stats->counters = isc_mem_get(mctx, counters_alloc_size);
56 	isc_refcount_init(&stats->references, 1);
57 	memset(stats->counters, 0, counters_alloc_size);
58 	stats->mctx = NULL;
59 	isc_mem_attach(mctx, &stats->mctx);
60 	stats->ncounters = ncounters;
61 	stats->magic = ISC_STATS_MAGIC;
62 	*statsp = stats;
63 
64 	return (ISC_R_SUCCESS);
65 }
66 
67 void
68 isc_stats_attach(isc_stats_t *stats, isc_stats_t **statsp) {
69 	REQUIRE(ISC_STATS_VALID(stats));
70 	REQUIRE(statsp != NULL && *statsp == NULL);
71 
72 	isc_refcount_increment(&stats->references);
73 	*statsp = stats;
74 }
75 
76 void
77 isc_stats_detach(isc_stats_t **statsp) {
78 	isc_stats_t *stats;
79 
80 	REQUIRE(statsp != NULL && ISC_STATS_VALID(*statsp));
81 
82 	stats = *statsp;
83 	*statsp = NULL;
84 
85 	if (isc_refcount_decrement(&stats->references) == 1) {
86 		isc_refcount_destroy(&stats->references);
87 		isc_mem_put(stats->mctx, stats->counters,
88 			    sizeof(isc__atomic_statcounter_t) *
89 				    stats->ncounters);
90 		isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats));
91 	}
92 }
93 
94 int
95 isc_stats_ncounters(isc_stats_t *stats) {
96 	REQUIRE(ISC_STATS_VALID(stats));
97 
98 	return (stats->ncounters);
99 }
100 
101 isc_result_t
102 isc_stats_create(isc_mem_t *mctx, isc_stats_t **statsp, int ncounters) {
103 	REQUIRE(statsp != NULL && *statsp == NULL);
104 
105 	return (create_stats(mctx, ncounters, statsp));
106 }
107 
108 void
109 isc_stats_increment(isc_stats_t *stats, isc_statscounter_t counter) {
110 	REQUIRE(ISC_STATS_VALID(stats));
111 	REQUIRE(counter < stats->ncounters);
112 
113 	atomic_fetch_add_relaxed(&stats->counters[counter], 1);
114 }
115 
116 void
117 isc_stats_decrement(isc_stats_t *stats, isc_statscounter_t counter) {
118 	REQUIRE(ISC_STATS_VALID(stats));
119 	REQUIRE(counter < stats->ncounters);
120 	atomic_fetch_sub_release(&stats->counters[counter], 1);
121 }
122 
123 void
124 isc_stats_dump(isc_stats_t *stats, isc_stats_dumper_t dump_fn, void *arg,
125 	       unsigned int options) {
126 	int i;
127 
128 	REQUIRE(ISC_STATS_VALID(stats));
129 
130 	for (i = 0; i < stats->ncounters; i++) {
131 		uint32_t counter = atomic_load_acquire(&stats->counters[i]);
132 		if ((options & ISC_STATSDUMP_VERBOSE) == 0 && counter == 0) {
133 			continue;
134 		}
135 		dump_fn((isc_statscounter_t)i, counter, arg);
136 	}
137 }
138 
139 void
140 isc_stats_set(isc_stats_t *stats, uint64_t val, isc_statscounter_t counter) {
141 	REQUIRE(ISC_STATS_VALID(stats));
142 	REQUIRE(counter < stats->ncounters);
143 
144 	atomic_store_release(&stats->counters[counter], val);
145 }
146 
147 void
148 isc_stats_update_if_greater(isc_stats_t *stats, isc_statscounter_t counter,
149 			    isc_statscounter_t value) {
150 	REQUIRE(ISC_STATS_VALID(stats));
151 	REQUIRE(counter < stats->ncounters);
152 
153 	isc_statscounter_t curr_value =
154 		atomic_load_acquire(&stats->counters[counter]);
155 	do {
156 		if (curr_value >= value) {
157 			break;
158 		}
159 	} while (!atomic_compare_exchange_weak_acq_rel(
160 		&stats->counters[counter], &curr_value, value));
161 }
162 
163 isc_statscounter_t
164 isc_stats_get_counter(isc_stats_t *stats, isc_statscounter_t counter) {
165 	REQUIRE(ISC_STATS_VALID(stats));
166 	REQUIRE(counter < stats->ncounters);
167 
168 	return (atomic_load_acquire(&stats->counters[counter]));
169 }
170