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