cpuinfo.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 procs to determine the number of CPUs / cores.
  10. include "system/inclrtl"
  11. when not defined(windows):
  12. import posix
  13. when defined(freebsd) or defined(macosx):
  14. {.emit:"#include <sys/types.h>".}
  15. when defined(openbsd) or defined(netbsd):
  16. {.emit:"#include <sys/param.h>".}
  17. when defined(macosx) or defined(bsd):
  18. # we HAVE to emit param.h before sysctl.h so we cannot use .header here
  19. # either. The amount of archaic bullshit in Poonix based OSes is just insane.
  20. {.emit:"#include <sys/sysctl.h>".}
  21. const
  22. CTL_HW = 6
  23. HW_AVAILCPU = 25
  24. HW_NCPU = 3
  25. proc sysctl(x: ptr array[0..3, cint], y: cint, z: pointer,
  26. a: var csize_t, b: pointer, c: csize_t): cint {.
  27. importc: "sysctl", nodecl.}
  28. when defined(genode):
  29. include genode/env
  30. proc affinitySpaceTotal(env: GenodeEnvPtr): cuint {.
  31. importcpp: "@->cpu().affinity_space().total()".}
  32. when defined(haiku):
  33. type
  34. SystemInfo {.importc: "system_info", header: "<OS.h>".} = object
  35. cpuCount {.importc: "cpu_count".}: uint32
  36. proc getSystemInfo(info: ptr SystemInfo): int32 {.importc: "get_system_info",
  37. header: "<OS.h>".}
  38. proc countProcessors*(): int {.rtl, extern: "ncpi$1".} =
  39. ## returns the number of the processors/cores the machine has.
  40. ## Returns 0 if it cannot be detected.
  41. when defined(windows):
  42. type
  43. SYSTEM_INFO {.final, pure.} = object
  44. u1: int32
  45. dwPageSize: int32
  46. lpMinimumApplicationAddress: pointer
  47. lpMaximumApplicationAddress: pointer
  48. dwActiveProcessorMask: ptr int32
  49. dwNumberOfProcessors: int32
  50. dwProcessorType: int32
  51. dwAllocationGranularity: int32
  52. wProcessorLevel: int16
  53. wProcessorRevision: int16
  54. proc GetSystemInfo(lpSystemInfo: var SYSTEM_INFO) {.stdcall, dynlib: "kernel32", importc: "GetSystemInfo".}
  55. var
  56. si: SYSTEM_INFO
  57. GetSystemInfo(si)
  58. result = si.dwNumberOfProcessors
  59. elif defined(macosx) or defined(bsd):
  60. var
  61. mib: array[0..3, cint]
  62. numCPU: int
  63. mib[0] = CTL_HW
  64. mib[1] = HW_AVAILCPU
  65. var len = sizeof(numCPU).csize_t
  66. discard sysctl(addr(mib), 2, addr(numCPU), len, nil, 0)
  67. if numCPU < 1:
  68. mib[1] = HW_NCPU
  69. discard sysctl(addr(mib), 2, addr(numCPU), len, nil, 0)
  70. result = numCPU
  71. elif defined(hpux):
  72. result = mpctl(MPC_GETNUMSPUS, nil, nil)
  73. elif defined(irix):
  74. var SC_NPROC_ONLN {.importc: "_SC_NPROC_ONLN", header: "<unistd.h>".}: cint
  75. result = sysconf(SC_NPROC_ONLN)
  76. elif defined(genode):
  77. result = runtimeEnv.affinitySpaceTotal().int
  78. elif defined(haiku):
  79. var sysinfo: SystemInfo
  80. if getSystemInfo(addr sysinfo) == 0:
  81. result = sysinfo.cpuCount.int
  82. else:
  83. result = sysconf(SC_NPROCESSORS_ONLN)
  84. if result <= 0: result = 0
  85. runnableExamples:
  86. block:
  87. doAssert countProcessors() > 0