All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: fam@euphon.net, berrange@redhat.com,
	"Richard Henderson" <richard.henderson@linaro.org>,
	f4bug@amsat.org, stefanha@redhat.com, crosa@redhat.com,
	pbonzini@redhat.com, "Alex Bennée" <alex.bennee@linaro.org>,
	aurelien@aurel32.net
Subject: [PATCH v2 28/30] tests/tcg: add a multiarch signals test to stress test signal delivery
Date: Tue,  4 May 2021 11:02:21 +0100	[thread overview]
Message-ID: <20210504100223.25427-29-alex.bennee@linaro.org> (raw)
In-Reply-To: <20210504100223.25427-1-alex.bennee@linaro.org>

This adds a simple signal test that combines the POSIX timer_create
with signal delivery across multiple threads. The aim is to provide a
bit more of a stress test to flush out signal handling issues for
easily than the occasional random crash we sometimes see in linux-test
or threadcount.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

---
v2
  - minor cosmetic tweaks
  - skip for alpha/sparc64 (including with plugins)
---
 tests/tcg/multiarch/signals.c       | 149 ++++++++++++++++++++++++++++
 tests/tcg/alpha/Makefile.target     |   7 ++
 tests/tcg/multiarch/Makefile.target |   2 +
 tests/tcg/sparc64/Makefile.target   |   7 ++
 4 files changed, 165 insertions(+)
 create mode 100644 tests/tcg/multiarch/signals.c

diff --git a/tests/tcg/multiarch/signals.c b/tests/tcg/multiarch/signals.c
new file mode 100644
index 0000000000..998c8fdefd
--- /dev/null
+++ b/tests/tcg/multiarch/signals.c
@@ -0,0 +1,149 @@
+/*
+ * linux-user signal handling tests.
+ *
+ * Copyright (c) 2021 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <pthread.h>
+#include <string.h>
+#include <signal.h>
+#include <time.h>
+#include <sys/time.h>
+
+static void error1(const char *filename, int line, const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    fprintf(stderr, "%s:%d: ", filename, line);
+    vfprintf(stderr, fmt, ap);
+    fprintf(stderr, "\n");
+    va_end(ap);
+    exit(1);
+}
+
+static int __chk_error(const char *filename, int line, int ret)
+{
+    if (ret < 0) {
+        error1(filename, line, "%m (ret=%d, errno=%d/%s)",
+               ret, errno, strerror(errno));
+    }
+    return ret;
+}
+
+#define error(fmt, ...) error1(__FILE__, __LINE__, fmt, ## __VA_ARGS__)
+
+#define chk_error(ret) __chk_error(__FILE__, __LINE__, (ret))
+
+/*
+ * Thread handling
+ */
+typedef struct ThreadJob ThreadJob;
+
+struct ThreadJob {
+    int number;
+    int sleep;
+    int count;
+};
+
+static pthread_t *threads;
+static int max_threads = 10;
+__thread int signal_count;
+int total_signal_count;
+
+static void *background_thread_func(void *arg)
+{
+    ThreadJob *job = (ThreadJob *) arg;
+
+    printf("thread%d: started\n", job->number);
+    while (total_signal_count < job->count) {
+        usleep(job->sleep);
+    }
+    printf("thread%d: saw %d alarms from %d\n", job->number,
+           signal_count, total_signal_count);
+    return NULL;
+}
+
+static void spawn_threads(void)
+{
+    int i;
+    threads = calloc(sizeof(pthread_t), max_threads);
+
+    for (i = 0; i < max_threads; i++) {
+        ThreadJob *job = calloc(sizeof(ThreadJob), 1);
+        job->number = i;
+        job->sleep = i * 1000;
+        job->count = i * 100;
+        pthread_create(threads + i, NULL, background_thread_func, job);
+    }
+}
+
+static void close_threads(void)
+{
+    int i;
+    for (i = 0; i < max_threads; i++) {
+        pthread_join(threads[i], NULL);
+    }
+    free(threads);
+    threads = NULL;
+}
+
+static void sig_alarm(int sig, siginfo_t *info, void *puc)
+{
+    if (sig != SIGRTMIN) {
+        error("unexpected signal");
+    }
+    signal_count++;
+    __atomic_fetch_add(&total_signal_count, 1, __ATOMIC_SEQ_CST);
+}
+
+static void test_signals(void)
+{
+    struct sigaction act;
+    struct itimerspec it;
+    timer_t tid;
+    struct sigevent sev;
+
+    /* Set up SIG handler */
+    act.sa_sigaction = sig_alarm;
+    sigemptyset(&act.sa_mask);
+    act.sa_flags = SA_SIGINFO;
+    chk_error(sigaction(SIGRTMIN, &act, NULL));
+
+    /* Create POSIX timer */
+    sev.sigev_notify = SIGEV_SIGNAL;
+    sev.sigev_signo = SIGRTMIN;
+    sev.sigev_value.sival_ptr = &tid;
+    chk_error(timer_create(CLOCK_REALTIME, &sev, &tid));
+
+    it.it_interval.tv_sec = 0;
+    it.it_interval.tv_nsec = 1000000;
+    it.it_value.tv_sec = 0;
+    it.it_value.tv_nsec = 1000000;
+    chk_error(timer_settime(tid, 0, &it, NULL));
+
+    spawn_threads();
+
+    do {
+        usleep(1000);
+    } while (total_signal_count < 2000);
+
+    printf("shutting down after: %d signals\n", total_signal_count);
+
+    close_threads();
+
+    chk_error(timer_delete(tid));
+}
+
+int main(int argc, char **argv)
+{
+    test_signals();
+    return 0;
+}
diff --git a/tests/tcg/alpha/Makefile.target b/tests/tcg/alpha/Makefile.target
index a585080328..49ce5cd69f 100644
--- a/tests/tcg/alpha/Makefile.target
+++ b/tests/tcg/alpha/Makefile.target
@@ -14,5 +14,12 @@ test-cmov: test-cond.c
 
 run-test-cmov: test-cmov
 
+# currently broken: executes a halt on sigreturn leading to being terminated by signal SIGILL (Illegal instruction)
+run-signals: signals
+	$(call skip-test, $<, "BROKEN")
+
+run-plugin-signals-with-%: signals
+	$(call skip-test, $<, "BROKEN")
+
 # On Alpha Linux only supports 8k pages
 EXTRA_RUNS+=run-test-mmap-8192
diff --git a/tests/tcg/multiarch/Makefile.target b/tests/tcg/multiarch/Makefile.target
index a3a751723d..3f283eabe6 100644
--- a/tests/tcg/multiarch/Makefile.target
+++ b/tests/tcg/multiarch/Makefile.target
@@ -30,6 +30,8 @@ testthread: LDFLAGS+=-lpthread
 
 threadcount: LDFLAGS+=-lpthread
 
+signals: LDFLAGS+=-lrt -lpthread
+
 # We define the runner for test-mmap after the individual
 # architectures have defined their supported pages sizes. If no
 # additional page sizes are defined we only run the default test.
diff --git a/tests/tcg/sparc64/Makefile.target b/tests/tcg/sparc64/Makefile.target
index 5bd7f90583..fb3825b7fa 100644
--- a/tests/tcg/sparc64/Makefile.target
+++ b/tests/tcg/sparc64/Makefile.target
@@ -7,5 +7,12 @@
 run-linux-test: linux-test
 	$(call skip-test, $<, "BROKEN")
 
+# currently broken: hangs after the first thread exits
+run-signals: signals
+	$(call skip-test, $<, "BROKEN")
+
+run-plugin-signals-with-%: signals
+	$(call skip-test, $<, "BROKEN")
+
 # On Sparc64 Linux support 8k pages
 EXTRA_RUNS+=run-test-mmap-8192
