Home | History | Annotate | Line # | Download | only in ns
stats.c revision 1.8
      1 /*	$NetBSD: stats.c,v 1.8 2025/01/26 16:25:46 christos 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 <isc/magic.h>
     19 #include <isc/mem.h>
     20 #include <isc/refcount.h>
     21 #include <isc/stats.h>
     22 #include <isc/util.h>
     23 
     24 #include <ns/stats.h>
     25 
     26 #define NS_STATS_MAGIC	  ISC_MAGIC('N', 's', 't', 't')
     27 #define NS_STATS_VALID(x) ISC_MAGIC_VALID(x, NS_STATS_MAGIC)
     28 
     29 struct ns_stats {
     30 	/*% Unlocked */
     31 	unsigned int magic;
     32 	isc_mem_t *mctx;
     33 	isc_stats_t *counters;
     34 	isc_refcount_t references;
     35 };
     36 
     37 void
     38 ns_stats_attach(ns_stats_t *stats, ns_stats_t **statsp) {
     39 	REQUIRE(NS_STATS_VALID(stats));
     40 	REQUIRE(statsp != NULL && *statsp == NULL);
     41 
     42 	isc_refcount_increment(&stats->references);
     43 
     44 	*statsp = stats;
     45 }
     46 
     47 void
     48 ns_stats_detach(ns_stats_t **statsp) {
     49 	ns_stats_t *stats;
     50 
     51 	REQUIRE(statsp != NULL && NS_STATS_VALID(*statsp));
     52 
     53 	stats = *statsp;
     54 	*statsp = NULL;
     55 
     56 	if (isc_refcount_decrement(&stats->references) == 1) {
     57 		isc_stats_detach(&stats->counters);
     58 		isc_refcount_destroy(&stats->references);
     59 		isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats));
     60 	}
     61 }
     62 
     63 void
     64 ns_stats_create(isc_mem_t *mctx, int ncounters, ns_stats_t **statsp) {
     65 	REQUIRE(statsp != NULL && *statsp == NULL);
     66 
     67 	ns_stats_t *stats = isc_mem_get(mctx, sizeof(*stats));
     68 	stats->counters = NULL;
     69 
     70 	isc_refcount_init(&stats->references, 1);
     71 
     72 	isc_stats_create(mctx, &stats->counters, ncounters);
     73 
     74 	stats->magic = NS_STATS_MAGIC;
     75 	stats->mctx = NULL;
     76 	isc_mem_attach(mctx, &stats->mctx);
     77 	*statsp = stats;
     78 }
     79 
     80 /*%
     81  * Increment/Decrement methods
     82  */
     83 isc_statscounter_t
     84 ns_stats_increment(ns_stats_t *stats, isc_statscounter_t counter) {
     85 	REQUIRE(NS_STATS_VALID(stats));
     86 
     87 	return isc_stats_increment(stats->counters, counter);
     88 }
     89 
     90 void
     91 ns_stats_decrement(ns_stats_t *stats, isc_statscounter_t counter) {
     92 	REQUIRE(NS_STATS_VALID(stats));
     93 
     94 	isc_stats_decrement(stats->counters, counter);
     95 }
     96 
     97 isc_stats_t *
     98 ns_stats_get(ns_stats_t *stats) {
     99 	REQUIRE(NS_STATS_VALID(stats));
    100 
    101 	return stats->counters;
    102 }
    103 
    104 void
    105 ns_stats_update_if_greater(ns_stats_t *stats, isc_statscounter_t counter,
    106 			   isc_statscounter_t value) {
    107 	REQUIRE(NS_STATS_VALID(stats));
    108 
    109 	isc_stats_update_if_greater(stats->counters, counter, value);
    110 }
    111 
    112 isc_statscounter_t
    113 ns_stats_get_counter(ns_stats_t *stats, isc_statscounter_t counter) {
    114 	REQUIRE(NS_STATS_VALID(stats));
    115 
    116 	return isc_stats_get_counter(stats->counters, counter);
    117 }
    118