-
Notifications
You must be signed in to change notification settings - Fork 8.1k
posix: multi process: add support for times() #97357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cfriedt
merged 2 commits into
zephyrproject-rtos:main
from
cfriedt:add-rough-times-support
Oct 14, 2025
+262
−31
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2025 Tenstorrent AI ULC | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZEPHYR_INCLUDE_POSIX_SYS_TIMES_H_ | ||
#define ZEPHYR_INCLUDE_POSIX_SYS_TIMES_H_ | ||
|
||
#include <time.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#if defined(_POSIX_MULTI_PROCESS) || defined(__DOXYGEN__) | ||
|
||
#if !defined(_TMS_DECLARED) && !defined(__tms_defined) | ||
struct tms { | ||
clock_t tms_utime; | ||
clock_t tms_stime; | ||
clock_t tms_cutime; | ||
clock_t tms_cstime; | ||
}; | ||
#define _TMS_DECLARED | ||
#define __tms_defined | ||
#endif | ||
|
||
clock_t times(struct tms *buf); | ||
|
||
#endif /* _POSIX_MULTI_PROCESS */ | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* ZEPHYR_INCLUDE_POSIX_SYS_TIMES_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(posix_multi_process) | ||
|
||
FILE(GLOB app_sources src/*.c) | ||
target_sources(app PRIVATE ${app_sources}) | ||
|
||
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CONFIG_POSIX_API=y | ||
CONFIG_ZTEST=y | ||
|
||
CONFIG_POSIX_AEP_CHOICE_BASE=y | ||
CONFIG_POSIX_MULTI_PROCESS=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright (c) 2025 Tenstorrent AI ULC | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
ZTEST_SUITE(posix_multi_process, NULL, NULL, NULL, NULL, NULL); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) 2025 Tenstorrent AI ULC | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <unistd.h> | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
ZTEST(posix_multi_process, test_getpid) | ||
{ | ||
pid_t pid = getpid(); | ||
|
||
zexpect_true(pid > 0, "invalid pid: %d", pid); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2022, Meta | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <unistd.h> | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
struct waker_work { | ||
k_tid_t tid; | ||
struct k_work_delayable dwork; | ||
}; | ||
static struct waker_work wake_work; | ||
|
||
static void waker_func(struct k_work *work) | ||
{ | ||
struct waker_work *ww; | ||
struct k_work_delayable *dwork = k_work_delayable_from_work(work); | ||
|
||
ww = CONTAINER_OF(dwork, struct waker_work, dwork); | ||
k_wakeup(ww->tid); | ||
} | ||
K_WORK_DELAYABLE_DEFINE(waker, waker_func); | ||
|
||
ZTEST(posix_multi_process, test_sleep) | ||
{ | ||
uint32_t then; | ||
uint32_t now; | ||
/* call sleep(10), wakeup after 1s, expect >= 8s left */ | ||
const uint32_t sleep_min_s = 1; | ||
const uint32_t sleep_max_s = 10; | ||
const uint32_t sleep_rem_s = 8; | ||
|
||
/* sleeping for 0s should return 0 */ | ||
zassert_ok(sleep(0)); | ||
|
||
/* test that sleeping for 1s sleeps for at least 1s */ | ||
then = k_uptime_get(); | ||
zassert_equal(0, sleep(1)); | ||
now = k_uptime_get(); | ||
zassert_true((now - then) >= 1 * MSEC_PER_SEC); | ||
|
||
/* test that sleeping for 2s sleeps for at least 2s */ | ||
then = k_uptime_get(); | ||
zassert_equal(0, sleep(2)); | ||
now = k_uptime_get(); | ||
zassert_true((now - then) >= 2 * MSEC_PER_SEC); | ||
|
||
/* test that sleep reports the remainder */ | ||
wake_work.tid = k_current_get(); | ||
k_work_init_delayable(&wake_work.dwork, waker_func); | ||
zassert_equal(1, k_work_schedule(&wake_work.dwork, K_SECONDS(sleep_min_s))); | ||
zassert_true(sleep(sleep_max_s) >= sleep_rem_s); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2025 Tenstorrent AI ULC | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <sys/times.h> | ||
|
||
#include <zephyr/kernel.h> | ||
#include <zephyr/sys/util.h> | ||
#include <zephyr/ztest.h> | ||
|
||
ZTEST(posix_multi_process, test_times) | ||
{ | ||
static const struct { | ||
const char *name; | ||
size_t offset; | ||
} fields[] = { | ||
{ | ||
.name = "utime", | ||
.offset = offsetof(struct tms, tms_utime), | ||
}, | ||
{ | ||
.name = "stime", | ||
.offset = offsetof(struct tms, tms_stime), | ||
}, | ||
{ | ||
.name = "cutime", | ||
.offset = offsetof(struct tms, tms_cutime), | ||
}, | ||
{ | ||
.name = "cstime", | ||
.offset = offsetof(struct tms, tms_cstime), | ||
}, | ||
}; | ||
struct tms test_tms[2] = {}; | ||
clock_t rtime[2]; | ||
|
||
rtime[0] = times(&test_tms[0]); | ||
k_msleep(MSEC_PER_SEC); | ||
rtime[1] = times(&test_tms[1]); | ||
|
||
zexpect_not_equal(rtime[0], -1); | ||
zexpect_not_equal(rtime[1], -1); | ||
|
||
printk("t0: rtime: %ld utime: %ld stime: %ld cutime: %ld cstime: %ld\n", rtime[0], | ||
test_tms[0].tms_utime, test_tms[0].tms_stime, test_tms[0].tms_cutime, | ||
test_tms[0].tms_cstime); | ||
printk("t1: rtime: %ld utime: %ld stime: %ld cutime: %ld cstime: %ld\n", rtime[1], | ||
test_tms[1].tms_utime, test_tms[1].tms_stime, test_tms[1].tms_cutime, | ||
test_tms[1].tms_cstime); | ||
|
||
ARRAY_FOR_EACH(fields, i) { | ||
const char *name = fields[i].name; | ||
size_t offset = fields[i].offset; | ||
|
||
clock_t t0 = *(clock_t *)((uint8_t *)&test_tms[0] + offset); | ||
clock_t t1 = *(clock_t *)((uint8_t *)&test_tms[1] + offset); | ||
|
||
zexpect_true(t1 >= t0, "time moved backward for tms_%s: t0: %d t1: %d", name, t0, | ||
t1); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
common: | ||
filter: not CONFIG_NATIVE_LIBC | ||
tags: | ||
- posix | ||
- multi_process | ||
# 1 tier0 platform per supported architecture | ||
platform_key: | ||
- arch | ||
- simulation | ||
integration_platforms: | ||
- qemu_riscv64 | ||
min_flash: 64 | ||
min_ram: 32 | ||
tests: | ||
portability.posix.muti_process: {} | ||
portability.posix.muti_process.minimal: | ||
extra_configs: | ||
- CONFIG_MINIMAL_LIBC=y | ||
portability.posix.muti_process.newlib: | ||
filter: TOOLCHAIN_HAS_NEWLIB == 1 | ||
extra_configs: | ||
- CONFIG_NEWLIB_LIBC=y | ||
portability.posix.muti_process.picolibc: | ||
tags: picolibc | ||
filter: CONFIG_PICOLIBC_SUPPORTED | ||
extra_configs: | ||
- CONFIG_PICOLIBC=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.