Browse Source

:new: clock()函数

fslongjin 2 years ago
parent
commit
da4867d662

+ 8 - 1
kernel/common/time.h

@@ -35,4 +35,11 @@ struct timespec
  * @param rmtp 返回的剩余休眠时间
  * @return int 
  */
-extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
+extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
+
+/**
+ * @brief 获取当前的CPU时间
+ * 
+ * @return uint64_t timer_jiffies
+ */
+extern uint64_t clock();

+ 3 - 1
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};

+ 1 - 0
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的系统调用

+ 14 - 4
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);
-}
+}
+
+uint64_t sys_clock(struct pt_regs *regs)
+{
+    return timer_jiffies;
+}
+
+uint64_t clock()
+{
+    return timer_jiffies;
+}

+ 3 - 0
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();

+ 1 - 1
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);
     }
 }

+ 11 - 1
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);
 }

+ 8 - 1
user/libs/libc/time.h

@@ -35,4 +35,11 @@ struct timespec
  * @param rmtp 返回的剩余休眠时间
  * @return int 
  */
-int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
+int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
+
+/**
+ * @brief 获取系统当前cpu时间
+ * 
+ * @return clock_t 
+ */
+clock_t clock();

+ 1 - 0
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 用户态系统调用函数