软中断&定时器重构 (#223)

* 软中断&定时器重构

Co-authored-by: houmkh<houjiaying@DragonOS.org>

* 修改timer的clock()

* 删除debug信息

---------

Co-authored-by: houmkh <1119644616@qq.com>
This commit is contained in:
login
2023-04-02 17:09:33 +08:00
committed by GitHub
parent 6d345b7742
commit bacd691c9e
33 changed files with 896 additions and 672 deletions

View File

@ -1,13 +0,0 @@
all: timer.o sleep.o
CFLAGS += -I .
timer.o: timer.c
$(CC) $(CFLAGS) -c timer.c -o timer.o
sleep.o: sleep.c
$(CC) $(CFLAGS) -c sleep.c -o sleep.o
clean:
echo "Done."

View File

@ -1,4 +1,6 @@
pub mod sleep;
pub mod timekeep;
pub mod timer;
/// 表示时间的结构体
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
@ -14,4 +16,4 @@ impl TimeSpec {
tv_nsec: nsec,
};
}
}
}

View File

@ -1,86 +0,0 @@
#include "sleep.h"
#include <common/errno.h>
#include <time/timer.h>
#include <process/process.h>
#include <sched/sched.h>
#include <mm/slab.h>
#include <common/cpu.h>
#include <common/glib.h>
/**
* @brief nanosleep定时事件到期后唤醒指定的进程
*
* @param pcb 待唤醒的进程的pcb
*/
void nanosleep_handler(void *pcb)
{
process_wakeup((struct process_control_block *)pcb);
}
/**
* @brief 休眠指定时间
*
* @param rqtp 指定休眠的时间
* @param rmtp 返回的剩余休眠时间
* @return int
*/
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
{
if (rqtp->tv_nsec < 0 || rqtp->tv_nsec >= 1000000000)
return -EINVAL;
// 对于小于500us的时间使用spin/rdtsc来进行定时
if (rqtp->tv_nsec < 500000)
{
uint64_t expired_tsc = rdtsc() + (((uint64_t)rqtp->tv_nsec) * Cpu_tsc_freq) / 1000000000;
while (rdtsc() < expired_tsc)
;
if (rmtp != NULL)
{
rmtp->tv_nsec = 0;
rmtp->tv_sec = 0;
}
return 0;
}
// 增加定时任务
struct timer_func_list_t *sleep_task = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
memset(sleep_task, 0, sizeof(struct timer_func_list_t));
timer_func_init_us(sleep_task, &nanosleep_handler, (void *)current_pcb, rqtp->tv_nsec / 1000);
timer_func_add(sleep_task);
current_pcb->state = PROC_INTERRUPTIBLE;
current_pcb->flags |= PF_NEED_SCHED;
sched();
// todo: 增加信号唤醒的功能后设置rmtp
if (rmtp != NULL)
{
rmtp->tv_nsec = 0;
rmtp->tv_sec = 0;
}
return 0;
}
/**
* @brief 睡眠指定时间
*
* @param usec 微秒
* @return int
*/
int usleep(useconds_t usec)
{
struct timespec ts = {
tv_sec : (long int)(usec / 1000000),
tv_nsec : (long int)(usec % 1000000) * 1000UL
};
return nanosleep(&ts, NULL);
}

View File

@ -12,7 +12,7 @@
* @param rmtp 返回的剩余休眠时间
* @return int
*/
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
int rs_nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
/**
* @brief 睡眠指定时间
@ -20,4 +20,4 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
* @param usec 微秒
* @return int
*/
int usleep(useconds_t usec);
int rs_usleep(useconds_t usec);

139
kernel/src/time/sleep.rs Normal file
View File

