-
Notifications
You must be signed in to change notification settings - Fork 1.4k
include/nuttx: Introduce seqlock, a sequential count based read-write lock. #17569
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
This commit added linux-style UP_RMB() and UP_WMB(). Signed-off-by: hujun5 <hujun5@xiaomi.com>
03:17:57 /home/work/ssd1/workspace/Vela-Multi-Boards-dev-system-Build@2/nuttx/include/nuttx/seqlock.h: In function 'read_seqbegin': 03:17:57 /home/work/ssd1/workspace/Vela-Multi-Boards-dev-system-Build@2/nuttx/include/nuttx/seqlock.h:107:3: error: 'asm' undeclared (first use in this function) 03:17:57 SMP_RMB(); 03:17:57 ^ 03:17:57 /home/work/ssd1/workspace/Vela-Multi-Boards-dev-system-Build@2/nuttx/include/nuttx/seqlock.h:107:3: note: each undeclared identifier is reported only once for each function it appears in 03:17:57 /home/work/ssd1/workspace/Vela-Multi-Boards-dev-system-Build@2/nuttx/include/nuttx/seqlock.h:107:3: error: expected ';' before 'volatile' 03:17:57 SMP_RMB(); 03:17:57 ^ Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit added seqlock.h. Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit removed unnecessary memory barriers of the seqlock implementation, since they may block the CPU pipeline and lead to performance degradation. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit added predict_xxx to increase performance. Signed-off-by: hujun5 <hujun5@xiaomi.com>
read_seq Loop 20,000,000 times before 233333376 after 183333375 Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit added SMP_WMB to seqlock. Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit provided a better implementation of the seqlock, which ensure the functional correctness and provide better performance. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit improved the seqlock performance on non-SMP and SMP platforms by 31.7% on average (83Mops -> 106Mops, tested on qemu-intel64/KVM). Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit implemented seqlock for non-SMP platforms, which achieves 1.62x performance improvement. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
The optimization reduces one judgment in read operations, improving write performance by 3% and read performance by 10%. Signed-off-by: hujun5 <hujun5@xiaomi.com>
remove warning about less headers. Signed-off-by: zhangyu117 <zhangyu117@xiaomi.com>
acassis
left a comment
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.
@Fix-Point thank you very much for this well detailed Summary and testing, however new feature requires new Documentation, otherwise we ending with more hidden features.
Thank you for the reminder. I will add the document in the next few days. |
72c5f01 to
540f7ac
Compare
a7ff8d4 to
950cd1c
Compare
The documentation has been added. Please check if it is sufficiently detailed. If anything needs to be added, please point it out. |
I support your point. If all these commits were completed by me alone, I would definitely consolidate them into a few commits (as I've done in past PRs). However, these PR also include commits of some of my colleagues, such as @hujun260 (who proposed the first seqcount implementation in this PR), @wangzhi16 and @zhangyuduck consolidating their commits would be disrespectful to their work. So I'm sorry that I can not make this change. |
Okay, let's respect the work achievements of every individual. |
b49389d to
950cd1c
Compare
950cd1c to
92e0336
Compare
Move the header files to decouple compilation dependencies. Signed-off-by: hujun5 <hujun5@xiaomi.com>
Since the adafruit-kb2040:smp do not support the `atomic_init`, we have to remove the function. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit fixed struct name and constant. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit added ducumentation for seqcount. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
92e0336 to
be8b128
Compare
anchao
left a comment
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.
LGTM. Let's merge it — some of the disputed points do not constitute mandatory changes to the project.
|
@acassis please review the documentation |




Summary
This PR introduces an efficient
Seqlock(sequential lock) mechanism suitable for concurrent scenarios with frequent reads and rare writes.Seqlockenables lock-free reading while ensuring data consistency through sequence counting.This is part I of the #17556
Core Features
1. Lock-Free Reading
2. Write Protection
Main Interfaces
Initialization
Read Operations
Reader usage pattern:
Write Operations
Writer usage pattern:
Technical Details
1. Sequence Number Mechanism
2. Memory Barriers
SMP_WMB(): Write memory barrierSMP_RMB(): Read memory barrier3. Atomic Operations
4. Interrupt Protection
Applicable Scenarios
Performance Advantages
This implementation accounts for differences between SMP and uniprocessor environments, ensuring correct operation across various configurations.
Impact
Since the
Seqlockhas not been used yet, it has not impact on current systems now.Testing
Funtional Correctness and Performance Evaluation
Our test case
spinlock_testis as follows: This test case starts different threads to increment a global variable protected by different locks by 1, then checks whether the value is correct after a specified number of operations, and outputs the throughput.The
spinlock_testwill be pushed tonuttx-appsafter #17556 is merged.We run the test case on
intel64:nsh_pci_smp(Intel Core i7 12700/KVM) usingsudo qemu-system-x86_64 -enable-kvm -cpu host,+invtsc,+vmware-cpuid-freq,kvmclock=off -smp 4 -m 2G -kernel nuttx -nographic -serial mon:stdio, the throughput of these locks are: