// os/src/syscall/mod.rs use crate::task::count_syscall; /// handle syscall exception with `syscall_id` and other arguments pubfnsyscall(syscall_id: usize, args: [usize; 3]) ->isize { count_syscall(syscall_id); match syscall_id { ... } }
// os/src/task/mod.rs implTaskManager { /// When a syscall is called, we need to increase the syscall_times fncount_syscall(&self, syscall_id: usize) { if syscall_id < MAX_SYSCALL_NUM { letmut inner = self.inner.exclusive_access(); letcurrent = inner.current_task; inner.tasks[current].syscall_times[syscall_id] += 1; } } }
// os/src/task/task.rs /// The task control block (TCB) of a task. #[derive(Copy, Clone)] pubstructTaskControlBlock { /// The task status in it's lifecycle pub task_status: TaskStatus, /// The task context pub task_cx: TaskContext, /// The task syscall times pub syscall_times: [u32; MAX_SYSCALL_NUM], }
/// The task control block (TCB) of a task. #[derive(Copy, Clone)] pubstructTaskControlBlock { /// The task status in it's lifecycle pub task_status: TaskStatus, /// The task context pub task_cx: TaskContext, /// The task syscall times pub syscall_times: [u32; MAX_SYSCALL_NUM], /// The start time of task pub start_time: Option<usize> }
start_time在每个程序运行之前记录一下
implTaskManager { fnrun_first_task(&self) -> ! { ... letnext_task_cx_ptr = &task0.task_cx as *const TaskContext; task0.start_time = Some(get_time_ms()); drop(inner); ... } /// Switch current `Running` task to the task we have found, /// or there is no `Ready` task and we can exit with all applications completed fnrun_next_task(&self) { ifletSome(next) = self.find_next_task() { ... letnext_task_cx_ptr = &inner.tasks[next].task_cx as *const TaskContext; if inner.tasks[next].start_time.is_none() { inner.tasks[next].start_time = Some(get_time_ms()); } drop(inner); ... } } /// Get the task run time fnget_run_time(&self) ->usize { letinner = self.inner.exclusive_access(); letcurrent = inner.current_task; letcurrent_time = get_time_ms(); ifletSome(start_time) = inner.tasks[current].start_time { return current_time - start_time; } else { return0; } } }