Skip to content

Commit f68a784

Browse files
committed
Merge remote-tracking branch 'upstream/master' into fancybits
2 parents 5c3aafc + b51f72e commit f68a784

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+731
-224
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ build_test: ## test only buildable
1717
GOOS=linux GOARCH=386 go test ./... | $(BUILD_FAIL_PATTERN)
1818
GOOS=linux GOARCH=arm go test ./... | $(BUILD_FAIL_PATTERN)
1919
GOOS=linux GOARCH=arm64 go test ./... | $(BUILD_FAIL_PATTERN)
20+
GOOS=linux GOARCH=loong64 go test ./... | $(BUILD_FAIL_PATTERN)
2021
GOOS=linux GOARCH=riscv64 go test ./... | $(BUILD_FAIL_PATTERN)
22+
GOOS=linux GOARCH=s390x go test ./... | $(BUILD_FAIL_PATTERN)
2123
GOOS=freebsd GOARCH=amd64 go test ./... | $(BUILD_FAIL_PATTERN)
2224
GOOS=freebsd GOARCH=386 go test ./... | $(BUILD_FAIL_PATTERN)
2325
GOOS=freebsd GOARCH=arm go test ./... | $(BUILD_FAIL_PATTERN)
@@ -51,11 +53,13 @@ vet:
5153
GOOS=linux GOARCH=amd64 go vet ./...
5254
GOOS=linux GOARCH=arm64 go vet ./...
5355
GOOS=linux GOARCH=arm go vet ./...
56+
GOOS=linux GOARCH=loong64 go vet ./...
5457
GOOS=linux GOARCH=mips64 go vet ./...
5558
GOOS=linux GOARCH=mips64le go vet ./...
5659
GOOS=linux GOARCH=mips go vet ./...
5760
GOOS=linux GOARCH=mipsle go vet ./...
5861
GOOS=linux GOARCH=ppc64le go vet ./...
62+
GOOS=linux GOARCH=ppc64 go vet ./...
5963
GOOS=linux GOARCH=riscv64 go vet ./...
6064
GOOS=linux GOARCH=s390x go vet ./...
6165

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ environment variable.
9090
You can set an alternative location to `/dev` by setting the `HOST_DEV`
9191
environment variable.
9292

93+
You can set an alternative location to `/` by setting the `HOST_ROOT`
94+
environment variable.
95+
9396
You can set an alternative location to `/proc/N/mountinfo` by setting the
9497
`HOST_PROC_MOUNTINFO` environment variable.
9598

cpu/cpu_aix_nocgo.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package cpu
66
import (
77
"context"
88
"regexp"
9-
"strings"
109
"strconv"
10+
"strings"
1111

1212
"github.com/shirou/gopsutil/v3/internal/common"
1313
)
@@ -28,19 +28,19 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
2828
}
2929

3030
ret := TimesStat{CPU: "cpu-total"}
31-
h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers
32-
v := whiteSpaces.Split(lines[len(lines)-2], -1) // values
31+
h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers
32+
v := whiteSpaces.Split(lines[len(lines)-2], -1) // values
3333
for i, header := range h {
3434
if t, err := strconv.ParseFloat(v[i], 64); err == nil {
3535
switch header {
36-
case `%usr`:
37-
ret.User = t
38-
case `%sys`:
39-
ret.System = t
40-
case `%wio`:
41-
ret.Iowait = t
42-
case `%idle`:
43-
ret.Idle = t
36+
case `%usr`:
37+
ret.User = t
38+
case `%sys`:
39+
ret.System = t
40+
case `%wio`:
41+
ret.Iowait = t
42+
case `%idle`:
43+
ret.Idle = t
4444
}
4545
}
4646
}

