Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: tklauser/numcpus
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.6.1
Choose a base ref
...
head repository: tklauser/numcpus
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.8.0
Choose a head ref
Loading
Showing with 59 additions and 50 deletions.
  1. +15 −5 .cirrus.yml
  2. +3 −3 .github/workflows/static-analysis.yml
  3. +7 −6 .github/workflows/test.yml
  4. +2 −2 go.mod
  5. +2 −2 go.sum
  6. +0 −1 numcpus_bsd.go
  7. +6 −7 numcpus_linux.go
  8. +24 −22 numcpus_linux_test.go
  9. +0 −1 numcpus_solaris.go
  10. +0 −1 numcpus_unsupported.go
20 changes: 15 additions & 5 deletions .cirrus.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
env:
CIRRUS_CLONE_DEPTH: 1
GO_VERSION: go1.20
GO_VERSION: go1.22.2

freebsd_12_task:
freebsd_13_task:
freebsd_instance:
image_family: freebsd-12-3
image_family: freebsd-13-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 -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-0
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 ./...
6 changes: 3 additions & 3 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -14,12 +14,12 @@ jobs:

steps:
- name: Install Go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
with:
go-version: '1.19'
go-version: '1.22'

- name: Check out code
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -12,25 +12,26 @@ jobs:
build-and-test:
strategy:
matrix:
go-version: ['1.18', '1.19', '1.20']
os: [ubuntu-latest, macos-latest, windows-latest]
# Oldest supported version is 1.18, plus the latest two releases.
go-version: ['1.18', '1.21', '1.22']
os: [ubuntu-20.04, ubuntu-22.04, macos-11, macos-12, macos-13, macos-14, windows-2019, windows-2022]
runs-on: ${{ matrix.os }}

steps:
- name: Install Go
uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491
with:
go-version: ${{ matrix.go-version }}

- name: Check out code
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f

- name: Check formatting
if: ${{ matrix.go-version == '1.20' && matrix.os == 'ubuntu-latest' }}
if: ${{ matrix.go-version == '1.22' && matrix.os == 'ubuntu-latest' }}
run: diff -u <(echo -n) <(gofmt -d .)

- name: Check Go modules
if: ${{ matrix.go-version == '1.20' && matrix.os == 'ubuntu-latest' }}
if: ${{ matrix.go-version == '1.22' && matrix.os == 'ubuntu-latest' }}
run: |
go mod tidy
git diff --exit-code
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/tklauser/numcpus

go 1.13
go 1.18

require golang.org/x/sys v0.8.0
require golang.org/x/sys v0.19.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1 change: 0 additions & 1 deletion numcpus_bsd.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
// limitations under the License.

//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package numcpus

13 changes: 6 additions & 7 deletions numcpus_linux.go
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
package numcpus

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -35,7 +34,7 @@ func getFromCPUAffinity() (int, error) {
}

func readCPURange(file string) (int, error) {
buf, err := ioutil.ReadFile(filepath.Join(sysfsCPUBasePath, file))
buf, err := os.ReadFile(filepath.Join(sysfsCPUBasePath, file))
if err != nil {
return 0, err
}
@@ -48,16 +47,16 @@ func parseCPURange(cpus string) (int, error) {
if len(cpuRange) == 0 {
continue
}
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
}
@@ -89,7 +88,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
}
46 changes: 24 additions & 22 deletions numcpus_linux_test.go
Original file line number Diff line number Diff line change
@@ -18,37 +18,39 @@ import "testing"

func TestParseCPURange(t *testing.T) {
testCases := []struct {
cpus string
n int
str string
n int
wantErr bool
}{
{"", 0},
{"0", 1},
{"0-1", 2},
{"0-7", 8},
{"1-7", 7},
{"1-15", 15},
{"0-3,7", 5},
{"0,2-4", 4},
{"0,2-4,7", 5},
{"0,2-4,7-15", 13},
{str: "", n: 0},
{str: "0", n: 1},
{str: "0-1", n: 2},
{str: "0-7", n: 8},
{str: "1-7", n: 7},
{str: "1-15", n: 15},
{str: "0-3,7", n: 5},
{str: "0,2-4", n: 4},
{str: "0,2-4,7", n: 5},
{str: "0,2-4,7-15", n: 13},
{str: "0,2-4,6,8-10", n: 8},
{str: "invalid", n: 0, wantErr: true},
{str: "0-", n: 0, wantErr: true},
{str: "0-,1", n: 0, wantErr: true},
{str: "0,-3,5", n: 0, wantErr: true},
}

for _, tc := range testCases {
n, err := parseCPURange(tc.cpus)
if err != nil {
t.Errorf("failed to parse CPU range: %v", err)
n, err := parseCPURange(tc.str)
if !tc.wantErr && err != nil {
t.Errorf("parseCPURange(%q) = %v, expected no error", tc.str, err)
} else if tc.wantErr && err == nil {
t.Errorf("parseCPURange(%q) expected error", tc.str)
}

if n != tc.n {
t.Errorf("parseCPURange(%q) = %d, expected %d", tc.cpus, n, tc.n)
t.Errorf("parseCPURange(%q) = %d, expected %d", tc.str, n, tc.n)
}
}

str := "invalid"
_, err := parseCPURange(str)
if err == nil {
t.Errorf("parseCPURange(%q) unexpectedly succeeded", str)
}
}

func TestGetFromCPUAffinity(t *testing.T) {
1 change: 0 additions & 1 deletion numcpus_solaris.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
// limitations under the License.

//go:build solaris
// +build solaris

package numcpus

1 change: 0 additions & 1 deletion numcpus_unsupported.go
Original file line number Diff line number Diff line change
@@ -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