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 15 // Package numcpus provides information about the number of CPUs in the system. 16 // 17 // It gets the number of CPUs (online, offline, present, possible or kernel 18 // maximum) on Linux, Darwin, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, 19 // Solaris/Illumos or Windows systems. 20 // 21 // On Linux, the information is retrieved by reading the corresponding CPU 22 // topology files in /sys/devices/system/cpu. 23 // 24 // On BSD systems, the information is retrieved using the hw.ncpu and 25 // hw.ncpuonline sysctls, if supported. 26 // 27 // On Windows systems, the information is retrieved using the 28 // GetActiveProcessorCount and GetMaximumProcessorCount functions, respectively. 29 // 30 // Not all functions are supported on Darwin, FreeBSD, NetBSD, OpenBSD, 31 // DragonflyBSD, Solaris/Illumos and Windows. ErrNotSupported is returned in 32 // case a function is not supported on a particular platform. 33 package numcpus 34 35 import "errors" 36 37 // ErrNotSupported is the error returned when the function is not supported. 38 var ErrNotSupported = errors.New("function not supported") 39 40 // GetConfigured returns the number of CPUs configured on the system. This 41 // function should return the same value as `getconf _SC_NPROCESSORS_CONF` on a 42 // unix system. 43 func GetConfigured() (int, error) { 44 return getConfigured() 45 } 46 47 // GetKernelMax returns the maximum number of CPUs allowed by the kernel 48 // configuration. This function is only supported on Linux and Windows systems. 49 func GetKernelMax() (int, error) { 50 return getKernelMax() 51 } 52 53 // GetOffline returns the number of offline CPUs, i.e. CPUs that are not online 54 // because they have been hotplugged off or exceed the limit of CPUs allowed by 55 // the kernel configuration (see GetKernelMax). This function is only supported 56 // on Linux systems. 57 func GetOffline() (int, error) { 58 return getOffline() 59 } 60 61 // GetOnline returns the number of CPUs that are online and being scheduled. 62 func GetOnline() (int, error) { 63 return getOnline() 64 } 65 66 // GetPossible returns the number of possible CPUs, i.e. CPUs that 67 // have been allocated resources and can be brought online if they are present. 68 func GetPossible() (int, error) { 69 return getPossible() 70 } 71 72 // GetPresent returns the number of CPUs present in the system. 73 func GetPresent() (int, error) { 74 return getPresent() 75 } 76