cpuinfo.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a proc to determine the number of CPUs / cores.
  10. runnableExamples:
  11. doAssert countProcessors() > 0
  12. include "system/inclrtl"
  13. when defined(posix) and not (defined(macosx) or defined(bsd)):
  14. import std/posix
  15. when defined(windows):
  16. import std/private/win_getsysteminfo
  17. when defined(freebsd) or defined(macosx):
  18. {.emit: "#include <sys/types.h>".}
  19. when defined(openbsd) or defined(netbsd):
  20. {.emit: "#include <sys/param.h>".}
  21. when defined(macosx) or defined(bsd):
  22. # we HAVE to emit param.h before sysctl.h so we cannot use .header here
  23. # either. The amount of archaic bullshit in Poonix based OSes is just insane.
  24. {.emit: "#include <sys/sysctl.h>".}
  25. const
  26. CTL_HW = 6
  27. HW_AVAILCPU = 25
  28. HW_NCPU = 3
  29. proc sysctl(x: ptr array[0..3, cint], y: cint, z: pointer,
  30. a: var csize_t, b: pointer, c: csize_t): cint {.
  31. importc: "sysctl", nodecl.}
  32. when defined(genode):
  33. import genode/env
  34. proc affinitySpaceTotal(env: GenodeEnvPtr): cuint {.
  35. importcpp: "@->cpu().affinity_space().total()".}
  36. when defined(haiku):
  37. type
  38. SystemInfo {.importc: "system_info", header: "<OS.h>".} = object
  39. cpuCount {.importc: "cpu_count".}: uint32
  40. proc getSystemInfo(info: ptr SystemInfo): int32 {.importc: "get_system_info",
  41. header: "<OS.h>".}
  42. proc countProcessors*(): int {.rtl, extern: "ncpi$1".} =
  43. ## Returns the number of the processors/cores the machine has.
  44. ## Returns 0 if it cannot be detected.
  45. when defined(windows):
  46. var
  47. si: SystemInfo
  48. getSystemInfo(addr si)
  49. result = int(si.dwNumberOfProcessors)
  50. elif defined(macosx) or defined(bsd):
  51. var
  52. mib: array[0..3, cint]
  53. numCPU: int
  54. mib[0] = CTL_HW
  55. mib[1] = HW_AVAILCPU
  56. var len = sizeof(numCPU).csize_t
  57. discard sysctl(addr(mib), 2, addr(numCPU), len, nil, 0)
  58. if numCPU < 1:
  59. mib[1] = HW_NCPU
  60. discard sysctl(addr(mib), 2, addr(numCPU), len, nil, 0)
  61. result = numCPU
  62. elif defined(hpux):
  63. result = mpctl(MPC_GETNUMSPUS, nil, nil)
  64. elif defined(irix):
  65. var SC_NPROC_ONLN {.importc: "_SC_NPROC_ONLN", header: "<unistd.h>".}: cint
  66. result = sysconf(SC_NPROC_ONLN)
  67. elif defined(genode):
  68. result = runtimeEnv.affinitySpaceTotal().int
  69. elif defined(haiku):
  70. var sysinfo: SystemInfo
  71. if getSystemInfo(addr sysinfo) == 0:
  72. result = sysinfo.cpuCount.int
  73. else:
  74. result = sysconf(SC_NPROCESSORS_ONLN)
  75. if result <= 0: result = 0