-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Conversation
感谢您的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 (
|
r? @GnoCiYeH |
PR的更改就直接push到原来分支就可以了,不用再开PR哈哈哈哈 |
收到 |
issue: #523 |
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.
有些小问题需要修改。请问有测试用例吗,或者说对比加入slab前后的内存分配情况?
return r; | ||
|
||
// self.local_alloc_zeroed(layout, 0) | ||
if layout.size() > 2048 || !slab_init_state() { |
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.
这里的写法感觉有点冗余,可以精简一下,因为两部分的区别只有一个type不一样而已
); | ||
|
||
return r; | ||
if layout.size() > 2048 || !slab_init_state() { |
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.
这里与上同理
); | ||
|
||
self.local_dealloc(ptr, layout); | ||
if layout.size() > 2048 || !slab_init_state() { |
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.
+1
kernel/src/mm/allocator/slab.rs
Outdated
len: usize, | ||
head: Option<&'static mut FreeBlock>, | ||
// slab初始化状态 | ||
pub(crate) static mut SLABINITSTATE: bool = false; |
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.
这里应该使用AtomicBool
,避免并发问题
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.
这个不Atomic也行的,因为只会从false变为true. 只要他改的时候,加好compiler_fence,防止编译器的神奇优化。就行了。
kernel/src/mm/allocator/slab.rs
Outdated
return self.free_block_list.len(); | ||
} | ||
// 全局slab分配器 | ||
pub(crate) static SLABALLOCATOR: SpinLock<Option<SlabAllocator>> = SpinLock::new(None); |
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.
这个地方感觉SpinLock<Option<SlabAllocator>>
不太好,可以使用Option<SpinLock<SlabAllocator>>
或者Lazy<SpinLock<SlabAllocator>>
。
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.
这个地方感觉
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 |
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.
多次使用到了这个判断条件,可以封装成一个内联函数。
没有测试用例,但是可以通过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) { |
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.
函数名不直观,改为layout_use_buddy(xxxxxx)
更好。
|
||
self.local_dealloc(ptr, layout); | ||
if allocator_select_condition(layout) { | ||
dealloc_debug_log(klog_types::LogSource::Buddy, layout, ptr); |
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.
这里存在问题:
在slab初始化之前,分配出去的对象(他们的layout小于等于2048,比如说32字节吧。),但仍然分配了4K出去。在归还的时候,归还了32到slab(而不是归还4k给buddy)
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.
后面可以跑一些现有的负载程序,比如http server,看一下内存占用优化情况(比如减少了百分之多少),因为这是内核里少有的可以量化的指标了
@dragonosbot review |
还有另一个问题就是,你下回记得不要在master上面开发哈哈哈 |
feat(mm): 添加slab内存分配器 --------- Co-authored-by: longjin <[email protected]>
No description provided.