numcpus_bsd.go 1.6 KB

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