Home | History | Annotate | Line # | Download | only in ns
stats.c revision 1.1.1.2
      1 /*	$NetBSD: stats.c,v 1.1.1.2 2019/01/09 16:48: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 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 		isc_mutex_destroy(&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 	isc_mutex_init(&stats->lock);
     86 
     87 	result = isc_stats_create(mctx, &stats->counters, ncounters);
     88 	if (result != ISC_R_SUCCESS)
     89 		goto clean_mutex;
     90 
     91 	stats->magic = NS_STATS_MAGIC;
     92 	stats->mctx = NULL;
     93 	isc_mem_attach(mctx, &stats->mctx);
     94 	*statsp = stats;
     95 
     96 	return (ISC_R_SUCCESS);
     97 
     98   clean_mutex:
     99 	isc_mutex_destroy(&stats->lock);
    100 	isc_mem_put(mctx, stats, sizeof(*stats));
    101 
    102 	return (result);
    103 }
    104 
    105 /*%
    106  * Increment/Decrement methods
    107  */
    108 void
    109 ns_stats_increment(ns_stats_t *stats, isc_statscounter_t counter) {
    110 	REQUIRE(NS_STATS_VALID(stats));
    111 
    112 	isc_stats_increment(stats->counters, counter);
    113 }
    114 
    115 void
    116 ns_stats_decrement(ns_stats_t *stats, isc_statscounter_t counter) {
    117 	REQUIRE(NS_STATS_VALID(stats));
    118 
    119 	isc_stats_decrement(stats->counters, counter);
    120 }
    121 
    122 isc_stats_t *
    123 ns_stats_get(ns_stats_t *stats) {
    124 	REQUIRE(NS_STATS_VALID(stats));
    125 
    126 	return (stats->counters);
    127 }
    128