numcpus.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2018-2022 Tobias Klauser
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package numcpus provides information about the number of CPUs in the system.
  15. //
  16. // It gets the number of CPUs (online, offline, present, possible or kernel
  17. // maximum) on Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD,
  18. // Solaris/Illumos or Windows systems.
  19. //
  20. // On Linux, the information is retrieved by reading the corresponding CPU
  21. // topology files in /sys/devices/system/cpu.
  22. //
  23. // On BSD systems, the information is retrieved using the hw.ncpu and
  24. // hw.ncpuonline sysctls, if supported.
  25. //
  26. // On Windows systems, the information is retrieved using the
  27. // GetActiveProcessorCount and GetMaximumProcessorCount functions, respectively.
  28. //
  29. // Not all functions are supported on Darwin, FreeBSD, NetBSD, OpenBSD,
  30. // DragonflyBSD, Solaris/Illumos and Windows. ErrNotSupported is returned in
  31. // case a function is not supported on a particular platform.
  32. package numcpus
  33. import "errors"
  34. // ErrNotSupported is the error returned when the function is not supported.
  35. var ErrNotSupported = errors.New("function not supported")
  36. // GetConfigured returns the number of CPUs configured on the system. This
  37. // function should return the same value as `getconf _SC_NPROCESSORS_CONF` on a
  38. // unix system.
  39. func GetConfigured() (int, error) {
  40. return getConfigured()
  41. }
  42. // GetKernelMax returns the maximum number of CPUs allowed by the kernel
  43. // configuration. This function is only supported on Linux and Windows systems.
  44. func GetKernelMax() (int, error) {
  45. return getKernelMax()
  46. }
  47. // GetOffline returns the number of offline CPUs, i.e. CPUs that are not online
  48. // because they have been hotplugged off or exceed the limit of CPUs allowed by
  49. // the kernel configuration (see GetKernelMax). This function is only supported
  50. // on Linux systems.
  51. func GetOffline() (int, error) {
  52. return getOffline()
  53. }
  54. // GetOnline returns the number of CPUs that are online and being scheduled.
  55. func GetOnline() (int, error) {
  56. return getOnline()
  57. }
  58. // GetPossible returns the number of possible CPUs, i.e. CPUs that
  59. // have been allocated resources and can be brought online if they are present.
  60. func GetPossible() (int, error) {
  61. return getPossible()
  62. }
  63. // GetPresent returns the number of CPUs present in the system.
  64. func GetPresent() (int, error) {
  65. return getPresent()
  66. }
  67. // ListOffline returns the list of offline CPUs. See [GetOffline] for details on
  68. // when a CPU is considered offline.
  69. func ListOffline() ([]int, error) {
  70. return listOffline()
  71. }
  72. // ListOnline returns the list of CPUs that are online and being scheduled.
  73. func ListOnline() ([]int, error) {
  74. return listOnline()
  75. }
  76. // ListPossible returns the list of possible CPUs. See [GetPossible] for
  77. // details on when a CPU is considered possible.
  78. func ListPossible() ([]int, error) {
  79. return listPossible()
  80. }
  81. // ListPresent returns the list of present CPUs. See [GetPresent] for
  82. // details on when a CPU is considered present.
  83. func ListPresent() ([]int, error) {
  84. return listPresent()
  85. }