tsc_linux.odin 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #+private
  2. #+build linux
  3. package time
  4. import "base:intrinsics"
  5. @(require) import linux "core:sys/linux"
  6. _get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
  7. when ODIN_ARCH == .arm64 {
  8. frequency := u64(intrinsics.read_cycle_counter_frequency())
  9. return frequency, true
  10. } else {
  11. // Get the file descriptor for the perf mapping
  12. perf_attr := linux.Perf_Event_Attr{}
  13. perf_attr.size = size_of(perf_attr)
  14. perf_attr.type = .HARDWARE
  15. perf_attr.config.hw = .INSTRUCTIONS
  16. perf_attr.flags = {.Disabled, .Exclude_Kernel, .Exclude_HV}
  17. fd, perf_errno := linux.perf_event_open(&perf_attr, linux.Pid(0), -1, linux.Fd(-1), {})
  18. if perf_errno != nil {
  19. return 0, false
  20. }
  21. defer linux.close(fd)
  22. // Map it into the memory
  23. page_size : uint = 4096
  24. addr, mmap_errno := linux.mmap(0, page_size, {.READ}, {.SHARED}, fd)
  25. if mmap_errno != nil {
  26. return 0, false
  27. }
  28. defer linux.munmap(addr, page_size)
  29. // Get the frequency from the mapped page
  30. event_page := cast(^linux.Perf_Event_Mmap_Page) addr
  31. if .User_Time not_in event_page.cap.flags {
  32. return 0, false
  33. }
  34. frequency := u64((u128(1_000_000_000) << u128(event_page.time_shift)) / u128(event_page.time_mult))
  35. return frequency, true
  36. }
  37. }