Extract bin->stats.nrequests mutation into bin_stats_nrequests_add

This commit is contained in:
Slobodan Predolac 2026-04-30 15:31:21 -07:00
parent 8edd101286
commit e286fba00a
3 changed files with 46 additions and 3 deletions

View file

@ -100,6 +100,13 @@ bin_t *bin_choose(tsdn_t *tsdn, arena_t *arena, szind_t binind,
unsigned *binshard_p);
/* Stats. */
static inline void
bin_stats_nrequests_add(tsdn_t *tsdn, bin_t *bin, uint64_t n) {
malloc_mutex_lock(tsdn, &bin->lock);
bin->stats.nrequests += n;
malloc_mutex_unlock(tsdn, &bin->lock);
}
static inline void
bin_stats_merge(tsdn_t *tsdn, bin_stats_data_t *dst_bin_stats, bin_t *bin) {
malloc_mutex_lock(tsdn, &bin->lock);

View file

@ -1287,9 +1287,8 @@ tcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {
}
if (i < SC_NBINS) {
bin_t *bin = bin_choose(tsdn, arena, i, NULL);
malloc_mutex_lock(tsdn, &bin->lock);
bin->stats.nrequests += cache_bin->tstats.nrequests;
malloc_mutex_unlock(tsdn, &bin->lock);
bin_stats_nrequests_add(tsdn, bin,
cache_bin->tstats.nrequests);
} else {
arena_stats_large_flush_nrequests_add(tsdn,
&arena->stats, i, cache_bin->tstats.nrequests);

View file

@ -647,6 +647,42 @@ TEST_BEGIN(test_bin_dalloc_slab_prepare) {
}
TEST_END
/*
* Test that bin_stats_nrequests_add accumulates under the bin lock.
*/
TEST_BEGIN(test_bin_stats_nrequests_add) {
tsdn_t *tsdn = tsdn_fetch();
bin_t bin;
bin_init(&bin);
if (config_stats) {
expect_u64_eq(bin.stats.nrequests, 0,
"Fresh bin should have zero nrequests");
}
/* Single add. */
bin_stats_nrequests_add(tsdn, &bin, 7);
if (config_stats) {
expect_u64_eq(bin.stats.nrequests, 7,
"nrequests should equal the added value");
}
/* Adds accumulate. */
bin_stats_nrequests_add(tsdn, &bin, 3);
if (config_stats) {
expect_u64_eq(bin.stats.nrequests, 10,
"nrequests should accumulate across calls");
}
/* Adding zero is a no-op. */
bin_stats_nrequests_add(tsdn, &bin, 0);
if (config_stats) {
expect_u64_eq(bin.stats.nrequests, 10,
"Adding zero should not change nrequests");
}
}
TEST_END
/*
* Test bin_shard_sizes_boot and bin_update_shard_size.
*/
@ -819,6 +855,7 @@ main(void) {
test_bin_lower_slab_replaces_slabcur,
test_bin_lower_slab_inserts_nonfull,
test_bin_dalloc_slab_prepare,
test_bin_stats_nrequests_add,
test_bin_shard_sizes,
test_bin_alloc_free_cycle,
test_bin_multi_size_class);