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

feat(mm): 添加slab内存分配器 #683

Merged
merged 9 commits into from
Apr 15, 2024
Merged

feat(mm): 添加slab内存分配器 #683

merged 9 commits into from
Apr 15, 2024

Conversation

laokengwt
Copy link
Contributor

No description provided.

@dragonosbot
Copy link

感谢您的pull request,欢迎加入!🎉 DragonOS社区很兴奋地期待审核您的更改,您将在接下来的两周内收到 @fslongjin @GnoCiYeH @Chiichen (NB. this repo may be misconfigured) 的回复。💬😊

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-等待审查 and S-等待作者修改) stays updated, invoking these commands when appropriate:

  • @dragonosbot author: 审查结束后,PR的作者应检查评论并采取相应行动
  • @dragonosbot review: 作者已完成修改,将此PR提交给reviewer进行审阅,此PR将再次在审阅者队列中排队

@dragonosbot dragonosbot added the S-等待审查 Status: 等待assignee以及相关方的审查。 label Apr 2, 2024
@fslongjin
Copy link
Member

r? @GnoCiYeH

@fslongjin
Copy link
Member

PR的更改就直接push到原来分支就可以了,不用再开PR哈哈哈哈

@laokengwt
Copy link
Contributor Author

收到

@fslongjin
Copy link
Member

issue: #523

Copy link
Member

@GnoCiYeH GnoCiYeH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有些小问题需要修改。请问有测试用例吗,或者说对比加入slab前后的内存分配情况?

kernel/src/mm/allocator/kernel_allocator.rs Show resolved Hide resolved
return r;

// self.local_alloc_zeroed(layout, 0)
if layout.size() > 2048 || !slab_init_state() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的写法感觉有点冗余,可以精简一下,因为两部分的区别只有一个type不一样而已

);

return r;
if layout.size() > 2048 || !slab_init_state() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里与上同理

);

self.local_dealloc(ptr, layout);
if layout.size() > 2048 || !slab_init_state() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

len: usize,
head: Option<&'static mut FreeBlock>,
// slab初始化状态
pub(crate) static mut SLABINITSTATE: bool = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该使用AtomicBool,避免并发问题

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不Atomic也行的,因为只会从false变为true. 只要他改的时候,加好compiler_fence,防止编译器的神奇优化。就行了。

return self.free_block_list.len();
}
// 全局slab分配器
pub(crate) static SLABALLOCATOR: SpinLock<Option<SlabAllocator>> = SpinLock::new(None);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方感觉SpinLock<Option<SlabAllocator>>不太好,可以使用Option<SpinLock<SlabAllocator>>或者Lazy<SpinLock<SlabAllocator>>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方感觉SpinLock<Option<SlabAllocator>>不太好,可以使用Option<SpinLock<SlabAllocator>>或者Lazy<SpinLock<SlabAllocator>>

首先不要用lazy,因为很慢。
然后就是,全局分配器的话,他这里应该是想实现延迟初始化。只要初始化时机控制的正确,就不需要锁,只要unsafe一下就行。

.map(|x| x.as_mut_ptr())
.unwrap_or(core::ptr::null_mut());
if layout.size() > 2048 || !slab_init_state() {
return self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多次使用到了这个判断条件,可以封装成一个内联函数。

@dragonosbot dragonosbot added S-等待作者修改 Status: 这正在等待作者的一些操作(例如代码更改或更多信息)。 and removed S-等待审查 Status: 等待assignee以及相关方的审查。 labels Apr 4, 2024
@laokengwt
Copy link
Contributor Author

有些小问题需要修改。请问有测试用例吗,或者说对比加入slab前后的内存分配情况?

没有测试用例,但是可以通过make log-monitor在log文件夹中打印出内存分配的log,可以得到其分配方式与原来的buddy分配不同,并且确实是按照slab分配的方式来分配内存的

@@ -62,21 +62,21 @@ impl KernelAllocator {
/// 为内核分配器实现LocalAlloc的trait
impl LocalAlloc for KernelAllocator {
unsafe fn local_alloc(&self, layout: Layout) -> *mut u8 {
if layout.size() > 2048 || !slab_init_state() {
if allocator_select_condition(layout) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数名不直观,改为layout_use_buddy(xxxxxx)更好。


self.local_dealloc(ptr, layout);
if allocator_select_condition(layout) {
dealloc_debug_log(klog_types::LogSource::Buddy, layout, ptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里存在问题:
在slab初始化之前,分配出去的对象(他们的layout小于等于2048,比如说32字节吧。),但仍然分配了4K出去。在归还的时候,归还了32到slab(而不是归还4k给buddy)

@fslongjin fslongjin added the A-mm Area: 内存管理子系统 label Apr 6, 2024
Copy link
Member

@Chiichen Chiichen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后面可以跑一些现有的负载程序,比如http server,看一下内存占用优化情况(比如减少了百分之多少),因为这是内核里少有的可以量化的指标了

@laokengwt
Copy link
Contributor Author

@dragonosbot review

@Chiichen Chiichen changed the title 添加slab内存分配器 feat(mm): 添加slab内存分配器 Apr 14, 2024
@fslongjin
Copy link
Member

还有另一个问题就是,你下回记得不要在master上面开发哈哈哈

@fslongjin fslongjin merged commit ceeb2e9 into DragonOS-Community:master Apr 15, 2024
1 check passed
BrahmaMantra pushed a commit to BrahmaMantra/DragonOS that referenced this pull request Dec 9, 2024
feat(mm): 添加slab内存分配器 
---------

Co-authored-by: longjin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mm Area: 内存管理子系统 S-等待作者修改 Status: 这正在等待作者的一些操作(例如代码更改或更多信息)。
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants