From 75df70f8e51d01d0a83b8a688f89054ccb654717 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Thu, 7 Nov 2024 15:53:38 -0800 Subject: [PATCH 1/2] Use lf_sleep instead of busywait --- core/threaded/scheduler_static.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/core/threaded/scheduler_static.c b/core/threaded/scheduler_static.c index be50ea1b9..6795c4f4f 100644 --- a/core/threaded/scheduler_static.c +++ b/core/threaded/scheduler_static.c @@ -282,20 +282,17 @@ void execute_inst_DU(lf_scheduler_t* scheduler, size_t worker_number, operand_t instant_t current_time = lf_time_physical(); instant_t wakeup_time = *src + op2.imm; LF_PRINT_DEBUG("DU wakeup time: %lld, base: %lld, offset: %lld", wakeup_time, *src, op2.imm); + // Check if we need to sleep. + instant_t current_time; + _lf_clock_gettime(¤t_time); instant_t wait_interval = wakeup_time - current_time; - // LF_PRINT_DEBUG("*** start_time: %lld, wakeup_time: %lld, op1: %lld, op2: %lld, current_physical_time: %lld\n", start_time, wakeup_time, *src, op2.imm, lf_time_physical()); - LF_PRINT_DEBUG("*** [Line %zu] Worker %zu delaying, current_physical_time: %lld, wakeup_time: %lld, wait_interval: %lld", *pc, worker_number, current_time, wakeup_time, wait_interval); + LF_PRINT_DEBUG( + "*** [Line %zu] Worker %zu delaying, current_physical_time: %lld, wakeup_time: %lld, wait_interval: %lld", *pc, + worker_number, current_time, wakeup_time, wait_interval); if (wait_interval > 0) { - // Approach 1: Only spin when the wait interval is less than SPIN_WAIT_THRESHOLD. - if (wait_interval < SPIN_WAIT_THRESHOLD) { - // Spin wait if the wait interval is less than 1 ms. - while (lf_time_physical() < wakeup_time); - } else { - // Otherwise sleep. - _lf_interruptable_sleep_until_locked(scheduler->env, wakeup_time); - } - // Approach 2: Spin wait. - // while (lf_time_physical() < wakeup_time); + // Recalculate the wakeup time for max accuracy. + _lf_clock_gettime(¤t_time); + lf_sleep(wakeup_time - current_time); } LF_PRINT_DEBUG("*** [Line %zu] Worker %zu done delaying", *pc, worker_number); *pc += 1; // Increment pc. From e8f0d5483fa9a92faffc2bc462dd51e81d1760cb Mon Sep 17 00:00:00 2001 From: erlingrj Date: Thu, 7 Nov 2024 16:05:21 -0800 Subject: [PATCH 2/2] Typo --- core/threaded/scheduler_static.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/threaded/scheduler_static.c b/core/threaded/scheduler_static.c index 6795c4f4f..e8979cd37 100644 --- a/core/threaded/scheduler_static.c +++ b/core/threaded/scheduler_static.c @@ -275,11 +275,7 @@ void execute_inst_DU(lf_scheduler_t* scheduler, size_t worker_number, operand_t int pc_orig = (int) *pc; tracepoint_static_scheduler_DU_starts(worker_number, pc_orig); #endif - // FIXME: There seems to be an overflow problem. - // When wakeup_time overflows but lf_time_physical() doesn't, - // _lf_interruptable_sleep_until_locked() terminates immediately. reg_t *src = op1.reg; - instant_t current_time = lf_time_physical(); instant_t wakeup_time = *src + op2.imm; LF_PRINT_DEBUG("DU wakeup time: %lld, base: %lld, offset: %lld", wakeup_time, *src, op2.imm); // Check if we need to sleep.