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: shirou/gopsutil
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.20.6
Choose a base ref
...
head repository: shirou/gopsutil
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.20.9
Choose a head ref
Loading
Showing with 1,191 additions and 891 deletions.
  1. +2 −2 Gopkg.lock
  2. +3 −1 cpu/cpu.go
  3. +18 −1 cpu/cpu_linux.go
  4. +53 −0 cpu/cpu_linux_test.go
  5. +4 −13 cpu/cpu_openbsd.go
  6. +27 −1 cpu/cpu_test.go
  7. +3 −1 disk/disk_openbsd_386.go
  8. +11 −0 disk/disk_test.go
  9. +100 −0 host/host.go
  10. +36 −0 host/host_bsd.go
  11. +6 −108 host/host_darwin.go
  12. +22 −0 host/host_darwin_arm64.go
  13. +0 −4 host/host_darwin_cgo.go
  14. +0 −4 host/host_darwin_nocgo.go
  15. +12 −28 host/host_fallback.go
  16. +8 −108 host/host_freebsd.go
  17. +10 −89 host/host_linux.go
  18. +6 −105 host/host_openbsd.go
  19. +33 −0 host/host_openbsd_386.go
  20. +1 −1 host/host_posix.go
  21. +68 −137 host/host_solaris.go
  22. +20 −2 host/host_test.go
  23. +13 −98 host/host_windows.go
  24. +2 −2 internal/common/common_darwin.go
  25. +14 −0 internal/common/common_linux.go
  26. +18 −0 internal/common/sleep.go
  27. +28 −0 internal/common/sleep_test.go
  28. +10 −0 load/load_test.go
  29. +4 −0 mem/mem.go
  30. +6 −0 mem/mem_linux.go
  31. +6 −25 mem/mem_openbsd.go
  32. +37 −0 mem/mem_openbsd_386.go
  33. +0 −90 mem/mem_openbsd_amd64.go
  34. +13 −3 mem/mem_test.go
  35. +1 −0 mem/mem_windows.go
  36. +0 −6 mem/types_openbsd.go
  37. +11 −35 mktypes.sh
  38. +1 −1 net/net_linux.go
  39. +23 −4 net/net_linux_test.go
  40. +15 −0 net/net_test.go
  41. +2 −3 net/net_windows.go
  42. +9 −1 process/process.go
  43. +15 −1 process/process_darwin.go
  44. +205 −0 process/process_darwin_arm64.go
  45. +4 −0 process/process_fallback.go
  46. +14 −0 process/process_freebsd.go
  47. +23 −4 process/process_linux.go
  48. +8 −0 process/process_openbsd.go
  49. +201 −0 process/process_openbsd_386.go
  50. +17 −0 process/process_test.go
  51. +47 −13 process/process_windows.go
  52. +1 −0 process/types_darwin.go
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion cpu/cpu.go
Original file line number Diff line number Diff line change
@@ -149,7 +149,9 @@ func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool
return nil, err
}

time.Sleep(interval)
if err := common.Sleep(ctx, interval); err != nil {
return nil, err
}

// And at the end of the interval.
cpuTimes2, err := Times(percpu)
19 changes: 18 additions & 1 deletion cpu/cpu_linux.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"

@@ -311,7 +312,23 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
}
return ret, nil
}
// physical cores https://github.com/giampaolo/psutil/blob/d01a9eaa35a8aadf6c519839e987a49d8be2d891/psutil/_pslinux.py#L628
// physical cores
// https://github.com/giampaolo/psutil/blob/122174a10b75c9beebe15f6c07dcf3afbe3b120d/psutil/_pslinux.py#L621-L629
var threadSiblingsLists = make(map[string]bool)
if files, err := filepath.Glob(common.HostSys("devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list")); err == nil {
for _, file := range files {
lines, err := common.ReadLines(file)
if err != nil || len(lines) != 1 {
continue
}
threadSiblingsLists[lines[0]] = true
}
ret := len(threadSiblingsLists)
if ret != 0 {
return ret, nil
}
}
// https://github.com/giampaolo/psutil/blob/122174a10b75c9beebe15f6c07dcf3afbe3b120d/psutil/_pslinux.py#L631-L652
filename := common.HostProc("cpuinfo")
lines, err := common.ReadLines(filename)
if err != nil {
53 changes: 53 additions & 0 deletions cpu/cpu_linux_test.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@ package cpu

import (
"os"
"os/exec"
"strconv"
"strings"
"testing"
)

@@ -38,3 +41,53 @@ func TestCPUparseStatLine_424(t *testing.T) {
}
os.Setenv("HOST_PROC", orig)
}

