1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-21 23:56:18 +04:00

Automatically enlarge heap when kernel heap is about to run out

This commit is contained in:
Jiajie Chen 2019-05-01 18:33:55 +08:00
parent 54de0d8ae7
commit 8651f09b31
9 changed files with 50 additions and 23 deletions

View File

@ -41,11 +41,11 @@ pub trait FrameAllocator: Debug + Clone + Send + Sync + 'static {
mod byframe;
mod delay;
mod linear;
mod file;
mod linear;
//mod swap;
pub use self::byframe::ByFrame;
pub use self::delay::Delay;
pub use self::linear::Linear;
pub use self::file::{File, Read};
pub use self::linear::Linear;

6
kernel/Cargo.lock generated
View File

@ -102,7 +102,7 @@ dependencies = [
[[package]]
name = "buddy_system_allocator"
version = "0.1.2"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -367,7 +367,7 @@ dependencies = [
"bitmap-allocator 0.1.0 (git+https://github.com/rcore-os/bitmap-allocator)",
"bitvec 0.11.0 (git+https://github.com/myrrlyn/bitvec.git)",
"bootloader 0.4.0 (git+https://github.com/rcore-os/bootloader)",
"buddy_system_allocator 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"buddy_system_allocator 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"cc 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
"console-traits 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"device_tree 1.0.3 (git+https://github.com/rcore-os/device_tree-rs)",
@ -661,7 +661,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum bitvec 0.11.0 (git+https://github.com/myrrlyn/bitvec.git)" = "<none>"
"checksum bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cfadef5c4e2c2e64067b9ecc061179f12ac7ec65ba613b1f60f3972bbada1f5b"
"checksum bootloader 0.4.0 (git+https://github.com/rcore-os/bootloader)" = "<none>"
"checksum buddy_system_allocator 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2ed828f1e227d6e32b998d6375b67fd63ac5389d50b23f258ce151d22b6cc595"
"checksum buddy_system_allocator 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "94a6c0143a07fea0db2f4b43cb9540dcc7c17af8a7beafdf2184e5e4e35aae91"
"checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb"
"checksum cc 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)" = "c9ce8bb087aacff865633f0bd5aeaed910fe2fe55b55f4739527f2e023a2e53d"
"checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4"

View File

@ -57,7 +57,7 @@ volatile = "0.2"
heapless = "0.4"
bitvec = { git = "https://github.com/myrrlyn/bitvec.git", default-features = false, features = ["alloc"] }
console-traits = "0.3"
buddy_system_allocator = "0.1"
buddy_system_allocator = "0.3"
pci = { git = "https://github.com/rcore-os/pci-rs" }
device_tree = { git = "https://github.com/rcore-os/device_tree-rs" }
isomorphic_drivers = { git = "https://github.com/rcore-os/isomorphic_drivers" }

View File

@ -9,15 +9,15 @@ use crate::arch::driver::ide;
pub use self::file::*;
pub use self::file_like::*;
pub use self::pipe::Pipe;
pub use self::stdio::{STDIN, STDOUT};
pub use self::pseudo::*;
pub use self::stdio::{STDIN, STDOUT};
mod device;
mod file;
mod file_like;
mod pipe;
mod stdio;
mod pseudo;
mod stdio;
/// Hard link user programs
#[cfg(feature = "link_user")]

View File

@ -14,7 +14,7 @@ impl Pseudo {
pub fn new(s: &str, type_: FileType) -> Self {
Pseudo {
content: Vec::from(s.as_bytes()),
type_
type_,
}
}
}

View File

@ -18,7 +18,7 @@ extern crate log;
extern crate lazy_static;
pub use crate::process::{new_kernel_context, processor};
use buddy_system_allocator::LockedHeap;
use buddy_system_allocator::LockedHeapWithRescue;
use rcore_thread::std_thread as thread;
#[macro_use] // print!
@ -65,4 +65,5 @@ pub fn kmain() -> ! {
///
/// It should be defined in memory mod, but in Rust `global_allocator` must be in root mod.
#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
static HEAP_ALLOCATOR: LockedHeapWithRescue =
LockedHeapWithRescue::new(crate::memory::enlarge_heap);

View File

@ -14,14 +14,17 @@
use super::HEAP_ALLOCATOR;
pub use crate::arch::paging::*;
use crate::consts::MEMORY_OFFSET;
use crate::consts::{KERNEL_OFFSET, MEMORY_OFFSET};
use crate::process::process_unsafe;
use crate::sync::SpinNoIrqLock;
use alloc::boxed::Box;
use alloc::vec::Vec;
use bitmap_allocator::BitAlloc;
use buddy_system_allocator::LockedHeap;
use buddy_system_allocator::{Heap, LockedHeap};
use lazy_static::*;
use log::*;
pub use rcore_memory::memory_set::{handler::*, MemoryArea, MemoryAttr};
use rcore_memory::paging::PageTable;
use rcore_memory::*;
pub type MemorySet = rcore_memory::memory_set::MemorySet<InactivePageTable0>;
@ -145,5 +148,32 @@ pub fn init_heap() {
info!("heap init end");
}
/// Allocator for the rest memory space on NO-MMU case.
pub static MEMORY_ALLOCATOR: LockedHeap = LockedHeap::empty();
pub fn enlarge_heap(heap: &mut Heap) {
let mut page_table = active_table();
let mut addrs = [(0, 0); 32];
let mut addr_len = 0;
let va_offset = KERNEL_OFFSET + 0xe0000000;
for i in 0..16384 {
let page = alloc_frame().unwrap();
let va = KERNEL_OFFSET + 0xe0000000 + page;
if addr_len > 0 {
let (ref mut addr, ref mut len) = addrs[addr_len-1];
if *addr - PAGE_SIZE == va {
*len += PAGE_SIZE;
*addr -= PAGE_SIZE;
continue;
}
}
addrs[addr_len] = (va, PAGE_SIZE);
addr_len += 1;
}
for (addr, len) in addrs[..addr_len].into_iter() {
for va in (*addr..(*addr + *len)).step_by(PAGE_SIZE) {
page_table.map(va, va - va_offset).update();
}
info!("Adding {:#X} {:#X} to heap", addr, len);
unsafe {
heap.init(*addr, *len);
}
}
}

View File

@ -31,8 +31,8 @@ mod net;
mod proc;
mod time;
use spin::Mutex;
use alloc::collections::BTreeMap;
use spin::Mutex;
#[cfg(feature = "profile")]
lazy_static! {
@ -45,9 +45,7 @@ lazy_static! {
#[deny(unreachable_patterns)]
pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
#[cfg(feature = "profile")]
let begin_time = unsafe {
core::arch::x86_64::_rdtsc()
};
let begin_time = unsafe { core::arch::x86_64::_rdtsc() };
let cid = cpu::id();
let pid = process().pid.clone();
let tid = processor().tid();
@ -268,9 +266,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize {
}
#[cfg(feature = "profile")]
{
let end_time = unsafe {
core::arch::x86_64::_rdtsc()
};
let end_time = unsafe { core::arch::x86_64::_rdtsc() };
*SYSCALL_TIMING.lock().entry(id).or_insert(0) += end_time - begin_time;
if end_time % 1000 == 0 {
let timing = SYSCALL_TIMING.lock();

2
user

@ -1 +1 @@
Subproject commit 05f0efd3fda084109e4b6da8ff30ecb1557a267f
Subproject commit aec8667eb056cb564e301d6c0937da1e4500a4d3