-
Notifications
You must be signed in to change notification settings - Fork 3k
RTOS: SysTimer: Fix test timing issues #9416
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
Conversation
@fkjagodzinski, thank you for your changes. |
Please share some background information on this, how will it improve time measurement?
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test is 100% OK with all STM32L0 and STM32L4 targets now!
@deepikabhavnani good question ! Can we have the answer captured in the commit msg (why are we changing this code - both are valid, arenth they) |
@deepikabhavnani, @0xc0170 This patch prevents the board from entering sleep or deepsleep modes while waiting for the semaphore. The difference can be seen with this code (which is just a simplified version of the code from the test). #ifndef MBED_TICKLESS
#error Tickless mode not supported for this target.
#endif
#if !DEVICE_LPTICKER
#error Current SysTimer implementation requires lp ticker support.
#endif
#ifndef MBED_CPU_STATS_ENABLED
#error Please set MBED_CPU_STATS_ENABLED to see the sleep stats
#endif
#include "mbed.h"
extern "C" {
#include "rtx_lib.h"
}
#include "rtos/TARGET_CORTEX/SysTimer.h"
#define TEST_TICKS 42UL
const us_timestamp_t DELAY_US = 1000000ULL * TEST_TICKS / OS_TICK_FREQ;
// Override the handler() -- the SysTick interrupt must not be set as pending by the test code.
class SysTimerTest: public rtos::internal::SysTimer {
private:
Semaphore _sem;
virtual void handler()
{
core_util_critical_section_enter();
_increment_tick();
core_util_critical_section_exit();
_sem.release();
}
public:
SysTimerTest() :
SysTimer(), _sem(0, 1)
{
}
SysTimerTest(const ticker_data_t *data) :
SysTimer(data), _sem(0, 1)
{
}
virtual ~SysTimerTest()
{
}
int32_t sem_wait(uint32_t millisec)
{
return _sem.wait(millisec);
}
};
int main()
{
SysTimerTest st;
st.schedule_tick(TEST_TICKS);
us_timestamp_t t1 = st.get_time();
int32_t sem_slots = 0;
#ifdef SEM_WAIT_FOREVER
#define WAIT_METHOD "osWaitForever"
sem_slots = st.sem_wait(osWaitForever);
#else
#define WAIT_METHOD "busy_loop"
while (sem_slots != 1) {
sem_slots = st.sem_wait(0);
}
#endif
us_timestamp_t t2 = st.get_time();
us_timestamp_t t_idle = mbed_time_idle();
us_timestamp_t t_sleep = mbed_time_sleep();
us_timestamp_t t_deepsleep = mbed_time_deepsleep();
printf("%15s%15s\r\n", "wait_method = ", WAIT_METHOD);
printf("%15s%15llu\r\n", "t_idle = ", t_idle);
printf("%15s%15llu\r\n", "t_sleep = ", t_sleep);
printf("%15s%15llu\r\n", "t_deepsleep = ", t_deepsleep);
printf("%15s%15llu\r\n\r\n", "t2 - t1 = ", t2 - t1);
} mbed compile -t GCC_ARM -m NUCLEO_L476RG -f --sterm -DMBED_CPU_STATS_ENABLED -DSEM_WAIT_FOREVER gives:
mbed compile -t GCC_ARM -m NUCLEO_L476RG -f --sterm -DMBED_CPU_STATS_ENABLED gives:
|
87ea36b
to
93c48e1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explanation @fkjagodzinski. Looks good to me 👍
It will be good to add below line to the commit message
"Looping in Semaphore::wait(0)
prevents the board from entering sleep or deepsleep modes while waiting for the semaphore. By skipping the overhead wakeup time, we get more accurate timings"
Use a busy loop with non-blocking Semaphore::wait(0) calls instead of a single Semaphore::wait(osWaitForever) to improve time measurement accuracy. Looping in Semaphore::wait(0) prevents the board from entering sleep or deepsleep modes while waiting for the semaphore. By skipping the overhead wakeup time, we get more accurate timings.
93c48e1
to
5840281
Compare
I've added a short comment in the code and updated the commit message as suggested. |
CI started |
Test run: SUCCESSSummary: 9 of 9 test jobs passed |
Description
Use a busy loop with non-blocking
Semaphore::wait(0)
calls instead of asingle
Semaphore::wait(osWaitForever)
to improve time measurementaccuracy.
This should resolve #9400.
Pull request type
Reviewers
@c1728p9 @jeromecoutant