numcpus.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }