2009-01-27 22:30:00 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2009-01-27 22:30:00 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10: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.
|
2009-01-27 22:30:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2009-01-27 22:30:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/atomic.h>
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/magic.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/platform.h>
|
|
|
|
#include <isc/print.h>
|
2019-02-07 19:41:43 -08:00
|
|
|
#include <isc/refcount.h>
|
2009-01-27 22:30:00 +00:00
|
|
|
#include <isc/stats.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define ISC_STATS_MAGIC ISC_MAGIC('S', 't', 'a', 't')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define ISC_STATS_VALID(x) ISC_MAGIC_VALID(x, ISC_STATS_MAGIC)
|
2009-01-27 22:30:00 +00:00
|
|
|
|
2019-06-07 15:21:43 +02:00
|
|
|
#if defined(_WIN32) && !defined(_WIN64)
|
2020-02-12 13:59:18 +01:00
|
|
|
typedef atomic_int_fast32_t isc__atomic_statcounter_t;
|
2020-02-13 21:48:23 +01:00
|
|
|
#else /* if defined(_WIN32) && !defined(_WIN64) */
|
2020-02-12 13:59:18 +01:00
|
|
|
typedef atomic_int_fast64_t isc__atomic_statcounter_t;
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if defined(_WIN32) && !defined(_WIN64) */
|
2009-01-27 22:30:00 +00:00
|
|
|
|
|
|
|
struct isc_stats {
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_refcount_t references;
|
|
|
|
int ncounters;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc__atomic_statcounter_t *counters;
|
2009-01-27 22:30:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
create_stats(isc_mem_t *mctx, int ncounters, isc_stats_t **statsp) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_t *stats;
|
2020-02-13 14:44:37 -08:00
|
|
|
size_t counters_alloc_size;
|
2009-01-27 22:30:00 +00:00
|
|
|
|
|
|
|
REQUIRE(statsp != NULL && *statsp == NULL);
|
|
|
|
|
|
|
|
stats = isc_mem_get(mctx, sizeof(*stats));
|
2019-11-05 17:48:47 -03:00
|
|
|
counters_alloc_size = sizeof(isc__atomic_statcounter_t) * ncounters;
|
|
|
|
stats->counters = isc_mem_get(mctx, counters_alloc_size);
|
2019-05-20 16:59:48 +02:00
|
|
|
isc_refcount_init(&stats->references, 1);
|
2019-11-05 17:48:47 -03:00
|
|
|
memset(stats->counters, 0, counters_alloc_size);
|
2009-01-27 22:30:00 +00:00
|
|
|
stats->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &stats->mctx);
|
|
|
|
stats->ncounters = ncounters;
|
|
|
|
stats->magic = ISC_STATS_MAGIC;
|
|
|
|
*statsp = stats;
|
|
|
|
|
2019-02-07 19:41:43 -08:00
|
|
|
return (ISC_R_SUCCESS);
|
2009-01-27 22:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_attach(isc_stats_t *stats, isc_stats_t **statsp) {
|
2009-01-27 22:30:00 +00:00
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
REQUIRE(statsp != NULL && *statsp == NULL);
|
|
|
|
|
2019-05-20 16:59:48 +02:00
|
|
|
isc_refcount_increment(&stats->references);
|
2009-01-27 22:30:00 +00:00
|
|
|
*statsp = stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_detach(isc_stats_t **statsp) {
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_stats_t *stats;
|
|
|
|
|
|
|
|
REQUIRE(statsp != NULL && ISC_STATS_VALID(*statsp));
|
|
|
|
|
|
|
|
stats = *statsp;
|
|
|
|
*statsp = NULL;
|
|
|
|
|
2019-05-20 16:59:48 +02:00
|
|
|
if (isc_refcount_decrement(&stats->references) == 1) {
|
2019-07-23 08:27:30 -04:00
|
|
|
isc_refcount_destroy(&stats->references);
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_mem_put(stats->mctx, stats->counters,
|
2019-11-05 17:48:47 -03:00
|
|
|
sizeof(isc__atomic_statcounter_t) *
|
2020-02-12 13:59:18 +01:00
|
|
|
stats->ncounters);
|
2009-01-27 22:30:00 +00:00
|
|
|
isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_ncounters(isc_stats_t *stats) {
|
2009-01-27 22:30:00 +00:00
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
|
|
|
|
return (stats->ncounters);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_create(isc_mem_t *mctx, isc_stats_t **statsp, int ncounters) {
|
2009-01-27 22:30:00 +00:00
|
|
|
REQUIRE(statsp != NULL && *statsp == NULL);
|
|
|
|
|
|
|
|
return (create_stats(mctx, ncounters, statsp));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_increment(isc_stats_t *stats, isc_statscounter_t counter) {
|
2009-01-27 22:30:00 +00:00
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
REQUIRE(counter < stats->ncounters);
|
|
|
|
|
2019-06-13 11:01:12 +02:00
|
|
|
atomic_fetch_add_relaxed(&stats->counters[counter], 1);
|
2009-01-27 22:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_decrement(isc_stats_t *stats, isc_statscounter_t counter) {
|
2009-01-27 22:30:00 +00:00
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
REQUIRE(counter < stats->ncounters);
|
2019-06-13 11:01:12 +02:00
|
|
|
atomic_fetch_sub_release(&stats->counters[counter], 1);
|
2009-01-27 22:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_stats_dump(isc_stats_t *stats, isc_stats_dumper_t dump_fn, void *arg,
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int options) {
|
2009-01-27 22:30:00 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
|
2018-08-14 11:42:06 +02:00
|
|
|
for (i = 0; i < stats->ncounters; i++) {
|
2019-06-13 11:01:12 +02:00
|
|
|
uint32_t counter = atomic_load_acquire(&stats->counters[i]);
|
2019-02-07 19:41:43 -08:00
|
|
|
if ((options & ISC_STATSDUMP_VERBOSE) == 0 && counter == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dump_fn((isc_statscounter_t)i, counter, arg);
|
2009-01-27 22:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 10:06:05 -07:00
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_set(isc_stats_t *stats, uint64_t val, isc_statscounter_t counter) {
|
2012-05-14 10:06:05 -07:00
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
REQUIRE(counter < stats->ncounters);
|
|
|
|
|
2019-06-13 11:01:12 +02:00
|
|
|
atomic_store_release(&stats->counters[counter], val);
|
2012-05-14 10:06:05 -07:00
|
|
|
}
|
2019-11-05 17:48:47 -03:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
void
|
|
|
|
isc_stats_update_if_greater(isc_stats_t *stats, isc_statscounter_t counter,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_statscounter_t value) {
|
2019-11-05 17:48:47 -03:00
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
REQUIRE(counter < stats->ncounters);
|
|
|
|
|
2019-11-06 10:49:28 +01:00
|
|
|
isc_statscounter_t curr_value =
|
2019-06-13 11:01:12 +02:00
|
|
|
atomic_load_acquire(&stats->counters[counter]);
|
2019-11-05 17:48:47 -03:00
|
|
|
do {
|
|
|
|
if (curr_value >= value) {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-13 11:01:12 +02:00
|
|
|
} while (!atomic_compare_exchange_weak_acq_rel(
|
|
|
|
&stats->counters[counter], &curr_value, value));
|
2019-11-05 17:48:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_statscounter_t
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_stats_get_counter(isc_stats_t *stats, isc_statscounter_t counter) {
|
2019-11-05 17:48:47 -03:00
|
|
|
REQUIRE(ISC_STATS_VALID(stats));
|
|
|
|
REQUIRE(counter < stats->ncounters);
|
|
|
|
|
2019-06-13 11:01:12 +02:00
|
|
|
return (atomic_load_acquire(&stats->counters[counter]));
|
2019-11-05 17:48:47 -03:00
|
|
|
}
|