Home | History | Annotate | Line # | Download | only in isc
stats.c revision 1.4.4.2
      1 /*	$NetBSD: stats.c,v 1.4.4.2 2024/02/29 12:35:03 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 /*! \file */
     17 
     18 #include <inttypes.h>
     19 #include <string.h>
     20 
     21 #include <isc/atomic.h>
     22 #include <isc/buffer.h>
     23 #include <isc/magic.h>
     24 #include <isc/mem.h>
     25 #include <isc/print.h>
     26 #include <isc/refcount.h>
     27 #include <isc/stats.h>
     28 #include <isc/util.h>
     29 
     30 #define ISC_STATS_MAGIC	   ISC_MAGIC('S', 't', 'a', 't')
     31 #define ISC_STATS_VALID(x) ISC_MAGIC_VALID(x, ISC_STATS_MAGIC)
     32 
     33 /*
     34  * Statistics are counted with an atomic int_fast64_t but exported to functions
     35  * taking uint64_t (isc_stats_dumper_t). A 128-bit native and fast architecture
     36  * doesn't exist in reality so these two are the same thing in practise.
     37  * However, a silent truncation happening silently in the future is still not
     38  * acceptable.
     39  */
     40 STATIC_ASSERT(sizeof(isc_statscounter_t) <= sizeof(uint64_t),
     41 	      "Exported statistics must fit into the statistic counter size");
     42 
     43 struct isc_stats {
     44 	unsigned int magic;
     45 	isc_mem_t *mctx;
     46 	isc_refcount_t references;
     47 	int ncounters;
     48 	isc_atomic_statscounter_t *counters;
     49 };
     50 
     51 static isc_result_t
     52 create_stats(isc_mem_t *mctx, int ncounters, isc_stats_t **statsp) {
     53 	isc_stats_t *stats;
     54 	size_t counters_alloc_size;
     55 
     56 	REQUIRE(statsp != NULL && *statsp == NULL);
     57 
     58 	stats = isc_mem_get(mctx, sizeof(*stats));
     59 	counters_alloc_size = sizeof(isc_atomic_statscounter_t) * ncounters;
     60 	stats->counters = isc_mem_get(mctx, counters_alloc_size);
     61 	isc_refcount_init(&stats->references, 1);
     62 	for (int i = 0; i < ncounters; i++) {
     63 		atomic_init(&stats->counters[i], 0);
     64 	}
     65 	stats->mctx = NULL;
     66 	isc_mem_attach(mctx, &stats->mctx);
     67 	stats->ncounters = ncounters;
     68 	stats->magic = ISC_STATS_MAGIC;
     69 	*statsp = stats;
     70 
     71 	return (ISC_R_SUCCESS);
     72 }
     73 
     74 void
     75 isc_stats_attach(isc_stats_t *stats, isc_stats_t **statsp) {
     76 	REQUIRE(ISC_STATS_VALID(stats));
     77 	REQUIRE(statsp != NULL && *statsp == NULL);
     78 
     79 	isc_refcount_increment(&stats->references);
     80 	*statsp = stats;
     81 }
     82 
     83 void
     84 isc_stats_detach(isc_stats_t **statsp) {
     85 	isc_stats_t *stats;
     86 
     87 	REQUIRE(statsp != NULL && ISC_STATS_VALID(*statsp));
     88 
     89 	stats = *statsp;
     90 	*statsp = NULL;
     91 
     92 	if (isc_refcount_decrement(&stats->references) == 1) {
     93 		isc_refcount_destroy(&stats->references);
     94 		isc_mem_put(stats->mctx, stats->counters,
     95 			    sizeof(isc_atomic_statscounter_t) *
     96 				    stats->ncounters);
     97 		isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats));
     98 	}
     99 }
    100 
    101 int
    102 isc_stats_ncounters(isc_stats_t *stats) {
    103 	REQUIRE(ISC_STATS_VALID(stats));
    104 
    105 	return (stats->ncounters);
    106 }
    107 
    108 isc_result_t
    109 isc_stats_create(isc_mem_t *mctx, isc_stats_t **statsp, int ncounters) {
    110 	REQUIRE(statsp != NULL && *statsp == NULL);
    111 
    112 	return (create_stats(mctx, ncounters, statsp));
    113 }
    114 
    115 void
    116 isc_stats_increment(isc_stats_t *stats, isc_statscounter_t counter) {
    117 	REQUIRE(ISC_STATS_VALID(stats));
    118 	REQUIRE(counter < stats->ncounters);
    119 
    120 	atomic_fetch_add_relaxed(&stats->counters[counter], 1);
    121 }
    122 
    123 void
    124 isc_stats_decrement(isc_stats_t *stats, isc_statscounter_t counter) {
    125 	REQUIRE(ISC_STATS_VALID(stats));
    126 	REQUIRE(counter < stats->ncounters);
    127 	atomic_fetch_sub_release(&stats->counters[counter], 1);
    128 }
    129 
    130 void
    131 isc_stats_dump(isc_stats_t *stats, isc_stats_dumper_t dump_fn, void *arg,
    132 	       unsigned int options) {
    133 	int i;
    134 
    135 	REQUIRE(ISC_STATS_VALID(stats));
    136 
    137 	for (i = 0; i < stats->ncounters; i++) {
    138 		isc_statscounter_t counter =
    139 			atomic_load_acquire(&stats->counters[i]);
    140 		if ((options & ISC_STATSDUMP_VERBOSE) == 0 && counter == 0) {
    141 			continue;
    142 		}
    143 		dump_fn((isc_statscounter_t)i, counter, arg);
    144 	}
    145 }
    146 
    147 void
    148 isc_stats_set(isc_stats_t *stats, uint64_t val, isc_statscounter_t counter) {
    149 	REQUIRE(ISC_STATS_VALID(stats));
    150 	REQUIRE(counter < stats->ncounters);
    151 
    152 	atomic_store_release(&stats->counters[counter], val);
    153 }
    154 
    155 void
    156 isc_stats_update_if_greater(isc_stats_t *stats, isc_statscounter_t counter,
    157 			    isc_statscounter_t value) {
    158 	REQUIRE(ISC_STATS_VALID(stats));
    159 	REQUIRE(counter < stats->ncounters);
    160 
    161 	isc_statscounter_t curr_value =
    162 		atomic_load_acquire(&stats->counters[counter]);
    163 	do {
    164 		if (curr_value >= value) {
    165 			break;
    166 		}
    167 	} while (!atomic_compare_exchange_weak_acq_rel(
    168 		&stats->counters[counter], &curr_value, value));
    169 }
    170 
    171 isc_statscounter_t
    172 isc_stats_get_counter(isc_stats_t *stats, isc_statscounter_t counter) {
    173 	REQUIRE(ISC_STATS_VALID(stats));
    174 	REQUIRE(counter < stats->ncounters);
    175 
    176 	return (atomic_load_acquire(&stats->counters[counter]));
    177 }
    178 
    179 void
    180 isc_stats_resize(isc_stats_t **statsp, int ncounters) {
    181 	isc_stats_t *stats;
    182 	size_t counters_alloc_size;
    183 	isc_atomic_statscounter_t *newcounters;
    184 
    185 	REQUIRE(statsp != NULL && *statsp != NULL);
    186 	REQUIRE(ISC_STATS_VALID(*statsp));
    187 	REQUIRE(ncounters > 0);
    188 
    189 	stats = *statsp;
    190 	if (stats->ncounters >= ncounters) {
    191 		/* We already have enough counters. */
    192 		return;
    193 	}
    194 
    195 	/* Grow number of counters. */
    196 	counters_alloc_size = sizeof(isc_atomic_statscounter_t) * ncounters;
    197 	newcounters = isc_mem_get(stats->mctx, counters_alloc_size);
    198 	for (int i = 0; i < ncounters; i++) {
    199 		atomic_init(&newcounters[i], 0);
    200 	}
    201 	for (int i = 0; i < stats->ncounters; i++) {
    202 		uint32_t counter = atomic_load_acquire(&stats->counters[i]);
    203 		atomic_store_release(&newcounters[i], counter);
    204 	}
    205 	isc_mem_put(stats->mctx, stats->counters,
    206 		    sizeof(isc_atomic_statscounter_t) * stats->ncounters);
    207 	stats->counters = newcounters;
    208 	stats->ncounters = ncounters;
    209 }
    210