@ -0,0 +1,139 @@
use core::{arch::x86_64::_rdtsc, hint::spin_loop, ptr::null_mut};
use alloc::{boxed::Box, sync::Arc};
use crate::{
arch::{
asm::current::current_pcb,
interrupt::{cli, sti},
sched::sched,
},
include::bindings::bindings::{timespec, useconds_t, Cpu_tsc_freq},
syscall::SystemError,
};
use super::{
timer::{next_n_us_timer_jiffies, Timer, WakeUpHelper},
TimeSpec,
};
/// @brief 休眠指定时间(单位:纳秒)
///
/// @param sleep_time 指定休眠的时间
///
/// @return Ok(TimeSpec) 剩余休眠时间
///
/// @return Err(SystemError) 错误码
pub fn nano_sleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
if sleep_time.tv_nsec < 0 || sleep_time.tv_nsec >= 1000000000 {
return Err(SystemError::EINVAL);
}
// 对于小于500us的时间使用spin/rdtsc来进行定时
if sleep_time.tv_nsec < 500000 {
let expired_tsc: u64 =
unsafe { _rdtsc() + (sleep_time.tv_nsec as u64 * Cpu_tsc_freq) / 1000000000 };
while unsafe { _rdtsc() } < expired_tsc {
spin_loop()
}
return Ok(TimeSpec {
tv_sec: 0,
tv_nsec: 0,
});
}
// 创建定时器
let handler: Box<WakeUpHelper> = WakeUpHelper::new(current_pcb());
let timer: Arc<Timer> = Timer::new(
handler,
next_n_us_timer_jiffies((sleep_time.tv_nsec / 1000) as u64),
);
cli();
timer.activate();
unsafe {
current_pcb().mark_sleep_interruptible();
}
sti();
sched();
// TODO: 增加信号唤醒的功能后,返回正确的剩余时间
return Ok(TimeSpec {
tv_sec: 0,
tv_nsec: 0,
});
}
/// @brief 休眠指定时间(单位:微秒)
///
/// @param usec 微秒
///
/// @return Ok(TimeSpec) 剩余休眠时间
///
/// @return Err(SystemError) 错误码
pub fn us_sleep(sleep_time: TimeSpec) -> Result<TimeSpec, SystemError> {
match nano_sleep(sleep_time) {
Ok(value) => return Ok(value),
Err(err) => return Err(err),
};
}
//===== 以下为提供给C的接口 =====
/// @brief 休眠指定时间单位纳秒提供给C的接口
///
/// @param sleep_time 指定休眠的时间
///
/// @param rm_time 剩余休眠时间(传出参数)
///
/// @return Ok(i32) 0
///
/// @return Err(SystemError) 错误码
#[no_mangle]
pub extern "C" fn rs_nanosleep(sleep_time: *const timespec, rm_time: *mut timespec) -> i32 {
if sleep_time == null_mut() {
return SystemError::EINVAL.to_posix_errno();
}
let slt_spec = TimeSpec {
tv_sec: unsafe { *sleep_time }.tv_sec,
tv_nsec: unsafe { *sleep_time }.tv_nsec,
};
match nano_sleep(slt_spec) {
Ok(value) => {
if rm_time != null_mut() {
unsafe { *rm_time }.tv_sec = value.tv_sec;
unsafe { *rm_time }.tv_nsec = value.tv_nsec;
}
return 0;
}
Err(err) => {
return err.to_posix_errno();
}
}
}
/// @brief 休眠指定时间单位微秒提供给C的接口
///
/// @param usec 微秒
///
/// @return Ok(i32) 0
///
/// @return Err(SystemError) 错误码
#[no_mangle]
pub extern "C" fn rs_usleep(usec: useconds_t) -> i32 {
let sleep_time = TimeSpec {
tv_sec: (usec / 1000000) as i64,
tv_nsec: ((usec % 1000000) * 1000) as i64,
};
match us_sleep(sleep_time) {
Ok(_) => {
return 0;
}
Err(err) => {
return err.to_posix_errno();
}
};
}

View File

