Skip to content

Commit

Permalink
ch3
Browse files Browse the repository at this point in the history
  • Loading branch information
weixin_46533160 committed Jan 14, 2025
1 parent c616ee5 commit 3e7d884
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions os/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ mod process;

use fs::*;
use process::*;
use crate::task::increase_sys_call;
/// handle syscall exception with `syscall_id` and other arguments
pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize {
increase_sys_call(syscall_id); // 新增
match syscall_id {
SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]),
SYSCALL_EXIT => sys_exit(args[0] as i32),
Expand Down
14 changes: 10 additions & 4 deletions os/src/syscall/process.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Process management syscalls
use crate::{
config::MAX_SYSCALL_NUM,
task::{exit_current_and_run_next, suspend_current_and_run_next, TaskStatus},
timer::get_time_us,
task::{exit_current_and_run_next, suspend_current_and_run_next, TaskStatus, get_sys_call_times},
timer::{get_time_us, get_time_ms},
};

#[repr(C)]
Expand Down Expand Up @@ -53,6 +53,12 @@ pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize {
/// YOUR JOB: Finish sys_task_info to pass testcases
pub fn sys_task_info(_ti: *mut TaskInfo) -> isize {
trace!("kernel: sys_task_info");
// let task = crate::task::current_task();
-1
unsafe {
*_ti = TaskInfo {
status: TaskStatus::Running,
syscall_times: get_sys_call_times(),
time: get_time_ms(),
}
}
0
}
20 changes: 19 additions & 1 deletion os/src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod switch;
#[allow(clippy::module_inception)]
mod task;

use crate::config::MAX_APP_NUM;
use crate::config::{MAX_APP_NUM, MAX_SYSCALL_NUM};
use crate::loader::{get_num_app, init_app_cx};
use crate::sync::UPSafeCell;
use lazy_static::*;
Expand Down Expand Up @@ -54,6 +54,7 @@ lazy_static! {
let mut tasks = [TaskControlBlock {
task_cx: TaskContext::zero_init(),
task_status: TaskStatus::UnInit,
sys_call_times: [0; MAX_SYSCALL_NUM],
}; MAX_APP_NUM];
for (i, task) in tasks.iter_mut().enumerate() {
task.task_cx = TaskContext::goto_restore(init_app_cx(i));
Expand Down Expand Up @@ -135,6 +136,11 @@ impl TaskManager {
panic!("All applications completed!");
}
}

fn get_sys_call_times(&self) -> [u32; MAX_SYSCALL_NUM] {
let inner: core::cell::RefMut<'_, TaskManagerInner> = self.inner.exclusive_access();
inner.tasks[inner.current_task].sys_call_times.clone()
}
}

/// Run the first task in task list.
Expand Down Expand Up @@ -169,3 +175,15 @@ pub fn exit_current_and_run_next() {
mark_current_exited();
run_next_task();
}

/// Increase the syscall times of the current task
pub fn increase_sys_call(syscall_id: usize) {
let mut inner = TASK_MANAGER.inner.exclusive_access();
let current = inner.current_task;
inner.tasks[current].sys_call_times[syscall_id] += 1;
}

/// Get the number of syscalls called by the current task
pub fn get_sys_call_times() -> [u32; MAX_SYSCALL_NUM] {
TASK_MANAGER.get_sys_call_times()
}
3 changes: 3 additions & 0 deletions os/src/task/task.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Types related to task management
use super::TaskContext;
use crate::config::MAX_SYSCALL_NUM;

/// The task control block (TCB) of a task.
#[derive(Copy, Clone)]
Expand All @@ -9,6 +10,8 @@ pub struct TaskControlBlock {
pub task_status: TaskStatus,
/// The task context
pub task_cx: TaskContext,
/// syscall time count
pub sys_call_times: [u32; MAX_SYSCALL_NUM], // 新增
}

/// The status of a task
Expand Down
Empty file added reports/lab1.md
Empty file.

0 comments on commit 3e7d884

Please sign in to comment.