From 9f24ca00d1e6af761f628e70d50c572b86df999e Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 3 Oct 2026 17:28:26 +0200 Subject: [PATCH] Use atomic operations instead of mutex for memory allocation counter --- cmdline/memory.c | 49 +++++++++++++++++++++++++++++++---------------- cmdline/memory.h | 5 +++++ cmdline/state.c | 3 ++- cmdline/support.c | 17 ---------------- cmdline/support.h | 6 ------ 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/cmdline/memory.c b/cmdline/memory.c index 5a005fff..f2c395a3 100644 --- a/cmdline/memory.c +++ b/cmdline/memory.c @@ -14,28 +14,45 @@ */ static size_t mcounter; +#if HAVE_THREAD +static int memory_is_single_thread; + +void memory_single_thread(int single_thread) +{ + memory_is_single_thread = single_thread; +} + +static inline void malloc_counter_inc(size_t inc) +{ + if (tommy_likely(memory_is_single_thread)) + mcounter += inc; + else + __atomic_fetch_add(&mcounter, inc, __ATOMIC_RELAXED); +} + size_t malloc_counter_get(void) { - size_t ret; - - lock_memory(); - - ret = mcounter; - - unlock_memory(); - - return ret; + if (tommy_likely(memory_is_single_thread)) + return mcounter; + return __atomic_load_n(&mcounter, __ATOMIC_RELAXED); } - -static void malloc_counter_inc(size_t inc) +#else +void memory_single_thread(int single_thread) { - lock_memory(); - - mcounter += inc; - - unlock_memory(); + (void)single_thread; } +static inline void malloc_counter_inc(size_t inc) +{ + mcounter += inc; +} + +size_t malloc_counter_get(void) +{ + return mcounter; +} +#endif + /* LCOV_EXCL_START */ static ssize_t malloc_print(int f, const char* str) { diff --git a/cmdline/memory.h b/cmdline/memory.h index 76950e67..0d05f376 100644 --- a/cmdline/memory.h +++ b/cmdline/memory.h @@ -12,6 +12,11 @@ */ size_t malloc_counter_get(void); +/** + * Enable or disable single-thread mode for memory counters. + */ +void memory_single_thread(int single_thread); + /** * Safe malloc. * If no memory is available, it aborts. diff --git a/cmdline/state.c b/cmdline/state.c index 9f504e21..c9c83ff0 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -2477,6 +2477,8 @@ static int state_read_block_run(STREAM* f, struct snapraid_file* file, block_off */ static void fs_single_thread(struct snapraid_state* state, int single_thread) { + memory_single_thread(single_thread); + #if HAVE_THREAD for (tommy_node* i = state->disklist; i != 0; i = i->next) { struct snapraid_disk* disk = i->data; @@ -2484,7 +2486,6 @@ static void fs_single_thread(struct snapraid_state* state, int single_thread) } #else (void)state; - (void)single_thread; #endif } diff --git a/cmdline/support.c b/cmdline/support.c index ae01598c..bbb91d7d 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -52,7 +52,6 @@ char* esc_buf(void) */ #if HAVE_THREAD static thread_mutex_t msg_lock; -static thread_mutex_t memory_lock; static thread_mutex_t random_lock; #endif @@ -70,20 +69,6 @@ void unlock_msg(void) #endif } -void lock_memory(void) -{ -#if HAVE_THREAD - thread_mutex_lock(&memory_lock); -#endif -} - -void unlock_memory(void) -{ -#if HAVE_THREAD - thread_mutex_unlock(&memory_lock); -#endif -} - void lock_random(void) { #if HAVE_THREAD @@ -103,7 +88,6 @@ void lock_init(void) #if HAVE_THREAD /* initialize the locks as first operation as log_fatal depends on them */ thread_mutex_init(&msg_lock); - thread_mutex_init(&memory_lock); thread_mutex_init(&random_lock); thread_key_create(&esc_key, esc_pool_free); #endif @@ -119,7 +103,6 @@ void lock_done(void) } thread_key_delete(esc_key); thread_mutex_destroy(&msg_lock); - thread_mutex_destroy(&memory_lock); thread_mutex_destroy(&random_lock); #endif } diff --git a/cmdline/support.h b/cmdline/support.h index 995901f8..34f945b0 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -36,12 +36,6 @@ void lock_done(void); void lock_msg(void); void unlock_msg(void); -/** - * Lock used for memory counter. - */ -void lock_memory(void); -void unlock_memory(void); - /****************************************************************************/ /* random */