Commit cd1f2208 authored by Daniel Vetter's avatar Daniel Vetter

lib/drmtest: extract igt_fork from gem_concurrent_blt

Making sure that we correctly collect the exit codes from all children
is a bit a hassle. So add another magic igt codeblock for easy forking
and joining.

Note that children are (currently at least) not allowed to call igt_skip.
Add an assert to enforce this.

v2:
- Properly propagate the exit code.
- Fix the segfault.
- Add a child int and num_children paramter to the magic codeblock as
  suggested by Chris Wilson.
- Don't dump noise into stdout when a child thread fails, the parent
  will do that for us already.

v3: Now with some docs.

v4: Fixup igt_waitchildren to properly reset state so it can be used
again.
Signed-off-by: Daniel Vetter's avatarDaniel Vetter <daniel.vetter@ffwll.ch>
parent 90a25055
......@@ -652,6 +652,12 @@ static enum {
CONT = 0, SKIP, FAIL
} skip_subtests_henceforth = CONT;
/* fork support state */
pid_t *test_children;
int num_test_children;
int test_children_sz;
bool test_child;
bool __igt_fixture(void)
{
assert(!in_fixture);
......@@ -852,6 +858,8 @@ void igt_skip(const char *f, ...)
va_list args;
skipped_one = true;
assert(!test_child);
if (!igt_only_list_subtests()) {
va_start(args, f);
vprintf(f, args);
......@@ -893,6 +901,10 @@ void igt_fail(int exitcode)
failed_one = true;
/* Silent exit, parent will do the yelling. */
if (test_child)
exit(exitcode);
if (in_subtest)
exit_subtest("FAIL");
else {
......@@ -946,6 +958,59 @@ void igt_exit(void)
exit(77);
}
bool __igt_fork(void)
{
assert(!test_with_subtests || in_subtest);
if (num_test_children >= test_children_sz) {
if (!test_children_sz)
test_children_sz = 4;
else
test_children_sz *= 2;
test_children = realloc(test_children,
sizeof(pid_t)*test_children_sz);
igt_assert(test_children);
}
switch (test_children[num_test_children++] = fork()) {
case -1:
igt_assert(0);
case 0:
test_child = true;
return true;
default:
return false;
}
}
/**
* igt_waitchildren - wait for all children forked with igt_fork
*
* The magic here is that exit codes from children will be correctly propagated
*/
void igt_waitchildren(void)
{
assert(!test_child);
for (int nc = 0; nc < num_test_children; nc++) {
int status = -1;
while (waitpid(test_children[nc], &status, 0) == -1 &&
errno == -EINTR)
;
if (status != 0) {
if (WIFEXITED(status))
igt_fail(WEXITSTATUS(status));
else
abort();
}
}
num_test_children = 0;
}
static bool env_set(const char *env_var, bool default_value)
{
char *val;
......
......@@ -204,6 +204,20 @@ void __igt_fixture_end(void) __attribute__((noreturn));
igt_tokencat(__tmpint,__LINE__) ++, \
__igt_fixture_complete())
bool __igt_fork(void);
/**
* igt_fork - fork parallel test threads with fork()
* @child: name of the int variable with the child number
* @num_children: number of children to fork
*
* Joining all test threads should be done with igt_waitchildren to ensure that
* the exit codes of all children are properly reflected in the test status.
*/
#define igt_fork(child, num_children) \
for (int child = 0; child < (num_children); child++) \
for (; __igt_fork(); exit(0))
void igt_waitchildren(void);
/* check functions which auto-skip tests by calling igt_skip() */
void gem_require_caching(int fd);
static inline void gem_require_ring(int fd, int ring_id)
......
......@@ -267,47 +267,37 @@ static void run_forked(struct access_mode *mode,
do_test do_test_func)
{
const int old_num_buffers = num_buffers;
pid_t children[16];
num_buffers /= ARRAY_SIZE(children);
num_buffers /= 16;
num_buffers += 2;
igt_fork_signal_helper();
for (int nc = 0; nc < ARRAY_SIZE(children); nc++) {
switch ((children[nc] = fork())) {
case -1: igt_assert(0);
default: break;
case 0:
/* recreate process local variables */
bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
drm_intel_bufmgr_gem_enable_reuse(bufmgr);
batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
for (int i = 0; i < num_buffers; i++) {
src[i] = mode->create_bo(bufmgr, i, width, height);
dst[i] = mode->create_bo(bufmgr, ~i, width, height);
}
dummy = mode->create_bo(bufmgr, 0, width, height);
for (int loop = 0; loop < 10; loop++)
do_test_func(mode, src, dst, dummy);
/* as we borrow the fd, we need to reap our bo */
for (int i = 0; i < num_buffers; i++) {
drm_intel_bo_unreference(src[i]);
drm_intel_bo_unreference(dst[i]);
}
drm_intel_bo_unreference(dummy);
intel_batchbuffer_free(batch);
drm_intel_bufmgr_destroy(bufmgr);
exit(0);
igt_fork(child, 16) {
igt_fail(6);
/* recreate process local variables */
bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
drm_intel_bufmgr_gem_enable_reuse(bufmgr);
batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
for (int i = 0; i < num_buffers; i++) {
src[i] = mode->create_bo(bufmgr, i, width, height);
dst[i] = mode->create_bo(bufmgr, ~i, width, height);
}
dummy = mode->create_bo(bufmgr, 0, width, height);
for (int loop = 0; loop < 10; loop++)
do_test_func(mode, src, dst, dummy);
/* as we borrow the fd, we need to reap our bo */
for (int i = 0; i < num_buffers; i++) {
drm_intel_bo_unreference(src[i]);
drm_intel_bo_unreference(dst[i]);
}
drm_intel_bo_unreference(dummy);
intel_batchbuffer_free(batch);
drm_intel_bufmgr_destroy(bufmgr);
}
for (int nc = 0; nc < ARRAY_SIZE(children); nc++) {
int status = -1;
while (waitpid(children[nc], &status, 0) == -1 &&
errno == -EINTR)
;
igt_assert(status == 0);
}
igt_waitchildren();
igt_stop_signal_helper();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment