From 05778a9a46e3d9a96e9f52f83e60f00dc3654b9a Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Fri, 14 Aug 2026 11:31:57 +0200 Subject: [PATCH] Serialize bandwidth rate limiter sleep inside mutex to eliminate parallel worker stall waves --- cmdline/bw.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/cmdline/bw.c b/cmdline/bw.c index 3d407545..ab13f6d6 100644 --- a/cmdline/bw.c +++ b/cmdline/bw.c @@ -56,12 +56,20 @@ void bw_limit(struct snapraid_bw* bw, uint64_t bytes) elapsed = eta + 10000; } -#if HAVE_THREAD - thread_mutex_unlock(&bw->lock); -#endif - + /* + * Sleep inside the lock to serialize delays across all worker threads. + * + * When multiple threads perform IO in parallel, holding the lock + * during sleep ensures each thread only sleeps for its own incremental + * delay, while waiting threads wait on the mutex and accurately measure + * the elapsed time once they acquire the lock. + */ if (eta > elapsed) { os_usleep((eta - elapsed) * 1000); } + +#if HAVE_THREAD + thread_mutex_unlock(&bw->lock); +#endif }