Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build-and-test:
strategy:
matrix:
go-version: ['1.19', '1.20', '1.21']
go-version: ['1.18', '1.20', '1.21']
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion 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.15.0
13 changes: 6 additions & 7 deletions numcpus_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package numcpus

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
46 changes: 24 additions & 22 deletions numcpus_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down