@ -1,178 +0,0 @@
#include "timer.h"
#include <common/kprint.h>
#include <driver/timers/HPET/HPET.h>
#include <exception/softirq.h>
#include <mm/slab.h>
#include <process/process.h>
#include <sched/sched.h>
struct timer_func_list_t timer_func_head;
static spinlock_t sched_lock;
// 定时器循环阈值每次最大执行20个定时器任务
#define TIMER_RUN_CYCLE_THRESHOLD 20
void test_timer()
{
printk_color(ORANGE, BLACK, "(test_timer)");
}
void timer_init()
{
spin_init(&sched_lock);
timer_jiffies = 0;
timer_func_init(&timer_func_head, NULL, NULL, -1UL);
register_softirq(TIMER_SIRQ, &do_timer_softirq, NULL);
struct timer_func_list_t *tmp = (struct timer_func_list_t *)kmalloc(sizeof(struct timer_func_list_t), 0);
timer_func_init(tmp, &test_timer, NULL, 5);
timer_func_add(tmp);
kdebug("timer func initialized.");
}
/**
* @brief 处理时间软中断
*
* @param data
*/
void do_timer_softirq(void *data)
{
// todo: 修改这里以及 softirq 的部分,使得 timer 具有并行性
struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
int cycle_count = 0;
while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies))
{
spin_lock(&sched_lock);
timer_func_del(tmp);
tmp->func(tmp->data);
kfree(tmp);
spin_unlock(&sched_lock);
++cycle_count;
// kdebug("SOLVE SOFT IRQ %d", cycle_count);
// 当前定时器达到阈值
if (cycle_count == TIMER_RUN_CYCLE_THRESHOLD)
break;
tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
}
}
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_ms 定时时长(单位ms)
*/
void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms)
{
list_init(&timer_func->list);
timer_func->func = func;
timer_func->data = data;
timer_func->expire_jiffies = cal_next_n_ms_jiffies(expire_ms); // 设置过期的时间片
}
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_us 定时时长(单位us)
*/
void timer_func_init_us(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_us)
{
list_init(&timer_func->list);
timer_func->func = func;
timer_func->data = data;
timer_func->expire_jiffies = cal_next_n_us_jiffies(expire_us); // 设置过期的时间片
// kdebug("timer_func->expire_jiffies=%ld",cal_next_n_us_jiffies(expire_us));
}
/**
* @brief 将定时功能添加到列表中
*
* @param timer_func 待添加的定时功能
*/
void timer_func_add(struct timer_func_list_t *timer_func)
{
struct timer_func_list_t *tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list);
if (list_empty(&timer_func_head.list) == false)
while (tmp->expire_jiffies < timer_func->expire_jiffies)
tmp = container_of(list_next(&tmp->list), struct timer_func_list_t, list);
list_add(&tmp->list, &(timer_func->list));
}
/**
* @brief 将定时功能从列表中删除
*
* @param timer_func
*/
void timer_func_del(struct timer_func_list_t *timer_func)
{
list_del(&timer_func->list);
}
uint64_t sys_clock(struct pt_regs *regs)
{
return timer_jiffies;
}
uint64_t clock()
{
return timer_jiffies;
}
/**
* @brief 辅助函数传进schedule_timeout函数中, 然后时间一到就唤醒 pcb 指向的进程(即自身)
*
* @param pcb process_control_block
*/
static void __wake_up_helper(void *pcb)
{
BUG_ON(pcb == NULL);
BUG_ON(process_wakeup((struct process_control_block *)pcb) != 0); // 正常唤醒,返回值为0
}
/**
* @brief 睡眠timeout的时间之后唤醒进程/线程
*
* @param timeout
* @return long
*/
long schedule_timeout_ms(long timeout)
{
if (timeout == MAX_TIMEOUT) // 无期停止, 意味着不会调用func
{
sched();
return MAX_TIMEOUT;
}
else if (timeout < 0)
{
BUG_ON(1);
return 0;
}
spin_lock(&sched_lock);
struct timer_func_list_t timer={0};
timer_func_init(&timer, &__wake_up_helper, current_pcb, timeout);
timer_func_add(&timer);
current_pcb->state &= ~(PROC_RUNNING);
spin_unlock(&sched_lock);
sched();
timeout -= timer_jiffies;
return timeout < 0 ? 0 : timeout;
}

View File

@ -3,74 +3,12 @@
#include <common/glib.h>
#include <driver/timers/HPET/HPET.h>
// 定义LONG_MAX为最大超时时间 - 允许负数
#define MAX_TIMEOUT (int64_t)((1ul << 63) - 1)
uint64_t volatile timer_jiffies = 0; // 系统时钟计数
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);
// 计算接下来n毫秒对应的系统时间片
#define cal_next_n_ms_jiffies(expire_ms) (timer_jiffies + 1000 * (expire_ms))
// 计算接下来n微秒对应的系统时间片
#define cal_next_n_us_jiffies(expire_us) (timer_jiffies + (expire_us))
void timer_init();
void do_timer_softirq(void *data);
/**
* @brief 定时功能队列
*
*/
struct timer_func_list_t
{
struct List list;
uint64_t expire_jiffies;
void (*func)(void *data);
void *data;
};
extern struct timer_func_list_t timer_func_head;
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_ms 定时时长(单位ms)
*/
void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms);
/**
* @brief 初始化定时功能
*
* @param timer_func 队列结构体
* @param func 定时功能处理函数
* @param data 传输的数据
* @param expire_us 定时时长(单位us)
*/
void timer_func_init_us(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_us);
/**
* @brief 将定时功能添加到列表中
*
* @param timer_func 待添加的定时功能
*/
void timer_func_add(struct timer_func_list_t *timer_func);
/**
* @brief 将定时功能从列表中删除
*
* @param timer_func
*/
void timer_func_del(struct timer_func_list_t *timer_func);
uint64_t clock();
/**
* @brief 睡眠timeout的时间之后唤醒进程/线程
*
* @param timeout
* @return long
*/
long schedule_timeout_ms(long timeout);
extern uint64_t rs_clock();

