updated gopsutil

This commit is contained in:
Sergey Kanzhelev
2025-08-13 21:53:17 +00:00
parent 8b4595d9cb
commit db7eb8366c
347 changed files with 15801 additions and 5633 deletions
+15 -5
View File
@@ -1,13 +1,23 @@
env:
CIRRUS_CLONE_DEPTH: 1
GO_VERSION: go1.20
GO_VERSION: go1.24.0
freebsd_12_task:
freebsd_13_task:
freebsd_instance:
image_family: freebsd-12-3
image_family: freebsd-13-5
install_script: |
pkg install -y go
GOBIN=$PWD/bin go install golang.org/dl/${GO_VERSION}@latest
bin/${GO_VERSION} download
build_script: bin/${GO_VERSION} build -buildvcs=false -v ./...
test_script: bin/${GO_VERSION} test -buildvcs=false -race ./...
build_script: bin/${GO_VERSION} build -v ./...
test_script: bin/${GO_VERSION} test -race ./...
freebsd_14_task:
freebsd_instance:
image_family: freebsd-14-2
install_script: |
pkg install -y go
GOBIN=$PWD/bin go install golang.org/dl/${GO_VERSION}@latest
bin/${GO_VERSION} download
build_script: bin/${GO_VERSION} build -v ./...
test_script: bin/${GO_VERSION} test -race ./...
+23
View File
@@ -73,3 +73,26 @@ func GetPossible() (int, error) {
func GetPresent() (int, error) {
return getPresent()
}
// ListOffline returns the list of offline CPUs. See [GetOffline] for details on
// when a CPU is considered offline.
func ListOffline() ([]int, error) {
return listOffline()
}
// ListOnline returns the list of CPUs that are online and being scheduled.
func ListOnline() ([]int, error) {
return listOnline()
}
// ListPossible returns the list of possible CPUs. See [GetPossible] for
// details on when a CPU is considered possible.
func ListPossible() ([]int, error) {
return listPossible()
}
// ListPresent returns the list of present CPUs. See [GetPresent] for
// details on when a CPU is considered present.
func ListPresent() ([]int, error) {
return listPresent()
}
-1
View File
@@ -13,7 +13,6 @@
// limitations under the License.
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd
package numcpus
+86 -18
View File
@@ -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)
}
+33
View File
@@ -0,0 +1,33 @@
// Copyright 2024 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.
//go:build !linux
package numcpus
func listOffline() ([]int, error) {
return nil, ErrNotSupported
}
func listOnline() ([]int, error) {
return nil, ErrNotSupported
}
func listPossible() ([]int, error) {
return nil, ErrNotSupported
}
func listPresent() ([]int, error) {
return nil, ErrNotSupported
}
-1
View File
@@ -13,7 +13,6 @@
// limitations under the License.
//go:build solaris
// +build solaris
package numcpus
-1
View File
@@ -13,7 +13,6 @@
// limitations under the License.
//go:build !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris && !windows
// +build !darwin,!dragonfly,!freebsd,!linux,!netbsd,!openbsd,!solaris,!windows
package numcpus