Home | History | Annotate | Line # | Download | only in isc
stats.c revision 1.1.2.2
      1 /*	$NetBSD: stats.c,v 1.1.2.2 2024/02/24 13:07:21 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/platform.h>
     26 #include <isc/print.h>
     27 #include <isc/refcount.h>
     28 #include <isc/stats.h>
     29 #include <isc/util.h>
     30 
     31 #define ISC_STATS_MAGIC	   ISC_MAGIC('S', 't', 'a', 't')
     32 #define ISC_STATS_VALID(x) ISC_MAGIC_VALID(x, ISC_STATS_MAGIC)
     33 
     34 #if (defined(_WIN32) && !defined(_WIN64)) || !defined(_LP64)
     35 typedef atomic_int_fast32_t isc__atomic_statcounter_t;
     36 #else  /* if defined(_WIN32) && !defined(_WIN64) */
     37 typedef atomic_int_fast64_t isc__atomic_statcounter_t;
     38 #endif /* if defined(_WIN32) && !defined(_WIN64) */
     39 
     40 struct isc_stats {
     41 	unsigned int magic;
     42 	isc_mem_t *mctx;
     43 	isc_refcount_t references;
     44 	int ncounters;
     45 	isc__atomic_statcounter_t *counters;
     46 };
     47 
     48 static isc_result_t
     49 create_stats(isc_mem_t *mctx, int ncounters, isc_stats_t **statsp) {
     50 	isc_stats_t *stats;
     51 	size_t counters_alloc_size;
     52 
     53 	REQUIRE(statsp != NULL && *statsp == NULL);
     54 
     55 	stats = isc_mem_get(mctx, sizeof(*stats));
     56 	counters_alloc_size = sizeof(isc__atomic_statcounter_t) * ncounters;
     57 	stats->counters = isc_mem_get(mctx, counters_alloc_size);
     58 	isc_refcount_init(&stats->references, 1);
     59 	for (int i = 0; i < ncounters; i++) {
     60 		atomic_init(&stats->counters[i], 0);
     61 	}
     62 	stats->mctx = NULL;
     63 	isc_mem_attach(mctx, &stats->mctx);
     64 	stats->ncounters = ncounters;
     65 	stats->magic = ISC_STATS_MAGIC;
     66 	*statsp = stats;
     67 
     68 	return (ISC_R_SUCCESS);
     69 }
     70 
     71 void
     72 isc_stats_attach(isc_stats_t *stats, isc_stats_t **statsp) {
     73 	REQUIRE(ISC_STATS_VALID(stats));
     74 	REQUIRE(statsp != NULL && *statsp == NULL);
     75 
     76 	isc_refcount_increment(&stats->references);
     77 	*statsp = stats;
     78 }
     79 
     80 void
     81 isc_stats_detach(isc_stats_t **statsp) {
     82 	isc_stats_t *stats;
     83 
     84 	REQUIRE(statsp != NULL && ISC_STATS_VALID(*statsp));
     85 
     86 	stats = *statsp;
     87 	*statsp = NULL;
     88 
     89 	if (isc_refcount_decrement(&stats->references) == 1) {
     90 		isc_refcount_destroy(&stats->references);
     91 		isc_mem_put(stats->mctx, stats->counters,
     92 			    sizeof(isc__atomic_statcounter_t) *
     93 				    stats->ncounters);
     94 		isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats));
     95 	}
     96 }
     97 
     98 int
     99 isc_stats_ncounters(isc_stats_t *stats) {
    100 	REQUIRE(ISC_STATS_VALID(stats));
    101 
    102 	return (stats->ncounters);
    103 }
    104 
    105 isc_result_t
    106 isc_stats_create(isc_mem_t *mctx, isc_stats_t **statsp, int ncounters) {
    107 	REQUIRE(statsp != NULL && *statsp == NULL);
    108 
    109 	return (create_stats(mctx, ncounters, statsp));
    110 }
    111 
    112 void
    113 isc_stats_increment(isc_stats_t *stats, isc_statscounter_t counter) {
    114 	REQUIRE(ISC_STATS_VALID(stats));
    115 	REQUIRE(counter < stats->ncounters);
    116 
    117 	atomic_fetch_add_relaxed(&stats->counters[counter], 1);
    118 }
    119 
    120 void
    121 isc_stats_decrement(isc_stats_t *stats, isc_statscounter_t counter) {
    122 	REQUIRE(ISC_STATS_VALID(stats));
    123 	REQUIRE(counter < stats->ncounters);
    124 	atomic_fetch_sub_release(&stats->counters[counter], 1);
    125 }
    126 
    127 void
    128 isc_stats_dump(isc_stats_t *stats, isc_stats_dumper_t dump_fn, void *arg,
    129 	       unsigned int options) {
    130 	int i;
    131 
    132 	REQUIRE(ISC_STATS_VALID(stats));
    133 
    134 	for (i = 0; i < stats->ncounters; i++) {
    135 		uint32_t counter = atomic_load_acquire(&stats->counters[i]);
    136 		if ((options & ISC_STATSDUMP_VERBOSE) == 0 && counter == 0) {
    137 			continue;
    138 		}
    139 		dump_fn((isc_statscounter_t)i, counter, arg);
    140 	}
    141 }
    142 
    143 void
    144 isc_stats_set(isc_stats_t *stats, uint64_t val, isc_statscounter_t counter) {
    145 	REQUIRE(ISC_STATS_VALID(stats));
    146 	REQUIRE(counter < stats->ncounters);
    147 
    148 	atomic_store_release(&stats->counters[counter], val);
    149 }
    150 
    151 void
    152 isc_stats_update_if_greater(isc_stats_t *stats, isc_statscounter_t counter,
    153 			    isc_statscounter_t value) {
    154 	REQUIRE(ISC_STATS_VALID(stats));
    155 	REQUIRE(counter < stats->ncounters);
    156 
    157 	isc_statscounter_t curr_value =
    158 		atomic_load_acquire(&stats->counters[counter]);
    159 	do {
    160 		if (curr_value >= value) {
    161 			break;
    162 		}
    163 	} while (!atomic_compare_exchange_weak_acq_rel(
    164 		&stats->counters[counter], &curr_value, value));
    165 }
    166 
    167 isc_statscounter_t
    168 isc_stats_get_counter(isc_stats_t *stats, isc_statscounter_t counter) {
    169 	REQUIRE(ISC_STATS_VALID(stats));
    170 	REQUIRE(counter < stats->ncounters);
    171 
    172 	return (atomic_load_acquire(&stats->counters[counter]));
    173 }
    174 
    175 void
    176 isc_stats_resize(isc_stats_t **statsp, int ncounters) {
    177 	isc_stats_t *stats;
    178 	size_t counters_alloc_size;
    179 	isc__atomic_statcounter_t *newcounters;
    180 
    181 	REQUIRE(statsp != NULL && *statsp != NULL);
    182 	REQUIRE(ISC_STATS_VALID(*statsp));
    183 	REQUIRE(ncounters > 0);
    184 
    185 	stats = *statsp;
    186 	if (stats->ncounters >= ncounters) {
    187 		/* We already have enough counters. */
    188 		return;
    189 	}
    190 
    191 	/* Grow number of counters. */
    192 	counters_alloc_size = sizeof(isc__atomic_statcounter_t) * ncounters;
    193 	newcounters = isc_mem_get(stats->mctx, counters_alloc_size);
    194 	for (int i = 0; i < ncounters; i++) {
    195 		atomic_init(&newcounters[i], 0);
    196 	}
    197 	for (int i = 0; i < stats->ncounters; i++) {
    198 		uint32_t counter = atomic_load_acquire(&stats->counters[i]);
    199 		atomic_store_release(&newcounters[i], counter);
    200 	}
    201 	isc_mem_put(stats->mctx, stats->counters,
    202 		    sizeof(isc__atomic_statcounter_t) * stats->ncounters);
    203 	stats->counters = newcounters;
    204 	stats->ncounters = ncounters;
    205 }
    206