-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Differences in cycles between k_busy_wait and k_sleep #23600
Comments
It maybe connects to #21762. |
On which version of Zephyr did you perform the tests? |
We are using version 2.2.99.0, It might be related to the issue you mentioned in your thread, but we have no actual idea whats causing this so can't say for sure |
Later on it was fixed. But as it seems now, it re-appeared. |
ok, do you know which version it was fixed in? |
ah, ok i must have missed that when i read your thread When we tried to run that version of Zephyr the problem seem to be the same, with the sleep still taking more cycles than the busy wait. |
@andyross can you please provide your input here |
It's just a precision issue. Timer subsystem calls like So by asking You can address precision issues like this by increasing the tick rate in CONFIG_SYS_CLOCK_TICKS_PER_SEC (most hardware platforms these days are using 10 kHz ticks by default -- 100 Hz is needed only by emulated systems that interact with a system clock), but you can't fix the underlying behavior, which is as-designed. Timer handling in the kernel happens in ticks, and by definition it isn't going to be any more accurate. And of course k_busy_wait() isn't a timer call and isn't subject to those restrictions. It spins (!!) on the CPU until the cycle counter increments by the amount requested, so it can get arbitrarily high precision limited only by the precision of the hardware counter and the CPU time required for the function call. That's why it exists, after all. [1] There are a few spots in other subsystems (not blocking calls like sleep!) where code knows it is registering a timeout underneath a timer ISR and thus can be exact. See k_timer for an example, also the announce_remaining handling inside z_clock_announce() has some similar semantics. |
Thank you for the answers |
closing since this is now answered |
Hi,
We are trying to create two tasks that should preempt each other whenever they are close to their deadline and sleep to the deadline when they have finished their execution. But we have noticed that both k_busy_wait and k_sleep takes different amount of cycles, even though they are given the same amount of time as an argument. This seems odd to us, because shouldn't the task sleep for the specified amount of time and that the cycle counter still advances forward the same amount as if the task was busy doing work? This is run on the UP squared board.
CPU specs:
https://ark.intel.com/content/www/us/en/ark/products/95598/intel-celeron-processor-n3350-2m-cache-up-to-2-4-ghz.html
Ex. Both functions should run for 10ms (1 tick)
From that test we can see that the busy_wait runs for the specified amount of time in cycles, but the sleep function actually goes beyond 10ms.
We have ran the timer_monotonic test several times with different parameters but we always get a delta of 68%, no matter what we set the HW cycles per second or the number of ticks per second. We also ran the Schedule_api test and when we used the default settings for the UPsqaured board, we failed the test_slice_reset test, but when we changed the number of ticks per second to 100 we passed the test.
The text was updated successfully, but these errors were encountered: