cpu_linux_intel.odin 945 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #+build i386, amd64
  2. #+build linux
  3. package sysinfo
  4. import "base:runtime"
  5. import "core:sys/linux"
  6. import "core:strings"
  7. import "core:strconv"
  8. @(init, private)
  9. init_cpu_core_count :: proc "contextless" () {
  10. context = runtime.default_context()
  11. fd, err := linux.open("/proc/cpuinfo", {})
  12. if err != .NONE { return }
  13. defer linux.close(fd)
  14. // This is probably enough right?
  15. buf: [4096]byte
  16. n, rerr := linux.read(fd, buf[:])
  17. if rerr != .NONE || n == 0 { return }
  18. str := string(buf[:n])
  19. for line in strings.split_lines_iterator(&str) {
  20. key, _, value := strings.partition(line, ":")
  21. key = strings.trim_space(key)
  22. value = strings.trim_space(value)
  23. if key == "cpu cores" {
  24. if num_physical_cores, ok := strconv.parse_int(value); ok {
  25. cpu.physical_cores = num_physical_cores
  26. }
  27. }
  28. if key == "siblings" {
  29. if num_logical_cores, ok := strconv.parse_int(value); ok {
  30. cpu.logical_cores = num_logical_cores
  31. }
  32. }
  33. }
  34. }