2017-09-08 13:39:09 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2017-09-08 13:39:09 -07:00
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2017-09-08 13:39:09 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
|
|
|
#include <isc/magic.h>
|
|
|
|
#include <isc/mem.h>
|
2019-05-16 19:27:01 +02:00
|
|
|
#include <isc/refcount.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <isc/stats.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <ns/stats.h>
|
|
|
|
|
|
|
|
#define NS_STATS_MAGIC ISC_MAGIC('N', 's', 't', 't')
|
|
|
|
#define NS_STATS_VALID(x) ISC_MAGIC_VALID(x, NS_STATS_MAGIC)
|
|
|
|
|
|
|
|
struct ns_stats {
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_stats_t *counters;
|
2019-05-16 19:27:01 +02:00
|
|
|
isc_refcount_t references;
|
2017-09-08 13:39:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_stats_attach(ns_stats_t *stats, ns_stats_t **statsp) {
|
|
|
|
REQUIRE(NS_STATS_VALID(stats));
|
|
|
|
REQUIRE(statsp != NULL && *statsp == NULL);
|
|
|
|
|
2019-05-16 19:27:01 +02:00
|
|
|
isc_refcount_increment(&stats->references);
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
*statsp = stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_stats_detach(ns_stats_t **statsp) {
|
|
|
|
ns_stats_t *stats;
|
|
|
|
|
|
|
|
REQUIRE(statsp != NULL && NS_STATS_VALID(*statsp));
|
|
|
|
|
|
|
|
stats = *statsp;
|
|
|
|
*statsp = NULL;
|
|
|
|
|
2019-05-16 19:27:01 +02:00
|
|
|
if (isc_refcount_decrement(&stats->references) == 1) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_stats_detach(&stats->counters);
|
|
|
|
isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_stats_create(isc_mem_t *mctx, int ncounters, ns_stats_t **statsp) {
|
|
|
|
ns_stats_t *stats;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(statsp != NULL && *statsp == NULL);
|
|
|
|
|
|
|
|
stats = isc_mem_get(mctx, sizeof(*stats));
|
|
|
|
if (stats == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
stats->counters = NULL;
|
2019-05-16 19:27:01 +02:00
|
|
|
atomic_init(&stats->references, 1);
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
result = isc_stats_create(mctx, &stats->counters, ncounters);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto clean_mutex;
|
|
|
|
|
|
|
|
stats->magic = NS_STATS_MAGIC;
|
|
|
|
stats->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &stats->mctx);
|
|
|
|
*statsp = stats;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
clean_mutex:
|
|
|
|
isc_mem_put(mctx, stats, sizeof(*stats));
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Increment/Decrement methods
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ns_stats_increment(ns_stats_t *stats, isc_statscounter_t counter) {
|
|
|
|
REQUIRE(NS_STATS_VALID(stats));
|
|
|
|
|
|
|
|
isc_stats_increment(stats->counters, counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_stats_decrement(ns_stats_t *stats, isc_statscounter_t counter) {
|
|
|
|
REQUIRE(NS_STATS_VALID(stats));
|
|
|
|
|
|
|
|
isc_stats_decrement(stats->counters, counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_stats_t *
|
|
|
|
ns_stats_get(ns_stats_t *stats) {
|
|
|
|
REQUIRE(NS_STATS_VALID(stats));
|
|
|
|
|
|
|
|
return (stats->counters);
|
|
|
|
}
|