303
kernel/src/time/timer.rs Normal file
View File

@ -0,0 +1,303 @@
use core::sync::atomic::{AtomicBool, Ordering};
use alloc::{
boxed::Box,
collections::LinkedList,
sync::{Arc, Weak},
};
use crate::{
arch::{
asm::current::current_pcb,
interrupt::{cli, sti},
sched::sched,
},
exception::softirq::{softirq_vectors, SoftirqNumber, SoftirqVec},
include::bindings::bindings::{process_control_block, process_wakeup, pt_regs, PROC_RUNNING},
kdebug, kerror,
libs::spinlock::SpinLock,
syscall::SystemError,
};
const MAX_TIMEOUT: i64 = i64::MAX;
const TIMER_RUN_CYCLE_THRESHOLD: usize = 20;
static mut TIMER_JIFFIES: u64 = 0;
lazy_static! {
pub static ref TIMER_LIST: SpinLock<LinkedList<Arc<Timer>>> = SpinLock::new(LinkedList::new());
}
/// 定时器要执行的函数的特征
pub trait TimerFunction: Send + Sync {
fn run(&mut self);
}
/// WakeUpHelper函数对应的结构体
pub struct WakeUpHelper {
pcb: &'static mut process_control_block,
}
impl WakeUpHelper {
pub fn new(pcb: &'static mut process_control_block) -> Box<WakeUpHelper> {
return Box::new(WakeUpHelper { pcb });
}
}
impl TimerFunction for WakeUpHelper {
fn run(&mut self) {
unsafe {
process_wakeup(self.pcb);
}
}
}
pub struct Timer(SpinLock<InnerTimer>);
impl Timer {
/// @brief 创建一个定时器单位ms
///
/// @param timer_func 定时器需要执行的函数对应的结构体
///
/// @param expire_jiffies 定时器结束时刻
///
/// @return 定时器结构体
pub fn new(timer_func: Box<dyn TimerFunction>, expire_jiffies: u64) -> Arc<Self> {
let result: Arc<Timer> = Arc::new(Timer(SpinLock::new(InnerTimer {
expire_jiffies,
timer_func,
self_ref: Weak::default(),
})));
result.0.lock().self_ref = Arc::downgrade(&result);
return result;
}
/// @brief 将定时器插入到定时器链表中
pub fn activate(&self) {
let timer_list = &mut TIMER_LIST.lock();
let inner_guard = self.0.lock();
// 链表为空,则直接插入
if timer_list.is_empty() {
// FIXME push_timer
timer_list.push_back(inner_guard.self_ref.upgrade().unwrap());
return;
}
let mut split_pos: usize = 0;
for (pos, elt) in timer_list.iter().enumerate() {
if elt.0.lock().expire_jiffies > inner_guard.expire_jiffies {
split_pos = pos;
break;
}
}
let mut temp_list: LinkedList<Arc<Timer>> = timer_list.split_off(split_pos);
timer_list.push_back(inner_guard.self_ref.upgrade().unwrap());
timer_list.append(&mut temp_list);
}
#[inline]
fn run(&self) {
self.0.lock().timer_func.run();
}
}
/// 定时器类型
pub struct InnerTimer {
/// 定时器结束时刻
pub expire_jiffies: u64,
/// 定时器需要执行的函数结构体
pub timer_func: Box<dyn TimerFunction>,
/// self_ref
self_ref: Weak<Timer>,
}
#[derive(Debug)]
pub struct DoTimerSoftirq {
running: AtomicBool,
}
impl DoTimerSoftirq {
pub fn new() -> Self {
return DoTimerSoftirq {
running: AtomicBool::new(false),
};
}
fn set_run(&self) -> bool {
let x = self
.running
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed);
if x.is_ok() {
return true;
} else {
return false;
}
}
fn clear_run(&self) {
self.running.store(false, Ordering::Release);
}
}
impl SoftirqVec for DoTimerSoftirq {
fn run(&self) {
if self.set_run() == false {
return;
}
// 最多只处理TIMER_RUN_CYCLE_THRESHOLD个计时器
for _ in 0..TIMER_RUN_CYCLE_THRESHOLD {
// kdebug!("DoTimerSoftirq run");
let timer_list = &mut TIMER_LIST.lock();
if timer_list.is_empty() {
break;
}
if timer_list.front().unwrap().0.lock().expire_jiffies
<= unsafe { TIMER_JIFFIES as u64 }
{
let timer = timer_list.pop_front().unwrap();
drop(timer_list);
timer.run();
}
}
self.clear_run();
}
}
/// @brief 初始化timer模块
pub fn timer_init() {
// FIXME 调用register_trap
let do_timer_softirq = Arc::new(DoTimerSoftirq::new());
softirq_vectors()
.register_softirq(SoftirqNumber::TIMER, do_timer_softirq)
.expect("Failed to register timer softirq");
kdebug!("timer initiated successfully");
}
/// 计算接下来n毫秒对应的定时器时间片
pub fn next_n_ms_timer_jiffies(expire_ms: u64) -> u64 {
return unsafe { TIMER_JIFFIES as u64 } + 1000 * (expire_ms);
}
/// 计算接下来n微秒对应的定时器时间片
pub fn next_n_us_timer_jiffies(expire_us: u64) -> u64 {
return unsafe { TIMER_JIFFIES as u64 } + (expire_us);
}
/// @brief 让pcb休眠timeout个jiffies
///
/// @param timeout 需要休眠的时间(单位jiffies)
///
/// @return Ok(i64) 剩余需要休眠的时间(单位jiffies)
///
/// @return Err(SystemError) 错误码
pub fn schedule_timeout(mut timeout: i64) -> Result<i64, SystemError> {
// kdebug!("schedule_timeout");
if timeout == MAX_TIMEOUT {
sched();
return Ok(MAX_TIMEOUT);
} else if timeout < 0 {
kerror!("timeout can't less than 0");
return Err(SystemError::EINVAL);
} else {
// 禁用中断,防止在这段期间发生调度,造成死锁
cli();
timeout += unsafe { TIMER_JIFFIES } as i64;
let timer = Timer::new(WakeUpHelper::new(current_pcb()), timeout as u64);
timer.activate();
current_pcb().state &= (!PROC_RUNNING) as u64;
sti();
sched();
let time_remaining: i64 = timeout - unsafe { TIMER_JIFFIES } as i64;
if time_remaining >= 0 {
// 被提前唤醒,返回剩余时间
return Ok(time_remaining);
} else {
return Ok(0);
}
}
}
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() {
Ok(timer_list) => {
// kdebug!("rs_timer_get_first_expire TIMER_LIST lock successfully");
if timer_list.is_empty() {
// kdebug!("timer_list is empty");
return Ok(0);
} else {
// kdebug!("timer_list not empty");
return Ok(timer_list.front().unwrap().0.lock().expire_jiffies);
}
}
// 加锁失败返回啥??
Err(_) => continue,
}
}
return Err(SystemError::EAGAIN);
}
pub fn update_timer_jiffies(add_jiffies: u64) -> u64 {
unsafe { TIMER_JIFFIES += add_jiffies };
return unsafe { TIMER_JIFFIES };
}
pub fn clock() -> u64 {
return unsafe { TIMER_JIFFIES };
}
// ====== 重构完成后请删掉extern C ======
#[no_mangle]
pub extern "C" fn rs_clock() -> u64 {
clock()
}
#[no_mangle]
pub extern "C" fn sys_clock(_regs: *const pt_regs) -> 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(e) => return e.to_posix_errno() as i64,
}
}
#[no_mangle]
pub extern "C" fn rs_update_timer_jiffies(add_jiffies: u64) -> u64 {
return update_timer_jiffies(add_jiffies);
}