Skip to content
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

tests: kernel: spinlock: Added busy wait of 5us #87436

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sswetha18
Copy link
Contributor

This commit adds a busy wait of 5us in the
test_trylock testcase to address timing
inconsistencies observed on high clock speed
platforms.

Faster execution on these platforms causes the
lock to be acquired consistently, preventing
expected failure cases. Introducing a brief
busy wait adjusts timing, allowing failures to
occur as intended and ensuring consistent
test behavior across platforms.

This commit fixes the assertion failure of
zassert_true(trylock_failures > 0) in the
test_trylock testcase by introducing a
busy wait of 5us in the bounce_once function
to address timing inconsistencies observed on
high clock speed platforms.

Faster execution on these platforms causes the
lock to be acquired consistently, preventing
expected failure cases. Introducing a brief
busy wait adjusts timing, allowing failures to
occur as intended and ensuring consistent
test behavior across platforms.

Signed-off-by: S Swetha <s.swetha@intel.com>
@cfriedt
Copy link
Member

cfriedt commented Mar 21, 2025

Could ticket spin locks help here?

@npitre
Copy link
Collaborator

npitre commented Mar 21, 2025 via email

@sswetha18
Copy link
Contributor Author

Ticket spin locks won't change anything. Problem is about the test itself being flawed. It is not condusive to lock contention. If given thread was the last lock owner, it releases the lock right away and waits for 100 us before trying again. If given thread was not the lock owner then it marks itself as the owner, loops 5 times for 1 us and releases the lock, to reacquire it right away just to notice it was the last owner and release the lock to wait for 100 us. So you have 95% chances that at the end of your 100 us wait period, the other thread would have acquired the lock, waited its 5 us with the lock held, then release+acquire+release and be in the middle of its own 100 us waiting period as well with the lock uncontended. In other words, probability of lock contention is only 5%. I'm pretty sure that's not what the actual intent of this test is. The proposed fix is merely creating an imbalance so that one thread would wait 105 us instead of 100 so once every so often both threads would be synchronized and attempt to acquire the lock simultaneously. That's one way to "fix" it and it should probably be done for the non trylock as well so the non trylock version gets its chance at experiencing a few contentions too. But I think it'd be better if the test was reworked so to significantly increase the odds for contention instead.

Acknowledged. I will work on a fundamental rework to improve lock contention consistency

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants