...

Source file src/github.com/tklauser/numcpus/numcpus_bsd.go

Documentation: github.com/tklauser/numcpus

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

View as plain text