Skip to content

Commit ec7b501

Browse files
committed
arch/sim: Implement host_set_timeratio on Windows sim host.
The POSIX sim host exposes host_set_timeratio for --sim-rt-ratio, but the Windows host implementation did not, causing unresolved symbol errors when linking nuttx.exe in WINDOWS_NATIVE CMake builds. Match POSIX behavior for monotonic host_gettime and host_sleepuntil using the same simulated-to-real time ratio. Signed-off-by: Arjav Patel <arjav1528@gmail.com>
1 parent 513daf1 commit ec7b501

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

arch/sim/src/sim/win/sim_hosttime.c

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,30 @@
2424
* Included Files
2525
****************************************************************************/
2626

27-
#include <stdint.h>
27+
#include <errno.h>
2828
#include <stdbool.h>
29+
#include <stdint.h>
2930
#include <windows.h>
3031

32+
#include "sim_internal.h"
33+
3134
/****************************************************************************
3235
* Pre-processor Definitions
3336
****************************************************************************/
3437

3538
#define POW10_9 (1000000000ull)
3639

40+
/****************************************************************************
41+
* Private Data
42+
****************************************************************************/
43+
44+
/* Ratio of simulated time to real time in percent. 100 means real-time
45+
* (default). Values > 100 speed up simulated time; values < 100 slow it
46+
* down. Overridable at runtime via --sim-rt-ratio=<percent>.
47+
*/
48+
49+
static int g_time_ratio = CONFIG_SIM_WALLTIME_RATIO;
50+
3751
/* Number of 100ns-seconds between the beginning of the Windows epoch
3852
* (Jan. 1, 1601) and the Unix epoch (Jan. 1, 1970)
3953
*/
@@ -89,7 +103,9 @@ uint64_t host_gettime(bool rtc)
89103
start = current;
90104
}
91105

92-
return current - start;
106+
/* Apply time ratio: simulated_time = real_elapsed * ratio / 100 */
107+
108+
return ((current - start) * g_time_ratio) / 100;
93109
}
94110

95111
/****************************************************************************
@@ -125,9 +141,33 @@ void host_sleepuntil(uint64_t nsec)
125141
uint64_t now;
126142

127143
now = host_gettime(false);
128-
if (nsec > now)
144+
if (nsec > now + 1000)
145+
{
146+
/* nsec is in simulated time; convert back to real duration to sleep */
147+
148+
host_sleep((((nsec - now) * 100) / g_time_ratio));
149+
}
150+
}
151+
152+
/****************************************************************************
153+
* Name: host_set_timeratio
154+
*
155+
* Description:
156+
* Set the ratio of simulated time to real time in percent. 100 (default)
157+
* means simulated time advances at the same rate as real time. Values
158+
* greater than 100 speed up simulated time; values less than 100 slow it
159+
* down.
160+
*
161+
* Input Parameters:
162+
* ratio - The new time ratio in percent (must be > 0)
163+
*
164+
****************************************************************************/
165+
166+
void host_set_timeratio(int ratio)
167+
{
168+
if (ratio > 0)
129169
{
130-
host_sleep(nsec - now);
170+
g_time_ratio = ratio;
131171
}
132172
}
133173

0 commit comments

Comments
 (0)