From 87a92d995fe1a13ef96c031cbc71f69584119eec Mon Sep 17 00:00:00 2001 From: ligd Date: Fri, 10 Feb 2023 22:54:55 +0800 Subject: [PATCH] sim: fix nuttx consumes much CPU time Signed-off-by: ligd --- arch/sim/src/sim/sim_oneshot.c | 38 +++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/arch/sim/src/sim/sim_oneshot.c b/arch/sim/src/sim/sim_oneshot.c index 113843fefc..df89fd8ae3 100644 --- a/arch/sim/src/sim/sim_oneshot.c +++ b/arch/sim/src/sim/sim_oneshot.c @@ -122,6 +122,20 @@ static inline void sim_timer_current(struct timespec *ts) ts->tv_nsec = nsec; } +/**************************************************************************** + * Name: sim_reset_alarm + * + * Description: + * Reset the alarm to MAX + * + ****************************************************************************/ + +static inline void sim_reset_alarm(struct timespec *alarm) +{ + alarm->tv_sec = UINT_MAX; + alarm->tv_nsec = NSEC_PER_SEC - 1; +} + /**************************************************************************** * Name: sim_update_hosttimer * @@ -217,16 +231,18 @@ static void sim_process_tick(sq_entry_t *entry) DEBUGASSERT(priv != NULL); + struct timespec current; + + sim_timer_current(¤t); + if (clock_timespec_compare(&priv->alarm, ¤t) > 0) + { + return; /* Alarm doesn't expire yet */ + } + + sim_reset_alarm(&priv->alarm); + if (priv->callback) { - struct timespec current; - - sim_timer_current(¤t); - if (clock_timespec_compare(&priv->alarm, ¤t) > 0) - { - return; /* Alarm doesn't expire yet */ - } - /* Sample and nullify BEFORE executing callback (in case the callback * restarts the oneshot). */ @@ -355,9 +371,7 @@ static int sim_cancel(struct oneshot_lowerhalf_s *lower, sim_timer_current(¤t); clock_timespec_subtract(&priv->alarm, ¤t, ts); - priv->alarm.tv_sec = UINT_MAX; - priv->alarm.tv_nsec = NSEC_PER_SEC - 1; - + sim_reset_alarm(&priv->alarm); sim_update_hosttimer(); priv->callback = NULL; @@ -505,7 +519,6 @@ void up_timer_initialize(void) void sim_timer_update(void) { -#ifdef CONFIG_SIM_WALLTIME_SLEEP static uint64_t until; /* Wait a bit so that the timing is close to the correct rate. */ @@ -513,6 +526,7 @@ void sim_timer_update(void) until += NSEC_PER_TICK; host_sleepuntil(until); +#ifdef CONFIG_SIM_WALLTIME_SLEEP sim_timer_update_internal(); #endif }