|
@@ -5,12 +5,7 @@ package time
|
|
|
import "core:intrinsics"
|
|
|
import "core:sys/unix"
|
|
|
|
|
|
-_get_tsc_frequency :: proc "contextless" () -> u64 {
|
|
|
- @(static) frequency : u64 = 0
|
|
|
- if frequency > 0 {
|
|
|
- return frequency
|
|
|
- }
|
|
|
-
|
|
|
+_get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
|
|
|
perf_attr := unix.Perf_Event_Attr{}
|
|
|
perf_attr.type = u32(unix.Perf_Type_Id.Hardware)
|
|
|
perf_attr.config = u64(unix.Perf_Hardware_Id.Instructions)
|
|
@@ -18,26 +13,23 @@ _get_tsc_frequency :: proc "contextless" () -> u64 {
|
|
|
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
|
|
|
+ return 0, false
|
|
|
}
|
|
|
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 < 0 && ret > -4096 {
|
|
|
- frequency = 1
|
|
|
- return 0
|
|
|
+ return 0, false
|
|
|
}
|
|
|
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
|
|
|
+ return 0, false
|
|
|
}
|
|
|
|
|
|
- frequency = u64((u128(1_000_000_000) << u128(event_page.time_shift)) / u128(event_page.time_mult))
|
|
|
- return frequency
|
|
|
+ frequency := u64((u128(1_000_000_000) << u128(event_page.time_shift)) / u128(event_page.time_mult))
|
|
|
+ return frequency, true
|
|
|
}
|