cpu/cpu_linux.go

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,71 @@ import (
1717

1818
var ClocksPerSec = float64(100)
1919

20+
var armModelToModelName = map[uint64]string{
21+
0x810: "ARM810",
22+
0x920: "ARM920",
23+
0x922: "ARM922",
24+
0x926: "ARM926",
25+
0x940: "ARM940",
26+
0x946: "ARM946",
27+
0x966: "ARM966",
28+
0xa20: "ARM1020",
29+
0xa22: "ARM1022",
30+
0xa26: "ARM1026",
31+
0xb02: "ARM11 MPCore",
32+
0xb36: "ARM1136",
33+
0xb56: "ARM1156",
34+
0xb76: "ARM1176",
35+
0xc05: "Cortex-A5",
36+
0xc07: "Cortex-A7",
37+
0xc08: "Cortex-A8",
38+
0xc09: "Cortex-A9",
39+
0xc0d: "Cortex-A17",
40+
0xc0f: "Cortex-A15",
41+
0xc0e: "Cortex-A17",
42+
0xc14: "Cortex-R4",
43+
0xc15: "Cortex-R5",
44+
0xc17: "Cortex-R7",
45+
0xc18: "Cortex-R8",
46+
0xc20: "Cortex-M0",
47+
0xc21: "Cortex-M1",
48+
0xc23: "Cortex-M3",
49+
0xc24: "Cortex-M4",
50+
0xc27: "Cortex-M7",
51+
0xc60: "Cortex-M0+",
52+
0xd01: "Cortex-A32",
53+
0xd02: "Cortex-A34",
54+
0xd03: "Cortex-A53",
55+
0xd04: "Cortex-A35",
56+
0xd05: "Cortex-A55",
57+
0xd06: "Cortex-A65",
58+
0xd07: "Cortex-A57",
59+
0xd08: "Cortex-A72",
60+
0xd09: "Cortex-A73",
61+
0xd0a: "Cortex-A75",
62+
0xd0b: "Cortex-A76",
63+
0xd0c: "Neoverse-N1",
64+
0xd0d: "Cortex-A77",
65+
0xd0e: "Cortex-A76AE",
66+
0xd13: "Cortex-R52",
67+
0xd20: "Cortex-M23",
68+
0xd21: "Cortex-M33",
69+
0xd40: "Neoverse-V1",
70+
0xd41: "Cortex-A78",
71+
0xd42: "Cortex-A78AE",
72+
0xd43: "Cortex-A65AE",
73+
0xd44: "Cortex-X1",
74+
0xd46: "Cortex-A510",
75+
0xd47: "Cortex-A710",
76+
0xd48: "Cortex-X2",
77+
0xd49: "Neoverse-N2",
78+
0xd4a: "Neoverse-E1",
79+
0xd4b: "Cortex-A78C",
80+
0xd4c: "Cortex-X1C",
81+
0xd4d: "Cortex-A715",
82+
0xd4e: "Cortex-X3",
83+
}
84+
2085
func init() {
2186
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
2287
// ignore errors
@@ -125,7 +190,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
125190
switch key {
126191
case "Processor":
127192
processorName = value
128-
case "processor":
193+
case "processor", "cpu number":
129194
if c.CPU >= 0 {
130195
finishCPUInfo(&c)
131196
ret = append(ret, c)
@@ -138,6 +203,9 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
138203
c.CPU = int32(t)
139204
case "vendorId", "vendor_id":
140205
c.VendorID = value
206+
if strings.Contains(value, "S390") {
207+
processorName = "S390"
208+
}
141209
case "CPU implementer":
142210
if v, err := strconv.ParseUint(value, 0, 8); err == nil {
143211
switch v {
@@ -177,7 +245,18 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
177245
c.Family = value
178246
case "model", "CPU part":
179247
c.Model = value
180-
case "model name", "cpu":
248+
// if CPU is arm based, model name is found via model number. refer to: arch/arm64/kernel/cpuinfo.c
249+
if c.VendorID == "ARM" {
250+
if v, err := strconv.ParseUint(c.Model, 0, 16); err == nil {
251+
modelName, exist := armModelToModelName[v]
252+
if exist {
253+
c.ModelName = modelName
254+
} else {
255+
c.ModelName = "Undefined"
256+
}
257+
}
258+
}
259+
case "Model Name", "model name", "cpu":
181260
c.ModelName = value
182261
if strings.Contains(value, "POWER8") ||
183262
strings.Contains(value, "POWER7") {
@@ -197,7 +276,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
197276
return ret, err
198277
}
199278
c.Stepping = int32(t)
200-
case "cpu MHz", "clock":
279+
case "cpu MHz", "clock", "cpu MHz dynamic":
201280
// treat this as the fallback value, thus we ignore error
202281
if t, err := strconv.ParseFloat(strings.Replace(value, "MHz", "", 1), 64); err == nil {
203282
c.Mhz = t

cpu/cpu_linux_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ package cpu
22

33
import (
44
"errors"
5-
"os"
65
"os/exec"
76
"strconv"
87
"strings"
98
"testing"
109
)
1110

1211
func TestTimesEmpty(t *testing.T) {
13-
orig := os.Getenv("HOST_PROC")
14-
os.Setenv("HOST_PROC", "testdata/linux/times_empty")
12+
t.Setenv("HOST_PROC", "testdata/linux/times_empty")
1513
_, err := Times(true)
1614
if err != nil {
1715
t.Error("Times(true) failed")
@@ -20,12 +18,10 @@ func TestTimesEmpty(t *testing.T) {
2018
if err != nil {
2119
t.Error("Times(false) failed")
2220
}
23-
os.Setenv("HOST_PROC", orig)
2421
}
2522

2623
func TestCPUparseStatLine_424(t *testing.T) {
27-
orig := os.Getenv("HOST_PROC")
28-
os.Setenv("HOST_PROC", "testdata/linux/424/proc")
24+
t.Setenv("HOST_PROC", "testdata/linux/424/proc")
2925
{
3026
l, err := Times(true)
3127
if err != nil || len(l) == 0 {
@@ -40,7 +36,6 @@ func TestCPUparseStatLine_424(t *testing.T) {
4036
}
4137
t.Logf("Times(false): %#v", l)
4238
}
43-
os.Setenv("HOST_PROC", orig)
4439
}
4540

4641
func TestCPUCountsAgainstLscpu(t *testing.T) {
@@ -53,7 +48,9 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
5348
}
5449
t.Errorf("error executing lscpu: %v", err)
5550
}
56-
var threadsPerCore, coresPerSocket, sockets int
51+
var threadsPerCore, coresPerSocket, sockets, books, drawers int
52+
books = 1
53+
drawers = 1
5754
lines := strings.Split(string(out), "\n")
5855
for _, line := range lines {
5956
fields := strings.Split(line, ":")
@@ -65,14 +62,18 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
6562
threadsPerCore, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
6663
case "Core(s) per socket":
6764
coresPerSocket, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
68-
case "Socket(s)":
65+
case "Socket(s)", "Socket(s) per book":
6966
sockets, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
67+
case "Book(s) per drawer":
68+
books, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
69+
case "Drawer(s)":
70+
drawers, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
7071
}
7172
}
7273
if threadsPerCore == 0 || coresPerSocket == 0 || sockets == 0 {
7374
t.Errorf("missing info from lscpu: threadsPerCore=%d coresPerSocket=%d sockets=%d", threadsPerCore, coresPerSocket, sockets)
7475
}
75-
expectedPhysical := coresPerSocket * sockets
76+
expectedPhysical := coresPerSocket * sockets * books * drawers
7677
expectedLogical := expectedPhysical * threadsPerCore
7778
physical, err := Counts(false)
7879
skipIfNotImplementedErr(t, err)
@@ -93,9 +94,7 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
9394
}
9495

9596
func TestCPUCountsLogicalAndroid_1037(t *testing.T) { // https://github.com/shirou/gopsutil/issues/1037
96-
orig := os.Getenv("HOST_PROC")
97-
os.Setenv("HOST_PROC", "testdata/linux/1037/proc")
98-
defer os.Setenv("HOST_PROC", orig)
97+
t.Setenv("HOST_PROC", "testdata/linux/1037/proc")
9998

10099
count, err := Counts(true)
101100
if err != nil {

cpu/cpu_plan9_test.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package cpu
55

66
import (
7-
"os"
87
"path/filepath"
98
"testing"
109

@@ -30,13 +29,9 @@ var timesTests = []struct {
3029
}
3130

3231
func TestTimesPlan9(t *testing.T) {
33-
origRoot := os.Getenv("HOST_ROOT")
34-
t.Cleanup(func() {
35-
os.Setenv("HOST_ROOT", origRoot)
36-
})
3732
for _, tt := range timesTests {
3833
t.Run(tt.mockedRootFS, func(t *testing.T) {
39-
os.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
34+
t.Setenv("HOST_ROOT", filepath.Join("testdata/plan9", tt.mockedRootFS))
4035
stats, err := Times(false)
4136
skipIfNotImplementedErr(t, err)
4237
if err != nil {

cpu/cpu_windows.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414
)
1515

1616
var (
17-
procGetActiveProcessorCount = common.Modkernel32.NewProc("GetActiveProcessorCount")
18-
procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")
17+
procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")
1918
)
2019

2120
type win32_Processor struct {
@@ -204,15 +203,12 @@ type systemInfo struct {
204203
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
205204
if logical {
206205
// https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_psutil_windows.c#L97
207-
err := procGetActiveProcessorCount.Find()
208-
if err == nil { // Win7+
209-
ret, _, _ := procGetActiveProcessorCount.Call(uintptr(0xffff)) // ALL_PROCESSOR_GROUPS is 0xffff according to Rust's winapi lib https://docs.rs/winapi/*/x86_64-pc-windows-msvc/src/winapi/shared/ntdef.rs.html#120
210-
if ret != 0 {
211-
return int(ret), nil
212-
}
206+
ret := windows.GetActiveProcessorCount(windows.ALL_PROCESSOR_GROUPS)
207+
if ret != 0 {
208+
return int(ret), nil
213209
}
214210
var systemInfo systemInfo
215-
_, _, err = procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo)))
211+
_, _, err := procGetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&systemInfo)))
216212
if systemInfo.dwNumberOfProcessors == 0 {
217213
return 0, err
218214
}

disk/disk_aix_nocgo.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import (
88
"regexp"
99
"strings"
1010

11-
"golang.org/x/sys/unix"
1211
"github.com/shirou/gopsutil/v3/internal/common"
12+
"golang.org/x/sys/unix"
1313
)
1414

1515
var whiteSpaces = regexp.MustCompile(`\s+`)
1616
var startBlank = regexp.MustCompile(`^\s+`)
1717

1818
var ignoreFSType = map[string]bool{"procfs": true}
1919
var FSType = map[int]string{
20-
0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc",
21-
16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs",
22-
33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs",
23-
39: "ahafs", 40: "sterm-nfs", 41: "asmfs",
24-
}
20+
0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc",
21+
16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs",
22+
33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs",
23+
39: "ahafs", 40: "sterm-nfs", 41: "asmfs",
24+
}
2525

2626
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
2727
var ret []PartitionStat
@@ -42,7 +42,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
4242
start := 0
4343
finished := false
4444
for pos, ch := range lines[1] {
45-
if ch == ' ' && ! finished {
45+
if ch == ' ' && !finished {
4646
name := strings.TrimSpace(lines[0][start:pos])
4747
colidx[name] = idx
4848
finished = true

disk/disk_darwin.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
2020
return ret, err
2121
}
2222
fs := make([]unix.Statfs_t, count)
23-
if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
23+
count, err = unix.Getfsstat(fs, unix.MNT_WAIT)
24+
if err != nil {
2425
return ret, err
2526
}
27+
// On 10.14, and possibly other OS versions, the actual count may
28+
// be less than from the first call. Truncate to the returned count
29+
// to prevent accessing uninitialized entries.
30+
// https://github.com/shirou/gopsutil/issues/1390
31+
fs = fs[:count]
2632
for _, stat := range fs {
2733
opts := []string{"rw"}
2834
if stat.Flags&unix.MNT_RDONLY != 0 {

disk/disk_freebsd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import (
1212
"strconv"
1313
"strings"
1414

15-
"golang.org/x/sys/unix"
16-
1715
"github.com/shirou/gopsutil/v3/internal/common"
16+
"golang.org/x/sys/unix"
1817
)
1918

2019
// PartitionsWithContext returns disk partition.

0 commit comments

Comments
 (0)