mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
begin adding tsc frequency getters
This commit is contained in:
+22
-1
@@ -1,6 +1,7 @@
|
||||
package time
|
||||
|
||||
import "core:runtime"
|
||||
import "core:intrinsics"
|
||||
|
||||
Tick :: struct {
|
||||
_nsec: i64, // relative amount
|
||||
@@ -40,6 +41,26 @@ _tick_duration_end :: proc "contextless" (d: ^Duration, t: Tick) {
|
||||
d^ = tick_since(t)
|
||||
}
|
||||
|
||||
when ODIN_ARCH == .amd64 {
|
||||
_has_invariant_tsc :: proc "contextless" () -> bool {
|
||||
eax, _, _, _ := intrinsics.x86_cpuid(0x80_000_000, 0)
|
||||
|
||||
// Is this processor *really* ancient?
|
||||
if eax < 0x80_000_007 {
|
||||
return false
|
||||
}
|
||||
|
||||
// check if the invariant TSC bit is set
|
||||
_, _, _, edx := intrinsics.x86_cpuid(0x80_000_007, 0)
|
||||
return (edx & (1 << 8)) != 0
|
||||
|
||||
}
|
||||
} else {
|
||||
_has_invariant_tsc :: proc "contextless" () -> bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Benchmark helpers
|
||||
*/
|
||||
@@ -94,4 +115,4 @@ benchmark :: proc(options: ^Benchmark_Options, allocator := context.allocator) -
|
||||
options->teardown(allocator) or_return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
//+private
|
||||
//+build linux
|
||||
package time
|
||||
|
||||
import "core:intrinsics"
|
||||
import "core:sys/unix"
|
||||
|
||||
_get_tsc_frequency :: proc "contextless" () -> u64 {
|
||||
@(static) frequency : u64 = 0
|
||||
if frequency > 0 {
|
||||
return frequency
|
||||
}
|
||||
|
||||
perf_attr := unix.Perf_Event_Attr{}
|
||||
perf_attr.type = u32(unix.Perf_Type_Id.Hardware)
|
||||
perf_attr.config = u64(unix.Perf_Hardware_Id.Instructions)
|
||||
perf_attr.size = size_of(perf_attr)
|
||||
perf_attr.flags = {.Disabled, .Exclude_Kernel, .Exclude_HV}
|
||||
fd := unix.sys_perf_event_open(&perf_attr, 0, -1, -1, 0)
|
||||
if fd == -1 {
|
||||
frequency = 1
|
||||
return 0
|
||||
}
|
||||
defer unix.sys_close(fd)
|
||||
|
||||
page_size : uint = 4096
|
||||
ret := unix.sys_mmap(nil, page_size, unix.PROT_READ, unix.MAP_SHARED, fd, 0)
|
||||
if ret == unix.MAP_FAILED {
|
||||
frequency = 1
|
||||
return 0
|
||||
}
|
||||
addr := rawptr(uintptr(ret))
|
||||
defer unix.sys_munmap(addr, page_size)
|
||||
|
||||
event_page := (^unix.Perf_Event_mmap_Page)(addr)
|
||||
if .User_Time not_in event_page.cap.flags {
|
||||
frequency = 1
|
||||
return 0
|
||||
}
|
||||
|
||||
frequency = u64((u128(1_000_000_000) << u128(event_page.time_shift)) / u128(event_page.time_mult))
|
||||
return frequency
|
||||
}
|
||||
Reference in New Issue
Block a user