Add memory information
This commit is contained in:
parent
c3648d1cc4
commit
6927a4c5cf
3
Makefile
3
Makefile
|
@ -390,6 +390,7 @@ coreutils: \
|
|||
filesystem/bin/echo \
|
||||
filesystem/bin/env \
|
||||
filesystem/bin/false \
|
||||
filesystem/bin/free \
|
||||
filesystem/bin/head \
|
||||
filesystem/bin/ls \
|
||||
filesystem/bin/mkdir \
|
||||
|
@ -411,7 +412,7 @@ coreutils: \
|
|||
filesystem/bin/true \
|
||||
filesystem/bin/wc \
|
||||
filesystem/bin/yes
|
||||
#filesystem/bin/free filesystem/bin/shutdown filesystem/bin/test
|
||||
#filesystem/bin/shutdown filesystem/bin/test
|
||||
|
||||
extrautils: \
|
||||
filesystem/bin/calc \
|
||||
|
|
|
@ -43,6 +43,48 @@ impl AreaFrameAllocator {
|
|||
}
|
||||
|
||||
impl FrameAllocator for AreaFrameAllocator {
|
||||
fn free_frames(&self) -> usize {
|
||||
let mut count = 0;
|
||||
|
||||
for area in self.areas.clone() {
|
||||
let start_frame = Frame::containing_address(PhysicalAddress::new(area.base_addr as usize));
|
||||
let end_frame = Frame::containing_address(PhysicalAddress::new((area.base_addr + area.length - 1) as usize));
|
||||
for frame in Frame::range_inclusive(start_frame, end_frame) {
|
||||
if frame >= self.kernel_start && frame <= self.kernel_end {
|
||||
// Inside of kernel range
|
||||
} else if frame >= self.next_free_frame {
|
||||
// Frame is in free range
|
||||
count += 1;
|
||||
} else {
|
||||
// Inside of used range
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
|
||||
fn used_frames(&self) -> usize {
|
||||
let mut count = 0;
|
||||
|
||||
for area in self.areas.clone() {
|
||||
let start_frame = Frame::containing_address(PhysicalAddress::new(area.base_addr as usize));
|
||||
let end_frame = Frame::containing_address(PhysicalAddress::new((area.base_addr + area.length - 1) as usize));
|
||||
for frame in Frame::range_inclusive(start_frame, end_frame) {
|
||||
if frame >= self.kernel_start && frame <= self.kernel_end {
|
||||
// Inside of kernel range
|
||||
count += 1
|
||||
} else if frame >= self.next_free_frame {
|
||||
// Frame is in free range
|
||||
} else {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
|
||||
fn allocate_frames(&mut self, count: usize) -> Option<Frame> {
|
||||
if count == 0 {
|
||||
None
|
||||
|
|
|
@ -90,6 +90,24 @@ pub fn deallocate_frame(frame: Frame) {
|
|||
deallocate_frames(frame, 1)
|
||||
}
|
||||
|
||||
/// Get the number of frames available
|
||||
pub fn free_frames() -> usize {
|
||||
if let Some(ref allocator) = *ALLOCATOR.lock() {
|
||||
allocator.free_frames()
|
||||
} else {
|
||||
panic!("frame allocator not initialized");
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the number of frames used
|
||||
pub fn used_frames() -> usize {
|
||||
if let Some(ref allocator) = *ALLOCATOR.lock() {
|
||||
allocator.used_frames()
|
||||
} else {
|
||||
panic!("frame allocator not initialized");
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocate a range of frames
|
||||
pub fn allocate_frames(count: usize) -> Option<Frame> {
|
||||
if let Some(ref mut allocator) = *ALLOCATOR.lock() {
|
||||
|
@ -164,6 +182,8 @@ impl Iterator for FrameIter {
|
|||
}
|
||||
|
||||
pub trait FrameAllocator {
|
||||
fn free_frames(&self) -> usize;
|
||||
fn used_frames(&self) -> usize;
|
||||
fn allocate_frames(&mut self, size: usize) -> Option<Frame>;
|
||||
fn deallocate_frames(&mut self, frame: Frame, size: usize);
|
||||
}
|
||||
|
|
10
kernel/scheme/sys/memory.rs
Normal file
10
kernel/scheme/sys/memory.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
use collections::Vec;
|
||||
|
||||
use arch::memory::{free_frames, used_frames};
|
||||
use syscall::error::Result;
|
||||
|
||||
pub fn resource() -> Result<Vec<u8>> {
|
||||
let string = format!("Memory Used: {} KB\nMemory Free: {} KB\n", used_frames() * 4, free_frames() * 4);
|
||||
|
||||
Ok(string.into_bytes())
|
||||
}
|
|
@ -10,10 +10,10 @@ use syscall::flag::{MODE_DIR, MODE_FILE, SEEK_CUR, SEEK_END, SEEK_SET};
|
|||
use syscall::scheme::Scheme;
|
||||
|
||||
mod context;
|
||||
mod memory;
|
||||
mod scheme;
|
||||
//mod interrupt;
|
||||
//mod log;
|
||||
//mod memory;
|
||||
//mod test;
|
||||
|
||||
struct Handle {
|
||||
|
@ -37,10 +37,10 @@ impl SysScheme {
|
|||
let mut files: BTreeMap<&'static [u8], Box<SysFn>> = BTreeMap::new();
|
||||
|
||||
files.insert(b"context", Box::new(move || context::resource()));
|
||||
files.insert(b"memory", Box::new(move || memory::resource()));
|
||||
files.insert(b"scheme", Box::new(move || scheme::resource()));
|
||||
//files.insert(b"interrupt", Box::new(move || interrupt::resource()));
|
||||
//files.insert(b"log", Box::new(move || log::resource()));
|
||||
//files.insert(b"memory", Box::new(move || memory::resource()));
|
||||
//files.insert(b"test", Box::new(move || test::resource()));
|
||||
|
||||
SysScheme {
|
||||
|
|
Loading…
Reference in a new issue