Natanael Copa wrote:
> > It passed with chrt --fifo (I had to do it from outside the LXC container):
> > 
> > # time chrt --fifo 10 ./test-pthread-rwlock
> > Starting test_rwlock ... OK
> > real        0m 33.00s
> > user        6m 50.63s
> > sys 0m 16.23s

I then wrote:
> * On x86_64 (Alpine Linux), the test performs about 25% faster
>   under SCHED_FIFO and SCHED_RR.

Note: that measurement was in an Alpine Linux VM with 1 CPU.

I tried to incorporate this speedup into the test, by use of the
attached patch. But unfortunately, it does not help. Here are
the timings on that Alpine Linux VM (-fifo is the patched unit test):

                                                       1 CPU        2 CPUs      
  4 CPUs         8 CPUs

$ time ./test-pthread-rwlock                           1.56 s       2.02 s      
  1.09 s         0.70 s

$ time ./test-pthread-rwlock-fifo                      1.57 s       2.00 s      
  1.06 s         0.74 s

# time chrt --fifo 10 ./test-pthread-rwlock            1.24 s       3.3 s       
  1.8 s          0.69 s

# time chrt --fifo 10 ./test-pthread-rwlock-fifo       1.26 s       3.2 s       
  1.8 s          0.69 s

And on Solaris and Cygwin, I don't see a measurable difference either.

Bruno
>From 3a46de9041b213e838c2654f29aa1e7cc13347cb Mon Sep 17 00:00:00 2001
From: Bruno Haible <br...@clisp.org>
Date: Fri, 16 May 2025 13:15:56 +0200
Subject: [PATCH] pthread-rwlock tests: Attempt to minimize writer starvation
 symptoms.

Reported by Natanael Copa <nc...@alpinelinux.org> in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00164.html>.

* tests/test-pthread-rwlock.c (test_rwlock): Create the threads with the
schedpolicy attribute set to SCHED_FIFO.
---
 ChangeLog                   |  8 ++++++++
 tests/test-pthread-rwlock.c | 28 +++++++++++++++++++++-------
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 371af7fada..cf9721df7e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-05-16  Bruno Haible  <br...@clisp.org>
+
+	pthread-rwlock tests: Attempt to minimize writer starvation symptoms.
+	Reported by Natanael Copa <nc...@alpinelinux.org> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00164.html>.
+	* tests/test-pthread-rwlock.c (test_rwlock): Create the threads with the
+	schedpolicy attribute set to SCHED_FIFO.
+
 2025-05-14  Bruno Haible  <br...@clisp.org>
 
 	asyncsafe-spin: Rely on module 'spin'.
diff --git a/tests/test-pthread-rwlock.c b/tests/test-pthread-rwlock.c
index 90d6ac5017..83d09c8c96 100644
--- a/tests/test-pthread-rwlock.c
+++ b/tests/test-pthread-rwlock.c
@@ -175,13 +175,27 @@ test_rwlock (void)
   set_atomic_int_value (&rwlock_checker_done, 0);
 
   /* Spawn the threads.  */
-  for (i = 0; i < THREAD_COUNT; i++)
-    ASSERT (pthread_create (&checkerthreads[i], NULL,
-                            rwlock_checker_thread, NULL)
-            == 0);
-  for (i = 0; i < THREAD_COUNT; i++)
-    ASSERT (pthread_create (&threads[i], NULL, rwlock_mutator_thread, NULL)
-            == 0);
+  {
+    pthread_attr_t attr;
+    ASSERT (pthread_attr_init (&attr) == 0);
+    /* Attempt to minimize the writer starvation symptoms, especially on
+       musl libc.  See
+       <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_rwlock_rdlock.html>
+       <https://lists.gnu.org/archive/html/bug-gnulib/2025-05/msg00164.html>  */
+    #if !(__GLIBC__ >= 2) && defined SCHED_FIFO
+    (void) pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
+    #endif
+
+    for (i = 0; i < THREAD_COUNT; i++)
+      ASSERT (pthread_create (&checkerthreads[i], &attr,
+                              rwlock_checker_thread, NULL)
+              == 0);
+    for (i = 0; i < THREAD_COUNT; i++)
+      ASSERT (pthread_create (&threads[i], &attr, rwlock_mutator_thread, NULL)
+              == 0);
+
+    ASSERT (pthread_attr_destroy (&attr) == 0);
+  }
 
   /* Wait for the threads to terminate.  */
   for (i = 0; i < THREAD_COUNT; i++)
-- 
2.43.0

Reply via email to