Skip to content

Commit 735d34a

Browse files
authored
Merge pull request #798 from AndreasAakesson/dev
kernel: OS uptime is now using RTC + getter for CPU freq
2 parents 9db6c4a + 8ddc4da commit 735d34a

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

api/kernel/os.hpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <hw/cpu.hpp>
3030
#include <hertz>
3131
#include <vector>
32+
#include <kernel/rtc.hpp>
3233

3334
namespace hw{ class Serial; }
3435

@@ -55,11 +56,18 @@ class OS {
5556
return cycles_since_boot() / cpu_mhz_.count();
5657
}
5758

58-
/** Uptime in seconds. */
59-
static double uptime() {
60-
return cycles_since_boot() / Hz(cpu_mhz_).count();
59+
/** Timestamp for when OS was booted */
60+
static RTC::timestamp_t boot_timestamp()
61+
{ return booted_at_; }
62+
63+
/** Uptime in whole seconds. */
64+
static RTC::timestamp_t uptime() {
65+
return RTC::now() - booted_at_;
6166
}
6267

68+
static MHz cpu_freq()
69+
{ return cpu_mhz_; }
70+
6371
/**
6472
* Shutdown operating system
6573
*
@@ -159,6 +167,8 @@ class OS {
159167

160168
static hw::Serial& com1;
161169

170+
static RTC::timestamp_t booted_at_;
171+
162172

163173
struct Custom_init_struct {
164174
Custom_init_struct(Custom_init f, const char* n)

api/kernel/rtc.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ class RTC
2828
using timestamp_t = int64_t;
2929

3030
/// returns a 64-bit unix timestamp of the current time
31-
static timestamp_t get();
32-
33-
static timestamp_t now()
34-
{ return get(); }
31+
static timestamp_t now();
3532

3633
/// start an auto-calibration process
3734
static void init();

src/kernel/os.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extern uintptr_t _MAX_MEM_MIB_;
4646

4747
bool OS::power_ {true};
4848
MHz OS::cpu_mhz_ {1000};
49+
RTC::timestamp_t OS::booted_at_ {0};
4950
uintptr_t OS::low_memory_size_ {0};
5051
uintptr_t OS::high_memory_size_ {0};
5152
uintptr_t OS::heap_max_ {0xfffffff};
@@ -222,6 +223,7 @@ void OS::start(uint32_t boot_magic, uint32_t boot_addr) {
222223

223224
// Realtime/monotonic clock
224225
RTC::init();
226+
booted_at_ = RTC::now();
225227

226228
// Trying custom initialization functions
227229
MYINFO("Calling custom initialization functions");

src/kernel/rtc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ void RTC::init()
2828
});
2929
}
3030

31-
RTC::timestamp_t RTC::get()
31+
RTC::timestamp_t RTC::now()
3232
{
3333
auto ticks = hw::CPU::rdtsc() - current_ticks;
3434
auto diff = ticks / Hz(MHz(_CPUFreq_)).count();
35-
35+
3636
return current_time + diff;
3737
}

src/kernel/syscalls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ int wait(int* UNUSED(status)) {
149149
}
150150

151151
int gettimeofday(struct timeval* p, void*) {
152-
p->tv_sec = RTC::get();
152+
p->tv_sec = RTC::now();
153153
p->tv_usec = 0;
154154
return 0;
155155
}
@@ -212,7 +212,7 @@ void abort_ex(const char* why) {
212212
// Basic second-resolution implementation - using CMOS directly for now.
213213
int clock_gettime(clockid_t clk_id, struct timespec *tp){
214214
if (clk_id == CLOCK_REALTIME) {
215-
tp->tv_sec = RTC::get();
215+
tp->tv_sec = RTC::now();
216216
tp->tv_nsec = 0;
217217
return 0;
218218
}

0 commit comments

Comments
 (0)