HelenOS sources
This source file includes following definitions.
- clock_counter_init
- clock_update_counters
- cpu_update_accounting
- clock
#include <time/clock.h>
#include <time/timeout.h>
#include <config.h>
#include <synch/spinlock.h>
#include <synch/waitq.h>
#include <halt.h>
#include <proc/scheduler.h>
#include <cpu.h>
#include <arch.h>
#include <adt/list.h>
#include <atomic.h>
#include <proc/thread.h>
#include <sysinfo/sysinfo.h>
#include <barrier.h>
#include <mm/frame.h>
#include <ddi/ddi.h>
#include <arch/cycle.h>
#include <preemption.h>
uptime_t *uptime;
static parea_t clock_parea;
void clock_counter_init(void)
{
uintptr_t faddr = frame_alloc(1, FRAME_LOWMEM | FRAME_ATOMIC, 0);
if (faddr == 0)
panic("Cannot allocate page for clock.");
uptime = (uptime_t *) PA2KA(faddr);
uptime->seconds1 = 0;
uptime->seconds2 = 0;
uptime->useconds = 0;
ddi_parea_init(&clock_parea);
clock_parea.pbase = faddr;
clock_parea.frames = 1;
clock_parea.unpriv = true;
clock_parea.mapped = false;
ddi_parea_register(&clock_parea);
sysinfo_set_item_val("clock.faddr", NULL, (sysarg_t) faddr);
}
static void clock_update_counters(uint64_t current_tick)
{
if (CPU->id == 0) {
uint64_t usec = (1000000 / HZ) * current_tick;
sysarg_t secs = usec / 1000000;
sysarg_t usecs = usec % 1000000;
uptime->seconds1 = secs;
write_barrier();
uptime->useconds = usecs;
write_barrier();
uptime->seconds2 = secs;
}
}
static void cpu_update_accounting(void)
{
uint64_t now = get_cycle();
atomic_time_increment(&CPU->busy_cycles, now - CPU_LOCAL->last_cycle);
CPU_LOCAL->last_cycle = now;
}
void clock(void)
{
size_t missed_clock_ticks = CPU_LOCAL->missed_clock_ticks;
CPU_LOCAL->missed_clock_ticks = 0;
CPU_LOCAL->current_clock_tick += missed_clock_ticks + 1;
uint64_t current_clock_tick = CPU_LOCAL->current_clock_tick;
clock_update_counters(current_clock_tick);
cpu_update_accounting();
irq_spinlock_lock(&CPU->timeoutlock, false);
link_t *cur;
while ((cur = list_first(&CPU->timeout_active_list)) != NULL) {
timeout_t *timeout = list_get_instance(cur, timeout_t, link);
if (current_clock_tick <= timeout->deadline) {
break;
}
list_remove(cur);
timeout_handler_t handler = timeout->handler;
void *arg = timeout->arg;
atomic_bool *finished = &timeout->finished;
irq_spinlock_unlock(&CPU->timeoutlock, false);
handler(arg);
atomic_store_explicit(finished, true, memory_order_release);
irq_spinlock_lock(&CPU->timeoutlock, false);
}
irq_spinlock_unlock(&CPU->timeoutlock, false);
if (THREAD) {
if (current_clock_tick >= CPU_LOCAL->preempt_deadline && PREEMPTION_ENABLED) {
thread_yield();
#ifdef CONFIG_UDEBUG
istate_t *istate = THREAD->udebug.uspace_state;
if ((istate) && (istate_from_uspace(istate)))
udebug_before_thread_runs();
#endif
}
}
}
HelenOS homepage, sources at GitHub