summaryrefslogtreecommitdiff
path: root/vendor/github.com/tklauser/numcpus/numcpus_linux.go
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-05-11 21:12:57 -0600
committermo khan <mo@mokhan.ca>2025-05-11 21:12:57 -0600
commit60440f90dca28e99a31dd328c5f6d5dc0f9b6a2e (patch)
tree2f54adf55086516f162f0a55a5347e6b25f7f176 /vendor/github.com/tklauser/numcpus/numcpus_linux.go
parent05ca9b8d3a9c7203a3a3b590beaa400900bd9007 (diff)
chore: vendor go dependencies
Diffstat (limited to 'vendor/github.com/tklauser/numcpus/numcpus_linux.go')
-rw-r--r--vendor/github.com/tklauser/numcpus/numcpus_linux.go120
1 files changed, 120 insertions, 0 deletions
diff --git a/vendor/github.com/tklauser/numcpus/numcpus_linux.go b/vendor/github.com/tklauser/numcpus/numcpus_linux.go
new file mode 100644
index 0000000..1a30525
--- /dev/null
+++ b/vendor/github.com/tklauser/numcpus/numcpus_linux.go
@@ -0,0 +1,120 @@
+// Copyright 2018 Tobias Klauser
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package numcpus
+
+import (
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+
+ "golang.org/x/sys/unix"
+)
+
+const sysfsCPUBasePath = "/sys/devices/system/cpu"
+
+func getFromCPUAffinity() (int, error) {
+ var cpuSet unix.CPUSet
+ if err := unix.SchedGetaffinity(0, &cpuSet); err != nil {
+ return 0, err
+ }
+ return cpuSet.Count(), nil
+}
+
+func readCPURange(file string) (int, error) {
+ buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, file))
+ if err != nil {
+ return 0, err
+ }
+ return parseCPURange(strings.Trim(string(buf), "\n "))
+}
+
+func parseCPURange(cpus string) (int, error) {
+ n := int(0)
+ for _, cpuRange := range strings.Split(cpus, ",") {
+ if len(cpuRange) == 0 {
+ continue
+ }
+ rangeOp := strings.SplitN(cpuRange, "-", 2)
+ first, err := strconv.ParseUint(rangeOp[0], 10, 32)
+ if err != nil {
+ return 0, err
+ }
+ if len(rangeOp) == 1 {
+ n++
+ continue
+ }
+ last, err := strconv.ParseUint(rangeOp[1], 10, 32)
+ if err != nil {
+ return 0, err
+ }
+ n += int(last - first + 1)
+ }
+ return n, nil
+}
+
+func getConfigured() (int, error) {
+ d, err := os.Open(sysfsCPUBasePath)
+ if err != nil {
+ return 0, err
+ }
+ defer d.Close()
+ fis, err := d.Readdir(-1)
+ if err != nil {
+ return 0, err
+ }
+ count := 0
+ for _, fi := range fis {
+ if name := fi.Name(); fi.IsDir() && strings.HasPrefix(name, "cpu") {
+ _, err := strconv.ParseInt(name[3:], 10, 64)
+ if err == nil {
+ count++
+ }
+ }
+ }
+ return count, nil
+}
+
+func getKernelMax() (int, error) {
+ buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, "kernel_max"))
+ if err != nil {
+ return 0, err
+ }
+ n, err := strconv.ParseInt(strings.Trim(string(buf), "\n "), 10, 32)
+ if err != nil {
+ return 0, err
+ }
+ return int(n), nil
+}
+
+func getOffline() (int, error) {
+ return readCPURange("offline")
+}
+
+func getOnline() (int, error) {
+ if n, err := getFromCPUAffinity(); err == nil {
+ return n, nil
+ }
+ return readCPURange("online")
+}
+
+func getPossible() (int, error) {
+ return readCPURange("possible")
+}
+
+func getPresent() (int, error) {
+ return readCPURange("present")
+}