refactor: time -> clock

This commit is contained in:
Loïc Grégoire
2026-04-18 14:07:42 -04:00
parent a1db75746a
commit 44cf08e57c
13 changed files with 95 additions and 83 deletions
+5 -4
View File
@@ -4,7 +4,7 @@ const acpi = b.pl.acpi;
const mm = b.mm;
const ke = b.ke;
var hpet_timer: ke.TimeCounter = .{
var hpet_cs: ke.ClockSource = .{
.name = "HPET",
.quality = 50,
.frequency = 0, // Filled at runtime
@@ -19,9 +19,10 @@ var hpet_regs: *volatile acpi.HpetRegs = undefined;
const femtos_per_s = 1_000_000_000_000_000;
pub fn init(hpet: *acpi.HpetTable) linksection(b.init) void {
hpet_regs = @ptrFromInt(mm.p2v(hpet.base_address.address));
if (ke.clock.is_better_than(&hpet_cs)) return;
hpet_timer.frequency = femtos_per_s / (b.mmio_read(u64, @intFromPtr(&hpet_regs.general_capabilities)) >> 32);
hpet_regs = @ptrFromInt(mm.p2v(hpet.base_address.address));
hpet_cs.frequency = femtos_per_s / (b.mmio_read(u64, @intFromPtr(&hpet_regs.general_capabilities)) >> 32);
// Disable timer.
b.mmio_write(u64, @intFromPtr(&hpet_regs.general_configuration), 0);
@@ -32,7 +33,7 @@ pub fn init(hpet: *acpi.HpetTable) linksection(b.init) void {
// Enable timer.
b.mmio_write(u64, @intFromPtr(&hpet_regs.general_configuration), 1);
ke.time.register_source(&hpet_timer);
ke.clock.register_source(&hpet_cs);
}
fn read_hpet() u64 {
+4 -2
View File
@@ -4,7 +4,7 @@ const acpi = b.pl.acpi;
const ke = b.ke;
const arch = @import("arch");
var pm_timer: ke.TimeCounter = .{
var pm_timer: ke.ClockSource = .{
.name = "ACPI",
.quality = 25,
.frequency = 3579545, // 3.579545 MHz
@@ -27,6 +27,8 @@ pub fn init(fadt: *acpi.Fadt) linksection(b.init) void {
return;
}
if (ke.clock.is_better_than(&pm_timer)) return;
timer_gas.address_space_id = .SystemIo;
timer_gas.address = fadt.pm_tmr_blk;
timer_gas.access_size = 3;
@@ -46,7 +48,7 @@ pub fn init(fadt: *acpi.Fadt) linksection(b.init) void {
pm_timer.mask = std.math.maxInt(u32);
}
ke.time.register_source(&pm_timer);
ke.clock.register_source(&pm_timer);
}
fn read_timer() u64 {
+2 -2
View File
@@ -160,7 +160,7 @@ fn lapic_calibrate(ms: u64) u64 {
write(.LvtTimer, (1 << 16));
write(.TimerInitialCount, std.math.maxInt(u32));
ke.time.sleep(std.time.ns_per_ms * ms);
ke.clock.sleep(std.time.ns_per_ms * ms);
const ticks = std.math.maxInt(u32) - read(.TimerCurrentCount);
@@ -198,7 +198,7 @@ fn timer_init() linksection(r.init) void {
pub fn arm_timer(ns: r.Nanoseconds) void {
if (amd64.cpu_features.tsc_deadline) {
const now = amd64.rdtsc();
const deadline = now + (ns * tsc.tsc_timer.frequency / std.time.ns_per_s);
const deadline = now + (ns * tsc.tsc.frequency / std.time.ns_per_s);
amd64.write_msr(.TscDeadline, deadline);
return;
}
+3 -2
View File
@@ -6,7 +6,7 @@ const mm = r.mm;
const ke = r.ke;
var pvclock_src: kvm.PVClockSource = undefined;
var pvclock_tc: ke.TimeCounter = .{
var pvclock: ke.ClockSource = .{
.name = "pvclock",
.quality = 75,
.frequency = std.time.ns_per_s,
@@ -17,9 +17,10 @@ var pvclock_tc: ke.TimeCounter = .{
};
pub fn init() linksection(r.init) void {
if (ke.clock.is_better_than(&pvclock)) return;
const page = mm.phys.alloc();
pvclock_src = kvm.PVClockSource.init(page, mm.p2v(page));
ke.time.register_source(&pvclock_tc);
ke.clock.register_source(&pvclock);
}
fn read_pvclock() u64 {
+2 -2
View File
@@ -103,9 +103,9 @@ pub fn init() linksection(r.init) void {
// Send the INIT-SIPI-SIPI sequence to start the AP.
apic.send_init(apic_id);
ke.time.sleep(init_delay);
ke.clock.sleep(init_delay);
apic.send_sipi(apic_id, 0x08);
ke.time.sleep(sipi_delay);
ke.clock.sleep(sipi_delay);
apic.send_sipi(apic_id, 0x08);
}
+10 -10
View File
@@ -6,7 +6,7 @@ const ke = r.ke;
const log = std.log.scoped(.@"amd64/tsc");
pub var tsc_timer: ke.TimeCounter = .{
pub var tsc: ke.ClockSource = .{
.name = "TSC",
.quality = 100,
.frequency = 0,
@@ -21,7 +21,7 @@ pub fn init() linksection(r.init) void {
if (!features.invariant_tsc) {
log.info("invariant TSC not supported (get a new PC), using fallback", .{});
if (ke.time.best()) |best| {
if (ke.clock.best()) |best| {
log.info("using {s} as timecounter source", .{best.name});
}
return;
@@ -33,14 +33,14 @@ pub fn init() linksection(r.init) void {
if (cpuid_state.ebx != 0 and cpuid_state.ecx != 0 and cpuid_state.eax != 0) {
const tsc_freq = (cpuid_state.ebx / cpuid_state.eax) * cpuid_state.ecx;
log.info("TSC frequency determined via CPUID: {} Hz", .{tsc_freq});
tsc_timer.frequency = tsc_freq;
tsc.frequency = tsc_freq;
ke.time.register_source(&tsc_timer);
ke.clock.register_source(&tsc);
return;
}
// Fallback to the other best time source to calibrate.
const best = ke.time.best() orelse return;
const best = ke.clock.best() orelse return;
const calib_cost_runs = 5;
var calib_cost: u64 = 0;
@@ -48,7 +48,7 @@ pub fn init() linksection(r.init) void {
// Calculate how many ticks it costs to call tc_sleep.
for (0..calib_cost_runs) |_| {
const start = amd64.rdtsc();
ke.time.sleep(0);
ke.clock.sleep(0);
const end = amd64.rdtsc();
calib_cost += (end - start);
}
@@ -63,7 +63,7 @@ pub fn init() linksection(r.init) void {
for (0..runs) |i| {
const start = amd64.rdtsc();
ke.time.sleep(calib_time);
ke.clock.sleep(calib_time);
const end = amd64.rdtsc();
freqs[i] = (end - start - calib_cost) * (std.time.ns_per_s / calib_time);
}
@@ -73,11 +73,11 @@ pub fn init() linksection(r.init) void {
var sum: u64 = 0;
for (freqs[1 .. runs - 1]) |f| sum += f;
tsc_timer.frequency = sum / (runs - 2);
tsc.frequency = sum / (runs - 2);
ke.time.register_source(&tsc_timer);
ke.clock.register_source(&tsc);
log.info("frequency calibrated using {s}: {}.{} MHz", .{ best.name, tsc_timer.frequency / 1_000_000, (tsc_timer.frequency % 1_000_000) / 1000 });
log.info("frequency calibrated using {s}: {}.{} MHz", .{ best.name, tsc.frequency / 1_000_000, (tsc.frequency % 1_000_000) / 1000 });
}
fn read_tsc() u64 {
+2 -2
View File
@@ -39,7 +39,7 @@ fn tc_read() u64 {
return @intCast(ts.sec * std.time.ns_per_s + ts.nsec);
}
var tc: ke.TimeCounter = .{
var tc: ke.ClockSource = .{
.read_count = tc_read,
.frequency = std.time.ns_per_s,
.name = "CLOCK_MONOTONIC",
@@ -59,7 +59,7 @@ pub fn init() void {
posix.sigaction(posix.SIG.ALRM, &act, null);
ke.time.register_source(&tc);
ke.clock.register_source(&tc);
}
pub fn init_cpu() void {