cpu_linux_intel.odin 870 B

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