Skip to content

增加了timekeeping模块 #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kernel/src/include/bindings/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
14 changes: 8 additions & 6 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down Expand Up @@ -68,9 +72,7 @@ pub fn panic(info: &PanicInfo) -> ! {
unsafe {
process_do_exit(u64::MAX);
};
loop {

}
loop {}
}

/// 该函数用作测试,在process.c的initial_kernel_thread()中调用了此函数
Expand Down
1 change: 1 addition & 0 deletions kernel/src/time/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod timekeep;
68 changes: 68 additions & 0 deletions kernel/src/time/timekeep.rs
Original file line number Diff line number Diff line change
@@ -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);
}