增加了timekeeping模块 (#106)

* 增加了timekeeping模块

* 格式化文档和细节更改

Co-authored-by: longjin <longjin@RinGoTek.cn>
This commit is contained in:
Gou Ngai 2022-12-14 15:13:54 +08:00 committed by GitHub
parent 728aca3089
commit 01876902fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 6 deletions

View File

@ -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>

View File

@ -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
kernel/src/time/mod.rs Normal file
View File

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

View File

@ -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);
}