Skip to content

arch/sim: add simulated time ratio support#18801

Merged
xiaoxiang781216 merged 2 commits intoapache:masterfrom
LingaoM:add_sim_walltime_ratio
Apr 25, 2026
Merged

arch/sim: add simulated time ratio support#18801
xiaoxiang781216 merged 2 commits intoapache:masterfrom
LingaoM:add_sim_walltime_ratio

Conversation

@LingaoM
Copy link
Copy Markdown
Contributor

@LingaoM LingaoM commented Apr 24, 2026

Add CONFIG_SIM_WALLTIME_RATIO Kconfig option and --sim-rt-ratio=
command-line argument to control the ratio of simulated time to
real time in percent. 100 means real-time (default), 200 means
simulated time advances twice as fast, 50 means half speed.

The implementation applies the ratio in host_gettime(), host_sleepuntil() and host_settimer() so both SIM_WALLTIME_SLEEP and SIM_WALLTIME_SIGNAL modes are supported.

Tested on sim:nsh with the following sleep test:

$ echo -e "sleep 2\nexit" | time ./nuttx
real    0m2.0xxs

$ echo -e "sleep 2\nexit" | time ./nuttx --sim-rt-ratio=200
real    0m1.0xxs

$ echo -e "sleep 2\nexit" | time ./nuttx --sim-rt-ratio=50
real    0m4.0xxs

@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Size: S The size of the change in this PR is small labels Apr 24, 2026
Comment thread arch/sim/src/sim/sim_internal.h Outdated
Comment thread arch/sim/Kconfig Outdated
@LingaoM LingaoM force-pushed the add_sim_walltime_ratio branch 3 times, most recently from fe1ac86 to d087837 Compare April 25, 2026 04:36
Add CONFIG_SIM_WALLTIME_RATIO Kconfig option and --sim-rt-ratio=
command-line argument to control the ratio of simulated time to
real time in percent. 100 means real-time (default), 200 means
simulated time advances twice as fast, 50 means half speed.

The implementation applies the ratio in host_gettime(), host_sleepuntil()
and host_settimer() so both SIM_WALLTIME_SLEEP and SIM_WALLTIME_SIGNAL
modes are supported.

This is inspired by the --rt-ratio feature in Zephyr's native_sim board.

Tested on sim:nsh with the following sleep test:

  $ echo -e "sleep 2\nexit" | time ./nuttx
  real    0m2.0xxs

  $ echo -e "sleep 2\nexit" | time ./nuttx --sim-rt-ratio=200
  real    0m1.0xxs

  $ echo -e "sleep 2\nexit" | time ./nuttx --sim-rt-ratio=50
  real    0m4.0xxs

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
@LingaoM LingaoM force-pushed the add_sim_walltime_ratio branch from d087837 to 0da9c1e Compare April 25, 2026 04:43
dispatch_walltime() expects a delta in nanoseconds from now, but the
previous code subtracted CLOCK_REALTIME from a CLOCK_MONOTONIC absolute
timestamp, which have different epochs and produce a meaningless result.

Fix by subtracting the current CLOCK_MONOTONIC absolute time to get the
correct remaining duration.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
@LingaoM LingaoM force-pushed the add_sim_walltime_ratio branch from 0da9c1e to 228ee23 Compare April 25, 2026 04:53
@LingaoM LingaoM requested a review from xiaoxiang781216 April 25, 2026 04:55
@xiaoxiang781216 xiaoxiang781216 merged commit 62629d5 into apache:master Apr 25, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: simulator Issues related to the SIMulator Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants