增加getrusage,并把apic timer的频率调整为系统HZ (#435)

This commit is contained in:
LoGin
2023-11-12 16:36:17 +08:00
committed by GitHub
parent 02e249f30b
commit be8cdf4b8e
10 changed files with 119 additions and 63 deletions

View File

@ -40,7 +40,7 @@ pub static mut FINISHED_BOOTING: AtomicBool = AtomicBool::new(false);
/// Interval: 0.5sec Threshold: 0.0625s
/// 系统节拍率
pub const HZ: u64 = 1000;
pub const HZ: u64 = 250;
/// watchdog检查间隔
pub const WATCHDOG_INTERVAL: u64 = HZ >> 1;
/// 最大能接受的误差大小

View File

@ -1,6 +1,5 @@
use alloc::sync::Arc;
use core::sync::atomic::{compiler_fence, AtomicBool, AtomicI64, Ordering};
use x86_64::align_up;
use crate::{
arch::CurrentIrqArch,
@ -238,9 +237,6 @@ pub fn timekeeping_init() {
/// # 使用当前时钟源增加wall time
pub fn update_wall_time() {
let rsp = unsafe { crate::include::bindings::bindings::get_rsp() } as usize;
let _stack_use = align_up(rsp as u64, 32768) - rsp as u64;
// kdebug!("enter update_wall_time, stack_use = {:}",stack_use);
compiler_fence(Ordering::SeqCst);
let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
@ -267,7 +263,8 @@ pub fn update_wall_time() {
// }
// ================
compiler_fence(Ordering::SeqCst);
// 一分钟同步一次
// !!! todo: 这里是硬编码了HPET的500us中断需要修改
__ADDED_USEC.fetch_add(500, Ordering::SeqCst);
compiler_fence(Ordering::SeqCst);
let mut retry = 10;
@ -279,6 +276,7 @@ pub fn update_wall_time() {
__ADDED_SEC.fetch_add(1, Ordering::SeqCst);
compiler_fence(Ordering::SeqCst);
}
// 一分钟同步一次
loop {
if (usec & !((1 << 26) - 1)) != 0 {
if __ADDED_USEC
@ -289,7 +287,7 @@ pub fn update_wall_time() {
// 同步时间
// 我感觉这里会出问题:多个读者不退出的话,写者就无法写入
// 然后这里会超时,导致在中断返回之后,会不断的进入这个中断,最终爆栈。
let mut timekeeper = timekeeper().0.write();
let mut timekeeper = timekeeper().0.write_irqsave();
timekeeper.xtime.tv_nsec = ktime_get_real_ns();
timekeeper.xtime.tv_sec = 0;
__ADDED_SEC.store(0, Ordering::SeqCst);

View File

@ -7,9 +7,5 @@
#define MAX_TIMEOUT (int64_t)((1ul << 63) - 1)
extern void rs_timer_init();
extern int64_t rs_timer_get_first_expire();
extern uint64_t rs_timer_next_n_ms_jiffies(uint64_t expire_ms);
extern int64_t rs_schedule_timeout(int64_t timeout);
extern uint64_t rs_clock();
extern void rs_jiffies_init();

View File

@ -16,7 +16,7 @@ use crate::{
softirq::{softirq_vectors, SoftirqNumber, SoftirqVec},
InterruptArch,
},
kdebug, kerror, kinfo,
kerror, kinfo,
libs::spinlock::SpinLock,
process::{ProcessControlBlock, ProcessManager},
syscall::SystemError,
@ -253,6 +253,7 @@ pub fn next_n_us_timer_jiffies(expire_us: u64) -> u64 {
pub fn schedule_timeout(mut timeout: i64) -> Result<i64, SystemError> {
// kdebug!("schedule_timeout");
if timeout == MAX_TIMEOUT {
ProcessManager::mark_sleep(true).ok();
sched();
return Ok(MAX_TIMEOUT);
} else if timeout < 0 {
@ -287,7 +288,7 @@ pub fn timer_get_first_expire() -> Result<u64, SystemError> {
// FIXME
// kdebug!("rs_timer_get_first_expire,timer_jif = {:?}", TIMER_JIFFIES);
for _ in 0..10 {
match TIMER_LIST.try_lock() {
match TIMER_LIST.try_lock_irqsave() {
Ok(timer_list) => {
// kdebug!("rs_timer_get_first_expire TIMER_LIST lock successfully");
if timer_list.is_empty() {
@ -305,6 +306,9 @@ pub fn timer_get_first_expire() -> Result<u64, SystemError> {
return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
}
/// 更新系统时间片
///
/// todo: 这里的实现有问题貌似把HPET的500us当成了500个jiffies然后update_wall_time()里面也硬编码了这个500us
pub fn update_timer_jiffies(add_jiffies: u64) -> u64 {
let prev = TIMER_JIFFIES.fetch_add(add_jiffies, Ordering::SeqCst);
compiler_fence(Ordering::SeqCst);
@ -317,50 +321,10 @@ pub fn update_timer_jiffies(add_jiffies: u64) -> u64 {
pub fn clock() -> u64 {
return TIMER_JIFFIES.load(Ordering::SeqCst);
}
// ====== 重构完成后请删掉extern C ======
#[no_mangle]
pub extern "C" fn rs_clock() -> u64 {
clock()
}
// ====== 以下为给C提供的接口 ======
#[no_mangle]
pub extern "C" fn rs_schedule_timeout(timeout: i64) -> i64 {
match schedule_timeout(timeout) {
Ok(v) => {
return v;
}
Err(e) => {
kdebug!("rs_schedule_timeout run failed");
return e.to_posix_errno() as i64;
}
}
}
#[no_mangle]
pub extern "C" fn rs_timer_init() {
timer_init();
}
#[no_mangle]
pub extern "C" fn rs_timer_next_n_ms_jiffies(expire_ms: u64) -> u64 {
return next_n_ms_timer_jiffies(expire_ms);
}
#[no_mangle]
pub extern "C" fn rs_timer_next_n_us_jiffies(expire_us: u64) -> u64 {
return next_n_us_timer_jiffies(expire_us);
}
#[no_mangle]
pub extern "C" fn rs_timer_get_first_expire() -> i64 {
match timer_get_first_expire() {
Ok(v) => return v as i64,
Err(_) => return 0,
}
}
#[no_mangle]
pub extern "C" fn rs_update_timer_jiffies(add_jiffies: u64) -> u64 {
return update_timer_jiffies(add_jiffies);
}