numcpus_bsd.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2018 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. //go:build darwin || dragonfly || freebsd || netbsd || openbsd
  15. package numcpus
  16. import (
  17. "runtime"
  18. "golang.org/x/sys/unix"
  19. )
  20. func getConfigured() (int, error) {
  21. n, err := unix.SysctlUint32("hw.ncpu")
  22. return int(n), err
  23. }
  24. func getKernelMax() (int, error) {
  25. if runtime.GOOS == "freebsd" {
  26. n, err := unix.SysctlUint32("kern.smp.maxcpus")
  27. return int(n), err
  28. }
  29. return 0, ErrNotSupported
  30. }
  31. func getOffline() (int, error) {
  32. return 0, ErrNotSupported
  33. }
  34. func getOnline() (int, error) {
  35. var n uint32
  36. var err error
  37. switch runtime.GOOS {
  38. case "netbsd", "openbsd":
  39. n, err = unix.SysctlUint32("hw.ncpuonline")
  40. if err != nil {
  41. n, err = unix.SysctlUint32("hw.ncpu")
  42. }
  43. default:
  44. n, err = unix.SysctlUint32("hw.ncpu")
  45. }
  46. return int(n), err
  47. }
  48. func getPossible() (int, error) {
  49. n, err := unix.SysctlUint32("hw.ncpu")
  50. return int(n), err
  51. }
  52. func getPresent() (int, error) {
  53. n, err := unix.SysctlUint32("hw.ncpu")
  54. return int(n), err
  55. }