1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022-3 ARM Limited.
7 #define _POSIX_C_SOURCE 199309L
20 #include <sys/epoll.h>
21 #include <sys/prctl.h>
22 #include <sys/types.h>
25 #include <asm/hwcap.h>
27 #include "../../kselftest.h"
40 static struct child_data
*children
;
41 static struct epoll_event
*evs
;
43 static int num_children
;
44 static bool terminate
;
46 static int startup_pipe
[2];
48 static int num_processors(void)
50 long nproc
= sysconf(_SC_NPROCESSORS_CONF
);
52 perror("Unable to read number of processors\n");
59 static void start_thread(struct child_data
*child
, int id
)
61 int ret
, pipefd
[2], i
;
62 struct epoll_event ev
;
66 ksft_exit_fail_msg("Failed to create stdout pipe: %s (%d)\n",
67 strerror(errno
), errno
);
71 ksft_exit_fail_msg("fork() failed: %s (%d)\n",
72 strerror(errno
), errno
);
76 * In child, replace stdout with the pipe, errors to
77 * stderr from here as kselftest prints to stdout.
79 ret
= dup2(pipefd
[1], 1);
81 fprintf(stderr
, "dup2() %d\n", errno
);
86 * Duplicate the read side of the startup pipe to
87 * FD 3 so we can close everything else.
89 ret
= dup2(startup_pipe
[0], 3);
91 fprintf(stderr
, "dup2() %d\n", errno
);
96 * Very dumb mechanism to clean open FDs other than
97 * stdio. We don't want O_CLOEXEC for the pipes...
99 for (i
= 4; i
< 8192; i
++)
103 * Read from the startup pipe, there should be no data
104 * and we should block until it is closed. We just
105 * carry on on error since this isn't super critical.
107 ret
= read(3, &i
, sizeof(i
));
109 fprintf(stderr
, "read(startp pipe) failed: %s (%d)\n",
110 strerror(errno
), errno
);
112 fprintf(stderr
, "%d bytes of data on startup pipe\n",
116 ret
= execl("gcs-stress-thread", "gcs-stress-thread", NULL
);
117 fprintf(stderr
, "execl(gcs-stress-thread) failed: %d (%s)\n",
118 errno
, strerror(errno
));
123 * In parent, remember the child and close our copy of the
124 * write side of stdout.
127 child
->stdout
= pipefd
[0];
128 child
->output
= NULL
;
129 child
->exited
= false;
130 child
->output_seen
= false;
132 ev
.events
= EPOLLIN
| EPOLLHUP
;
135 ret
= asprintf(&child
->name
, "Thread-%d", id
);
137 ksft_exit_fail_msg("asprintf() failed\n");
139 ret
= epoll_ctl(epoll_fd
, EPOLL_CTL_ADD
, child
->stdout
, &ev
);
141 ksft_exit_fail_msg("%s EPOLL_CTL_ADD failed: %s (%d)\n",
142 child
->name
, strerror(errno
), errno
);
146 ksft_print_msg("Started %s\n", child
->name
);
150 static bool child_output_read(struct child_data
*child
)
152 char read_data
[1024];
154 int ret
, len
, cur_work
, cur_read
;
156 ret
= read(child
->stdout
, read_data
, sizeof(read_data
));
161 ksft_print_msg("%s: read() failed: %s (%d)\n",
162 child
->name
, strerror(errno
),
168 child
->output_seen
= true;
170 /* Pick up any partial read */
172 strncpy(work
, child
->output
, sizeof(work
) - 1);
173 cur_work
= strnlen(work
, sizeof(work
));
175 child
->output
= NULL
;
181 while (cur_read
< len
) {
182 work
[cur_work
] = read_data
[cur_read
++];
184 if (work
[cur_work
] == '\n') {
185 work
[cur_work
] = '\0';
186 ksft_print_msg("%s: %s\n", child
->name
, work
);
194 work
[cur_work
] = '\0';
195 ret
= asprintf(&child
->output
, "%s", work
);
197 ksft_exit_fail_msg("Out of memory\n");
203 static void child_output(struct child_data
*child
, uint32_t events
,
208 if (events
& EPOLLIN
) {
210 read_more
= child_output_read(child
);
214 if (events
& EPOLLHUP
) {
215 close(child
->stdout
);
220 if (flush
&& child
->output
) {
221 ksft_print_msg("%s: %s<EOF>\n", child
->name
, child
->output
);
223 child
->output
= NULL
;
227 static void child_tickle(struct child_data
*child
)
229 if (child
->output_seen
&& !child
->exited
)
230 kill(child
->pid
, SIGUSR1
);
233 static void child_stop(struct child_data
*child
)
236 kill(child
->pid
, SIGTERM
);
239 static void child_cleanup(struct child_data
*child
)
245 if (!child
->exited
) {
247 ret
= waitpid(child
->pid
, &status
, 0);
248 if (ret
== -1 && errno
== EINTR
)
252 ksft_print_msg("waitpid(%d) failed: %s (%d)\n",
253 child
->pid
, strerror(errno
),
259 if (WIFEXITED(status
)) {
260 child
->exit_status
= WEXITSTATUS(status
);
261 child
->exited
= true;
264 if (WIFSIGNALED(status
)) {
265 child
->exit_signal
= WTERMSIG(status
);
266 ksft_print_msg("%s: Exited due to signal %d\n",
267 child
->name
, child
->exit_signal
);
269 child
->exited
= true;
271 } while (!child
->exited
);
274 if (!child
->output_seen
) {
275 ksft_print_msg("%s no output seen\n", child
->name
);
279 if (child
->exit_status
!= 0) {
280 ksft_print_msg("%s exited with error code %d\n",
281 child
->name
, child
->exit_status
);
285 ksft_test_result(!fail
, "%s\n", child
->name
);
288 static void handle_child_signal(int sig
, siginfo_t
*info
, void *context
)
293 for (i
= 0; i
< num_children
; i
++) {
294 if (children
[i
].pid
== info
->si_pid
) {
295 children
[i
].exited
= true;
296 children
[i
].exit_status
= info
->si_status
;
303 ksft_print_msg("SIGCHLD for unknown PID %d with status %d\n",
304 info
->si_pid
, info
->si_status
);
307 static void handle_exit_signal(int sig
, siginfo_t
*info
, void *context
)
311 /* If we're already exiting then don't signal again */
315 ksft_print_msg("Got signal, exiting...\n");
320 * This should be redundant, the main loop should clean up
321 * after us, but for safety stop everything we can here.
323 for (i
= 0; i
< num_children
; i
++)
324 child_stop(&children
[i
]);
327 /* Handle any pending output without blocking */
328 static void drain_output(bool flush
)
334 ret
= epoll_wait(epoll_fd
, evs
, tests
, 0);
338 ksft_print_msg("epoll_wait() failed: %s (%d)\n",
339 strerror(errno
), errno
);
342 for (i
= 0; i
< ret
; i
++)
343 child_output(evs
[i
].data
.ptr
, evs
[i
].events
, flush
);
347 static const struct option options
[] = {
348 { "timeout", required_argument
, NULL
, 't' },
352 int main(int argc
, char **argv
)
355 bool all_children_started
= false;
361 while ((c
= getopt_long(argc
, argv
, "t:", options
, NULL
)) != -1) {
364 ret
= sscanf(optarg
, "%d", &timeout
);
366 ksft_exit_fail_msg("Failed to parse timeout %s\n",
370 ksft_exit_fail_msg("Unknown argument\n");
374 cpus
= num_processors();
377 if (getauxval(AT_HWCAP
) & HWCAP_GCS
) {
378 /* One extra thread, trying to trigger migrations */
379 gcs_threads
= cpus
+ 1;
380 tests
+= gcs_threads
;
386 ksft_set_plan(tests
);
388 ksft_print_msg("%d CPUs, %d GCS threads\n",
392 ksft_exit_skip("No tests scheduled\n");
395 ksft_print_msg("Will run for %ds\n", timeout
);
397 ksft_print_msg("Will run until terminated\n");
399 children
= calloc(sizeof(*children
), tests
);
401 ksft_exit_fail_msg("Unable to allocate child data\n");
403 ret
= epoll_create1(EPOLL_CLOEXEC
);
405 ksft_exit_fail_msg("epoll_create1() failed: %s (%d)\n",
406 strerror(errno
), ret
);
409 /* Create a pipe which children will block on before execing */
410 ret
= pipe(startup_pipe
);
412 ksft_exit_fail_msg("Failed to create startup pipe: %s (%d)\n",
413 strerror(errno
), errno
);
415 /* Get signal handers ready before we start any children */
416 memset(&sa
, 0, sizeof(sa
));
417 sa
.sa_sigaction
= handle_exit_signal
;
418 sa
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
419 sigemptyset(&sa
.sa_mask
);
420 ret
= sigaction(SIGINT
, &sa
, NULL
);
422 ksft_print_msg("Failed to install SIGINT handler: %s (%d)\n",
423 strerror(errno
), errno
);
424 ret
= sigaction(SIGTERM
, &sa
, NULL
);
426 ksft_print_msg("Failed to install SIGTERM handler: %s (%d)\n",
427 strerror(errno
), errno
);
428 sa
.sa_sigaction
= handle_child_signal
;
429 ret
= sigaction(SIGCHLD
, &sa
, NULL
);
431 ksft_print_msg("Failed to install SIGCHLD handler: %s (%d)\n",
432 strerror(errno
), errno
);
434 evs
= calloc(tests
, sizeof(*evs
));
436 ksft_exit_fail_msg("Failed to allocated %d epoll events\n",
439 for (i
= 0; i
< gcs_threads
; i
++)
440 start_thread(&children
[i
], i
);
443 * All children started, close the startup pipe and let them
446 close(startup_pipe
[0]);
447 close(startup_pipe
[1]);
451 /* Did we get a signal asking us to exit? */
456 * Timeout is counted in 100ms with no output, the
457 * tests print during startup then are silent when
458 * running so this should ensure they all ran enough
459 * to install the signal handler, this is especially
460 * useful in emulation where we will both be slow and
461 * likely to have a large set of VLs.
463 ret
= epoll_wait(epoll_fd
, evs
, tests
, 100);
467 ksft_exit_fail_msg("epoll_wait() failed: %s (%d)\n",
468 strerror(errno
), errno
);
473 for (i
= 0; i
< ret
; i
++) {
474 child_output(evs
[i
].data
.ptr
, evs
[i
].events
,
480 /* Otherwise epoll_wait() timed out */
483 * If the child processes have not produced output they
484 * aren't actually running the tests yet.
486 if (!all_children_started
) {
489 for (i
= 0; i
< num_children
; i
++)
490 if (children
[i
].output_seen
||
494 if (seen_children
!= num_children
) {
495 ksft_print_msg("Waiting for %d children\n",
496 num_children
- seen_children
);
500 all_children_started
= true;
503 ksft_print_msg("Sending signals, timeout remaining: %d00ms\n",
506 for (i
= 0; i
< num_children
; i
++)
507 child_tickle(&children
[i
]);
509 /* Negative timeout means run indefinitely */
516 ksft_print_msg("Finishing up...\n");
519 for (i
= 0; i
< tests
; i
++)
520 child_stop(&children
[i
]);
524 for (i
= 0; i
< tests
; i
++)
525 child_cleanup(&children
[i
]);