From 63c133a50e31c497b6b35c14fa9fc6974ad56375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=85kesson?= Date: Thu, 1 Sep 2016 11:22:34 +0200 Subject: [PATCH 1/2] kernel: OS uptime is now using RTC + getter for CPU freq --- api/kernel/os.hpp | 16 +++++++++++++--- src/kernel/os.cpp | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/api/kernel/os.hpp b/api/kernel/os.hpp index fc064ef8cb..0d49dd6215 100644 --- a/api/kernel/os.hpp +++ b/api/kernel/os.hpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace hw{ class Serial; } @@ -55,11 +56,18 @@ class OS { return cycles_since_boot() / cpu_mhz_.count(); } - /** Uptime in seconds. */ - static double uptime() { - return cycles_since_boot() / Hz(cpu_mhz_).count(); + /** Timestamp for when OS was booted */ + static RTC::timestamp_t boot_timestamp() + { return booted_at_; } + + /** Uptime in whole seconds. */ + static RTC::timestamp_t uptime() { + return RTC::now() - booted_at_; } + static MHz cpu_freq() + { return cpu_mhz_; } + /** * Shutdown operating system * @@ -161,6 +169,8 @@ class OS { static hw::Serial& com1; + static RTC::timestamp_t booted_at_; + struct Custom_init_struct { Custom_init_struct(Custom_init f, const char* n) diff --git a/src/kernel/os.cpp b/src/kernel/os.cpp index 05c701641c..e7f5ff3fff 100644 --- a/src/kernel/os.cpp +++ b/src/kernel/os.cpp @@ -46,6 +46,7 @@ extern uintptr_t _MAX_MEM_MIB_; bool OS::power_ {true}; MHz OS::cpu_mhz_ {1000}; +RTC::timestamp_t OS::booted_at_ {0}; uintptr_t OS::low_memory_size_ {0}; uintptr_t OS::high_memory_size_ {0}; uintptr_t OS::heap_max_ {0xfffffff}; @@ -214,6 +215,7 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr) { // Realtime/monotonic clock RTC::init(); + booted_at_ = RTC::now(); // Trying custom initialization functions MYINFO("Calling custom initialization functions"); From 8ddc4da0de37d22f5452541f3e00ee827efb6e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=85kesson?= Date: Thu, 1 Sep 2016 11:29:56 +0200 Subject: [PATCH 2/2] kernel: Deprecated RTC::get() --- api/kernel/rtc.hpp | 5 +---- src/kernel/rtc.cpp | 4 ++-- src/kernel/syscalls.cpp | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/api/kernel/rtc.hpp b/api/kernel/rtc.hpp index e6d8b5b42e..ccba22d08c 100644 --- a/api/kernel/rtc.hpp +++ b/api/kernel/rtc.hpp @@ -28,10 +28,7 @@ class RTC using timestamp_t = int64_t; /// returns a 64-bit unix timestamp of the current time - static timestamp_t get(); - - static timestamp_t now() - { return get(); } + static timestamp_t now(); /// start an auto-calibration process static void init(); diff --git a/src/kernel/rtc.cpp b/src/kernel/rtc.cpp index 6938863308..be28595cdd 100644 --- a/src/kernel/rtc.cpp +++ b/src/kernel/rtc.cpp @@ -28,10 +28,10 @@ void RTC::init() }); } -RTC::timestamp_t RTC::get() +RTC::timestamp_t RTC::now() { auto ticks = hw::CPU::rdtsc() - current_ticks; auto diff = ticks / Hz(MHz(_CPUFreq_)).count(); - + return current_time + diff; } diff --git a/src/kernel/syscalls.cpp b/src/kernel/syscalls.cpp index ebec9a7596..19423f22b2 100644 --- a/src/kernel/syscalls.cpp +++ b/src/kernel/syscalls.cpp @@ -149,7 +149,7 @@ int wait(int* UNUSED(status)) { } int gettimeofday(struct timeval* p, void*) { - p->tv_sec = RTC::get(); + p->tv_sec = RTC::now(); p->tv_usec = 0; return 0; } @@ -212,7 +212,7 @@ void abort_ex(const char* why) { // Basic second-resolution implementation - using CMOS directly for now. int clock_gettime(clockid_t clk_id, struct timespec *tp){ if (clk_id == CLOCK_REALTIME) { - tp->tv_sec = RTC::get(); + tp->tv_sec = RTC::now(); tp->tv_nsec = 0; return 0; }