Home | History | Annotate | Line # | Download | only in ns
stats.c revision 1.7
      1 /*	$NetBSD: stats.c,v 1.7 2022/09/23 12:15:36 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 isc_result_t
     64 ns_stats_create(isc_mem_t *mctx, int ncounters, ns_stats_t **statsp) {
     65 	ns_stats_t *stats;
     66 	isc_result_t result;
     67 
     68 	REQUIRE(statsp != NULL && *statsp == NULL);
     69 
     70 	stats = isc_mem_get(mctx, sizeof(*stats));
     71 	stats->counters = NULL;
     72 
     73 	isc_refcount_init(&stats->references, 1);
     74 
     75 	result = isc_stats_create(mctx, &stats->counters, ncounters);
     76 	if (result != ISC_R_SUCCESS) {
     77 		goto clean_mem;
     78 	}
     79 
     80 	stats->magic = NS_STATS_MAGIC;
     81 	stats->mctx = NULL;
     82 	isc_mem_attach(mctx, &stats->mctx);
     83 	*statsp = stats;
     84 
     85 	return (ISC_R_SUCCESS);
     86 
     87 clean_mem:
     88 	isc_mem_put(mctx, stats, sizeof(*stats));
     89 
     90 	return (result);
     91 }
     92 
     93 /*%
     94  * Increment/Decrement methods
     95  */
     96 void
     97 ns_stats_increment(ns_stats_t *stats, isc_statscounter_t counter) {
     98 	REQUIRE(NS_STATS_VALID(stats));
     99 
    100 	isc_stats_increment(stats->counters, counter);
    101 }
    102 
    103 void
    104 ns_stats_decrement(ns_stats_t *stats, isc_statscounter_t counter) {
    105 	REQUIRE(NS_STATS_VALID(stats));
    106 
    107 	isc_stats_decrement(stats->counters, counter);
    108 }
    109 
    110 isc_stats_t *
    111 ns_stats_get(ns_stats_t *stats) {
    112 	REQUIRE(NS_STATS_VALID(stats));
    113 
    114 	return (stats->counters);
    115 }
    116 
    117 void
    118 ns_stats_update_if_greater(ns_stats_t *stats, isc_statscounter_t counter,
    119 			   isc_statscounter_t value) {
    120 	REQUIRE(NS_STATS_VALID(stats));
    121 
    122 	isc_stats_update_if_greater(stats->counters, counter, value);
    123 }
    124 
    125 isc_statscounter_t
    126 ns_stats_get_counter(ns_stats_t *stats, isc_statscounter_t counter) {
    127 	REQUIRE(NS_STATS_VALID(stats));
    128 
    129 	return (isc_stats_get_counter(stats->counters, counter));
    130 }
    131