From da4867d6623093512ff1684fca2e8aca7d07b3f5 Mon Sep 17 00:00:00 2001 From: fslongjin Date: Tue, 12 Jul 2022 13:20:01 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20clock()=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/common/time.h | 9 ++++++++- kernel/syscall/syscall.c | 4 +++- kernel/syscall/syscall_num.h | 1 + kernel/time/timer.c | 18 ++++++++++++++---- kernel/time/timer.h | 3 +++ user/apps/shell/cmd.c | 2 +- user/libs/libc/time.c | 12 +++++++++++- user/libs/libc/time.h | 9 ++++++++- user/libs/libsystem/syscall.h | 1 + 9 files changed, 50 insertions(+), 9 deletions(-) diff --git a/kernel/common/time.h b/kernel/common/time.h index 24572865..d6fdaeb8 100644 --- a/kernel/common/time.h +++ b/kernel/common/time.h @@ -35,4 +35,11 @@ struct timespec * @param rmtp 返回的剩余休眠时间 * @return int */ -extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); \ No newline at end of file +extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); + +/** + * @brief 获取当前的CPU时间 + * + * @return uint64_t timer_jiffies + */ +extern uint64_t clock(); \ No newline at end of file diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index da2d71c8..883b87a9 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -16,6 +16,7 @@ extern void system_call(void); extern void syscall_int(void); +extern uint64_t sys_clock(struct pt_regs* regs); /** @@ -770,5 +771,6 @@ system_call_t system_call_table[MAX_SYSTEM_CALL_NUM] = [16] = sys_exit, [17] = sys_mkdir, [18] = sys_nanosleep, - [19 ... 254] = system_call_not_exists, + [19] = sys_clock, + [20 ... 254] = system_call_not_exists, [255] = sys_ahci_end_req}; diff --git a/kernel/syscall/syscall_num.h b/kernel/syscall/syscall_num.h index a293aa70..633ef8ba 100644 --- a/kernel/syscall/syscall_num.h +++ b/kernel/syscall/syscall_num.h @@ -29,5 +29,6 @@ #define SYS_EXIT 16 // 进程退出 #define SYS_MKDIR 17 // 创建文件夹 #define SYS_NANOSLEEP 18 // 纳秒级休眠 +#define SYS_CLOCK 19 // 获取当前cpu时间 #define SYS_AHCI_END_REQ 255 // AHCI DMA请求结束end_request的系统调用 \ No newline at end of file diff --git a/kernel/time/timer.c b/kernel/time/timer.c index bf82b058..d23d1ae0 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -35,17 +35,17 @@ void do_timer_softirq(void *data) int cycle_count = 0; while ((!list_empty(&timer_func_head.list)) && (tmp->expire_jiffies <= timer_jiffies)) { - + timer_func_del(tmp); tmp->func(tmp->data); kfree(tmp); ++cycle_count; // 当前定时器达到阈值 - if(cycle_count == TIMER_RUN_CYCLE_THRESHOLD) + if (cycle_count == TIMER_RUN_CYCLE_THRESHOLD) break; tmp = container_of(list_next(&timer_func_head.list), struct timer_func_list_t, list); - } + } } /** @@ -105,4 +105,14 @@ void timer_func_add(struct timer_func_list_t *timer_func) void timer_func_del(struct timer_func_list_t *timer_func) { list_del(&timer_func->list); -} \ No newline at end of file +} + +uint64_t sys_clock(struct pt_regs *regs) +{ + return timer_jiffies; +} + +uint64_t clock() +{ + return timer_jiffies; +} diff --git a/kernel/time/timer.h b/kernel/time/timer.h index 08420639..0d3ba390 100644 --- a/kernel/time/timer.h +++ b/kernel/time/timer.h @@ -61,3 +61,6 @@ void timer_func_add(struct timer_func_list_t *timer_func); * @param timer_func */ void timer_func_del(struct timer_func_list_t *timer_func); + + +uint64_t clock(); diff --git a/user/apps/shell/cmd.c b/user/apps/shell/cmd.c index db72508b..12f929c0 100644 --- a/user/apps/shell/cmd.c +++ b/user/apps/shell/cmd.c @@ -432,7 +432,7 @@ int shell_cmd_exec(int argc, char **argv) // printf("parent process wait for pid:[ %d ]\n", pid); waitpid(pid, &retval, 0); - printf("parent process wait pid [ %d ], exit code=%d\n", pid, retval); + // printf("parent process wait pid [ %d ], exit code=%d\n", pid, retval); free(argv); } } diff --git a/user/libs/libc/time.c b/user/libs/libc/time.c index bb0a8273..f3531600 100644 --- a/user/libs/libc/time.c +++ b/user/libs/libc/time.c @@ -27,6 +27,16 @@ int usleep(useconds_t usec) tv_sec : (long int)(usec / 1000000), tv_nsec : (long int)(usec % 1000000) * 1000UL }; - + return nanosleep(&ts, NULL); +} + +/** + * @brief 获取系统当前cpu时间 + * + * @return clock_t + */ +clock_t clock() +{ + return (clock_t)syscall_invoke(SYS_CLOCK, 0,0,0,0,0,0,0,0); } \ No newline at end of file diff --git a/user/libs/libc/time.h b/user/libs/libc/time.h index 1c2ffc5c..e0e17e20 100644 --- a/user/libs/libc/time.h +++ b/user/libs/libc/time.h @@ -35,4 +35,11 @@ struct timespec * @param rmtp 返回的剩余休眠时间 * @return int */ -int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); \ No newline at end of file +int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); + +/** + * @brief 获取系统当前cpu时间 + * + * @return clock_t + */ +clock_t clock(); \ No newline at end of file diff --git a/user/libs/libsystem/syscall.h b/user/libs/libsystem/syscall.h index 6c8d61c0..942f8750 100644 --- a/user/libs/libsystem/syscall.h +++ b/user/libs/libsystem/syscall.h @@ -23,6 +23,7 @@ #define SYS_EXIT 16 // 进程退出 #define SYS_MKDIR 17 // 创建文件夹 #define SYS_NANOSLEEP 18 // 纳秒级休眠 +#define SYS_CLOCK 19 // 获取当前cpu时间 /** * @brief 用户态系统调用函数