func TestCPUCountsAgainstLscpu(t *testing.T) {
lscpu, err := exec.LookPath("lscpu")
if err != nil {
t.Skip("no lscpu to compare with")
}
cmd := exec.Command(lscpu)
cmd.Env = []string{"LC_ALL=C"}
out, err := cmd.Output()
if err != nil {
t.Errorf("error executing lscpu: %v", err)
}
var threadsPerCore, coresPerSocket, sockets int
lines := strings.Split(string(out), "\n")
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2 {
continue
}
switch fields[0] {
case "Thread(s) per core":
threadsPerCore, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Core(s) per socket":
coresPerSocket, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
case "Socket(s)":
sockets, _ = strconv.Atoi(strings.TrimSpace(fields[1]))
}
}
if threadsPerCore == 0 || coresPerSocket == 0 || sockets == 0 {
t.Errorf("missing info from lscpu: threadsPerCore=%d coresPerSocket=%d sockets=%d", threadsPerCore, coresPerSocket, sockets)
}
expectedPhysical := coresPerSocket * sockets
expectedLogical := expectedPhysical * threadsPerCore
physical, err := Counts(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
logical, err := Counts(true)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if expectedPhysical != physical {
t.Errorf("expected %v, got %v", expectedPhysical, physical)
}
if expectedLogical != logical {
t.Errorf("expected %v, got %v", expectedLogical, logical)
}
}
17 changes: 4 additions & 13 deletions cpu/cpu_openbsd.go
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ const (
CTLKern = 1 // "high kernel": proc, limits
CTLHw = 6 // CTL_HW
SMT = 24 // HW_SMT
NCpuOnline = 25 // HW_NCPUONLINE
KernCptime = 40 // KERN_CPTIME
KernCptime2 = 71 // KERN_CPTIME2
)
@@ -163,25 +162,17 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {

c := InfoStat{}

var u32 uint32
if u32, err = unix.SysctlUint32("hw.cpuspeed"); err != nil {
return nil, err
}
c.Mhz = float64(u32)

mib := []int32{CTLHw, NCpuOnline}
buf, _, err := common.CallSyscall(mib)
mhz, err := unix.SysctlUint32("hw.cpuspeed")
if err != nil {
return nil, err
}
c.Mhz = float64(mhz)

var ncpu int32
br := bytes.NewReader(buf)
err = binary.Read(br, binary.LittleEndian, &ncpu)
ncpu, err := unix.SysctlUint32("hw.ncpuonline")
if err != nil {
return nil, err
}
c.Cores = ncpu
c.Cores = int32(ncpu)

if c.ModelName, err = unix.Sysctl("hw.model"); err != nil {
return nil, err
28 changes: 27 additions & 1 deletion cpu/cpu_test.go
Original file line number Diff line number Diff line change
@@ -7,11 +7,19 @@ import (
"testing"
"time"

"github.com/shirou/gopsutil/internal/common"
"github.com/stretchr/testify/assert"
)

func skipIfNotImplementedErr(t *testing.T, err error) {
if err == common.ErrNotImplementedError {
t.Skip("not implemented")
}
}

func TestCpu_times(t *testing.T) {
v, err := Times(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
@@ -27,13 +35,15 @@ func TestCpu_times(t *testing.T) {

// test sum of per cpu stats is within margin of error for cpu total stats
cpuTotal, err := Times(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if len(cpuTotal) == 0 {
t.Error("could not get CPUs ", err)
}
perCPU, err := Times(true)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
@@ -56,12 +66,23 @@ func TestCpu_times(t *testing.T) {

func TestCpu_counts(t *testing.T) {
v, err := Counts(true)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("could not get logical CPU counts: %v", v)
}
t.Logf("logical cores: %d", v)
v, err = Counts(false)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if v == 0 {
t.Errorf("could not get CPU counts: %v", v)
t.Errorf("could not get physical CPU counts: %v", v)
}
t.Logf("physical cores: %d", v)
}

func TestCPUTimeStat_String(t *testing.T) {
@@ -79,6 +100,7 @@ func TestCPUTimeStat_String(t *testing.T) {

func TestCpuInfo(t *testing.T) {
v, err := Info()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
@@ -99,6 +121,7 @@ func testCPUPercent(t *testing.T, percpu bool) {
if runtime.GOOS != "windows" {
testCount = 100
v, err := Percent(time.Millisecond, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
@@ -112,6 +135,7 @@ func testCPUPercent(t *testing.T, percpu bool) {
for i := 0; i < testCount; i++ {
duration := time.Duration(10) * time.Microsecond
v, err := Percent(duration, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
@@ -132,6 +156,7 @@ func testCPUPercentLastUsed(t *testing.T, percpu bool) {
if runtime.GOOS != "windows" {
testCount = 2
v, err := Percent(time.Millisecond, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
@@ -144,6 +169,7 @@ func testCPUPercentLastUsed(t *testing.T, percpu bool) {
}
for i := 0; i < testCount; i++ {
v, err := Percent(0, percpu)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
4 changes: 3 additions & 1 deletion disk/disk_openbsd_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions disk/disk_test.go
Original file line number Diff line number Diff line change
@@ -5,14 +5,23 @@ import (
"runtime"
"sync"
"testing"

"github.com/shirou/gopsutil/internal/common"
)

func skipIfNotImplementedErr(t *testing.T, err error) {
if err == common.ErrNotImplementedError {
t.Skip("not implemented")
}
}

func TestDisk_usage(t *testing.T) {
path := "/"
if runtime.GOOS == "windows" {
path = "C:"
}
v, err := Usage(path)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
@@ -23,6 +32,7 @@ func TestDisk_usage(t *testing.T) {

func TestDisk_partitions(t *testing.T) {
ret, err := Partitions(false)
skipIfNotImplementedErr(t, err)
if err != nil || len(ret) == 0 {
t.Errorf("error %v", err)
}
@@ -41,6 +51,7 @@ func TestDisk_partitions(t *testing.T) {

func TestDisk_io_counters(t *testing.T) {
ret, err := IOCounters()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
Loading