cpuinfo.nim 3.0 KB

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