Compare commits

...

2 Commits

Author SHA1 Message Date
shirou edcde3adba
Merge pull request #1797 from mmorel-35/golangci-lint-matrix
chore(golangci-lint): GOOS and GOARCH matrix
3 weeks ago
Matthieu MOREL 30aff030d0 chore(golangci-lint): GOOS and GOARCH matrix
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
3 weeks ago

@ -12,12 +12,41 @@ jobs:
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
os: [ubuntu-latest, windows-latest, macos-latest] include:
- {os: macos-latest, GOOS: darwin, GOARCH: amd64}
- {os: macos-latest, GOOS: darwin, GOARCH: arm64}
- {os: ubuntu-latest, GOOS: dragonfly, GOARCH: amd64}
- {os: ubuntu-latest, GOOS: freebsd, GOARCH: amd64}
- {os: ubuntu-latest, GOOS: freebsd, GOARCH: 386}
- {os: ubuntu-latest, GOOS: freebsd, GOARCH: arm}
- {os: ubuntu-latest, GOOS: linux, GOARCH: 386}
- {os: ubuntu-latest, GOOS: linux, GOARCH: amd64}
- {os: ubuntu-latest, GOOS: linux, GOARCH: arm64}
- {os: ubuntu-latest, GOOS: linux, GOARCH: arm}
- {os: ubuntu-latest, GOOS: linux, GOARCH: mips64}
- {os: ubuntu-latest, GOOS: linux, GOARCH: mips64le}
- {os: ubuntu-latest, GOOS: linux, GOARCH: mips}
- {os: ubuntu-latest, GOOS: linux, GOARCH: mipsle}
- {os: ubuntu-latest, GOOS: linux, GOARCH: ppc64le}
- {os: ubuntu-latest, GOOS: linux, GOARCH: ppc64}
- {os: ubuntu-latest, GOOS: linux, GOARCH: riscv64}
- {os: ubuntu-latest, GOOS: linux, GOARCH: s390x}
- {os: ubuntu-latest, GOOS: netbsd, GOARCH: amd64}
- {os: ubuntu-latest, GOOS: openbsd, GOARCH: 386}
- {os: ubuntu-latest, GOOS: openbsd, GOARCH: amd64}
- {os: ubuntu-latest, GOOS: plan9, GOARCH: amd64}
- {os: ubuntu-latest, GOOS: plan9, GOARCH: 386}
- {os: ubuntu-latest, GOOS: solaris, GOARCH: amd64}
- {os: windows-latest, GOOS: windows, GOARCH: amd64}
- {os: windows-latest, GOOS: windows, GOARCH: 386}
permissions: permissions:
contents: read # for actions/checkout to fetch code contents: read # for actions/checkout to fetch code
pull-requests: read # for golangci/golangci-lint-action to fetch pull requests pull-requests: read # for golangci/golangci-lint-action to fetch pull requests
name: lint name: lint
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
env:
GOARCH: ${{ matrix.GOARCH }}
GOOS: ${{ matrix.GOOS }}
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

