[LTP] [PATCH v1 2/2] lib: moves test infrastructure states into a shared context structure
Cyril Hrubis
chrubis@suse.cz
Wed Jun 4 13:55:25 CEST 2025
Hi!
> if (tst_test->needs_checkpoints) {
> - tst_futexes = (char *)results + sizeof(struct results);
> - tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
> + tst_futexes = ipc->futexes;
> +
> + size_t futexes_offset = (char *)ipc->futexes - (char *)ipc;
> + tst_max_futexes = (size - futexes_offset) / sizeof(futex_t);
^
This would be better as:
offsetof(struct ipc, futexes)
> }
>
> - memset(results, 0 , size);
> - results->lib_pid = getpid();
> + /* Set environment variable for exec()'d children */
> + if (tst_test->needs_checkpoints || tst_test->child_needs_reinit) {
> + snprintf(ipc_path, sizeof(ipc_path), IPC_ENV_VAR "=%s", shm_path);
> + putenv(ipc_path);
> + } else {
> + SAFE_UNLINK(shm_path);
> + }
> }
>
> static void cleanup_ipc(void)
> @@ -157,9 +175,11 @@ static void cleanup_ipc(void)
> if (shm_path[0] && !access(shm_path, F_OK) && unlink(shm_path))
> tst_res(TWARN | TERRNO, "unlink(%s) failed", shm_path);
>
> - if (results) {
> - msync((void *)results, size, MS_SYNC);
> - munmap((void *)results, size);
> + if (ipc) {
> + msync((void *)ipc, size, MS_SYNC);
> + munmap((void *)ipc, size);
> + ipc = NULL;
> + context = NULL;
> results = NULL;
> }
> }
> @@ -177,12 +197,22 @@ void tst_reinit(void)
> tst_brk(TBROK, "File %s does not exist!", path);
>
> fd = SAFE_OPEN(path, O_RDWR);
> + ipc = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> + SAFE_CLOSE(fd);
>
> - results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> - tst_futexes = (char *)results + sizeof(struct results);
> - tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t);
> + if (ipc->magic != LTP_MAGIC)
> + tst_brk(TBROK, "Invalid shared memory region (bad magic)");
>
> - SAFE_CLOSE(fd);
> + /* Restore the parent context from IPC region */
> + context = &ipc->context;
> + results = &ipc->results;
> +
> + tst_futexes = ipc->futexes;
> + size_t futexes_offset = (char *)ipc->futexes - (char *)ipc;
> + tst_max_futexes = (size - futexes_offset) / sizeof(futex_t);
^
Here as well.
> + if (context->tdebug)
> + tst_res(TINFO, "tst_reinit(): restored metadata for PID %d", getpid());
> }
>
> extern char **environ;
> @@ -236,19 +266,19 @@ static void update_results(int ttype)
>
> switch (ttype) {
> case TCONF:
> - tst_atomic_inc(&results->skipped);
> + tst_atomic_inc((int *)&results->skipped);
> break;
> case TPASS:
> - tst_atomic_inc(&results->passed);
> + tst_atomic_inc((int *)&results->passed);
> break;
> case TWARN:
> - tst_atomic_inc(&results->warnings);
> + tst_atomic_inc((int *)&results->warnings);
> break;
> case TFAIL:
> - tst_atomic_inc(&results->failed);
> + tst_atomic_inc((int *)&results->failed);
> break;
> case TBROK:
> - tst_atomic_inc(&results->broken);
> + tst_atomic_inc((int *)&results->broken);
> break;
This gets ugly. I guess that it would be better to keep the results as
int unless we change the tst_atomic.h to work with int32_t.
Maybe we can actually drop the assembly fallbacks from tst_atomic.h
since as far as I can tell the __atomic_*() functions were added to
gcc-4.7 and the __sync_*() function were added into gcc-4.1 so unless we
need to support compiler older than 4.1 we can drop the assembly and
easily add support for atomic operations for int32_t.
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list