aboutsummaryrefslogtreecommitdiff
path: root/core/time/tsc_linux.odin
blob: f59a0338b6ea3558a3870e5e8cb8d70dbb6739f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#+private
#+build linux
package time

import "base:intrinsics"
@(require) import linux "core:sys/linux"

_get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
	when ODIN_ARCH == .arm64 {
		frequency := u64(intrinsics.read_cycle_counter_frequency())
		return frequency, true
	} else {
		// Get the file descriptor for the perf mapping
		perf_attr := linux.Perf_Event_Attr{}
		perf_attr.size = size_of(perf_attr)
		perf_attr.type = .HARDWARE
		perf_attr.config.hw = .INSTRUCTIONS
		perf_attr.flags = {.Disabled, .Exclude_Kernel, .Exclude_HV}
		fd, perf_errno := linux.perf_event_open(&perf_attr, linux.Pid(0), -1, linux.Fd(-1), {})
		if perf_errno != nil {
			return 0, false
		}
		defer linux.close(fd)
		// Map it into the memory
		page_size : uint = 4096
		addr, mmap_errno := linux.mmap(0, page_size, {.READ}, {.SHARED}, fd)
		if mmap_errno != nil {
			return 0, false
		}
		defer linux.munmap(addr, page_size)
		// Get the frequency from the mapped page
		event_page := cast(^linux.Perf_Event_Mmap_Page) addr
		if .User_Time not_in event_page.cap.flags {
			return 0, false
		}
		frequency := u64((u128(1_000_000_000) << u128(event_page.time_shift)) / u128(event_page.time_mult))
		return frequency, true
	}
}