]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
fixup! fixup! fixup! Cleanups
authorAlessio Podda <alessio@isc.org>
Wed, 1 Oct 2025 09:18:24 +0000 (11:18 +0200)
committerAlessio Podda <alessio@isc.org>
Wed, 1 Oct 2025 09:18:24 +0000 (11:18 +0200)
lib/isc/statsmulti.c

index 8455be10c9c51f9cfd5f7a7e2cedc49c6a197e6f..e7e874d621d24a85d51e4b643fc03e3e5818ccd2 100644 (file)
@@ -42,15 +42,15 @@ struct isc_statsmulti {
        isc_refcount_t references;
        int n_counters;
        int n_additive;
-       int n_highwater;
        int per_thread_capacity;
        int num_threads_plus_one;
-       char *counters;
+       isc_atomic_statscounter_t *counters;
 };
 
 static isc_statscounter_t
 additive_counter(isc_statsmulti_t *stats, isc_statscounter_t counter) {
        REQUIRE(counter < stats->n_additive);
+
        return counter;
 }
 
@@ -59,7 +59,6 @@ highwater_counter(isc_statsmulti_t *stats, isc_statscounter_t counter) {
        REQUIRE(counter >= stats->n_additive);
        REQUIRE(counter < stats->n_counters);
 
-       /* Map counter to internal highwater position */
        return counter;
 }
 
@@ -72,13 +71,9 @@ to_index(isc_statsmulti_t *stats, isc_tid_t tid, isc_statscounter_t internal_cou
        return thread_id * stats->per_thread_capacity + internal_counter;
 }
 
-static isc_atomic_statscounter_t *
-ptr_from_index(isc_statsmulti_t *stats, int index) {
-       return (isc_atomic_statscounter_t *)&stats->counters[index * sizeof(isc_atomic_statscounter_t)];
-}
 
 static void
-atomic_maximum(isc_atomic_statscounter_t *counter, isc_statscounter_t value) {
+atomic_max_relaxed(isc_atomic_statscounter_t *counter, isc_statscounter_t value) {
        isc_statscounter_t current = atomic_load_relaxed(counter);
        while (value > current) {
                if (atomic_compare_exchange_weak_relaxed(counter, &current, value)) {
@@ -105,7 +100,6 @@ isc_statsmulti_create(isc_mem_t *mctx, isc_statsmulti_t **statsp, int n_additive
                .mctx = NULL,
                .n_counters = ncounters,
                .n_additive = n_additive,
-               .n_highwater = n_highwater,
                .per_thread_capacity = per_thread_capacity,
                .num_threads_plus_one = num_threads_plus_one,
                .counters = isc_mem_cget(mctx, per_thread_capacity * num_threads_plus_one, sizeof(isc_atomic_statscounter_t)),
@@ -148,9 +142,9 @@ isc_statsmulti_increment(isc_statsmulti_t *stats, isc_statscounter_t counter) {
 
        int index = to_index(stats, isc_tid(), counter);
        if (isc_tid() == 0) {
-               return atomic_fetch_add_relaxed(ptr_from_index(stats, index), 1);
+               return atomic_fetch_add_relaxed(&stats->counters[index], 1);
        } else {
-               isc_atomic_statscounter_t *ptr = ptr_from_index(stats, index);
+               isc_atomic_statscounter_t *ptr = &stats->counters[index];
                int_fast64_t tmp = atomic_load_relaxed(ptr);
                atomic_store_relaxed(ptr, tmp + 1);
                return tmp;
@@ -164,9 +158,9 @@ isc_statsmulti_decrement(isc_statsmulti_t *stats, isc_statscounter_t counter) {
 
        int index = to_index(stats, isc_tid(), counter);
        if (isc_tid() == 0) {
-               atomic_fetch_sub_relaxed(ptr_from_index(stats, index), 1);
+               atomic_fetch_sub_relaxed(&stats->counters[index], 1);
        } else {
-               isc_atomic_statscounter_t *ptr = ptr_from_index(stats, index);
+               isc_atomic_statscounter_t *ptr = &stats->counters[index];
                int_fast64_t tmp = atomic_load_relaxed(ptr);
                atomic_store_relaxed(ptr, tmp - 1);
        }
@@ -201,12 +195,12 @@ isc_statsmulti_get_counter(isc_statsmulti_t *stats, isc_statscounter_t counter)
        REQUIRE(ISC_STATSMULTI_VALID(stats));
        counter = additive_counter(stats, counter);
 
-       int index0 = to_index(stats, 0, counter);
-       isc_statscounter_t total = atomic_load_acquire(ptr_from_index(stats, index0));
+       int idx_0 = to_index(stats, 0, counter);
+       isc_statscounter_t total = atomic_load_acquire(&stats->counters[idx_0]);
 
        for (int thread = 1; thread < stats->num_threads_plus_one; thread++) {
                int index = to_index(stats, thread, counter);
-               total += atomic_load_relaxed(ptr_from_index(stats, index));
+               total += atomic_load_relaxed(&stats->counters[index]);
        }
 
        return total;
@@ -216,20 +210,18 @@ void
 isc_statsmulti_clear(isc_statsmulti_t *stats) {
        REQUIRE(ISC_STATSMULTI_VALID(stats));
 
-       for (int i = 0; i < stats->per_thread_capacity * stats->num_threads_plus_one; i++) {
-               atomic_store_relaxed(ptr_from_index(stats, i), 0);
+       for (int idx = 0; idx < stats->per_thread_capacity * stats->num_threads_plus_one; idx++) {
+               atomic_store_relaxed(&stats->counters[idx], 0);
        }
 }
 
 void
 isc_statsmulti_update_if_greater(isc_statsmulti_t *stats, isc_statscounter_t counter, isc_statscounter_t value) {
        REQUIRE(ISC_STATSMULTI_VALID(stats));
+       counter = highwater_counter(stats, counter);
 
-       isc_statscounter_t internal_counter = highwater_counter(stats, counter);
-
-       int index = to_index(stats, isc_tid(), internal_counter);
-       
-       atomic_maximum(ptr_from_index(stats, index), value);
+       int index = to_index(stats, isc_tid(), counter);
+       atomic_max_relaxed(&stats->counters[index], value);
 }
 
 isc_statscounter_t
@@ -239,14 +231,12 @@ isc_statsmulti_get_highwater(isc_statsmulti_t *stats, isc_statscounter_t counter
        isc_statscounter_t internal_counter = highwater_counter(stats, counter);
 
        int index0 = to_index(stats, 0, internal_counter);
-       isc_statscounter_t max_value = atomic_load_acquire(ptr_from_index(stats, index0));
+       isc_statscounter_t max_value = atomic_load_acquire(&stats->counters[index0]);
 
        for (int thread = 1; thread < stats->num_threads_plus_one; thread++) {
                int index = to_index(stats, thread, internal_counter);
-               isc_statscounter_t value = atomic_load_acquire(ptr_from_index(stats, index));
-               if (value > max_value) {
-                       max_value = value;
-               }
+               isc_statscounter_t value = atomic_load_acquire(&stats->counters[index]);
+               max_value = ISC_MAX(value, max_value);
        }
        return max_value;
 }
@@ -259,6 +249,6 @@ isc_statsmulti_reset_highwater(isc_statsmulti_t *stats, isc_statscounter_t count
 
        for (int thread = 0; thread < stats->num_threads_plus_one; thread++) {
                int index = to_index(stats, thread, internal_counter);
-               atomic_store_relaxed(ptr_from_index(stats, index), 0);
+               atomic_store_relaxed(&stats->counters[index], 0);
        }
 }