summaryrefslogtreecommitdiff
path: root/vendor/github.com/tklauser/numcpus/numcpus_linux.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/tklauser/numcpus/numcpus_linux.go')
-rw-r--r--vendor/github.com/tklauser/numcpus/numcpus_linux.go104
1 files changed, 86 insertions, 18 deletions
diff --git a/vendor/github.com/tklauser/numcpus/numcpus_linux.go b/vendor/github.com/tklauser/numcpus/numcpus_linux.go
index 1a30525..7b991da 100644
--- a/vendor/github.com/tklauser/numcpus/numcpus_linux.go
+++ b/vendor/github.com/tklauser/numcpus/numcpus_linux.go
@@ -15,7 +15,7 @@
package numcpus
import (
- "io/ioutil"
+ "fmt"
"os"
"path/filepath"
"strconv"
@@ -24,7 +24,14 @@ import (
"golang.org/x/sys/unix"
)
-const sysfsCPUBasePath = "/sys/devices/system/cpu"
+const (
+ sysfsCPUBasePath = "/sys/devices/system/cpu"
+
+ offline = "offline"
+ online = "online"
+ possible = "possible"
+ present = "present"
+)
func getFromCPUAffinity() (int, error) {
var cpuSet unix.CPUSet
@@ -34,38 +41,83 @@ func getFromCPUAffinity() (int, error) {
return cpuSet.Count(), nil
}
-func readCPURange(file string) (int, error) {
- buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, file))
+func readCPURangeWith[T any](file string, f func(cpus string) (T, error)) (T, error) {
+ var zero T
+ buf, err := os.ReadFile(filepath.Join(sysfsCPUBasePath, file))
if err != nil {
- return 0, err
+ return zero, err
}
- return parseCPURange(strings.Trim(string(buf), "\n "))
+ return f(strings.Trim(string(buf), "\n "))
}
-func parseCPURange(cpus string) (int, error) {
+func countCPURange(cpus string) (int, error) {
+ // Treat empty file as valid. This might be the case if there are no offline CPUs in which
+ // case /sys/devices/system/cpu/offline is empty.
+ if cpus == "" {
+ return 0, nil
+ }
+
n := int(0)
for _, cpuRange := range strings.Split(cpus, ",") {
- if len(cpuRange) == 0 {
- continue
+ if cpuRange == "" {
+ return 0, fmt.Errorf("empty CPU range in CPU string %q", cpus)
}
- rangeOp := strings.SplitN(cpuRange, "-", 2)
- first, err := strconv.ParseUint(rangeOp[0], 10, 32)
+ from, to, found := strings.Cut(cpuRange, "-")
+ first, err := strconv.ParseUint(from, 10, 32)
if err != nil {
return 0, err
}
- if len(rangeOp) == 1 {
+ if !found {
n++
continue
}
- last, err := strconv.ParseUint(rangeOp[1], 10, 32)
+ last, err := strconv.ParseUint(to, 10, 32)
if err != nil {
return 0, err
}
+ if last < first {
+ return 0, fmt.Errorf("last CPU in range (%d) less than first (%d)", last, first)
+ }
n += int(last - first + 1)
}
return n, nil
}
+func listCPURange(cpus string) ([]int, error) {
+ // See comment in countCPURange.
+ if cpus == "" {
+ return []int{}, nil
+ }
+
+ list := []int{}
+ for _, cpuRange := range strings.Split(cpus, ",") {
+ if cpuRange == "" {
+ return nil, fmt.Errorf("empty CPU range in CPU string %q", cpus)
+ }
+ from, to, found := strings.Cut(cpuRange, "-")
+ first, err := strconv.ParseUint(from, 10, 32)
+ if err != nil {
+ return nil, err
+ }
+ if !found {
+ // range containing a single element
+ list = append(list, int(first))
+ continue
+ }
+ last, err := strconv.ParseUint(to, 10, 32)
+ if err != nil {
+ return nil, err
+ }
+ if last < first {
+ return nil, fmt.Errorf("last CPU in range (%d) less than first (%d)", last, first)
+ }
+ for cpu := int(first); cpu <= int(last); cpu++ {
+ list = append(list, cpu)
+ }
+ }
+ return list, nil
+}
+
func getConfigured() (int, error) {
d, err := os.Open(sysfsCPUBasePath)
if err != nil {
@@ -89,7 +141,7 @@ func getConfigured() (int, error) {
}
func getKernelMax() (int, error) {
- buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, "kernel_max"))
+ buf, err := os.ReadFile(filepath.Join(sysfsCPUBasePath, "kernel_max"))
if err != nil {
return 0, err
}
@@ -101,20 +153,36 @@ func getKernelMax() (int, error) {
}
func getOffline() (int, error) {
- return readCPURange("offline")
+ return readCPURangeWith(offline, countCPURange)
}
func getOnline() (int, error) {
if n, err := getFromCPUAffinity(); err == nil {
return n, nil
}
- return readCPURange("online")
+ return readCPURangeWith(online, countCPURange)
}
func getPossible() (int, error) {
- return readCPURange("possible")
+ return readCPURangeWith(possible, countCPURange)
}
func getPresent() (int, error) {
- return readCPURange("present")
+ return readCPURangeWith(present, countCPURange)
+}
+
+func listOffline() ([]int, error) {
+ return readCPURangeWith(offline, listCPURange)
+}
+
+func listOnline() ([]int, error) {
+ return readCPURangeWith(online, listCPURange)
+}
+
+func listPossible() ([]int, error) {
+ return readCPURangeWith(possible, listCPURange)
+}
+
+func listPresent() ([]int, error) {
+ return readCPURangeWith(present, listCPURange)
}