mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-29 04:13:24 +00:00
Add /proc/meminfo support
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
ff525112d0
commit
12ed40578d
@ -17,6 +17,7 @@ mod offset;
|
||||
pub(crate) mod page;
|
||||
pub(crate) mod page_prop;
|
||||
pub(crate) mod page_table;
|
||||
pub mod stat;
|
||||
pub mod vm_space;
|
||||
|
||||
use alloc::vec::Vec;
|
||||
|
@ -15,7 +15,47 @@ use spin::Once;
|
||||
use super::{cont_pages::ContPages, meta::PageMeta, Page};
|
||||
use crate::{boot::memory_region::MemoryRegionType, mm::PAGE_SIZE, sync::SpinLock};
|
||||
|
||||
pub(in crate::mm) static PAGE_ALLOCATOR: Once<SpinLock<FrameAllocator>> = Once::new();
|
||||
/// FrameAllocator with a counter for allocated memory
|
||||
pub(in crate::mm) struct CountingFrameAllocator {
|
||||
allocator: FrameAllocator,
|
||||
total: usize,
|
||||
allocated: usize,
|
||||
}
|
||||
|
||||
impl CountingFrameAllocator {
|
||||
pub fn new(allocator: FrameAllocator, total: usize) -> Self {
|
||||
CountingFrameAllocator {
|
||||
allocator,
|
||||
total,
|
||||
allocated: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn alloc(&mut self, count: usize) -> Option<usize> {
|
||||
match self.allocator.alloc(count) {
|
||||
Some(value) => {
|
||||
self.allocated += count * PAGE_SIZE;
|
||||
Some(value)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dealloc(&mut self, start_frame: usize, count: usize) {
|
||||
self.allocator.dealloc(start_frame, count);
|
||||
self.allocated -= count * PAGE_SIZE;
|
||||
}
|
||||
|
||||
pub fn mem_total(&self) -> usize {
|
||||
self.total
|
||||
}
|
||||
|
||||
pub fn mem_available(&self) -> usize {
|
||||
self.total - self.allocated
|
||||
}
|
||||
}
|
||||
|
||||
pub(in crate::mm) static PAGE_ALLOCATOR: Once<SpinLock<CountingFrameAllocator>> = Once::new();
|
||||
|
||||
/// Allocate a single page.
|
||||
pub(crate) fn alloc_single<M: PageMeta>() -> Option<Page<M>> {
|
||||
@ -63,6 +103,7 @@ pub(crate) fn alloc<M: PageMeta>(len: usize) -> Option<Vec<Page<M>>> {
|
||||
|
||||
pub(crate) fn init() {
|
||||
let regions = crate::boot::memory_regions();
|
||||
let mut total: usize = 0;
|
||||
let mut allocator = FrameAllocator::<32>::new();
|
||||
for region in regions.iter() {
|
||||
if region.typ() == MemoryRegionType::Usable {
|
||||
@ -75,6 +116,7 @@ pub(crate) fn init() {
|
||||
}
|
||||
// Add global free pages to the frame allocator.
|
||||
allocator.add_frame(start, end);
|
||||
total += (end - start) * PAGE_SIZE;
|
||||
info!(
|
||||
"Found usable region, start:{:x}, end:{:x}",
|
||||
region.base(),
|
||||
@ -82,5 +124,6 @@ pub(crate) fn init() {
|
||||
);
|
||||
}
|
||||
}
|
||||
PAGE_ALLOCATOR.call_once(|| SpinLock::new(allocator));
|
||||
let counting_allocator = CountingFrameAllocator::new(allocator, total);
|
||||
PAGE_ALLOCATOR.call_once(|| SpinLock::new(counting_allocator));
|
||||
}
|
||||
|
21
ostd/src/mm/stat/mod.rs
Normal file
21
ostd/src/mm/stat/mod.rs
Normal file
@ -0,0 +1,21 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
//! APIs for memory statistics.
|
||||
|
||||
use crate::mm::page::allocator::PAGE_ALLOCATOR;
|
||||
|
||||
/// Total memory available for any usages in the system (in bytes).
|
||||
///
|
||||
/// It would be only a slightly less than total physical memory of the system
|
||||
/// in most occasions. For example, bad memory, kernel statically-allocated
|
||||
/// memory or firmware reserved memories do not count.
|
||||
pub fn mem_total() -> usize {
|
||||
PAGE_ALLOCATOR.get().unwrap().lock().mem_total()
|
||||
}
|
||||
|
||||
/// Current readily available memory (in bytes).
|
||||
///
|
||||
/// Such memory can be directly used for allocation without reclaiming.
|
||||
pub fn mem_available() -> usize {
|
||||
PAGE_ALLOCATOR.get().unwrap().lock().mem_available()
|
||||
}
|
Reference in New Issue
Block a user