build(deps): bump github.com/testcontainers/testcontainers-go
Bumps [github.com/testcontainers/testcontainers-go](https://github.com/testcontainers/testcontainers-go) from 0.40.0 to 0.41.0. - [Release notes](https://github.com/testcontainers/testcontainers-go/releases) - [Commits](https://github.com/testcontainers/testcontainers-go/compare/v0.40.0...v0.41.0) --- updated-dependencies: - dependency-name: github.com/testcontainers/testcontainers-go dependency-version: 0.41.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
fef601f104
commit
de59bb63ad
+85
-12
@@ -15,6 +15,7 @@
|
||||
package numcpus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -23,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
|
||||
@@ -33,19 +41,28 @@ func getFromCPUAffinity() (int, error) {
|
||||
return cpuSet.Count(), nil
|
||||
}
|
||||
|
||||
func readCPURange(file string) (int, error) {
|
||||
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(string(buf))
|
||||
}
|
||||
|
||||
func parseCPURange(cpus string) (int, error) {
|
||||
func countCPURange(cpus string) (int, error) {
|
||||
cpus = strings.Trim(cpus, "\n ")
|
||||
|
||||
// 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
|
||||
for cpuRange := range strings.SplitSeq(cpus, ",") {
|
||||
if cpuRange == "" {
|
||||
return 0, fmt.Errorf("empty CPU range in CPU string %q", cpus)
|
||||
}
|
||||
from, to, found := strings.Cut(cpuRange, "-")
|
||||
first, err := strconv.ParseUint(from, 10, 32)
|
||||
@@ -60,11 +77,51 @@ func parseCPURange(cpus string) (int, error) {
|
||||
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) {
|
||||
cpus = strings.Trim(cpus, "\n ")
|
||||
|
||||
// See comment in countCPURange.
|
||||
if cpus == "" {
|
||||
return []int{}, nil
|
||||
}
|
||||
|
||||
list := []int{}
|
||||
for cpuRange := range strings.SplitSeq(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 {
|
||||
@@ -100,20 +157,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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user