Browse Source

增加了timekeeping模块 (#106)

* 增加了timekeeping模块

* 格式化文档和细节更改

Co-authored-by: longjin <[email protected]>
Gou Ngai 2 years ago
parent
commit
01876902fb
4 changed files with 78 additions and 6 deletions
  1. 1 0
      kernel/src/include/bindings/wrapper.h
  2. 8 6
      kernel/src/lib.rs
  3. 1 0
      kernel/src/time/mod.rs
  4. 68 0
      kernel/src/time/timekeep.rs

+ 1 - 0
kernel/src/include/bindings/wrapper.h

@@ -23,6 +23,7 @@
 #include <common/spinlock.h>
 #include <common/unistd.h>
 #include <common/glib.h>
+#include <driver/timers/rtc/rtc.h>
 #include <include/DragonOS/refcount.h>
 #include <include/DragonOS/signal.h>
 #include <mm/mm.h>

+ 8 - 6
kernel/src/lib.rs

@@ -3,7 +3,7 @@
 #![feature(core_intrinsics)] // <2>
 #![feature(alloc_error_handler)]
 #![feature(panic_info_message)]
-#![feature(drain_filter)]// 允许Vec的drain_filter特性
+#![feature(drain_filter)] // 允许Vec的drain_filter特性
 
 #[allow(non_upper_case_globals)]
 #[allow(non_camel_case_types)]
@@ -18,18 +18,22 @@ mod ipc;
 
 #[macro_use]
 mod libs;
+mod driver;
 mod mm;
 mod process;
 mod sched;
 mod smp;
-mod driver;
+mod time;
 
 extern crate alloc;
 
 use mm::allocator::KernelAllocator;
 
 // <3>
-use crate::{include::bindings::bindings::{process_do_exit, BLACK, GREEN}, arch::x86_64::asm::current::current_pcb};
+use crate::{
+    arch::x86_64::asm::current::current_pcb,
+    include::bindings::bindings::{process_do_exit, BLACK, GREEN},
+};
 
 // 声明全局的slab分配器
 #[cfg_attr(not(test), global_allocator)]
@@ -68,9 +72,7 @@ pub fn panic(info: &PanicInfo) -> ! {
     unsafe {
         process_do_exit(u64::MAX);
     };
-    loop {
-        
-    }
+    loop {}
 }
 
 /// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数

+ 1 - 0
kernel/src/time/mod.rs

@@ -0,0 +1 @@
+pub mod timekeep;

+ 68 - 0
kernel/src/time/timekeep.rs

@@ -0,0 +1,68 @@
+#![allow(dead_code)]
+use crate::include::bindings::bindings::{rtc_get_cmos_time, rtc_time_t};
+
+#[allow(non_camel_case_types)]
+pub type ktime_t = i64;
+
+// @brief 将ktime_t类型转换为纳秒类型
+#[inline]
+fn ktime_to_ns(kt: ktime_t) -> i64 {
+    return kt as i64;
+}
+
+/// @brief 从RTC获取当前时间,然后计算时间戳。
+/// 时间戳为从UTC+0 1970-01-01 00:00到当前UTC+0时间,所经过的纳秒数。
+/// 注意,由于当前未引入时区,因此本函数默认时区为UTC+8来计算
+fn ktime_get_real() -> ktime_t {
+    let mut rtc_time: rtc_time_t = rtc_time_t {
+        second: (0),
+        minute: (0),
+        hour: (0),
+        day: (0),
+        month: (0),
+        year: (0),
+    };
+
+    unsafe {
+        //调用rtc.h里面的函数
+        rtc_get_cmos_time(&mut rtc_time);
+    }
+
+    let mut day_count: i32 = 0;
+    for year in 1970..rtc_time.year {
+        let leap: bool = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
+        if leap {
+            day_count += 366;
+        } else {
+            day_count += 365;
+        }
+        //println!("{},{}",year,day_count);
+    }
+    //println!("day count1: {}",day_count);
+    for month in 1..rtc_time.month {
+        match month {
+            1 | 3 | 5 | 7 | 8 | 10 | 12 => day_count += 31,
+            2 => day_count += 28,
+            4 | 6 | 9 | 11 => day_count += 30,
+            _ => day_count += 0,
+        }
+        //println!("{}:{}",month,day_count);
+    }
+
+    day_count += rtc_time.day - 1;
+    //println!("day count2: {}",day_count);
+    //转换成纳秒
+    let timestamp: ktime_t = day_count as i64 * 86_400_000_000_000i64
+        + (rtc_time.hour - 8) as i64 * 3_600_000_000_000i64
+        + rtc_time.minute as i64 * 60_000_000_000i64
+        + rtc_time.second as i64 * 1_000_000_000i64 as ktime_t;
+
+    return timestamp;
+}
+
+/// @brief 暴露给外部使用的接口,返回一个时间戳
+#[inline]
+pub fn ktime_get_real_ns() -> i64 {
+    let kt: ktime_t = ktime_get_real();
+    return ktime_to_ns(kt);
+}