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