Coverage Report

Created: 2025-07-18 07:03

/src/bind9/lib/isc/stats.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
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 <inttypes.h>
17
#include <string.h>
18
19
#include <isc/atomic.h>
20
#include <isc/buffer.h>
21
#include <isc/magic.h>
22
#include <isc/mem.h>
23
#include <isc/refcount.h>
24
#include <isc/stats.h>
25
#include <isc/util.h>
26
27
2
#define ISC_STATS_MAGIC    ISC_MAGIC('S', 't', 'a', 't')
28
#define ISC_STATS_VALID(x) ISC_MAGIC_VALID(x, ISC_STATS_MAGIC)
29
30
/*
31
 * Statistics are counted with an atomic int_fast64_t but exported to functions
32
 * taking uint64_t (isc_stats_dumper_t). A 128-bit native and fast architecture
33
 * doesn't exist in reality so these two are the same thing in practise.
34
 * However, a silent truncation happening silently in the future is still not
35
 * acceptable.
36
 */
37
STATIC_ASSERT(sizeof(isc_statscounter_t) <= sizeof(uint64_t),
38
        "Exported statistics must fit into the statistic counter size");
39
40
struct isc_stats {
41
  unsigned int magic;
42
  isc_mem_t *mctx;
43
  isc_refcount_t references;
44
  int ncounters;
45
  isc_atomic_statscounter_t *counters;
46
};
47
48
void
49
2
isc_stats_attach(isc_stats_t *stats, isc_stats_t **statsp) {
50
2
  REQUIRE(ISC_STATS_VALID(stats));
51
2
  REQUIRE(statsp != NULL && *statsp == NULL);
52
53
2
  isc_refcount_increment(&stats->references);
54
2
  *statsp = stats;
55
2
}
56
57
void
58
0
isc_stats_detach(isc_stats_t **statsp) {
59
0
  isc_stats_t *stats;
60
61
0
  REQUIRE(statsp != NULL && ISC_STATS_VALID(*statsp));
62
63
0
  stats = *statsp;
64
0
  *statsp = NULL;
65
66
0
  if (isc_refcount_decrement(&stats->references) == 1) {
67
0
    isc_refcount_destroy(&stats->references);
68
0
    isc_mem_cput(stats->mctx, stats->counters, stats->ncounters,
69
0
           sizeof(isc_atomic_statscounter_t));
70
0
    isc_mem_putanddetach(&stats->mctx, stats, sizeof(*stats));
71
0
  }
72
0
}
73
74
int
75
0
isc_stats_ncounters(isc_stats_t *stats) {
76
0
  REQUIRE(ISC_STATS_VALID(stats));
77
78
0
  return stats->ncounters;
79
0
}
80
81
void
82
2
isc_stats_create(isc_mem_t *mctx, isc_stats_t **statsp, int ncounters) {
83
2
  REQUIRE(statsp != NULL && *statsp == NULL);
84
85
2
  isc_stats_t *stats = isc_mem_get(mctx, sizeof(*stats));
86
2
  size_t counters_alloc_size = sizeof(isc_atomic_statscounter_t) *
87
2
             ncounters;
88
2
  stats->counters = isc_mem_get(mctx, counters_alloc_size);
89
2
  isc_refcount_init(&stats->references, 1);
90
10
  for (int i = 0; i < ncounters; i++) {
91
8
    atomic_init(&stats->counters[i], 0);
92
8
  }
93
2
  stats->mctx = NULL;
94
2
  isc_mem_attach(mctx, &stats->mctx);
95
2
  stats->ncounters = ncounters;
96
2
  stats->magic = ISC_STATS_MAGIC;
97
2
  *statsp = stats;
98
2
}
99
100
isc_statscounter_t
101
0
isc_stats_increment(isc_stats_t *stats, isc_statscounter_t counter) {
102
0
  REQUIRE(ISC_STATS_VALID(stats));
103
0
  REQUIRE(counter < stats->ncounters);
104
105
0
  return atomic_fetch_add_relaxed(&stats->counters[counter], 1);
106
0
}
107
108
void
109
0
isc_stats_decrement(isc_stats_t *stats, isc_statscounter_t counter) {
110
0
  REQUIRE(ISC_STATS_VALID(stats));
111
0
  REQUIRE(counter < stats->ncounters);
112
#if ISC_STATS_CHECKUNDERFLOW
113
  REQUIRE(atomic_fetch_sub_release(&stats->counters[counter], 1) > 0);
114
#else
115
0
  atomic_fetch_sub_release(&stats->counters[counter], 1);
116
0
#endif
117
0
}
118
119
void
120
isc_stats_dump(isc_stats_t *stats, isc_stats_dumper_t dump_fn, void *arg,
121
0
         unsigned int options) {
122
0
  int i;
123
124
0
  REQUIRE(ISC_STATS_VALID(stats));
125
126
0
  for (i = 0; i < stats->ncounters; i++) {
127
0
    isc_statscounter_t counter =
128
0
      atomic_load_acquire(&stats->counters[i]);
129
0
    if ((options & ISC_STATSDUMP_VERBOSE) == 0 && counter == 0) {
130
0
      continue;
131
0
    }
132
0
    dump_fn((isc_statscounter_t)i, counter, arg);
133
0
  }
134
0
}
135
136
void
137
0
isc_stats_set(isc_stats_t *stats, uint64_t val, isc_statscounter_t counter) {
138
0
  REQUIRE(ISC_STATS_VALID(stats));
139
0
  REQUIRE(counter < stats->ncounters);
140
141
0
  atomic_store_release(&stats->counters[counter], val);
142
0
}
143
144
void
145
isc_stats_update_if_greater(isc_stats_t *stats, isc_statscounter_t counter,
146
0
          isc_statscounter_t value) {
147
0
  REQUIRE(ISC_STATS_VALID(stats));
148
0
  REQUIRE(counter < stats->ncounters);
149
150
0
  isc_statscounter_t curr_value =
151
0
    atomic_load_acquire(&stats->counters[counter]);
152
0
  do {
153
0
    if (curr_value >= value) {
154
0
      break;
155
0
    }
156
0
  } while (!atomic_compare_exchange_weak_acq_rel(
157
0
    &stats->counters[counter], &curr_value, value));
158
0
}
159
160
isc_statscounter_t
161
0
isc_stats_get_counter(isc_stats_t *stats, isc_statscounter_t counter) {
162
0
  REQUIRE(ISC_STATS_VALID(stats));
163
0
  REQUIRE(counter < stats->ncounters);
164
165
0
  return atomic_load_acquire(&stats->counters[counter]);
166
0
}
167
168
void
169
0
isc_stats_resize(isc_stats_t **statsp, int ncounters) {
170
0
  isc_stats_t *stats;
171
0
  size_t counters_alloc_size;
172
0
  isc_atomic_statscounter_t *newcounters;
173
174
0
  REQUIRE(statsp != NULL && *statsp != NULL);
175
0
  REQUIRE(ISC_STATS_VALID(*statsp));
176
0
  REQUIRE(ncounters > 0);
177
178
0
  stats = *statsp;
179
0
  if (stats->ncounters >= ncounters) {
180
    /* We already have enough counters. */
181
0
    return;
182
0
  }
183
184
  /* Grow number of counters. */
185
0
  counters_alloc_size = sizeof(isc_atomic_statscounter_t) * ncounters;
186
0
  newcounters = isc_mem_get(stats->mctx, counters_alloc_size);
187
0
  for (int i = 0; i < ncounters; i++) {
188
0
    atomic_init(&newcounters[i], 0);
189
0
  }
190
0
  for (int i = 0; i < stats->ncounters; i++) {
191
0
    uint32_t counter = atomic_load_acquire(&stats->counters[i]);
192
0
    atomic_store_release(&newcounters[i], counter);
193
0
  }
194
0
  isc_mem_cput(stats->mctx, stats->counters, stats->ncounters,
195
0
         sizeof(isc_atomic_statscounter_t));
196
0
  stats->counters = newcounters;
197
0
  stats->ncounters = ncounters;
198
0
}