-
-
Notifications
You must be signed in to change notification settings - Fork 140
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): add slab usage calculation #768
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a03cebf
Add slab free space calculation and add it to freeram of sysinfo
laokengwt 073d782
Modified the code
laokengwt dc6661d
modified the code
laokengwt f99d5be
modified
laokengwt b560d3b
make fmt modified
laokengwt 77f0dea
Merge branch 'DragonOS-Community:master' into slab
laokengwt 3f5dc56
modified
laokengwt 43e47d5
modified
laokengwt ad0ab34
modified
laokengwt 9985d3a
modified
laokengwt 44fa38c
modified
laokengwt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
//! The ZoneAllocator achieves this by having many `SCAllocator` | ||
|
||
use crate::*; | ||
use core::sync::atomic::Ordering; | ||
|
||
/// Creates an instance of a zone, we do this in a macro because we | ||
/// re-use the code in const and non-const functions | ||
|
@@ -24,6 +25,7 @@ macro_rules! new_zone { | |
SCAllocator::new(1 << 10), // 1024 | ||
SCAllocator::new(1 << 11), // 2048 ], | ||
], | ||
total: 0, | ||
} | ||
}; | ||
} | ||
|
@@ -38,6 +40,7 @@ macro_rules! new_zone { | |
/// to provide the underlying `SCAllocator` with more memory in case it runs out. | ||
pub struct ZoneAllocator<'a> { | ||
small_slabs: [SCAllocator<'a, ObjectPage<'a>>; ZoneAllocator::MAX_BASE_SIZE_CLASSES], | ||
total: u64, | ||
} | ||
|
||
impl<'a> Default for ZoneAllocator<'a> { | ||
|
@@ -52,17 +55,13 @@ enum Slab { | |
} | ||
|
||
impl<'a> ZoneAllocator<'a> { | ||
/// Maximum size that allocated within LargeObjectPages (2 MiB). | ||
/// This is also the maximum object size that this allocator can handle. | ||
pub const MAX_ALLOC_SIZE: usize = 1 << 11; | ||
|
||
/// Maximum size which is allocated with ObjectPages (4 KiB pages). | ||
/// | ||
/// e.g. this is 4 KiB - 80 bytes of meta-data. | ||
pub const MAX_BASE_ALLOC_SIZE: usize = 256; | ||
pub const MAX_BASE_ALLOC_SIZE: usize = 1 << 11; | ||
|
||
/// How many allocators of type SCAllocator<ObjectPage> we have. | ||
const MAX_BASE_SIZE_CLASSES: usize = 9; | ||
pub const MAX_BASE_SIZE_CLASSES: usize = 9; | ||
|
||
#[cfg(feature = "unstable")] | ||
pub const fn new() -> ZoneAllocator<'a> { | ||
|
@@ -126,6 +125,43 @@ impl<'a> ZoneAllocator<'a> { | |
} | ||
} | ||
} | ||
|
||
/// 获取scallocator中的还未被分配的空间 | ||
pub fn free_space(&mut self) -> u64 { | ||
// 记录空闲空间 | ||
let mut free = 0; | ||
// scallocator的数量 | ||
let mut count = 0; | ||
|
||
// 遍历所有scallocator | ||
while count < ZoneAllocator::MAX_BASE_SIZE_CLASSES { | ||
// 获取scallocator | ||
let scallocator = &mut self.small_slabs[count]; | ||
// 遍历scallocator中的部分分配的page(partial_page) | ||
for slab_page in scallocator.slabs.iter_mut() { | ||
// 每个page的obj_per_page,即每个page的最大object数,这里用来赋值给obj_count | ||
// 用来统计page中还可以分配多少个object | ||
let mut obj_count = scallocator.obj_per_page; | ||
// 遍历page中的bitfield(用来统计内存分配情况的u64数组) | ||
for (_base_idx, b) in slab_page.bitfield().iter().enumerate() { | ||
let bitval = b.load(Ordering::Relaxed); | ||
let negated = !bitval; | ||
let allocated_count = negated.trailing_zeros() as usize; | ||
// 减去已分配的object数 | ||
obj_count -= allocated_count; | ||
} | ||
// 剩余可分配object数乘上page中规定的每个object的大小,即空闲空间 | ||
free += obj_count * scallocator.size(); | ||
} | ||
count -= 1; | ||
} | ||
free as u64 | ||
} | ||
|
||
pub fn usage(&mut self) -> SlabUsage { | ||
let free_num = self.free_space(); | ||
SlabUsage::new(self.total, free_num) | ||
} | ||
} | ||
|
||
unsafe impl<'a> crate::Allocator<'a> for ZoneAllocator<'a> { | ||
|
@@ -162,9 +198,38 @@ unsafe impl<'a> crate::Allocator<'a> for ZoneAllocator<'a> { | |
match ZoneAllocator::get_slab(layout.size()) { | ||
Slab::Base(idx) => { | ||
self.small_slabs[idx].refill(new_page); | ||
// 每refill一个page就为slab的总空间统计加上4KB | ||
self.total += 4096; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个不要直接写4096,而是应该用OBJECT_PAGE_SIZEE。 |
||
Ok(()) | ||
} | ||
Slab::Unsupported => Err(AllocationError::InvalidLayout), | ||
} | ||
} | ||
} | ||
|
||
/// Slab内存空间使用情况 | ||
pub struct SlabUsage { | ||
// slab总共使用的内存空间 | ||
total: u64, | ||
// slab的空闲空间 | ||
free: u64, | ||
} | ||
|
||
impl SlabUsage { | ||
/// 初始化SlabUsage | ||
pub fn new(total: u64, free: u64) -> Self { | ||
Self { total, free } | ||
} | ||
|
||
pub fn total(&self) -> u64 { | ||
self.total | ||
} | ||
|
||
pub fn used(&self) -> u64 { | ||
self.total - self.free | ||
} | ||
|
||
pub fn free(&self) -> u64 { | ||
self.free | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这个测试过吗?
count -= 1;
怎么能跑的。