On Wed, Apr 03 2024 at 17:43, Thomas Gleixner wrote:
On Wed, Apr 03 2024 at 17:03, Oleg Nesterov wrote:
Why distribution_thread() can't simply exit if got_signal != 0 ?
See https://lore.kernel.org/all/20230128195641.GA14906@redhat.com/
Indeed. It's too obvious :)
Revised simpler version below.
Thanks,
tglx --- Subject: selftests/timers/posix_timers: Make signal distribution test less fragile From: Thomas Gleixner tglx@linutronix.de
The signal distribution test has a tendency to hang for a long time as the signal delivery is not really evenly distributed. In fact it might never be distributed across all threads ever in the way it is written.
Address this by:
1) Adding a timeout which aborts the test
2) Letting the test threads exit once they got a signal instead of running continuously. That ensures that the other threads will have a chance to expire the timer and get the signal.
3) Adding a detection whether all signals arrvied at the main thread, which allows to run the test on older kernels and emit 'SKIP'.
While at it get rid of the pointless atomic operation on a the thread local variable in the signal handler.
Signed-off-by: Thomas Gleixner tglx@linutronix.de --- tools/testing/selftests/timers/posix_timers.c | 41 ++++++++++++++++---------- 1 file changed, 26 insertions(+), 15 deletions(-)
--- a/tools/testing/selftests/timers/posix_timers.c +++ b/tools/testing/selftests/timers/posix_timers.c @@ -184,18 +184,19 @@ static int check_timer_create(int which) return 0; }
-int remain; -__thread int got_signal; +static int remain; +static __thread int got_signal;
static void *distribution_thread(void *arg) { - while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); + while (!done && !got_signal); + return NULL; }
static void distribution_handler(int nr) { - if (!__atomic_exchange_n(&got_signal, 1, __ATOMIC_RELAXED)) + if (++got_signal == 1) __atomic_fetch_sub(&remain, 1, __ATOMIC_RELAXED); }
@@ -205,8 +206,6 @@ static void distribution_handler(int nr) */ static int check_timer_distribution(void) { - int err, i; - timer_t id; const int nthreads = 10; pthread_t threads[nthreads]; struct itimerspec val = { @@ -215,7 +214,11 @@ static int check_timer_distribution(void .it_interval.tv_sec = 0, .it_interval.tv_nsec = 1000 * 1000, }; + time_t start, now; + timer_t id; + int err, i;
+ done = 0; remain = nthreads + 1; /* worker threads + this thread */ signal(SIGALRM, distribution_handler); err = timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); @@ -230,8 +233,7 @@ static int check_timer_distribution(void }
for (i = 0; i < nthreads; i++) { - err = pthread_create(&threads[i], NULL, distribution_thread, - NULL); + err = pthread_create(&threads[i], NULL, distribution_thread, NULL); if (err) { ksft_print_msg("Can't create thread: %s (%d)\n", strerror(errno), errno); @@ -240,7 +242,18 @@ static int check_timer_distribution(void }
/* Wait for all threads to receive the signal. */ - while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); + now = start = time(NULL); + while (__atomic_load_n(&remain, __ATOMIC_RELAXED)) { + now = time(NULL); + if (now - start > 2) + break; + } + done = 1; + + if (timer_delete(id)) { + ksft_perror("Can't delete timer\n"); + return -1; + }
for (i = 0; i < nthreads; i++) { err = pthread_join(threads[i], NULL); @@ -251,12 +264,10 @@ static int check_timer_distribution(void } }
- if (timer_delete(id)) { - ksft_perror("Can't delete timer"); - return -1; - } - - ksft_test_result_pass("check_timer_distribution\n"); + if (__atomic_load_n(&remain, __ATOMIC_RELAXED) == nthreads) + ksft_test_result_skip("No signal distribution. Assuming old kernel\n"); + else + ksft_test_result(now - start <= 2, "check signal distribution\n"); return 0; }