[go: nahoru, domu]

Skip to content

Commit

Permalink
calculate time elapsed during boot
Browse files Browse the repository at this point in the history
  • Loading branch information
PauloMigAlmeida committed Dec 17, 2021
1 parent b049bf4 commit 40ec4fa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
5 changes: 3 additions & 2 deletions include/kernel/time/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#include "kernel/arch/cmos.h"

void rtc_init(void);
cmos_clock_t rtc_startup_time(void);
void rtc_init_curr_time(void);
uint64_t rtc_covert_to_unixtime(cmos_clock_t now);

extern uint64_t rtc_curr_time;
extern uint64_t rtc_curr_unixtime;
extern uint64_t rtc_startup_unixtime;

#endif /* INCLUDE_KERNEL_TIME_RTC_H_ */
4 changes: 2 additions & 2 deletions src/kernel/arch/pit.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ void pit_enable(void) {
void pit_timer_handle_irq(void) {
++jiffies;
if (jiffies % 1000 == 0)
printk_info("pit_timer_handle_irq: %llu", jiffies);
printk_fine("pit_timer_handle_irq: %llu", jiffies);

/* increment current time counter */
++rtc_curr_time;
++rtc_curr_unixtime;

/* give scheduler a change to change its mind */
scheduler_tick();
Expand Down
3 changes: 3 additions & 0 deletions src/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ void kmain(void) {
/* enable syscalls */
syscall_init();

/* how long did the boot take until here? */
printk_info("System boot completed in %.16llu ms", rtc_curr_unixtime - rtc_startup_unixtime);

/* initialise scheduler */
task_struct_t *init_proc = create_process(0x1C000);
scheduler_init(init_proc);
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/syscall/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "kernel/time/rtc.h"

time_t sys_time(void) {
time_t ret = rtc_curr_time;
time_t ret = rtc_curr_unixtime;

/* convert it to UNIX epoch time (seconds) */
return ret / 1000;
Expand Down
56 changes: 30 additions & 26 deletions src/kernel/time/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ static long month[12] = {
RTC_MILLISEC_IN_DAY * (31L + 28L + 31L + 30L + 31L + 30L + 31L + 31L + 30L + 31L + 30L)
};

static cmos_clock_t startup_time;
uint64_t rtc_curr_time;
uint64_t rtc_startup_unixtime;
uint64_t rtc_curr_unixtime;

void rtc_init(void) {
/* meant to be called when inturrupts are enabled */
startup_time = cmos_read_rtc();
}

cmos_clock_t rtc_startup_time(void) {
return startup_time;
cmos_clock_t now = cmos_read_rtc();
rtc_startup_unixtime = rtc_covert_to_unixtime(now);
}

static bool is_leap_year(int year) {
Expand All @@ -56,42 +53,49 @@ static bool is_leap_year(int year) {
return ret;
}

void rtc_init_curr_time(void) {
/* pigback on already calculated stuff */
rtc_curr_time = CMOS_ELAPSED_MSECS_UNTIL_20_CENTURY;

/* critical path - start*/
disable_interrupts();

/* read from the RTC */
cmos_clock_t now = cmos_read_rtc();
uint64_t rtc_covert_to_unixtime(cmos_clock_t now) {

/* critical path - end*/
enable_interrupts();
/* pigback on already calculated stuff */
uint64_t ret = CMOS_ELAPSED_MSECS_UNTIL_20_CENTURY;

/* calculate the diff - previous years */
for (uint8_t i = 0; i < now.year; i++) {
if (is_leap_year(2000 + i))
rtc_curr_time += RTC_MILLISEC_LEAP_YEAR;
ret += RTC_MILLISEC_LEAP_YEAR;
else
rtc_curr_time += RTC_MILLISEC_YEAR;
ret += RTC_MILLISEC_YEAR;
}

/* calculate the diff - current year - month */
rtc_curr_time += month[now.month - 1];
ret += month[now.month - 1];

/* adjust leap year calc if needed */
if (now.month > 2 && is_leap_year(2000 + now.year))
rtc_curr_time += RTC_MILLISEC_IN_DAY;
ret += RTC_MILLISEC_IN_DAY;

/* days */
rtc_curr_time += RTC_MILLISEC_IN_DAY * (now.day - 1);
ret += RTC_MILLISEC_IN_DAY * (now.day - 1);
/* hours */
rtc_curr_time += RTC_MILLISEC_IN_HOUR * now.hour;
ret += RTC_MILLISEC_IN_HOUR * now.hour;
/* minutes */
rtc_curr_time += RTC_MILLISEC_IN_MIN * now.minute;
ret += RTC_MILLISEC_IN_MIN * now.minute;
/* seconds */
rtc_curr_time += RTC_MILLISEC_IN_SEC * now.second;
ret += RTC_MILLISEC_IN_SEC * now.second;

return ret;

}

void rtc_init_curr_time(void) {
/* critical path - start*/
disable_interrupts();

/* read from the RTC */
cmos_clock_t now = cmos_read_rtc();

/* critical path - end*/
enable_interrupts();

rtc_curr_unixtime = rtc_covert_to_unixtime(now);
}

0 comments on commit 40ec4fa

Please sign in to comment.