Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions kernel/src/fs/procfs/meminfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

use alloc::format;

use ostd::mm::{PagingConsts, page_size};

use crate::{
fs::{
procfs::template::{FileOps, ProcFileBuilder},
utils::Inode,
},
prelude::*,
vm::num_anon_hugepages,
};

/// Represents the inode at `/proc/meminfo`.
Expand All @@ -37,9 +40,11 @@ impl FileOps for MemInfoFileOps {
let total = total / 1024;
let available = available / 1024;
let free = total - available;
let hugepage_size = page_size::<PagingConsts>(2) / 1024;
let anon_hugepages = num_anon_hugepages() as usize * hugepage_size;
let output = format!(
"MemTotal:\t{} kB\nMemFree:\t{} kB\nMemAvailable:\t{} kB\n",
total, free, available
"MemTotal: {:>8} kB\nMemFree: {:>8} kB\nMemAvailable: {:>8} kB\nAnonHugePages: {:>8} kB\nHugepagesize: {:>8} kB\n",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the output of this look like? Is it:

MemTotal:........++++1234 kB

If I'm wrong, ignore the following:

The linux rule is to construct a table where the labels are left aligned, the values are right aligned and the units are left aligned. So if we want to match it, you should make a list of k/v pairs, find the length of the longest key, and then do something like {key}: {" " * (len_longest_key - len(key))}{value:>8} {unit}

That's kind of a pain to do, so let's at least just change it to "MemTotal: {:>8}" and call it a day.

total, free, available, anon_hugepages, hugepage_size
);
Ok(output.into_bytes())
}
Expand Down
16 changes: 10 additions & 6 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use ostd::{
};
use process::{Process, spawn_init_process};
use sched::SchedPolicy;
use spin::Once;

use crate::{
kcmdline::set_kernel_cmd_line,
Expand Down Expand Up @@ -76,6 +77,7 @@ pub(crate) mod vdso;
pub mod vm;

mod benchmarks;
pub static INITPROC: Once<Arc<Process>> = Once::new();

#[ostd::main]
#[controlled]
Expand Down Expand Up @@ -186,12 +188,14 @@ fn init_thread() {
exit_qemu(QemuExitCode::Success);
}

let initproc = spawn_init_process(
karg.get_initproc_path().unwrap(),
karg.get_initproc_argv().to_vec(),
karg.get_initproc_envp().to_vec(),
)
.expect("Run init process failed.");
let initproc = INITPROC.call_once(|| {
spawn_init_process(
karg.get_initproc_path().unwrap(),
karg.get_initproc_argv().to_vec(),
karg.get_initproc_envp().to_vec(),
)
.expect("Run init process failed.")
});

#[cfg(not(baseline_asterinas))]
if karg
Expand Down
44 changes: 43 additions & 1 deletion kernel/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use ostd::{
};
use vmar::{PageFaultOQueueMessage, RssType};

use crate::process::{PauseProcGuard, Process};
use crate::{
INITPROC, Vec,
process::{PauseProcGuard, Process},
};
#[cfg(not(baseline_asterinas))]
pub mod hugepaged;

Expand Down Expand Up @@ -66,6 +69,45 @@ pub fn mem_total() -> usize {

static PROMOTED_PAGE_SIZE: usize = page_size::<PagingConsts>(2);

pub fn num_anon_hugepages() -> i32 {
let mut count = 0;
let mut procs: Vec<Arc<Process>> = Vec::new();
let Some(initproc) = INITPROC.get() else {
// Handle the case for integration tests when hugepages haven't been allocated
return 0;
};

procs.push(initproc.clone());
while let Some(proc) = procs.pop() {
Comment thread
aneeshdurg marked this conversation as resolved.
proc.current_children()
.iter()
.for_each(|c| procs.push(c.clone()));

let proc_vm = proc.vm();
let proc_vm_guard = proc_vm.lock_root_vmar();
let Some(proc_vmar) = proc_vm_guard.as_ref() else {
continue;
};
let preempt_guard = disable_preempt();
let space_len = proc_vmar.size();
let vm_space = proc_vmar.vm_space();
let Ok(mut cursor) = vm_space.cursor_mut(&preempt_guard, &(0..space_len)) else {
continue;
};
cursor
.do_for_each_submapping(0, space_len, |range, _, _| {
if (range.end - range.start) >= PROMOTED_PAGE_SIZE
&& range.start % PROMOTED_PAGE_SIZE == 0
{
count += 1;
}
Ok(())
})
.ok();
}
count
}

fn promote_hugepages(
proc: &Arc<Process>,
fault_hint: Option<PageFaultOQueueMessage>,
Expand Down
Loading