-- 
2.20.1



  parent reply	other threads:[~2021-05-04 10:13 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-04 10:01 [PATCH v2 00/30] testing/next (hexagon, tricore, ppc, signals, cirrus) Alex Bennée
2021-05-04 10:01 ` [PATCH v2 01/30] checkpatch: Fix use of uninitialized value Alex Bennée
2021-05-12  4:47   ` Philippe Mathieu-Daudé
2021-05-04 10:01 ` [PATCH v2 02/30] tests/docker: fix copying of executable in "update" Alex Bennée
2021-05-04 10:01 ` [PATCH v2 03/30] tests/docker: make executable an optional argument to "update" Alex Bennée
2021-05-04 10:01 ` [PATCH v2 04/30] tests/docker: allow "update" to add the current user Alex Bennée
2021-05-04 10:01 ` [PATCH v2 05/30] tests/docker: add "fetch" sub-command Alex Bennée
2021-05-04 10:01 ` [PATCH v2 06/30] docker: Add Hexagon image Alex Bennée
2021-05-04 10:02 ` [PATCH v2 07/30] tests/tcg: Use Hexagon Docker image Alex Bennée
2021-05-04 10:02 ` [PATCH v2 08/30] gitlab: add build-user-hexagon test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 09/30] tests/tcg: don't iterate through other arch compilers Alex Bennée
2021-05-04 10:02 ` [PATCH v2 10/30] tests/docker: Added libbpf library to the docker files Alex Bennée
2021-05-04 10:02 ` [PATCH v2 11/30] tests/tcg: Add docker_as and docker_ld cmds Alex Bennée
2021-05-04 10:02 ` [PATCH v2 12/30] tests/tcg: Run timeout cmds using --foreground Alex Bennée
2021-05-04 10:02 ` [PATCH v2 13/30] hw/tricore: Add testdevice for tests in tests/tcg/ Alex Bennée
2021-05-04 10:02 ` [PATCH v2 14/30] tests/tcg/tricore: Add build infrastructure Alex Bennée
2021-05-04 10:02 ` [PATCH v2 15/30] configure: Emit HOST_CC to config-host.mak Alex Bennée
2021-05-04 10:02 ` [PATCH v2 16/30] tests/tcg/tricore: Add macros to create tests and first test 'abs' Alex Bennée
2021-05-04 10:02 ` [PATCH v2 17/30] tests/tcg/tricore: Add bmerge test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 18/30] tests/tcg/tricore: Add clz test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 19/30] tests/tcg/tricore: Add dvstep test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 20/30] tests/tcg/tricore: Add fadd test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 21/30] tests/tcg/tricore: Add fmul test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 22/30] tests/tcg/tricore: Add ftoi test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 23/30] tests/tcg/tricore: Add madd test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 24/30] tests/tcg/tricore: Add msub test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 25/30] tests/tcg/tricore: Add muls test Alex Bennée
2021-05-04 10:02 ` [PATCH v2 26/30] tests/docker: gcc-10 based images for ppc64{, le} tests Alex Bennée
2021-05-04 10:02 ` [PATCH v2 27/30] tests/tcg/ppc64le: tests for brh/brw/brd Alex Bennée
2021-05-04 10:02 ` Alex Bennée [this message]
2021-05-04 10:02 ` [PATCH v2 29/30] cirrus.yml: Fix the MSYS2 task Alex Bennée
2021-05-12  4:49   ` Philippe Mathieu-Daudé
2021-05-12  4:53     ` 罗勇刚(Yonggang Luo)
2021-05-12  4:56   ` 罗勇刚(Yonggang Luo)
2021-05-04 10:02 ` [PATCH v2 30/30] configure: use cc, not host_cc to set cross_cc for build arch Alex Bennée
2021-05-12  4:50   ` Philippe Mathieu-Daudé
2021-05-04 10:39 ` [PATCH v2 00/30] testing/next (hexagon, tricore, ppc, signals, cirrus) no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210504100223.25427-29-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=aurelien@aurel32.net \
    --cc=berrange@redhat.com \
    --cc=crosa@redhat.com \
    --cc=f4bug@amsat.org \
    --cc=fam@euphon.net \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.