use the libc call on darwin so sysctlbyname works

This commit is contained in:
Colin Davidson
2023-02-21 05:38:07 -08:00
parent 45b742be23
commit f860b09065
4 changed files with 21 additions and 37 deletions
+4 -5
View File
@@ -42,7 +42,7 @@ _tick_duration_end :: proc "contextless" (d: ^Duration, t: Tick) {
} }
when ODIN_ARCH == .amd64 { when ODIN_ARCH == .amd64 {
_has_invariant_tsc :: proc "contextless" () -> bool { x86_has_invariant_tsc :: proc "contextless" () -> bool {
eax, _, _, _ := intrinsics.x86_cpuid(0x80_000_000, 0) eax, _, _, _ := intrinsics.x86_cpuid(0x80_000_000, 0)
// Is this processor *really* ancient? // Is this processor *really* ancient?
@@ -53,11 +53,10 @@ when ODIN_ARCH == .amd64 {
// check if the invariant TSC bit is set // check if the invariant TSC bit is set
_, _, _, edx := intrinsics.x86_cpuid(0x80_000_007, 0) _, _, _, edx := intrinsics.x86_cpuid(0x80_000_007, 0)
return (edx & (1 << 8)) != 0 return (edx & (1 << 8)) != 0
} }
} else {
_has_invariant_tsc :: proc "contextless" () -> bool { x86_get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
return false return _x86_get_tsc_frequency()
} }
} }
+9 -11
View File
@@ -2,22 +2,20 @@
//+build darwin //+build darwin
package time package time
import "core:sys/darwin" import "core:c"
_get_tsc_frequency :: proc "contextless" () -> u64 { foreign import libc "System.framework"
@(static) frequency : u64 = 0 foreign libc {
if frequency > 0 { @(link_name="sysctlbyname") _sysctlbyname :: proc(path: cstring, oldp: rawptr, oldlenp: rawptr, newp: rawptr, newlen: int) -> c.int ---
return frequency }
}
_x86_get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
tmp_freq : u64 = 0 tmp_freq : u64 = 0
tmp_size : i64 = size_of(tmp_freq) tmp_size : i64 = size_of(tmp_freq)
ret := darwin.syscall_sysctlbyname("machdep.tsc.frequency", &tmp_freq, &tmp_size, nil, 0) ret := _sysctlbyname("machdep.tsc.frequency", &tmp_freq, &tmp_size, nil, 0)
if ret < 0 { if ret < 0 {
frequency = 1 return 0, false
return 0
} }
frequency = tmp_freq return tmp_freq, true
return frequency
} }
+6 -14
View File
@@ -5,12 +5,7 @@ package time
import "core:intrinsics" import "core:intrinsics"
import "core:sys/unix" import "core:sys/unix"
_get_tsc_frequency :: proc "contextless" () -> u64 { _get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
@(static) frequency : u64 = 0
if frequency > 0 {
return frequency
}
perf_attr := unix.Perf_Event_Attr{} perf_attr := unix.Perf_Event_Attr{}
perf_attr.type = u32(unix.Perf_Type_Id.Hardware) perf_attr.type = u32(unix.Perf_Type_Id.Hardware)
perf_attr.config = u64(unix.Perf_Hardware_Id.Instructions) 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} perf_attr.flags = {.Disabled, .Exclude_Kernel, .Exclude_HV}
fd := unix.sys_perf_event_open(&perf_attr, 0, -1, -1, 0) fd := unix.sys_perf_event_open(&perf_attr, 0, -1, -1, 0)
if fd == -1 { if fd == -1 {
frequency = 1 return 0, false
return 0
} }
defer unix.sys_close(fd) defer unix.sys_close(fd)
page_size : uint = 4096 page_size : uint = 4096
ret := unix.sys_mmap(nil, page_size, unix.PROT_READ, unix.MAP_SHARED, fd, 0) ret := unix.sys_mmap(nil, page_size, unix.PROT_READ, unix.MAP_SHARED, fd, 0)
if ret < 0 && ret > -4096 { if ret < 0 && ret > -4096 {
frequency = 1 return 0, false
return 0
} }
addr := rawptr(uintptr(ret)) addr := rawptr(uintptr(ret))
defer unix.sys_munmap(addr, page_size) defer unix.sys_munmap(addr, page_size)
event_page := (^unix.Perf_Event_mmap_Page)(addr) event_page := (^unix.Perf_Event_mmap_Page)(addr)
if .User_Time not_in event_page.cap.flags { if .User_Time not_in event_page.cap.flags {
frequency = 1 return 0, false
return 0
} }
frequency = u64((u128(1_000_000_000) << u128(event_page.time_shift)) / u128(event_page.time_mult)) frequency := u64((u128(1_000_000_000) << u128(event_page.time_shift)) / u128(event_page.time_mult))
return frequency return frequency, true
} }
+2 -7
View File
@@ -5,12 +5,7 @@ package time
import "core:intrinsics" import "core:intrinsics"
import win32 "core:sys/windows" import win32 "core:sys/windows"
_get_tsc_frequency :: proc "contextless" () -> u64 { _get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
@(static) frequency : u64 = 0
if frequency > 0 {
return frequency
}
qpc_begin: win32.LARGE_INTEGER qpc_begin: win32.LARGE_INTEGER
win32.QueryPerformanceCounter(&qpc_begin) win32.QueryPerformanceCounter(&qpc_begin)
tsc_begin := intrinsics.read_cycle_counter() tsc_begin := intrinsics.read_cycle_counter()
@@ -25,5 +20,5 @@ _get_tsc_frequency :: proc "contextless" () -> u64 {
win32.QueryPerformanceFrequency(&qpc_frequency) win32.QueryPerformanceFrequency(&qpc_frequency)
frequency = u64((u128(tsc_end - tsc_begin) * u128(qpc_frequency)) / u128(qpc_end - qpc_begin)) frequency = u64((u128(tsc_end - tsc_begin) * u128(qpc_frequency)) / u128(qpc_end - qpc_begin))
return frequency return frequency, true
} }