mirror of
https://github.com/hermitcore/libhermit.git
synced 2025-03-09 00:00:03 +01:00
uses time stamp counter to calculate the uptime
This commit is contained in:
parent
6eee6b8bd1
commit
1173c1b7fd
4 changed files with 65 additions and 24 deletions
|
@ -50,7 +50,7 @@ extern int32_t boot_processor;
|
|||
|
||||
#ifdef DYNAMIC_TICKS
|
||||
DEFINE_PER_CORE(uint64_t, last_tsc, 0);
|
||||
static uint64_t boot_tsc = 0;
|
||||
static uint64_t boot_tsc __attribute__ ((section(".data"))) = 0;
|
||||
|
||||
void check_ticks(void)
|
||||
{
|
||||
|
@ -59,7 +59,7 @@ void check_ticks(void)
|
|||
return;
|
||||
|
||||
const uint64_t curr_tsc = get_cntpct();
|
||||
rmb();
|
||||
mb();
|
||||
|
||||
const uint64_t diff_tsc = curr_tsc - per_core(last_tsc);
|
||||
const uint64_t diff_ticks = (diff_tsc * (uint64_t) TIMER_FREQ) / freq_hz;
|
||||
|
@ -70,7 +70,22 @@ void check_ticks(void)
|
|||
rmb();
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t get_uptime(void)
|
||||
{
|
||||
const uint64_t curr_tsc = get_cntpct();
|
||||
|
||||
mb();
|
||||
uint64_t diff = curr_tsc - per_core(last_rdtsc);
|
||||
|
||||
return (1000ULL*diff) / freq_hz;
|
||||
}
|
||||
#else
|
||||
uint64_t get_uptime(void)
|
||||
{
|
||||
return (get_clock_tick() * 1000) / TIMER_FREQ;
|
||||
}
|
||||
|
||||
static void restart_periodic_timer(void)
|
||||
{
|
||||
set_cntp_tval(freq_hz / TIMER_FREQ);
|
||||
|
@ -172,13 +187,19 @@ int timer_wait(unsigned int ticks)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int clock_init(void)
|
||||
{
|
||||
#ifdef DYNAMIC_TICKS
|
||||
if (!boot_tsc)
|
||||
boot_tsc = get_cntpct();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int timer_init(void)
|
||||
{
|
||||
#ifdef DYNAMIC_TICKS
|
||||
if (boot_tsc)
|
||||
return 0;
|
||||
|
||||
boot_tsc = get_cntpct();
|
||||
set_per_core(last_tsc, boot_tsc);
|
||||
#endif
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ extern int32_t boot_processor;
|
|||
|
||||
#ifdef DYNAMIC_TICKS
|
||||
DEFINE_PER_CORE(uint64_t, last_rdtsc, 0);
|
||||
uint64_t boot_tsc = 0;
|
||||
uint64_t boot_tsc __attribute__ ((section(".data"))) = 0;
|
||||
|
||||
void check_ticks(void)
|
||||
{
|
||||
|
@ -56,7 +56,7 @@ void check_ticks(void)
|
|||
return;
|
||||
|
||||
const uint64_t curr_rdtsc = rdtsc();
|
||||
rmb();
|
||||
mb();
|
||||
|
||||
const uint64_t diff_cycles = curr_rdtsc - per_core(last_rdtsc);
|
||||
const uint64_t cpu_freq_hz = 1000000ULL * (uint64_t) get_cpu_frequency();
|
||||
|
@ -68,6 +68,22 @@ void check_ticks(void)
|
|||
mb();
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t get_uptime(void)
|
||||
{
|
||||
const uint64_t cpu_freq_hz = 1000000ULL * (uint64_t) get_cpu_frequency();
|
||||
const uint64_t curr_tsc = rdtsc();
|
||||
|
||||
mb();
|
||||
uint64_t diff = curr_tsc - per_core(last_rdtsc);
|
||||
|
||||
return (1000ULL*diff) / cpu_freq_hz;
|
||||
}
|
||||
#else
|
||||
uint64_t get_uptime(void)
|
||||
{
|
||||
return (get_clock_tick() * 1000) / TIMER_FREQ;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
@ -162,6 +178,16 @@ static int pit_init(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int clock_init(void)
|
||||
{
|
||||
#ifdef DYNAMIC_TICKS
|
||||
if (!boot_tsc)
|
||||
boot_tsc = has_rdtscp() ? rdtscp(NULL) : rdtsc();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets up the system clock by installing the timer handler
|
||||
* into IRQ0
|
||||
|
@ -169,11 +195,7 @@ static int pit_init(void)
|
|||
int timer_init(void)
|
||||
{
|
||||
#ifdef DYNAMIC_TICKS
|
||||
if (boot_tsc)
|
||||
{
|
||||
set_per_core(last_rdtsc, boot_tsc);
|
||||
return 0;
|
||||
}
|
||||
set_per_core(last_rdtsc, boot_tsc);
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
@ -183,11 +205,6 @@ int timer_init(void)
|
|||
irq_install_handler(32, timer_handler);
|
||||
irq_install_handler(123, timer_handler);
|
||||
|
||||
#ifdef DYNAMIC_TICKS
|
||||
boot_tsc = has_rdtscp() ? rdtscp(NULL) : rdtsc();
|
||||
set_per_core(last_rdtsc, boot_tsc);
|
||||
#endif
|
||||
|
||||
if (cpu_freq) // do we need to configure the timer?
|
||||
return 0;
|
||||
|
||||
|
|
|
@ -55,6 +55,9 @@ struct tms {
|
|||
*/
|
||||
int timer_init(void);
|
||||
|
||||
/** @brief Initialize clock */
|
||||
int clock_init(void);
|
||||
|
||||
/** @brief Initialized a timer
|
||||
*
|
||||
* @param ticks Amount of ticks to wait
|
||||
|
@ -81,9 +84,8 @@ static inline uint64_t get_clock_tick(void)
|
|||
*/
|
||||
static inline void sleep(unsigned int sec) { timer_wait(sec*TIMER_FREQ); }
|
||||
|
||||
/** @brief Get milliseconds since system boot
|
||||
*/
|
||||
static inline uint64_t get_uptime() { return (get_clock_tick() * 1000) / TIMER_FREQ; }
|
||||
/** @brief Get milliseconds since system boot */
|
||||
uint64_t get_uptime(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -113,14 +113,15 @@ extern void signal_init();
|
|||
|
||||
static int hermit_init(void)
|
||||
{
|
||||
uint32_t i;
|
||||
clock_init();
|
||||
|
||||
size_t sz = (size_t) &percore_end0 - (size_t) &percore_start;
|
||||
|
||||
// initialize .kbss sections
|
||||
memset((void*)&tdata_end, 0x00, (size_t) &__bss_start - (size_t) &tdata_end);
|
||||
|
||||
// initialize .percore section => copy first section to all other sections
|
||||
for(i=1; i<MAX_CORES; i++)
|
||||
for(uint32_t i=1; i<MAX_CORES; i++)
|
||||
memcpy((char*) &percore_start + i*sz, (char*) &percore_start, sz);
|
||||
|
||||
koutput_init();
|
||||
|
@ -391,7 +392,7 @@ static int initd(void* arg)
|
|||
uhyve_send(UHYVE_PORT_CMDVAL,
|
||||
(unsigned)virt_to_phys((size_t)&uhyve_cmdval_phys));
|
||||
|
||||
LOG_INFO("Boot time: %d ms\n", (get_clock_tick() * 1000) / TIMER_FREQ);
|
||||
LOG_INFO("Boot time: %d ms\n", get_uptime());
|
||||
libc_start(uhyve_cmdsize.argc, uhyve_cmdval.argv, uhyve_cmdval.envp);
|
||||
|
||||
for(i=0; i<uhyve_cmdsize.argc; i++)
|
||||
|
|
Loading…
Add table
Reference in a new issue