Use atomic operations instead of mutex for memory allocation counter

This commit is contained in:
Andrea Mazzoleni 2026-10-03 17:28:26 +02:00
parent 8589327114
commit 9f24ca00d1
5 changed files with 40 additions and 40 deletions

View file

@ -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)
{

View file

@ -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.

View file

@ -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
}

View file

@ -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
}

View file

@ -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 */