@ -11,9 +11,10 @@ import (
"strings" "strings"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/tklauser/go-sysconf" "github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
var ( var (
@ -135,7 +136,7 @@ func parseDmesgBoot(fileName string) (InfoStat, error) {
c.VendorID = matches[1] c.VendorID = matches[1]
t, err := strconv.ParseInt(matches[2], 10, 32) t, err := strconv.ParseInt(matches[2], 10, 32)
if err != nil { if err != nil {
return c, fmt.Errorf("unable to parse DragonflyBSD CPU stepping information from %q: %v", line, err) return c, fmt.Errorf("unable to parse DragonflyBSD CPU stepping information from %q: %w", line, err)
} }
c.Stepping = int32(t) c.Stepping = int32(t)
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil { } else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {

@ -57,7 +57,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
ncpu, err := unix.SysctlUint32("hw.ncpu") ncpu, err := unix.SysctlUint32("hw.ncpu")
if err != nil { if err != nil {
return return //nolint:nakedret //FIXME
} }
var i uint32 var i uint32

@ -9,9 +9,10 @@ import (
"runtime" "runtime"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/tklauser/go-sysconf" "github.com/tklauser/go-sysconf"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
const ( const (
@ -74,7 +75,7 @@ func TimesWithContext(ctx context.Context, percpu bool) (ret []TimesStat, err er
ncpu, err := unix.SysctlUint32("hw.ncpu") ncpu, err := unix.SysctlUint32("hw.ncpu")
if err != nil { if err != nil {
return return //nolint:nakedret //FIXME
} }
var i uint32 var i uint32

@ -9,6 +9,7 @@ import (
"runtime" "runtime"
stats "github.com/lufia/plan9stats" stats "github.com/lufia/plan9stats"
"github.com/shirou/gopsutil/v4/internal/common" "github.com/shirou/gopsutil/v4/internal/common"
) )

@ -42,7 +42,7 @@ var kstatSplit = regexp.MustCompile(`[:\s]+`)
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/") kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot execute kstat: %s", err) return nil, fmt.Errorf("cannot execute kstat: %w", err)
} }
cpu := make(map[float64]float64) cpu := make(map[float64]float64)
idle := make(map[float64]float64) idle := make(map[float64]float64)
@ -57,29 +57,29 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
} }
cpuNumber, err := strconv.ParseFloat(fields[1], 64) cpuNumber, err := strconv.ParseFloat(fields[1], 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse cpu number: %s", err) return nil, fmt.Errorf("cannot parse cpu number: %w", err)
} }
cpu[cpuNumber] = cpuNumber cpu[cpuNumber] = cpuNumber
switch fields[3] { switch fields[3] {
case "idle": case "idle":
idle[cpuNumber], err = strconv.ParseFloat(fields[4], 64) idle[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse idle: %s", err) return nil, fmt.Errorf("cannot parse idle: %w", err)
} }
case "user": case "user":
user[cpuNumber], err = strconv.ParseFloat(fields[4], 64) user[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse user: %s", err) return nil, fmt.Errorf("cannot parse user: %w", err)
} }
case "kernel": case "kernel":
kern[cpuNumber], err = strconv.ParseFloat(fields[4], 64) kern[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse kernel: %s", err) return nil, fmt.Errorf("cannot parse kernel: %w", err)
} }
case "iowait": case "iowait":
iowt[cpuNumber], err = strconv.ParseFloat(fields[4], 64) iowt[cpuNumber], err = strconv.ParseFloat(fields[4], 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse iowait: %s", err) return nil, fmt.Errorf("cannot parse iowait: %w", err)
} }
//not sure how this translates, don't report, add to kernel, something else? //not sure how this translates, don't report, add to kernel, something else?
/*case "swap": /*case "swap":
@ -121,22 +121,22 @@ func Info() ([]InfoStat, error) {
func InfoWithContext(ctx context.Context) ([]InfoStat, error) { func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
psrInfoOut, err := invoke.CommandWithContext(ctx, "psrinfo", "-p", "-v") psrInfoOut, err := invoke.CommandWithContext(ctx, "psrinfo", "-p", "-v")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot execute psrinfo: %s", err) return nil, fmt.Errorf("cannot execute psrinfo: %w", err)
} }
procs, err := parseProcessorInfo(string(psrInfoOut)) procs, err := parseProcessorInfo(string(psrInfoOut))
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing psrinfo output: %s", err) return nil, fmt.Errorf("error parsing psrinfo output: %w", err)
} }
isaInfoOut, err := invoke.CommandWithContext(ctx, "isainfo", "-b", "-v") isaInfoOut, err := invoke.CommandWithContext(ctx, "isainfo", "-b", "-v")
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot execute isainfo: %s", err) return nil, fmt.Errorf("cannot execute isainfo: %w", err)
} }
flags, err := parseISAInfo(string(isaInfoOut)) flags, err := parseISAInfo(string(isaInfoOut))
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing isainfo output: %s", err) return nil, fmt.Errorf("error parsing isainfo output: %w", err)
} }
result := make([]InfoStat, 0, len(flags)) result := make([]InfoStat, 0, len(flags))
@ -160,7 +160,7 @@ func parseISAInfo(cmdOutput string) ([]string, error) {
} }
flags := make([]string, len(words)-4) flags := make([]string, len(words)-4)
for i, val := range words[4:] { for i, val := range words[4:] { //nolint:gosimple //FIXME
flags[i] = val flags[i] = val
} }
sort.Strings(flags) sort.Strings(flags)
@ -194,7 +194,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
if physicalCPU[psrStepOffset] != "" { if physicalCPU[psrStepOffset] != "" {
stepParsed, err := strconv.ParseInt(physicalCPU[psrStepOffset], 10, 32) stepParsed, err := strconv.ParseInt(physicalCPU[psrStepOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse value %q for step as 32-bit integer: %s", physicalCPU[9], err) return nil, fmt.Errorf("cannot parse value %q for step as 32-bit integer: %w", physicalCPU[9], err)
} }
step = int32(stepParsed) step = int32(stepParsed)
} }
@ -202,7 +202,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
if physicalCPU[psrClockOffset] != "" { if physicalCPU[psrClockOffset] != "" {
clockParsed, err := strconv.ParseInt(physicalCPU[psrClockOffset], 10, 64) clockParsed, err := strconv.ParseInt(physicalCPU[psrClockOffset], 10, 64)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse value %q for clock as 32-bit integer: %s", physicalCPU[10], err) return nil, fmt.Errorf("cannot parse value %q for clock as 32-bit integer: %w", physicalCPU[10], err)
} }
clock = float64(clockParsed) clock = float64(clockParsed)
} }
@ -214,7 +214,7 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
case physicalCPU[psrNumCoresOffset] != "": case physicalCPU[psrNumCoresOffset] != "":
numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresOffset], 10, 32) numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[1], err) return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %w", physicalCPU[1], err)
} }
for i := 0; i < int(numCores); i++ { for i := 0; i < int(numCores); i++ {
@ -235,12 +235,12 @@ func parseProcessorInfo(cmdOutput string) ([]InfoStat, error) {
case physicalCPU[psrNumCoresHTOffset] != "": case physicalCPU[psrNumCoresHTOffset] != "":
numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresHTOffset], 10, 32) numCores, err = strconv.ParseInt(physicalCPU[psrNumCoresHTOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %s", physicalCPU[3], err) return nil, fmt.Errorf("cannot parse value %q for core count as 32-bit integer: %w", physicalCPU[3], err)
} }
numHT, err = strconv.ParseInt(physicalCPU[psrNumHTOffset], 10, 32) numHT, err = strconv.ParseInt(physicalCPU[psrNumHTOffset], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot parse value %q for hyperthread count as 32-bit integer: %s", physicalCPU[4], err) return nil, fmt.Errorf("cannot parse value %q for hyperthread count as 32-bit integer: %w", physicalCPU[4], err)
} }
for i := 0; i < int(numCores); i++ { for i := 0; i < int(numCores); i++ {

@ -8,8 +8,9 @@ import (
"context" "context"
"encoding/binary" "encoding/binary"
"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {

@ -7,6 +7,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"math" "math"
"os" "os"
@ -16,8 +17,9 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
const ( const (
@ -100,7 +102,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
} }
lines := strings.Split(strings.TrimSpace(string(kstatSysOut)), "\n") lines := strings.Split(strings.TrimSpace(string(kstatSysOut)), "\n")
if len(lines) == 0 { if len(lines) == 0 {
return nil, fmt.Errorf("no disk class found") return nil, errors.New("no disk class found")
} }
dnamearr := make(map[string]string) dnamearr := make(map[string]string)
nreadarr := make(map[string]uint64) nreadarr := make(map[string]uint64)

@ -12,9 +12,10 @@ import (
"strings" "strings"
"unsafe" "unsafe"
"golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common" "github.com/shirou/gopsutil/v4/internal/common"
"github.com/shirou/gopsutil/v4/process" "github.com/shirou/gopsutil/v4/process"
"golang.org/x/sys/unix"
) )
const ( const (

@ -7,6 +7,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
@ -31,14 +32,13 @@ func HostIDWithContext(ctx context.Context) (string, error) {
line := sc.Text() line := sc.Text()
// If we're in the global zone, rely on the hostname. // If we're in the global zone, rely on the hostname.
if line == "global" { if line != "global" {
hostname, err := os.Hostname()
if err == nil {
return hostname, nil
}
} else {
return strings.TrimSpace(line), nil return strings.TrimSpace(line), nil
} }
hostname, err := os.Hostname()
if err == nil {
return hostname, nil
}
} }
} }
} }
@ -84,7 +84,7 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
} }
func UptimeWithContext(ctx context.Context) (uint64, error) { func UptimeWithContext(ctx context.Context) (uint64, error) {
bootTime, err := BootTime() bootTime, err := BootTime() //nolint:contextcheck //FIXME
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -139,7 +139,7 @@ func parseUnameOutput(ctx context.Context) (string, string, string, error) {
fields := strings.Fields(string(out)) fields := strings.Fields(string(out))
if len(fields) < 3 { if len(fields) < 3 {
return "", "", "", fmt.Errorf("malformed `uname` output") return "", "", "", errors.New("malformed `uname` output")
} }
return fields[0], fields[1], fields[2], nil return fields[0], fields[1], fields[2], nil

@ -7,6 +7,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
const validFreeBSD = `Device: 1kB-blocks Used: const validFreeBSD = `Device: 1kB-blocks Used:
@ -24,33 +25,31 @@ const invalid = `Device: 512-blocks Used:
` `
func TestParseSwapctlOutput_FreeBSD(t *testing.T) { func TestParseSwapctlOutput_FreeBSD(t *testing.T) {
assert := assert.New(t)
stats, err := parseSwapctlOutput(validFreeBSD) stats, err := parseSwapctlOutput(validFreeBSD)
assert.NoError(err) require.NoError(t, err)
assert.Equal(*stats[0], SwapDevice{ assert.Equal(t, SwapDevice{
Name: "/dev/gpt/swapfs", Name: "/dev/gpt/swapfs",
UsedBytes: 1263616, UsedBytes: 1263616,
FreeBytes: 1072478208, FreeBytes: 1072478208,
}) }, *stats[0])
assert.Equal(*stats[1], SwapDevice{ assert.Equal(t, SwapDevice{
Name: "/dev/md0", Name: "/dev/md0",
UsedBytes: 681984, UsedBytes: 681984,
FreeBytes: 1073059840, FreeBytes: 1073059840,
}) }, *stats[1])
} }
func TestParseSwapctlOutput_OpenBSD(t *testing.T) { func TestParseSwapctlOutput_OpenBSD(t *testing.T) {
assert := assert.New(t)
stats, err := parseSwapctlOutput(validOpenBSD) stats, err := parseSwapctlOutput(validOpenBSD)
assert.NoError(err) require.NoError(t, err)
assert.Equal(*stats[0], SwapDevice{ assert.Equal(t, SwapDevice{
Name: "/dev/wd0b", Name: "/dev/wd0b",
UsedBytes: 1234 * 1024, UsedBytes: 1234 * 1024,
FreeBytes: 653791 * 1024, FreeBytes: 653791 * 1024,
}) }, *stats[0])
} }
func TestParseSwapctlOutput_Invalid(t *testing.T) { func TestParseSwapctlOutput_Invalid(t *testing.T) {

@ -151,17 +151,16 @@ const invalidFile = `INVALID Type Size Used Priority
` `
func TestParseSwapsFile_ValidFile(t *testing.T) { func TestParseSwapsFile_ValidFile(t *testing.T) {
assert := assert.New(t)
stats, err := parseSwapsFile(context.Background(), strings.NewReader(validFile)) stats, err := parseSwapsFile(context.Background(), strings.NewReader(validFile))
require.NoError(t, err) require.NoError(t, err)
assert.Equal(SwapDevice{ assert.Equal(t, SwapDevice{
Name: "/dev/dm-2", Name: "/dev/dm-2",
UsedBytes: 502566912, UsedBytes: 502566912,
FreeBytes: 68128825344, FreeBytes: 68128825344,
}, *stats[0]) }, *stats[0])
assert.Equal(SwapDevice{ assert.Equal(t, SwapDevice{
Name: "/swapfile", Name: "/swapfile",
UsedBytes: 1024, UsedBytes: 1024,
FreeBytes: 1024, FreeBytes: 1024,

@ -10,8 +10,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/internal/common"
) )
func GetPageSize() (uint64, error) { func GetPageSize() (uint64, error) {

@ -8,6 +8,7 @@ import (
"os" "os"
stats "github.com/lufia/plan9stats" stats "github.com/lufia/plan9stats"
"github.com/shirou/gopsutil/v4/internal/common" "github.com/shirou/gopsutil/v4/internal/common"
) )

@ -11,8 +11,9 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/tklauser/go-sysconf" "github.com/tklauser/go-sysconf"
"github.com/shirou/gopsutil/v4/internal/common"
) )
// VirtualMemory for Solaris is a minimal implementation which only returns // VirtualMemory for Solaris is a minimal implementation which only returns
@ -24,17 +25,17 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
result := &VirtualMemoryStat{} result := &VirtualMemoryStat{}
zoneName, err := zoneName() zoneName, err := zoneName() //nolint:contextcheck //FIXME
if err != nil { if err != nil {
return nil, err return nil, err
} }
if zoneName == "global" { if zoneName == "global" {
cap, err := globalZoneMemoryCapacity() capacity, err := globalZoneMemoryCapacity() //nolint:contextcheck //FIXME
if err != nil { if err != nil {
return nil, err return nil, err
} }
result.Total = cap result.Total = capacity
freemem, err := globalZoneFreeMemory(ctx) freemem, err := globalZoneFreeMemory(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -43,11 +44,11 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
result.Free = freemem result.Free = freemem
result.Used = result.Total - result.Free result.Used = result.Total - result.Free
} else { } else {
cap, err := nonGlobalZoneMemoryCapacity() capacity, err := nonGlobalZoneMemoryCapacity() //nolint:contextcheck //FIXME
if err != nil { if err != nil {
return nil, err return nil, err
} }
result.Total = cap result.Total = capacity
} }
return result, nil return result, nil

@ -7,6 +7,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
const validFile = `swapfile dev swaplo blocks free const validFile = `swapfile dev swaplo blocks free
@ -18,21 +19,20 @@ const invalidFile = `swapfile dev swaplo INVALID free
/dev/dsk/c0t0d0s1 136,1 16 1638608 1600528` /dev/dsk/c0t0d0s1 136,1 16 1638608 1600528`
func TestParseSwapsCommandOutput_Valid(t *testing.T) { func TestParseSwapsCommandOutput_Valid(t *testing.T) {
assert := assert.New(t)
stats, err := parseSwapsCommandOutput(validFile) stats, err := parseSwapsCommandOutput(validFile)
assert.NoError(err) require.NoError(t, err)
assert.Equal(*stats[0], SwapDevice{ assert.Equal(t, SwapDevice{
Name: "/dev/zvol/dsk/rpool/swap", Name: "/dev/zvol/dsk/rpool/swap",
UsedBytes: 0, UsedBytes: 0,
FreeBytes: 1058800 * 512, FreeBytes: 1058800 * 512,
}) }, *stats[0])
assert.Equal(*stats[1], SwapDevice{ assert.Equal(t, SwapDevice{
Name: "/dev/dsk/c0t0d0s1", Name: "/dev/dsk/c0t0d0s1",
UsedBytes: 38080 * 512, UsedBytes: 38080 * 512,
FreeBytes: 1600528 * 512, FreeBytes: 1600528 * 512,
}) }, *stats[1])
} }
func TestParseSwapsCommandOutput_Invalid(t *testing.T) { func TestParseSwapsCommandOutput_Invalid(t *testing.T) {

@ -255,7 +255,7 @@ func InterfacesWithContext(ctx context.Context) (InterfaceStatList, error) {
return ret, nil return ret, nil
} }
func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) { func getIOCountersAll(n []IOCountersStat) []IOCountersStat {
r := IOCountersStat{ r := IOCountersStat{
Name: "all", Name: "all",
} }
@ -270,7 +270,7 @@ func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) {
r.Dropout += nic.Dropout r.Dropout += nic.Dropout
} }
return []IOCountersStat{r}, nil return []IOCountersStat{r}
} }
// NetIOCounters returns network I/O statistics for every network // NetIOCounters returns network I/O statistics for every network

@ -249,7 +249,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
} }
if !pernic { if !pernic {
return getIOCountersAll(ret) return getIOCountersAll(ret), nil
} }
return ret, nil return ret, nil
} }

@ -85,7 +85,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
} }
if !pernic { if !pernic {
return getIOCountersAll(ret) return getIOCountersAll(ret), nil
} }
return ret, nil return ret, nil

@ -128,7 +128,7 @@ func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename stri
} }
if !pernic { if !pernic {
return getIOCountersAll(ret) return getIOCountersAll(ret), nil
} }
return ret, nil return ret, nil

@ -138,8 +138,6 @@ type AddrTest struct {
} }
func TestDecodeAddress(t *testing.T) { func TestDecodeAddress(t *testing.T) {
assert := assert.New(t)
addr := map[string]AddrTest{ addr := map[string]AddrTest{
"11111:0035": { "11111:0035": {
Error: true, Error: true,
@ -182,11 +180,11 @@ func TestDecodeAddress(t *testing.T) {
} }
addr, err := decodeAddress(uint32(family), src) addr, err := decodeAddress(uint32(family), src)
if dst.Error { if dst.Error {
assert.Error(err, src) assert.Error(t, err, src)
} else { } else {
require.NoError(t, err, src) require.NoError(t, err, src)
assert.Equal(dst.IP, addr.IP, src) assert.Equal(t, dst.IP, addr.IP, src)
assert.Equal(dst.Port, int(addr.Port), src) assert.Equal(t, dst.Port, int(addr.Port), src)
} }
} }
} }

@ -150,15 +150,15 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
ret = append(ret, ioc) ret = append(ret, ioc)
} }
if pernic == false { if !pernic {
return getIOCountersAll(ret) return getIOCountersAll(ret), nil
} }
return ret, nil return ret, nil
} }
func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) { func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
return IOCounters(pernic) return IOCounters(pernic) //nolint:contextcheck //FIXME
} }
func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {

@ -5,6 +5,7 @@ package net
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"regexp" "regexp"
"runtime" "runtime"
@ -29,7 +30,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
lines := strings.Split(strings.TrimSpace(string(kstatSysOut)), "\n") lines := strings.Split(strings.TrimSpace(string(kstatSysOut)), "\n")
if len(lines) == 0 { if len(lines) == 0 {
return nil, fmt.Errorf("no interface found") return nil, errors.New("no interface found")
} }
rbytes64arr := make(map[string]uint64) rbytes64arr := make(map[string]uint64)
ipackets64arr := make(map[string]uint64) ipackets64arr := make(map[string]uint64)
@ -104,7 +105,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
} }
if !pernic { if !pernic {
return getIOCountersAll(ret) return getIOCountersAll(ret), nil
} }
return ret, nil return ret, nil

@ -133,11 +133,7 @@ func TestGetNetIOCountersAll(t *testing.T) {
Errin: 10, Errin: 10,
}, },
} }
ret, err := getIOCountersAll(n) ret := getIOCountersAll(n)
skipIfNotImplementedErr(t, err)
if err != nil {
t.Error(err)
}
if len(ret) != 1 { if len(ret) != 1 {
t.Errorf("invalid return count") t.Errorf("invalid return count")
} }

@ -193,7 +193,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
} }
if !pernic { if !pernic {
return getIOCountersAll(counters) return getIOCountersAll(counters), nil
} }
return counters, nil return counters, nil
} }

@ -8,7 +8,6 @@ import (
"context" "context"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt"
"io" "io"
"path/filepath" "path/filepath"
"sort" "sort"
@ -16,11 +15,12 @@ import (
"strings" "strings"
"unsafe" "unsafe"
cpu "github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/internal/common"
mem "github.com/shirou/gopsutil/v4/mem"
net "github.com/shirou/gopsutil/v4/net"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
) )
func pidsWithContext(ctx context.Context) ([]int32, error) { func pidsWithContext(ctx context.Context) ([]int32, error) {
@ -130,7 +130,7 @@ func readPtr(r io.Reader) (uintptr, error) {
} }
return uintptr(p), nil return uintptr(p), nil
default: default:
return 0, fmt.Errorf("unsupported pointer size") return 0, errors.New("unsupported pointer size")
} }
} }

@ -247,10 +247,7 @@ func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error
return "", err return "", err
} }
ret := strings.FieldsFunc(string(cmdline), func(r rune) bool { ret := strings.FieldsFunc(string(cmdline), func(r rune) bool {
if r == '\u0000' { return r == '\u0000'
return true
}
return false
}) })
return strings.Join(ret, " "), nil return strings.Join(ret, " "), nil

@ -8,8 +8,9 @@ import (
"syscall" "syscall"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
"github.com/shirou/gopsutil/v4/internal/common"
) )
type PROCESS_MEMORY_COUNTERS struct { type PROCESS_MEMORY_COUNTERS struct {
@ -39,30 +40,27 @@ func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, er
) )
if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
return uint64(info.PebBaseAddress), nil return uint64(info.PebBaseAddress), nil
} else {
return 0, windows.NTStatus(ret)
} }
} else { return 0, windows.NTStatus(ret)
// we are on a 32-bit process reading an external 64-bit process }
if common.ProcNtWow64QueryInformationProcess64.Find() == nil { // avoid panic // we are on a 32-bit process reading an external 64-bit process
var info processBasicInformation64 if common.ProcNtWow64QueryInformationProcess64.Find() != nil {
return 0, errors.New("can't find API to query 64 bit process from 32 bit")
}
// avoid panic
var info processBasicInformation64
ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call(
uintptr(procHandle), uintptr(procHandle),
uintptr(common.ProcessBasicInformation), uintptr(common.ProcessBasicInformation),
uintptr(unsafe.Pointer(&info)), uintptr(unsafe.Pointer(&info)),
uintptr(unsafe.Sizeof(info)), uintptr(unsafe.Sizeof(info)),
uintptr(0), uintptr(0),
) )
if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS {
return info.PebBaseAddress, nil return info.PebBaseAddress, nil
} else {
return 0, windows.NTStatus(ret)
}
} else {
return 0, errors.New("can't find API to query 64 bit process from 32 bit")
}
} }
return 0, windows.NTStatus(ret)
} }
func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte { func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte {

@ -5,7 +5,7 @@ package sensors
import ( import (
"context" "context"
"fmt" "errors"
"unsafe" "unsafe"
"github.com/shirou/gopsutil/v4/internal/common" "github.com/shirou/gopsutil/v4/internal/common"
@ -129,7 +129,7 @@ func readSMC(smc *common.SMC, key string) (*smcReturn, error) {
resultSmc.kSMC = result.result resultSmc.kSMC = result.result
if err != nil || result.result != common.KSMCSuccess { if err != nil || result.result != common.KSMCSuccess {
return resultSmc, fmt.Errorf("ERROR: IOConnectCallStructMethod failed") return resultSmc, errors.New("ERROR: IOConnectCallStructMethod failed")
} }
resultSmc.dataSize = uint32(result.keyInfo.dataSize) resultSmc.dataSize = uint32(result.keyInfo.dataSize)
@ -158,7 +158,7 @@ func callSMC(smc *common.SMC, input *smcParamStruct) (*smcParamStruct, error) {
uintptr(unsafe.Pointer(input)), inputCnt, uintptr(unsafe.Pointer(output)), &outputCnt) uintptr(unsafe.Pointer(input)), inputCnt, uintptr(unsafe.Pointer(output)), &outputCnt)
if result != 0 { if result != 0 {
return output, fmt.Errorf("ERROR: IOConnectCallStructMethod failed") return output, errors.New("ERROR: IOConnectCallStructMethod failed")
} }
return output, nil return output, nil
@ -169,7 +169,7 @@ func toUint32(key string) uint32 {
return 0 return 0
} }
var ans uint32 = 0 var ans uint32
var shift uint32 = 24 var shift uint32 = 24
for i := 0; i < smcKeySize; i++ { for i := 0; i < smcKeySize; i++ {

@ -6,6 +6,7 @@ package sensors
import ( import (
"context" "context"
"encoding/csv" "encoding/csv"
"errors"
"io" "io"
"strconv" "strconv"
"strings" "strings"
@ -24,7 +25,7 @@ func TemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
r.FieldsPerRecord = -1 r.FieldsPerRecord = -1
for { for {
record, err := r.Read() record, err := r.Read()
if err == io.EOF { if errors.Is(err, io.EOF) {
break break
} }
if err != nil { if err != nil {

Loading…
Cancel
Save