From c8fd8c7b1fdcf6146ad49ea10acdc00e4a661944 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 19:53:14 +0100 Subject: [PATCH 01/25] enable unparam linter --- .golangci.yml | 3 ++ cpu/cpu_linux.go | 17 ++++-------- load/load_linux.go | 8 +++--- mem/mem_linux.go | 6 ++-- process/process_linux.go | 52 +++++++++++++++++------------------ process/process_linux_test.go | 8 +++--- 6 files changed, 45 insertions(+), 49 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 0a2a4c37b..4bb1a74e0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,9 +1,12 @@ +issues: + max-same-issues: 0 linters: enable: - errorlint - gci - gosimple - typecheck + - unparam disable: - deadcode - errcheck diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index ea4024496..563a78eb0 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -63,7 +63,7 @@ func sysCPUPath(cpu int32, relPath string) string { return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath) } -func finishCPUInfo(c *InfoStat) error { +func finishCPUInfo(c *InfoStat) { var lines []string var err error var value float64 @@ -82,17 +82,16 @@ func finishCPUInfo(c *InfoStat) error { // if we encounter errors below such as there are no cpuinfo_max_freq file, // we just ignore. so let Mhz is 0. if err != nil || len(lines) == 0 { - return nil + return } value, err = strconv.ParseFloat(lines[0], 64) if err != nil { - return nil + return } c.Mhz = value / 1000.0 // value is in kHz if c.Mhz > 9999 { c.Mhz = c.Mhz / 1000.0 // value in Hz } - return nil } // CPUInfo on linux will return 1 item per physical thread. @@ -127,10 +126,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) { processorName = value case "processor": if c.CPU >= 0 { - err := finishCPUInfo(&c) - if err != nil { - return ret, err - } + finishCPUInfo(&c) ret = append(ret, c) } c = InfoStat{Cores: 1, ModelName: processorName} @@ -224,10 +220,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) { } } if c.CPU >= 0 { - err := finishCPUInfo(&c) - if err != nil { - return ret, err - } + finishCPUInfo(&c) ret = append(ret, c) } return ret, nil diff --git a/load/load_linux.go b/load/load_linux.go index c981d99ba..cfe68d9c5 100644 --- a/load/load_linux.go +++ b/load/load_linux.go @@ -17,14 +17,14 @@ func Avg() (*AvgStat, error) { } func AvgWithContext(ctx context.Context) (*AvgStat, error) { - stat, err := fileAvgWithContext(ctx) + stat, err := fileAvgWithContext() if err != nil { - stat, err = sysinfoAvgWithContext(ctx) + stat, err = sysinfoAvgWithContext() } return stat, err } -func sysinfoAvgWithContext(ctx context.Context) (*AvgStat, error) { +func sysinfoAvgWithContext() (*AvgStat, error) { var info syscall.Sysinfo_t err := syscall.Sysinfo(&info) if err != nil { @@ -39,7 +39,7 @@ func sysinfoAvgWithContext(ctx context.Context) (*AvgStat, error) { }, nil } -func fileAvgWithContext(ctx context.Context) (*AvgStat, error) { +func fileAvgWithContext() (*AvgStat, error) { values, err := readLoadAvgFromFile() if err != nil { return nil, err diff --git a/mem/mem_linux.go b/mem/mem_linux.go index 50fb2dc09..08c087b0c 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -35,7 +35,7 @@ func VirtualMemory() (*VirtualMemoryStat, error) { } func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - vm, _, err := fillFromMeminfoWithContext(ctx) + vm, _, err := fillFromMeminfoWithContext() if err != nil { return nil, err } @@ -47,14 +47,14 @@ func VirtualMemoryEx() (*VirtualMemoryExStat, error) { } func VirtualMemoryExWithContext(ctx context.Context) (*VirtualMemoryExStat, error) { - _, vmEx, err := fillFromMeminfoWithContext(ctx) + _, vmEx, err := fillFromMeminfoWithContext() if err != nil { return nil, err } return vmEx, nil } -func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *VirtualMemoryExStat, error) { +func fillFromMeminfoWithContext() (*VirtualMemoryStat, *VirtualMemoryExStat, error) { filename := common.HostProc("meminfo") lines, _ := common.ReadLines(filename) diff --git a/process/process_linux.go b/process/process_linux.go index 598713133..670dffe5e 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -82,7 +82,7 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { func (p *Process) NameWithContext(ctx context.Context) (string, error) { if p.name == "" { - if err := p.fillNameWithContext(ctx); err != nil { + if err := p.fillNameWithContext(); err != nil { return "", err } } @@ -91,7 +91,7 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) { func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { if p.tgid == 0 { - if err := p.fillFromStatusWithContext(ctx); err != nil { + if err := p.fillFromStatusWithContext(); err != nil { return 0, err } } @@ -99,7 +99,7 @@ func (p *Process) TgidWithContext(ctx context.Context) (int32, error) { } func (p *Process) ExeWithContext(ctx context.Context) (string, error) { - return p.fillFromExeWithContext(ctx) + return p.fillFromExeWithContext() } func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { @@ -119,11 +119,11 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { } func (p *Process) CwdWithContext(ctx context.Context) (string, error) { - return p.fillFromCwdWithContext(ctx) + return p.fillFromCwdWithContext() } func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - err := p.fillFromStatusWithContext(ctx) + err := p.fillFromStatusWithContext() if err != nil { return nil, err } @@ -134,7 +134,7 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { } func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { - err := p.fillFromStatusWithContext(ctx) + err := p.fillFromStatusWithContext() if err != nil { return []string{""}, err } @@ -159,7 +159,7 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { } func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext(ctx) + err := p.fillFromStatusWithContext() if err != nil { return []int32{}, err } @@ -167,7 +167,7 @@ func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext(ctx) + err := p.fillFromStatusWithContext() if err != nil { return []int32{}, err } @@ -175,7 +175,7 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext(ctx) + err := p.fillFromStatusWithContext() if err != nil { return []int32{}, err } @@ -212,7 +212,7 @@ func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) { } func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) { - rlimits, err := p.fillFromLimitsWithContext(ctx) + rlimits, err := p.fillFromLimitsWithContext() if !gatherUsed || err != nil { return rlimits, err } @@ -221,7 +221,7 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ( if err != nil { return nil, err } - if err := p.fillFromStatusWithContext(ctx); err != nil { + if err := p.fillFromStatusWithContext(); err != nil { return nil, err } @@ -267,11 +267,11 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ( } func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) { - return p.fillFromIOWithContext(ctx) + return p.fillFromIOWithContext() } func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) { - err := p.fillFromStatusWithContext(ctx) + err := p.fillFromStatusWithContext() if err != nil { return nil, err } @@ -284,7 +284,7 @@ func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { } func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - err := p.fillFromStatusWithContext(ctx) + err := p.fillFromStatusWithContext() if err != nil { return 0, err } @@ -324,7 +324,7 @@ func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - meminfo, _, err := p.fillFromStatmWithContext(ctx) + meminfo, _, err := p.fillFromStatmWithContext() if err != nil { return nil, err } @@ -332,7 +332,7 @@ func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, e } func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) { - _, memInfoEx, err := p.fillFromStatmWithContext(ctx) + _, memInfoEx, err := p.fillFromStatmWithContext() if err != nil { return nil, err } @@ -513,7 +513,7 @@ func limitToUint(val string) (uint64, error) { } // Get num_fds from /proc/(pid)/limits -func (p *Process) fillFromLimitsWithContext(ctx context.Context) ([]RlimitStat, error) { +func (p *Process) fillFromLimitsWithContext() ([]RlimitStat, error) { pid := p.Pid limitsFile := common.HostProc(strconv.Itoa(int(pid)), "limits") d, err := os.Open(limitsFile) @@ -648,7 +648,7 @@ func (p *Process) fillFromfdWithContext(ctx context.Context) (int32, []*OpenFile } // Get cwd from /proc/(pid)/cwd -func (p *Process) fillFromCwdWithContext(ctx context.Context) (string, error) { +func (p *Process) fillFromCwdWithContext() (string, error) { pid := p.Pid cwdPath := common.HostProc(strconv.Itoa(int(pid)), "cwd") cwd, err := os.Readlink(cwdPath) @@ -659,7 +659,7 @@ func (p *Process) fillFromCwdWithContext(ctx context.Context) (string, error) { } // Get exe from /proc/(pid)/exe -func (p *Process) fillFromExeWithContext(ctx context.Context) (string, error) { +func (p *Process) fillFromExeWithContext() (string, error) { pid := p.Pid exePath := common.HostProc(strconv.Itoa(int(pid)), "exe") exe, err := os.Readlink(exePath) @@ -707,7 +707,7 @@ func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string } // Get IO status from /proc/(pid)/io -func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, error) { +func (p *Process) fillFromIOWithContext() (*IOCountersStat, error) { pid := p.Pid ioPath := common.HostProc(strconv.Itoa(int(pid)), "io") ioline, err := ioutil.ReadFile(ioPath) @@ -743,7 +743,7 @@ func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, e } // Get memory info from /proc/(pid)/statm -func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat, *MemoryInfoExStat, error) { +func (p *Process) fillFromStatmWithContext() (*MemoryInfoStat, *MemoryInfoExStat, error) { pid := p.Pid memPath := common.HostProc(strconv.Itoa(int(pid)), "statm") contents, err := ioutil.ReadFile(memPath) @@ -795,16 +795,16 @@ func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat } // Get name from /proc/(pid)/comm or /proc/(pid)/status -func (p *Process) fillNameWithContext(ctx context.Context) error { - err := p.fillFromCommWithContext(ctx) +func (p *Process) fillNameWithContext() error { + err := p.fillFromCommWithContext() if err == nil && p.name != "" && len(p.name) < 15 { return nil } - return p.fillFromStatusWithContext(ctx) + return p.fillFromStatusWithContext() } // Get name from /proc/(pid)/comm -func (p *Process) fillFromCommWithContext(ctx context.Context) error { +func (p *Process) fillFromCommWithContext() error { pid := p.Pid statPath := common.HostProc(strconv.Itoa(int(pid)), "comm") contents, err := ioutil.ReadFile(statPath) @@ -817,7 +817,7 @@ func (p *Process) fillFromCommWithContext(ctx context.Context) error { } // Get various status from /proc/(pid)/status -func (p *Process) fillFromStatusWithContext(ctx context.Context) error { +func (p *Process) fillFromStatusWithContext() error { pid := p.Pid statPath := common.HostProc(strconv.Itoa(int(pid)), "status") contents, err := ioutil.ReadFile(statPath) diff --git a/process/process_linux_test.go b/process/process_linux_test.go index afeaaeee0..a374b4cd2 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -110,7 +110,7 @@ func Test_fillFromCommWithContext(t *testing.T) { continue } p, _ := NewProcess(int32(pid)) - if err := p.fillFromCommWithContext(context.Background()); err != nil { + if err := p.fillFromCommWithContext(); err != nil { t.Error(err) } } @@ -132,7 +132,7 @@ func Test_fillFromStatusWithContext(t *testing.T) { continue } p, _ := NewProcess(int32(pid)) - if err := p.fillFromStatusWithContext(context.Background()); err != nil { + if err := p.fillFromStatusWithContext(); err != nil { t.Error(err) } } @@ -144,7 +144,7 @@ func Benchmark_fillFromCommWithContext(b *testing.B) { pid := 1060 p, _ := NewProcess(int32(pid)) for i := 0; i < b.N; i++ { - p.fillFromCommWithContext(context.Background()) + p.fillFromCommWithContext() } } @@ -154,7 +154,7 @@ func Benchmark_fillFromStatusWithContext(b *testing.B) { pid := 1060 p, _ := NewProcess(int32(pid)) for i := 0; i < b.N; i++ { - p.fillFromStatusWithContext(context.Background()) + p.fillFromStatusWithContext() } } From 5c7609f116bcc763ed8b988db9a88b7512315433 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 22:22:10 +0100 Subject: [PATCH 02/25] enable durationcheck linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 4bb1a74e0..29a6dc2e1 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,6 +2,7 @@ issues: max-same-issues: 0 linters: enable: + - durationcheck - errorlint - gci - gosimple From a2420eab6489fcdd20992c764340e7b77be7dae9 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 22:43:32 +0100 Subject: [PATCH 03/25] Create run-commit.yml --- .github/workflows/run-commit.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .github/workflows/run-commit.yml diff --git a/.github/workflows/run-commit.yml b/.github/workflows/run-commit.yml new file mode 100644 index 000000000..cd761e264 --- /dev/null +++ b/.github/workflows/run-commit.yml @@ -0,0 +1,19 @@ +name: Run and commit +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v2 + with: + node-version: 1.17 + - run: gofmt -s -w ./... + - name: Commit changes + uses: EndBug/add-and-commit@v7 + with: + author_name: ${{ github.actor }} + author_email: ${{ github.actor }}@users.noreply.github.com + message: 'Update dist/' From fbdbfec1589daf37291652dd711277bafc183173 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 22:43:58 +0100 Subject: [PATCH 04/25] Update run-commit.yml --- .github/workflows/run-commit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-commit.yml b/.github/workflows/run-commit.yml index cd761e264..de5d7d2aa 100644 --- a/.github/workflows/run-commit.yml +++ b/.github/workflows/run-commit.yml @@ -16,4 +16,4 @@ jobs: with: author_name: ${{ github.actor }} author_email: ${{ github.actor }}@users.noreply.github.com - message: 'Update dist/' + message: 'gofmt' From 04c870cb3da4139228e2fd931a45a00725bdbe74 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 22:45:57 +0100 Subject: [PATCH 05/25] Update run-commit.yml --- .github/workflows/run-commit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-commit.yml b/.github/workflows/run-commit.yml index de5d7d2aa..39fee9d17 100644 --- a/.github/workflows/run-commit.yml +++ b/.github/workflows/run-commit.yml @@ -10,7 +10,7 @@ jobs: - uses: actions/setup-go@v2 with: node-version: 1.17 - - run: gofmt -s -w ./... + - run: gofmt -s -w . - name: Commit changes uses: EndBug/add-and-commit@v7 with: From eb5f6203d8bd794830c0926a4474d74e69efdc97 Mon Sep 17 00:00:00 2001 From: mmorel-35 Date: Wed, 22 Dec 2021 21:46:33 +0000 Subject: [PATCH 06/25] gofmt --- cpu/cpu_aix.go | 15 +- cpu/cpu_windows.go | 2 +- disk/disk_aix.go | 112 +++++------ disk/disk_solaris.go | 16 +- host/host_freebsd_arm64.go | 20 +- host/host_windows.go | 2 +- load/load_aix.go | 71 ++++--- mem/mem.go | 2 +- mem/mem_aix.go | 78 ++++---- net/net_linux_test.go | 8 +- net/net_windows.go | 1 - process/process_freebsd_arm64.go | 318 +++++++++++++++---------------- process/process_windows.go | 117 ++++++------ 13 files changed, 379 insertions(+), 383 deletions(-) diff --git a/cpu/cpu_aix.go b/cpu/cpu_aix.go index a0c6946ea..28b61c2c4 100644 --- a/cpu/cpu_aix.go +++ b/cpu/cpu_aix.go @@ -36,9 +36,9 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { } ct := &TimesStat{ CPU: "cpu-total", - Idle: float64(c.IdlePct), - User: float64(c.UserPct), - System: float64(c.KernPct), + Idle: float64(c.IdlePct), + User: float64(c.UserPct), + System: float64(c.KernPct), Iowait: float64(c.WaitPct), } ret = append(ret, *ct) @@ -56,11 +56,11 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) { return nil, err } info := InfoStat{ - CPU: 0, - Mhz: float64(c.ProcessorHz / 1000000), + CPU: 0, + Mhz: float64(c.ProcessorHz / 1000000), Cores: int32(c.NCpusCfg), - } - result := []InfoStat{info}; + } + result := []InfoStat{info} return result, nil } @@ -71,4 +71,3 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) { } return c.NCpusCfg, nil } - diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index d6dbb850e..1808eb8b4 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -7,8 +7,8 @@ import ( "fmt" "unsafe" - "github.com/yusufpapurcu/wmi" "github.com/shirou/gopsutil/v3/internal/common" + "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows" ) diff --git a/disk/disk_aix.go b/disk/disk_aix.go index bbebd1761..a9cdc8145 100644 --- a/disk/disk_aix.go +++ b/disk/disk_aix.go @@ -3,84 +3,84 @@ package disk import ( - "context" - "fmt" + "context" + "fmt" - "github.com/power-devops/perfstat" - "github.com/shirou/gopsutil/v3/internal/common" + "github.com/power-devops/perfstat" + "github.com/shirou/gopsutil/v3/internal/common" ) var FSType map[int]string func init() { - FSType = map[int]string{0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc", - 16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs", - 33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs", - 39: "ahafs", 40: "sterm-nfs", 41: "asmfs" } + FSType = map[int]string{0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc", + 16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs", + 33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs", + 39: "ahafs", 40: "sterm-nfs", 41: "asmfs"} } func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { - f, err := perfstat.FileSystemStat() - if err != nil { - return nil, err - } - ret := make([]PartitionStat, len(f)) + f, err := perfstat.FileSystemStat() + if err != nil { + return nil, err + } + ret := make([]PartitionStat, len(f)) - for _, fs := range f { - fstyp, exists := FSType[fs.FSType] - if ! exists { - fstyp = "unknown" - } - info := PartitionStat{ - Device: fs.Device, - Mountpoint: fs.MountPoint, - Fstype: fstyp, - } - ret = append(ret,info) - } + for _, fs := range f { + fstyp, exists := FSType[fs.FSType] + if !exists { + fstyp = "unknown" + } + info := PartitionStat{ + Device: fs.Device, + Mountpoint: fs.MountPoint, + Fstype: fstyp, + } + ret = append(ret, info) + } - return ret, err + return ret, err } func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { - return nil, common.ErrNotImplementedError + return nil, common.ErrNotImplementedError } func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { - f, err := perfstat.FileSystemStat() - if err != nil { - return nil, err - } + f, err := perfstat.FileSystemStat() + if err != nil { + return nil, err + } - blocksize := uint64(512) - for _, fs := range f { - if path == fs.MountPoint { - fstyp, exists := FSType[fs.FSType] - if ! exists { - fstyp = "unknown" - } - info := UsageStat{ - Path: path, - Fstype: fstyp, - Total: uint64(fs.TotalBlocks) * blocksize, - Free: uint64(fs.FreeBlocks) * blocksize, - Used: uint64(fs.TotalBlocks - fs.FreeBlocks) * blocksize, - InodesTotal: uint64(fs.TotalInodes), - InodesFree: uint64(fs.FreeInodes), - InodesUsed: uint64(fs.TotalInodes - fs.FreeInodes), - } - info.UsedPercent = (float64(info.Used) / float64(info.Total)) * 100.0 - info.InodesUsedPercent = (float64(info.InodesUsed) / float64(info.InodesTotal)) * 100.0 - return &info, nil - } - } - return nil, fmt.Errorf("mountpoint %s not found", path) + blocksize := uint64(512) + for _, fs := range f { + if path == fs.MountPoint { + fstyp, exists := FSType[fs.FSType] + if !exists { + fstyp = "unknown" + } + info := UsageStat{ + Path: path, + Fstype: fstyp, + Total: uint64(fs.TotalBlocks) * blocksize, + Free: uint64(fs.FreeBlocks) * blocksize, + Used: uint64(fs.TotalBlocks-fs.FreeBlocks) * blocksize, + InodesTotal: uint64(fs.TotalInodes), + InodesFree: uint64(fs.FreeInodes), + InodesUsed: uint64(fs.TotalInodes - fs.FreeInodes), + } + info.UsedPercent = (float64(info.Used) / float64(info.Total)) * 100.0 + info.InodesUsedPercent = (float64(info.InodesUsed) / float64(info.InodesTotal)) * 100.0 + return &info, nil + } + } + return nil, fmt.Errorf("mountpoint %s not found", path) } func SerialNumberWithContext(ctx context.Context, name string) (string, error) { - return "", common.ErrNotImplementedError + return "", common.ErrNotImplementedError } func LabelWithContext(ctx context.Context, name string) (string, error) { - return "", common.ErrNotImplementedError + return "", common.ErrNotImplementedError } diff --git a/disk/disk_solaris.go b/disk/disk_solaris.go index 8601458e4..63c62d123 100644 --- a/disk/disk_solaris.go +++ b/disk/disk_solaris.go @@ -27,14 +27,14 @@ var ( // A blacklist of read-only virtual filesystems. Writable filesystems are of // operational concern and must not be included in this list. fsTypeBlacklist = map[string]struct{}{ - "ctfs": struct{}{}, - "dev": struct{}{}, - "fd": struct{}{}, - "lofs": struct{}{}, - "lxproc": struct{}{}, - "mntfs": struct{}{}, - "objfs": struct{}{}, - "proc": struct{}{}, + "ctfs": {}, + "dev": {}, + "fd": {}, + "lofs": {}, + "lxproc": {}, + "mntfs": {}, + "objfs": {}, + "proc": {}, } ) diff --git a/host/host_freebsd_arm64.go b/host/host_freebsd_arm64.go index 88dc11fca..1cb86314b 100644 --- a/host/host_freebsd_arm64.go +++ b/host/host_freebsd_arm64.go @@ -6,19 +6,19 @@ package host const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 - sizeOfUtmpx = 0xc5 + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 + sizeOfUtmpx = 0xc5 ) type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 ) type Utmp struct { diff --git a/host/host_windows.go b/host/host_windows.go index 27ad8708c..0ab5e763f 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -13,9 +13,9 @@ import ( "time" "unsafe" - "github.com/yusufpapurcu/wmi" "github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/process" + "github.com/yusufpapurcu/wmi" "golang.org/x/sys/windows" ) diff --git a/load/load_aix.go b/load/load_aix.go index 4df26df26..b3cc8f0bf 100644 --- a/load/load_aix.go +++ b/load/load_aix.go @@ -11,61 +11,60 @@ package load import "C" import ( - "context" - "unsafe" + "context" + "unsafe" - "github.com/power-devops/perfstat" + "github.com/power-devops/perfstat" ) func Avg() (*AvgStat, error) { - return AvgWithContext(context.Background()) + return AvgWithContext(context.Background()) } func AvgWithContext(ctx context.Context) (*AvgStat, error) { - c, err := perfstat.CpuTotalStat() - if err != nil { - return nil, err - } - ret := &AvgStat{ - Load1: float64(c.LoadAvg1), - Load5: float64(c.LoadAvg5), - Load15: float64(c.LoadAvg15), - } + c, err := perfstat.CpuTotalStat() + if err != nil { + return nil, err + } + ret := &AvgStat{ + Load1: float64(c.LoadAvg1), + Load5: float64(c.LoadAvg5), + Load15: float64(c.LoadAvg15), + } - return ret, nil + return ret, nil } // Misc returns miscellaneous host-wide statistics. // darwin use ps command to get process running/blocked count. // Almost same as Darwin implementation, but state is different. func Misc() (*MiscStat, error) { - return MiscWithContext(context.Background()) + return MiscWithContext(context.Background()) } func MiscWithContext(ctx context.Context) (*MiscStat, error) { - info := C.struct_procentry64{} - cpid := C.pid_t(0) + info := C.struct_procentry64{} + cpid := C.pid_t(0) - ret := MiscStat{} - for { - // getprocs first argument is a void* - num, err := C.getprocs64(unsafe.Pointer(&info), C.sizeof_struct_procentry64, nil, 0, &cpid, 1) - if err != nil { - return nil, err - } + ret := MiscStat{} + for { + // getprocs first argument is a void* + num, err := C.getprocs64(unsafe.Pointer(&info), C.sizeof_struct_procentry64, nil, 0, &cpid, 1) + if err != nil { + return nil, err + } - ret.ProcsTotal++ - switch info.pi_state { - case C.SACTIVE: - ret.ProcsRunning++ - case C.SSTOP: - ret.ProcsBlocked++ - } + ret.ProcsTotal++ + switch info.pi_state { + case C.SACTIVE: + ret.ProcsRunning++ + case C.SSTOP: + ret.ProcsBlocked++ + } - if num == 0 { - break - } + if num == 0 { + break } - return &ret, nil + } + return &ret, nil } - diff --git a/mem/mem.go b/mem/mem.go index 1e845d362..e56d5d35d 100644 --- a/mem/mem.go +++ b/mem/mem.go @@ -91,7 +91,7 @@ type SwapMemoryStat struct { // Linux specific numbers // https://www.kernel.org/doc/Documentation/cgroup-v2.txt - PgMajFault uint64 `json:"pgMajFault"` + PgMajFault uint64 `json:"pgMajFault"` } func (m VirtualMemoryStat) String() string { diff --git a/mem/mem_aix.go b/mem/mem_aix.go index 6ba5d28ee..312a569d5 100644 --- a/mem/mem_aix.go +++ b/mem/mem_aix.go @@ -3,56 +3,56 @@ package mem import ( - "context" + "context" - "github.com/power-devops/perfstat" + "github.com/power-devops/perfstat" ) func VirtualMemory() (*VirtualMemoryStat, error) { - return VirtualMemoryWithContext(context.Background()) + return VirtualMemoryWithContext(context.Background()) } func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { - m, err := perfstat.MemoryTotalStat() - if err != nil { - return nil, err - } - pagesize := uint64(4096) - ret := VirtualMemoryStat{ - Total: uint64(m.RealTotal) * pagesize, - Available: uint64(m.RealAvailable) * pagesize, - Free: uint64(m.RealFree) * pagesize, - Used: uint64(m.RealInUse) * pagesize, - UsedPercent: 100 * float64(m.RealInUse) / float64(m.RealTotal), - Active: uint64(m.VirtualActive) * pagesize, - SwapTotal: uint64(m.PgSpTotal) * pagesize, - SwapFree: uint64(m.PgSpFree) * pagesize, - } - return &ret, nil + m, err := perfstat.MemoryTotalStat() + if err != nil { + return nil, err + } + pagesize := uint64(4096) + ret := VirtualMemoryStat{ + Total: uint64(m.RealTotal) * pagesize, + Available: uint64(m.RealAvailable) * pagesize, + Free: uint64(m.RealFree) * pagesize, + Used: uint64(m.RealInUse) * pagesize, + UsedPercent: 100 * float64(m.RealInUse) / float64(m.RealTotal), + Active: uint64(m.VirtualActive) * pagesize, + SwapTotal: uint64(m.PgSpTotal) * pagesize, + SwapFree: uint64(m.PgSpFree) * pagesize, + } + return &ret, nil } func SwapMemory() (*SwapMemoryStat, error) { - return SwapMemoryWithContext(context.Background()) + return SwapMemoryWithContext(context.Background()) } func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { - m, err := perfstat.MemoryTotalStat() - if err != nil { - return nil, err - } - pagesize := uint64(4096) - swapUsed := uint64(m.PgSpTotal - m.PgSpFree - m.PgSpRsvd) * pagesize - swapTotal := uint64(m.PgSpTotal) * pagesize - ret := SwapMemoryStat{ - Total: swapTotal, - Free: uint64(m.PgSpFree) * pagesize, - Used: swapUsed, - UsedPercent: float64(100 * swapUsed) / float64(swapTotal), - Sin: uint64(m.PgSpIn), - Sout: uint64(m.PgSpOut), - PgIn: uint64(m.PageIn), - PgOut: uint64(m.PageOut), - PgFault: uint64(m.PageFaults), - } - return &ret, nil + m, err := perfstat.MemoryTotalStat() + if err != nil { + return nil, err + } + pagesize := uint64(4096) + swapUsed := uint64(m.PgSpTotal-m.PgSpFree-m.PgSpRsvd) * pagesize + swapTotal := uint64(m.PgSpTotal) * pagesize + ret := SwapMemoryStat{ + Total: swapTotal, + Free: uint64(m.PgSpFree) * pagesize, + Used: swapUsed, + UsedPercent: float64(100*swapUsed) / float64(swapTotal), + Sin: uint64(m.PgSpIn), + Sout: uint64(m.PgSpOut), + PgIn: uint64(m.PageIn), + PgOut: uint64(m.PageOut), + PgFault: uint64(m.PageFaults), + } + return &ret, nil } diff --git a/net/net_linux_test.go b/net/net_linux_test.go index d46fffcc3..b9dd3ff85 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -21,10 +21,10 @@ func TestIOCountersByFileParsing(t *testing.T) { assert.Nil(t, err, "Temporary file creation failed: ", err) cases := [4][2]string{ - [2]string{"eth0: ", "eth1: "}, - [2]string{"eth0:0: ", "eth1:0: "}, - [2]string{"eth0:", "eth1:"}, - [2]string{"eth0:0:", "eth1:0:"}, + {"eth0: ", "eth1: "}, + {"eth0:0: ", "eth1:0: "}, + {"eth0:", "eth1:"}, + {"eth0:0:", "eth1:0:"}, } for _, testCase := range cases { err = tmpfile.Truncate(0) diff --git a/net/net_windows.go b/net/net_windows.go index 691d01914..d450e2701 100644 --- a/net/net_windows.go +++ b/net/net_windows.go @@ -333,7 +333,6 @@ func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackSta return nil, common.ErrNotImplementedError } - // NetProtoCounters returns network statistics for the entire system // If protocols is empty then all protocols are returned, otherwise // just the protocols in the list are returned. diff --git a/process/process_freebsd_arm64.go b/process/process_freebsd_arm64.go index 99781d1a2..22679fd4d 100644 --- a/process/process_freebsd_arm64.go +++ b/process/process_freebsd_arm64.go @@ -6,196 +6,196 @@ package process const ( - CTLKern = 1 - KernProc = 14 - KernProcPID = 1 - KernProcProc = 8 - KernProcPathname = 12 - KernProcArgs = 7 + CTLKern = 1 + KernProc = 14 + KernProcPID = 1 + KernProcProc = 8 + KernProcPathname = 12 + KernProcArgs = 7 ) const ( - sizeofPtr = 0x8 - sizeofShort = 0x2 - sizeofInt = 0x4 - sizeofLong = 0x8 - sizeofLongLong = 0x8 + sizeofPtr = 0x8 + sizeofShort = 0x2 + sizeofInt = 0x4 + sizeofLong = 0x8 + sizeofLongLong = 0x8 ) const ( - sizeOfKinfoVmentry = 0x488 - sizeOfKinfoProc = 0x440 + sizeOfKinfoVmentry = 0x488 + sizeOfKinfoProc = 0x440 ) const ( - SIDL = 1 - SRUN = 2 - SSLEEP = 3 - SSTOP = 4 - SZOMB = 5 - SWAIT = 6 - SLOCK = 7 + SIDL = 1 + SRUN = 2 + SSLEEP = 3 + SSTOP = 4 + SZOMB = 5 + SWAIT = 6 + SLOCK = 7 ) type ( - _C_short int16 - _C_int int32 - _C_long int64 - _C_long_long int64 + _C_short int16 + _C_int int32 + _C_long int64 + _C_long_long int64 ) type Timespec struct { - Sec int64 - Nsec int64 + Sec int64 + Nsec int64 } type Timeval struct { - Sec int64 - Usec int64 + Sec int64 + Usec int64 } type Rusage struct { - Utime Timeval - Stime Timeval - Maxrss int64 - Ixrss int64 - Idrss int64 - Isrss int64 - Minflt int64 - Majflt int64 - Nswap int64 - Inblock int64 - Oublock int64 - Msgsnd int64 - Msgrcv int64 - Nsignals int64 - Nvcsw int64 - Nivcsw int64 + Utime Timeval + Stime Timeval + Maxrss int64 + Ixrss int64 + Idrss int64 + Isrss int64 + Minflt int64 + Majflt int64 + Nswap int64 + Inblock int64 + Oublock int64 + Msgsnd int64 + Msgrcv int64 + Nsignals int64 + Nvcsw int64 + Nivcsw int64 } type Rlimit struct { - Cur int64 - Max int64 + Cur int64 + Max int64 } type KinfoProc struct { - Structsize int32 - Layout int32 - Args *int64 /* pargs */ - Paddr *int64 /* proc */ - Addr *int64 /* user */ - Tracep *int64 /* vnode */ - Textvp *int64 /* vnode */ - Fd *int64 /* filedesc */ - Vmspace *int64 /* vmspace */ - Wchan *byte - Pid int32 - Ppid int32 - Pgid int32 - Tpgid int32 - Sid int32 - Tsid int32 - Jobc int16 - Spare_short1 int16 - Tdev_freebsd11 uint32 - Siglist [16]byte /* sigset */ - Sigmask [16]byte /* sigset */ - Sigignore [16]byte /* sigset */ - Sigcatch [16]byte /* sigset */ - Uid uint32 - Ruid uint32 - Svuid uint32 - Rgid uint32 - Svgid uint32 - Ngroups int16 - Spare_short2 int16 - Groups [16]uint32 - Size uint64 - Rssize int64 - Swrss int64 - Tsize int64 - Dsize int64 - Ssize int64 - Xstat uint16 - Acflag uint16 - Pctcpu uint32 - Estcpu uint32 - Slptime uint32 - Swtime uint32 - Cow uint32 - Runtime uint64 - Start Timeval - Childtime Timeval - Flag int64 - Kiflag int64 - Traceflag int32 - Stat uint8 - Nice int8 - Lock uint8 - Rqindex uint8 - Oncpu_old uint8 - Lastcpu_old uint8 - Tdname [17]uint8 - Wmesg [9]uint8 - Login [18]uint8 - Lockname [9]uint8 - Comm [20]int8 - Emul [17]uint8 - Loginclass [18]uint8 - Moretdname [4]uint8 - Sparestrings [46]uint8 - Spareints [2]int32 - Tdev uint64 - Oncpu int32 - Lastcpu int32 - Tracer int32 - Flag2 int32 - Fibnum int32 - Cr_flags uint32 - Jid int32 - Numthreads int32 - Tid int32 - Pri Priority - Rusage Rusage - Rusage_ch Rusage - Pcb *int64 /* pcb */ - Kstack *byte - Udata *byte - Tdaddr *int64 /* thread */ - Spareptrs [6]*byte - Sparelongs [12]int64 - Sflag int64 - Tdflags int64 + Structsize int32 + Layout int32 + Args *int64 /* pargs */ + Paddr *int64 /* proc */ + Addr *int64 /* user */ + Tracep *int64 /* vnode */ + Textvp *int64 /* vnode */ + Fd *int64 /* filedesc */ + Vmspace *int64 /* vmspace */ + Wchan *byte + Pid int32 + Ppid int32 + Pgid int32 + Tpgid int32 + Sid int32 + Tsid int32 + Jobc int16 + Spare_short1 int16 + Tdev_freebsd11 uint32 + Siglist [16]byte /* sigset */ + Sigmask [16]byte /* sigset */ + Sigignore [16]byte /* sigset */ + Sigcatch [16]byte /* sigset */ + Uid uint32 + Ruid uint32 + Svuid uint32 + Rgid uint32 + Svgid uint32 + Ngroups int16 + Spare_short2 int16 + Groups [16]uint32 + Size uint64 + Rssize int64 + Swrss int64 + Tsize int64 + Dsize int64 + Ssize int64 + Xstat uint16 + Acflag uint16 + Pctcpu uint32 + Estcpu uint32 + Slptime uint32 + Swtime uint32 + Cow uint32 + Runtime uint64 + Start Timeval + Childtime Timeval + Flag int64 + Kiflag int64 + Traceflag int32 + Stat uint8 + Nice int8 + Lock uint8 + Rqindex uint8 + Oncpu_old uint8 + Lastcpu_old uint8 + Tdname [17]uint8 + Wmesg [9]uint8 + Login [18]uint8 + Lockname [9]uint8 + Comm [20]int8 + Emul [17]uint8 + Loginclass [18]uint8 + Moretdname [4]uint8 + Sparestrings [46]uint8 + Spareints [2]int32 + Tdev uint64 + Oncpu int32 + Lastcpu int32 + Tracer int32 + Flag2 int32 + Fibnum int32 + Cr_flags uint32 + Jid int32 + Numthreads int32 + Tid int32 + Pri Priority + Rusage Rusage + Rusage_ch Rusage + Pcb *int64 /* pcb */ + Kstack *byte + Udata *byte + Tdaddr *int64 /* thread */ + Spareptrs [6]*byte + Sparelongs [12]int64 + Sflag int64 + Tdflags int64 } type Priority struct { - Class uint8 - Level uint8 - Native uint8 - User uint8 + Class uint8 + Level uint8 + Native uint8 + User uint8 } type KinfoVmentry struct { - Structsize int32 - Type int32 - Start uint64 - End uint64 - Offset uint64 - Vn_fileid uint64 - Vn_fsid_freebsd11 uint32 - Flags int32 - Resident int32 - Private_resident int32 - Protection int32 - Ref_count int32 - Shadow_count int32 - Vn_type int32 - Vn_size uint64 - Vn_rdev_freebsd11 uint32 - Vn_mode uint16 - Status uint16 - Vn_fsid uint64 - Vn_rdev uint64 - X_kve_ispare [8]int32 - Path [1024]uint8 + Structsize int32 + Type int32 + Start uint64 + End uint64 + Offset uint64 + Vn_fileid uint64 + Vn_fsid_freebsd11 uint32 + Flags int32 + Resident int32 + Private_resident int32 + Protection int32 + Ref_count int32 + Shadow_count int32 + Vn_type int32 + Vn_size uint64 + Vn_rdev_freebsd11 uint32 + Vn_mode uint16 + Status uint16 + Vn_fsid uint64 + Vn_rdev uint64 + X_kve_ispare [8]int32 + Path [1024]uint8 } diff --git a/process/process_windows.go b/process/process_windows.go index 4f004ff9f..f502720cf 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -106,75 +106,75 @@ type processBasicInformation64 struct { } type processEnvironmentBlock32 struct { - Reserved1 [2]uint8 - BeingDebugged uint8 - Reserved2 uint8 - Reserved3 [2]uint32 - Ldr uint32 + Reserved1 [2]uint8 + BeingDebugged uint8 + Reserved2 uint8 + Reserved3 [2]uint32 + Ldr uint32 ProcessParameters uint32 // More fields which we don't use so far } type processEnvironmentBlock64 struct { - Reserved1 [2]uint8 - BeingDebugged uint8 - Reserved2 uint8 - _ [4]uint8 // padding, since we are 64 bit, the next pointer is 64 bit aligned (when compiling for 32 bit, this is not the case without manual padding) - Reserved3 [2]uint64 - Ldr uint64 + Reserved1 [2]uint8 + BeingDebugged uint8 + Reserved2 uint8 + _ [4]uint8 // padding, since we are 64 bit, the next pointer is 64 bit aligned (when compiling for 32 bit, this is not the case without manual padding) + Reserved3 [2]uint64 + Ldr uint64 ProcessParameters uint64 // More fields which we don't use so far } type rtlUserProcessParameters32 struct { - Reserved1 [16]uint8 - ConsoleHandle uint32 - ConsoleFlags uint32 - StdInputHandle uint32 - StdOutputHandle uint32 - StdErrorHandle uint32 + Reserved1 [16]uint8 + ConsoleHandle uint32 + ConsoleFlags uint32 + StdInputHandle uint32 + StdOutputHandle uint32 + StdErrorHandle uint32 CurrentDirectoryPathNameLength uint16 - _ uint16 // Max Length - CurrentDirectoryPathAddress uint32 - CurrentDirectoryHandle uint32 - DllPathNameLength uint16 - _ uint16 // Max Length - DllPathAddress uint32 - ImagePathNameLength uint16 - _ uint16 // Max Length - ImagePathAddress uint32 - CommandLineLength uint16 - _ uint16 // Max Length - CommandLineAddress uint32 - EnvironmentAddress uint32 + _ uint16 // Max Length + CurrentDirectoryPathAddress uint32 + CurrentDirectoryHandle uint32 + DllPathNameLength uint16 + _ uint16 // Max Length + DllPathAddress uint32 + ImagePathNameLength uint16 + _ uint16 // Max Length + ImagePathAddress uint32 + CommandLineLength uint16 + _ uint16 // Max Length + CommandLineAddress uint32 + EnvironmentAddress uint32 // More fields which we don't use so far } type rtlUserProcessParameters64 struct { - Reserved1 [16]uint8 - ConsoleHandle uint64 - ConsoleFlags uint64 - StdInputHandle uint64 - StdOutputHandle uint64 - StdErrorHandle uint64 + Reserved1 [16]uint8 + ConsoleHandle uint64 + ConsoleFlags uint64 + StdInputHandle uint64 + StdOutputHandle uint64 + StdErrorHandle uint64 CurrentDirectoryPathNameLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - CurrentDirectoryPathAddress uint64 - CurrentDirectoryHandle uint64 - DllPathNameLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - DllPathAddress uint64 - ImagePathNameLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - ImagePathAddress uint64 - CommandLineLength uint16 - _ uint16 // Max Length - _ uint32 // Padding - CommandLineAddress uint64 - EnvironmentAddress uint64 + _ uint16 // Max Length + _ uint32 // Padding + CurrentDirectoryPathAddress uint64 + CurrentDirectoryHandle uint64 + DllPathNameLength uint16 + _ uint16 // Max Length + _ uint32 // Padding + DllPathAddress uint64 + ImagePathNameLength uint16 + _ uint16 // Max Length + _ uint32 // Padding + ImagePathAddress uint64 + CommandLineLength uint16 + _ uint16 // Max Length + _ uint32 // Padding + CommandLineAddress uint64 + EnvironmentAddress uint64 // More fields which we don't use so far } @@ -946,7 +946,6 @@ func getProcessCPUTimes(pid int32) (SYSTEM_TIMES, error) { return times, err } - func getUserProcessParams32(handle windows.Handle) (rtlUserProcessParameters32, error) { pebAddress, err := queryPebAddress(syscall.Handle(handle), true) if err != nil { @@ -1068,14 +1067,14 @@ func getProcessEnvironmentVariables(pid int32, ctx context.Context) ([]string, e is32BitProcess: procIs32Bits, offset: processParameterBlockAddress, }) - envvarScanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error){ + envvarScanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { if atEOF && len(data) == 0 { return 0, nil, nil } // Check for UTF-16 zero character - for i := 0; i < len(data) - 1; i+=2 { + for i := 0; i < len(data)-1; i += 2 { if data[i] == 0 && data[i+1] == 0 { - return i+2, data[0:i], nil + return i + 2, data[0:i], nil } } if atEOF { @@ -1105,9 +1104,9 @@ func getProcessEnvironmentVariables(pid int32, ctx context.Context) ([]string, e } type processReader struct { - processHandle windows.Handle + processHandle windows.Handle is32BitProcess bool - offset uint64 + offset uint64 } func (p *processReader) Read(buf []byte) (int, error) { From 3664dbb362085cf26df3f75179eae57f8299b4ab Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 22:48:12 +0100 Subject: [PATCH 07/25] Update run-commit.yml --- .github/workflows/run-commit.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/run-commit.yml b/.github/workflows/run-commit.yml index 39fee9d17..1c6c5b936 100644 --- a/.github/workflows/run-commit.yml +++ b/.github/workflows/run-commit.yml @@ -10,10 +10,12 @@ jobs: - uses: actions/setup-go@v2 with: node-version: 1.17 - - run: gofmt -s -w . + - run: | + go install mvdan.cc/gofumpt@latest + gofumpt -l -w . - name: Commit changes uses: EndBug/add-and-commit@v7 with: author_name: ${{ github.actor }} author_email: ${{ github.actor }}@users.noreply.github.com - message: 'gofmt' + message: 'gofumpt' From 5eac39f41807eab69c4e0c751a7f09ba10803625 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 22:54:08 +0100 Subject: [PATCH 08/25] Update run-commit.yml --- .github/workflows/run-commit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-commit.yml b/.github/workflows/run-commit.yml index 1c6c5b936..5e7941524 100644 --- a/.github/workflows/run-commit.yml +++ b/.github/workflows/run-commit.yml @@ -9,7 +9,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-go@v2 with: - node-version: 1.17 + go-version: 1.17 - run: | go install mvdan.cc/gofumpt@latest gofumpt -l -w . From 1e6b445a8ad4ffe097ccdb97a2f365a4e6287325 Mon Sep 17 00:00:00 2001 From: mmorel-35 Date: Wed, 22 Dec 2021 21:54:41 +0000 Subject: [PATCH 09/25] gofumpt --- _tools/v3migration/v3migration.go | 1 - cpu/cpu.go | 6 +- cpu/cpu_aix.go | 1 + cpu/cpu_darwin.go | 1 + cpu/cpu_darwin_cgo.go | 5 +- cpu/cpu_darwin_nocgo.go | 4 +- cpu/cpu_dragonfly.go | 18 ++-- cpu/cpu_fallback.go | 1 + cpu/cpu_freebsd.go | 20 ++-- cpu/cpu_freebsd_test.go | 2 +- cpu/cpu_linux.go | 5 +- cpu/cpu_openbsd.go | 3 +- cpu/cpu_plan9.go | 1 + cpu/cpu_plan9_test.go | 1 + cpu/cpu_solaris.go | 4 +- cpu/cpu_solaris_test.go | 24 +++-- cpu/cpu_test.go | 3 - cpu/cpu_windows.go | 1 + disk/disk_aix.go | 7 +- disk/disk_darwin.go | 1 + disk/disk_darwin_cgo.go | 4 +- disk/disk_darwin_nocgo.go | 4 +- disk/disk_fallback.go | 1 + disk/disk_freebsd.go | 1 + disk/disk_freebsd_386.go | 1 + disk/disk_freebsd_amd64.go | 1 + disk/disk_freebsd_arm.go | 1 + disk/disk_freebsd_arm64.go | 5 +- disk/disk_linux.go | 2 + disk/disk_openbsd.go | 1 + disk/disk_openbsd_386.go | 5 +- disk/disk_openbsd_arm64.go | 5 +- disk/disk_solaris.go | 28 +++--- disk/disk_unix.go | 1 + disk/disk_windows.go | 3 +- disk/types_freebsd.go | 8 +- disk/types_openbsd.go | 14 ++- docker/docker.go | 6 +- docker/docker_linux.go | 1 + docker/docker_linux_test.go | 1 + docker/docker_notlinux.go | 1 + host/host_bsd.go | 1 + host/host_darwin.go | 2 +- host/host_darwin_386.go | 1 + host/host_darwin_amd64.go | 1 + host/host_darwin_arm64.go | 5 +- host/host_darwin_cgo.go | 4 +- host/host_darwin_nocgo.go | 4 +- host/host_fallback.go | 1 + host/host_freebsd.go | 2 +- host/host_freebsd_arm64.go | 5 +- host/host_linux.go | 3 +- host/host_linux_386.go | 2 + host/host_linux_amd64.go | 2 + host/host_linux_arm.go | 2 + host/host_linux_arm64.go | 2 + host/host_linux_mips.go | 2 + host/host_linux_mips64.go | 2 + host/host_linux_mips64le.go | 2 + host/host_linux_mipsle.go | 2 + host/host_linux_ppc64le.go | 7 +- host/host_linux_riscv64.go | 2 + host/host_linux_s390x.go | 7 +- host/host_linux_test.go | 1 + host/host_openbsd.go | 1 + host/host_openbsd_386.go | 5 +- host/host_openbsd_amd64.go | 1 + host/host_openbsd_arm64.go | 5 +- host/host_posix.go | 1 + host/host_windows.go | 1 + host/types_darwin.go | 8 +- host/types_freebsd.go | 7 +- host/types_linux.go | 9 +- host/types_openbsd.go | 7 +- internal/common/binary.go | 6 +- internal/common/common.go | 3 +- internal/common/common_darwin.go | 1 + internal/common/common_freebsd.go | 1 + internal/common/common_linux.go | 3 +- internal/common/common_openbsd.go | 1 + internal/common/common_test.go | 4 + internal/common/common_unix.go | 1 + internal/common/common_windows.go | 3 +- internal/common/sleep.go | 2 +- internal/common/sleep_test.go | 8 +- load/load_aix.go | 1 + load/load_darwin.go | 1 + load/load_fallback.go | 1 + load/load_freebsd.go | 1 + load/load_linux.go | 1 + load/load_openbsd.go | 1 + load/load_solaris.go | 1 + load/load_test.go | 1 - load/load_windows.go | 1 + mem/mem_aix.go | 1 + mem/mem_bsd.go | 1 + mem/mem_bsd_test.go | 1 + mem/mem_darwin.go | 1 + mem/mem_darwin_cgo.go | 1 + mem/mem_darwin_nocgo.go | 1 + mem/mem_darwin_test.go | 1 + mem/mem_fallback.go | 1 + mem/mem_freebsd.go | 1 + mem/mem_linux.go | 5 +- mem/mem_linux_test.go | 149 +++++++++++++++--------------- mem/mem_openbsd.go | 1 + mem/mem_openbsd_386.go | 1 + mem/mem_openbsd_arm64.go | 1 + mem/mem_plan9.go | 1 + mem/mem_plan9_test.go | 29 +++--- mem/mem_solaris.go | 1 + mem/mem_solaris_test.go | 1 + mem/mem_windows.go | 1 + mem/types_openbsd.go | 1 + net/net_aix.go | 6 +- net/net_darwin.go | 4 +- net/net_fallback.go | 1 + net/net_freebsd.go | 1 + net/net_linux.go | 7 +- net/net_linux_netlink_test.go | 2 + net/net_openbsd.go | 2 +- net/net_test.go | 4 - net/net_unix.go | 2 +- net/net_windows.go | 20 ++-- net/types_darwin.go | 20 ++-- process/process_bsd.go | 1 + process/process_darwin.go | 4 +- process/process_darwin_386.go | 2 + process/process_darwin_amd64.go | 2 + process/process_darwin_arm64.go | 5 +- process/process_darwin_cgo.go | 5 +- process/process_darwin_nocgo.go | 4 +- process/process_fallback.go | 4 +- process/process_freebsd.go | 1 + process/process_freebsd_arm64.go | 5 +- process/process_linux.go | 8 +- process/process_linux_test.go | 1 + process/process_openbsd.go | 3 +- process/process_openbsd_386.go | 5 +- process/process_openbsd_arm64.go | 5 +- process/process_plan9.go | 4 +- process/process_posix.go | 1 + process/process_posix_test.go | 1 + process/process_race_test.go | 1 + process/process_solaris.go | 3 +- process/process_test.go | 6 +- process/process_windows.go | 22 ++--- process/process_windows_386.go | 18 ++-- process/process_windows_amd64.go | 4 +- process/types_darwin.go | 9 +- process/types_freebsd.go | 1 + process/types_openbsd.go | 1 + winservices/manager.go | 1 + winservices/winservices.go | 1 + 154 files changed, 462 insertions(+), 287 deletions(-) diff --git a/_tools/v3migration/v3migration.go b/_tools/v3migration/v3migration.go index 53ee6e840..eb826f465 100644 --- a/_tools/v3migration/v3migration.go +++ b/_tools/v3migration/v3migration.go @@ -103,5 +103,4 @@ func main() { issueRemoveUnusedValue() } } - } diff --git a/cpu/cpu.go b/cpu/cpu.go index caf4d463a..411a063a9 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -51,8 +51,10 @@ type lastPercent struct { lastPerCPUTimes []TimesStat } -var lastCPUPercent lastPercent -var invoke common.Invoker = common.Invoke{} +var ( + lastCPUPercent lastPercent + invoke common.Invoker = common.Invoke{} +) func init() { lastCPUPercent.Lock() diff --git a/cpu/cpu_aix.go b/cpu/cpu_aix.go index 28b61c2c4..8f35bdc0c 100644 --- a/cpu/cpu_aix.go +++ b/cpu/cpu_aix.go @@ -1,3 +1,4 @@ +//go:build aix // +build aix package cpu diff --git a/cpu/cpu_darwin.go b/cpu/cpu_darwin.go index 9f3988b34..7acb258d9 100644 --- a/cpu/cpu_darwin.go +++ b/cpu/cpu_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package cpu diff --git a/cpu/cpu_darwin_cgo.go b/cpu/cpu_darwin_cgo.go index 2a7d4a115..1d5f0772e 100644 --- a/cpu/cpu_darwin_cgo.go +++ b/cpu/cpu_darwin_cgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build cgo +//go:build darwin && cgo +// +build darwin,cgo package cpu @@ -108,5 +108,4 @@ func allCPUTimes() ([]TimesStat, error) { } return []TimesStat{c}, nil - } diff --git a/cpu/cpu_darwin_nocgo.go b/cpu/cpu_darwin_nocgo.go index 3eaaf88f8..e067e99f9 100644 --- a/cpu/cpu_darwin_nocgo.go +++ b/cpu/cpu_darwin_nocgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build !cgo +//go:build darwin && !cgo +// +build darwin,!cgo package cpu diff --git a/cpu/cpu_dragonfly.go b/cpu/cpu_dragonfly.go index a9c81cc5d..fef53e5dc 100644 --- a/cpu/cpu_dragonfly.go +++ b/cpu/cpu_dragonfly.go @@ -15,14 +15,16 @@ import ( "golang.org/x/sys/unix" ) -var ClocksPerSec = float64(128) -var cpuMatch = regexp.MustCompile(`^CPU:`) -var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`) -var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`) -var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`) -var cpuEnd = regexp.MustCompile(`^Trying to mount root`) -var cpuTimesSize int -var emptyTimes cpuTimes +var ( + ClocksPerSec = float64(128) + cpuMatch = regexp.MustCompile(`^CPU:`) + originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`) + featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`) + featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`) + cpuEnd = regexp.MustCompile(`^Trying to mount root`) + cpuTimesSize int + emptyTimes cpuTimes +) func init() { clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) diff --git a/cpu/cpu_fallback.go b/cpu/cpu_fallback.go index 5f0440be7..6d7007ff9 100644 --- a/cpu/cpu_fallback.go +++ b/cpu/cpu_fallback.go @@ -1,3 +1,4 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !dragonfly && !plan9 && !aix // +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9,!aix package cpu diff --git a/cpu/cpu_freebsd.go b/cpu/cpu_freebsd.go index 3b83cf315..d3f47353c 100644 --- a/cpu/cpu_freebsd.go +++ b/cpu/cpu_freebsd.go @@ -15,15 +15,17 @@ import ( "golang.org/x/sys/unix" ) -var ClocksPerSec = float64(128) -var cpuMatch = regexp.MustCompile(`^CPU:`) -var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`) -var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`) -var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`) -var cpuEnd = regexp.MustCompile(`^Trying to mount root`) -var cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`) -var cpuTimesSize int -var emptyTimes cpuTimes +var ( + ClocksPerSec = float64(128) + cpuMatch = regexp.MustCompile(`^CPU:`) + originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`) + featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`) + featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`) + cpuEnd = regexp.MustCompile(`^Trying to mount root`) + cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`) + cpuTimesSize int + emptyTimes cpuTimes +) func init() { clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK) diff --git a/cpu/cpu_freebsd_test.go b/cpu/cpu_freebsd_test.go index 39c80eb20..27a709d31 100644 --- a/cpu/cpu_freebsd_test.go +++ b/cpu/cpu_freebsd_test.go @@ -13,7 +13,7 @@ func TestParseDmesgBoot(t *testing.T) { t.SkipNow() } - var cpuTests = []struct { + cpuTests := []struct { file string cpuNum int cores int32 diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index 563a78eb0..4f26230d6 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package cpu @@ -30,7 +31,7 @@ func Times(percpu bool) ([]TimesStat, error) { func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { filename := common.HostProc("stat") - var lines = []string{} + lines := []string{} if percpu { statlines, err := common.ReadLines(filename) if err != nil || len(statlines) < 2 { @@ -338,7 +339,7 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) { } // physical cores // https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/_pslinux.py#L615-L628 - var threadSiblingsLists = make(map[string]bool) + threadSiblingsLists := make(map[string]bool) // These 2 files are the same but */core_cpus_list is newer while */thread_siblings_list is deprecated and may disappear in the future. // https://www.kernel.org/doc/Documentation/admin-guide/cputopology.rst // https://github.com/giampaolo/psutil/pull/1727#issuecomment-707624964 diff --git a/cpu/cpu_openbsd.go b/cpu/cpu_openbsd.go index 8eb28db72..701d45c51 100644 --- a/cpu/cpu_openbsd.go +++ b/cpu/cpu_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package cpu @@ -108,7 +109,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { j *= 2 } - var cpuTimes = make([]int32, cpUStates) + cpuTimes := make([]int32, cpUStates) var mib []int32 if percpu { mib = []int32{ctlKern, kernCptime2, int32(j)} diff --git a/cpu/cpu_plan9.go b/cpu/cpu_plan9.go index 1729b8571..a2e99d8c0 100644 --- a/cpu/cpu_plan9.go +++ b/cpu/cpu_plan9.go @@ -1,3 +1,4 @@ +//go:build plan9 // +build plan9 package cpu diff --git a/cpu/cpu_plan9_test.go b/cpu/cpu_plan9_test.go index 4f585f9f4..9acf4bf98 100644 --- a/cpu/cpu_plan9_test.go +++ b/cpu/cpu_plan9_test.go @@ -1,3 +1,4 @@ +//go:build plan9 // +build plan9 package cpu diff --git a/cpu/cpu_solaris.go b/cpu/cpu_solaris.go index 4e3dea02a..b5ee70f68 100644 --- a/cpu/cpu_solaris.go +++ b/cpu/cpu_solaris.go @@ -24,7 +24,7 @@ func init() { } } -//sum all values in a float64 map with float64 keys +// sum all values in a float64 map with float64 keys func msum(x map[float64]float64) float64 { total := 0.0 for _, y := range x { @@ -47,7 +47,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { user := make(map[float64]float64) kern := make(map[float64]float64) iowt := make(map[float64]float64) - //swap := make(map[float64]float64) + // swap := make(map[float64]float64) kstatSysOut, err := invoke.CommandWithContext(ctx, kstatSys, "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/") if err != nil { return nil, fmt.Errorf("cannot execute kstat: %s", err) diff --git a/cpu/cpu_solaris_test.go b/cpu/cpu_solaris_test.go index 6b1ca6e85..508aad5e6 100644 --- a/cpu/cpu_solaris_test.go +++ b/cpu/cpu_solaris_test.go @@ -15,28 +15,36 @@ func TestParseISAInfo(t *testing.T) { }{ { "1cpu_1core_isainfo.txt", - []string{"rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx", + []string{ + "rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx", "avx", "xsave", "pclmulqdq", "aes", "movbe", "sse4.2", "sse4.1", "ssse3", "popcnt", "tscp", "cx16", "sse3", "sse2", "sse", "fxsr", "mmx", "cmov", "amd_sysc", "cx8", - "tsc", "fpu"}, + "tsc", "fpu", + }, }, { "2cpu_1core_isainfo.txt", - []string{"rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx", + []string{ + "rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx", "avx", "xsave", "pclmulqdq", "aes", "movbe", "sse4.2", "sse4.1", "ssse3", "popcnt", "tscp", "cx16", "sse3", "sse2", "sse", "fxsr", "mmx", "cmov", "amd_sysc", "cx8", - "tsc", "fpu"}, + "tsc", "fpu", + }, }, { "2cpu_8core_isainfo.txt", - []string{"vmx", "avx", "xsave", "pclmulqdq", "aes", "sse4.2", "sse4.1", "ssse3", "popcnt", + []string{ + "vmx", "avx", "xsave", "pclmulqdq", "aes", "sse4.2", "sse4.1", "ssse3", "popcnt", "tscp", "cx16", "sse3", "sse2", "sse", "fxsr", "mmx", "cmov", "amd_sysc", "cx8", - "tsc", "fpu"}, + "tsc", "fpu", + }, }, { "2cpu_12core_isainfo.txt", - []string{"amd_svm", "amd_lzcnt", "popcnt", "amd_sse4a", "tscp", "ahf", "cx16", "sse3", "sse2", - "sse", "fxsr", "amd_3dnowx", "amd_3dnow", "amd_mmx", "mmx", "cmov", "amd_sysc", "cx8", "tsc", "fpu"}, + []string{ + "amd_svm", "amd_lzcnt", "popcnt", "amd_sse4a", "tscp", "ahf", "cx16", "sse3", "sse2", + "sse", "fxsr", "amd_3dnowx", "amd_3dnow", "amd_mmx", "mmx", "cmov", "amd_sysc", "cx8", "tsc", "fpu", + }, }, } diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index 66390b7e3..91b8e8ad9 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -74,7 +74,6 @@ func TestCpu_times(t *testing.T) { if cpuTotal[0].Idle != 0 { assert.InEpsilon(t, cpuTotal[0].Idle, perCPUIdleTimeSum, margin) } - } func TestCpu_counts(t *testing.T) { @@ -162,7 +161,6 @@ func testCPUPercent(t *testing.T, percpu bool) { } func testCPUPercentLastUsed(t *testing.T, percpu bool) { - numcpu := runtime.NumCPU() testCount := 10 @@ -194,7 +192,6 @@ func testCPUPercentLastUsed(t *testing.T, percpu bool) { } } } - } func TestCPUPercent(t *testing.T) { diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 1808eb8b4..d1a0e4cdb 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package cpu diff --git a/disk/disk_aix.go b/disk/disk_aix.go index a9cdc8145..e37686b8c 100644 --- a/disk/disk_aix.go +++ b/disk/disk_aix.go @@ -1,3 +1,4 @@ +//go:build aix // +build aix package disk @@ -13,10 +14,12 @@ import ( var FSType map[int]string func init() { - FSType = map[int]string{0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc", + FSType = map[int]string{ + 0: "jfs2", 1: "namefs", 2: "nfs", 3: "jfs", 5: "cdrom", 6: "proc", 16: "special-fs", 17: "cache-fs", 18: "nfs3", 19: "automount-fs", 20: "pool-fs", 32: "vxfs", 33: "veritas-fs", 34: "udfs", 35: "nfs4", 36: "nfs4-pseudo", 37: "smbfs", 38: "mcr-pseudofs", - 39: "ahafs", 40: "sterm-nfs", 41: "asmfs"} + 39: "ahafs", 40: "sterm-nfs", 41: "asmfs", + } } func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { diff --git a/disk/disk_darwin.go b/disk/disk_darwin.go index 985e9f350..0877b7611 100644 --- a/disk/disk_darwin.go +++ b/disk/disk_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package disk diff --git a/disk/disk_darwin_cgo.go b/disk/disk_darwin_cgo.go index 8551c2f19..b041c8d72 100644 --- a/disk/disk_darwin_cgo.go +++ b/disk/disk_darwin_cgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build cgo +//go:build darwin && cgo +// +build darwin,cgo package disk diff --git a/disk/disk_darwin_nocgo.go b/disk/disk_darwin_nocgo.go index a118be804..99bb8ba24 100644 --- a/disk/disk_darwin_nocgo.go +++ b/disk/disk_darwin_nocgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build !cgo +//go:build darwin && !cgo +// +build darwin,!cgo package disk diff --git a/disk/disk_fallback.go b/disk/disk_fallback.go index 4bb4633ab..476873340 100644 --- a/disk/disk_fallback.go +++ b/disk/disk_fallback.go @@ -1,3 +1,4 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !windows && !solaris && !aix // +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix package disk diff --git a/disk/disk_freebsd.go b/disk/disk_freebsd.go index 767a28b92..e74c18694 100644 --- a/disk/disk_freebsd.go +++ b/disk/disk_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd // +build freebsd package disk diff --git a/disk/disk_freebsd_386.go b/disk/disk_freebsd_386.go index 9fc7cb256..7fa1783dc 100644 --- a/disk/disk_freebsd_386.go +++ b/disk/disk_freebsd_386.go @@ -52,6 +52,7 @@ type devstat struct { Id *byte Sequence1 uint32 } + type bintime struct { Sec int32 Frac uint64 diff --git a/disk/disk_freebsd_amd64.go b/disk/disk_freebsd_amd64.go index ffafc8fd9..d86a308be 100644 --- a/disk/disk_freebsd_amd64.go +++ b/disk/disk_freebsd_amd64.go @@ -55,6 +55,7 @@ type devstat struct { Sequence1 uint32 Pad_cgo_2 [4]byte } + type bintime struct { Sec int64 Frac uint64 diff --git a/disk/disk_freebsd_arm.go b/disk/disk_freebsd_arm.go index 9fc7cb256..7fa1783dc 100644 --- a/disk/disk_freebsd_arm.go +++ b/disk/disk_freebsd_arm.go @@ -52,6 +52,7 @@ type devstat struct { Id *byte Sequence1 uint32 } + type bintime struct { Sec int32 Frac uint64 diff --git a/disk/disk_freebsd_arm64.go b/disk/disk_freebsd_arm64.go index a3912171b..f6b3f80df 100644 --- a/disk/disk_freebsd_arm64.go +++ b/disk/disk_freebsd_arm64.go @@ -1,5 +1,6 @@ -// +build freebsd -// +build arm64 +//go:build freebsd && arm64 +// +build freebsd,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs disk/types_freebsd.go diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 6d98c1884..24337d091 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package disk @@ -21,6 +22,7 @@ import ( const ( sectorSize = 512 ) + const ( // man statfs ADFS_SUPER_MAGIC = 0xadf5 diff --git a/disk/disk_openbsd.go b/disk/disk_openbsd.go index 24324a4fd..e0658650d 100644 --- a/disk/disk_openbsd.go +++ b/disk/disk_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package disk diff --git a/disk/disk_openbsd_386.go b/disk/disk_openbsd_386.go index 68f4e0456..f4c139f5e 100644 --- a/disk/disk_openbsd_386.go +++ b/disk/disk_openbsd_386.go @@ -1,5 +1,6 @@ -// +build openbsd -// +build 386 +//go:build openbsd && 386 +// +build openbsd,386 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs disk/types_openbsd.go diff --git a/disk/disk_openbsd_arm64.go b/disk/disk_openbsd_arm64.go index ff7b3e4a9..ae1cf57e1 100644 --- a/disk/disk_openbsd_arm64.go +++ b/disk/disk_openbsd_arm64.go @@ -1,5 +1,6 @@ -// +build openbsd -// +build arm64 +//go:build openbsd && arm64 +// +build openbsd,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs disk/types_openbsd.go diff --git a/disk/disk_solaris.go b/disk/disk_solaris.go index 63c62d123..126a9e1e9 100644 --- a/disk/disk_solaris.go +++ b/disk/disk_solaris.go @@ -1,3 +1,4 @@ +//go:build solaris // +build solaris package disk @@ -23,20 +24,18 @@ const ( _MNTTAB = "/etc/mnttab" ) -var ( - // A blacklist of read-only virtual filesystems. Writable filesystems are of - // operational concern and must not be included in this list. - fsTypeBlacklist = map[string]struct{}{ - "ctfs": {}, - "dev": {}, - "fd": {}, - "lofs": {}, - "lxproc": {}, - "mntfs": {}, - "objfs": {}, - "proc": {}, - } -) +// A blacklist of read-only virtual filesystems. Writable filesystems are of +// operational concern and must not be included in this list. +var fsTypeBlacklist = map[string]struct{}{ + "ctfs": {}, + "dev": {}, + "fd": {}, + "lofs": {}, + "lxproc": {}, + "mntfs": {}, + "objfs": {}, + "proc": {}, +} func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { ret := make([]PartitionStat, 0, _DEFAULT_NUM_MOUNTS) @@ -113,6 +112,7 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { return usageStat, nil } + func SerialNumberWithContext(ctx context.Context, name string) (string, error) { return "", common.ErrNotImplementedError } diff --git a/disk/disk_unix.go b/disk/disk_unix.go index 9ca3bb34c..bdb62b24d 100644 --- a/disk/disk_unix.go +++ b/disk/disk_unix.go @@ -1,3 +1,4 @@ +//go:build freebsd || linux || darwin // +build freebsd linux darwin package disk diff --git a/disk/disk_windows.go b/disk/disk_windows.go index 1293586c4..9106364c2 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package disk @@ -106,7 +107,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro uintptr(len(lpFileSystemNameBuffer))) if driveret == 0 { if typeret == 5 || typeret == 2 { - continue //device is not ready will happen if there is no disk in the drive + continue // device is not ready will happen if there is no disk in the drive } return ret, err } diff --git a/disk/types_freebsd.go b/disk/types_freebsd.go index c617e8543..47f55513a 100644 --- a/disk/types_freebsd.go +++ b/disk/types_freebsd.go @@ -1,4 +1,6 @@ +//go:build ignore // +build ignore + // Hand writing: _Ctype_struct___0 /* @@ -58,5 +60,7 @@ type ( _C_long_double C.longlong ) -type devstat C.struct_devstat -type bintime C.struct_bintime +type ( + devstat C.struct_devstat + bintime C.struct_bintime +) diff --git a/disk/types_openbsd.go b/disk/types_openbsd.go index 7709aadf8..abb43c806 100644 --- a/disk/types_openbsd.go +++ b/disk/types_openbsd.go @@ -1,4 +1,6 @@ +//go:build ignore // +build ignore + // Hand writing: _Ctype_struct___0 /* @@ -25,8 +27,12 @@ const ( sizeOfDiskstats = C.sizeof_struct_diskstats ) -type Diskstats C.struct_diskstats -type Timeval C.struct_timeval +type ( + Diskstats C.struct_diskstats + Timeval C.struct_timeval +) -type Diskstat C.struct_diskstat -type bintime C.struct_bintime +type ( + Diskstat C.struct_diskstat + bintime C.struct_bintime +) diff --git a/docker/docker.go b/docker/docker.go index b139d86da..dda7ba00a 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -8,8 +8,10 @@ import ( "github.com/shirou/gopsutil/v3/internal/common" ) -var ErrDockerNotAvailable = errors.New("docker not available") -var ErrCgroupNotAvailable = errors.New("cgroup not available") +var ( + ErrDockerNotAvailable = errors.New("docker not available") + ErrCgroupNotAvailable = errors.New("cgroup not available") +) var invoke common.Invoker = common.Invoke{} diff --git a/docker/docker_linux.go b/docker/docker_linux.go index 650f7a25b..0c073122d 100644 --- a/docker/docker_linux.go +++ b/docker/docker_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package docker diff --git a/docker/docker_linux_test.go b/docker/docker_linux_test.go index bd5b8c7d7..c6afb44a7 100644 --- a/docker/docker_linux_test.go +++ b/docker/docker_linux_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package docker diff --git a/docker/docker_notlinux.go b/docker/docker_notlinux.go index 8df6a7c7d..df335126d 100644 --- a/docker/docker_notlinux.go +++ b/docker/docker_notlinux.go @@ -1,3 +1,4 @@ +//go:build !linux // +build !linux package docker diff --git a/host/host_bsd.go b/host/host_bsd.go index fc45b8770..4dc2bba58 100644 --- a/host/host_bsd.go +++ b/host/host_bsd.go @@ -1,3 +1,4 @@ +//go:build darwin || freebsd || openbsd // +build darwin freebsd openbsd package host diff --git a/host/host_darwin.go b/host/host_darwin.go index 6ac7a82f1..575a08b24 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package host @@ -94,7 +95,6 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) { } return ret, nil - } func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { diff --git a/host/host_darwin_386.go b/host/host_darwin_386.go index c3596f9f5..8caeed2e8 100644 --- a/host/host_darwin_386.go +++ b/host/host_darwin_386.go @@ -14,6 +14,7 @@ type Utmpx struct { Host [256]int8 Pad [16]uint32 } + type Timeval struct { Sec int32 } diff --git a/host/host_darwin_amd64.go b/host/host_darwin_amd64.go index c3596f9f5..8caeed2e8 100644 --- a/host/host_darwin_amd64.go +++ b/host/host_darwin_amd64.go @@ -14,6 +14,7 @@ type Utmpx struct { Host [256]int8 Pad [16]uint32 } + type Timeval struct { Sec int32 } diff --git a/host/host_darwin_arm64.go b/host/host_darwin_arm64.go index 74c28f2f7..293bd4df8 100644 --- a/host/host_darwin_arm64.go +++ b/host/host_darwin_arm64.go @@ -1,5 +1,6 @@ -// +build darwin -// +build arm64 +//go:build darwin && arm64 +// +build darwin,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs host/types_darwin.go diff --git a/host/host_darwin_cgo.go b/host/host_darwin_cgo.go index 5c06d9031..ffdc7b78f 100644 --- a/host/host_darwin_cgo.go +++ b/host/host_darwin_cgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build cgo +//go:build darwin && cgo +// +build darwin,cgo package host diff --git a/host/host_darwin_nocgo.go b/host/host_darwin_nocgo.go index 96d80d706..6285ba94d 100644 --- a/host/host_darwin_nocgo.go +++ b/host/host_darwin_nocgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build !cgo +//go:build darwin && !cgo +// +build darwin,!cgo package host diff --git a/host/host_fallback.go b/host/host_fallback.go index 35b553743..585250f9a 100644 --- a/host/host_fallback.go +++ b/host/host_fallback.go @@ -1,3 +1,4 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows // +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows package host diff --git a/host/host_freebsd.go b/host/host_freebsd.go index d7efd6983..2c9aa9d0d 100644 --- a/host/host_freebsd.go +++ b/host/host_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd // +build freebsd package host @@ -81,7 +82,6 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) { } return ret, nil - } func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) { diff --git a/host/host_freebsd_arm64.go b/host/host_freebsd_arm64.go index 1cb86314b..41bec3c11 100644 --- a/host/host_freebsd_arm64.go +++ b/host/host_freebsd_arm64.go @@ -1,5 +1,6 @@ -// +build freebsd -// +build arm64 +//go:build freebsd && arm64 +// +build freebsd,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs host/types_freebsd.go diff --git a/host/host_linux.go b/host/host_linux.go index e7c9e0d8f..29ad9ab86 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package host @@ -121,7 +122,6 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) { } return ret, nil - } func getlsbStruct() (*lsbStruct, error) { @@ -304,7 +304,6 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil } return platform, family, version, nil - } func KernelVersionWithContext(ctx context.Context) (version string, err error) { diff --git a/host/host_linux_386.go b/host/host_linux_386.go index 79b5cb5d3..46e0c5d5a 100644 --- a/host/host_linux_386.go +++ b/host/host_linux_386.go @@ -35,10 +35,12 @@ type utmp struct { Addr_v6 [4]int32 X__unused [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type UtTv struct { Sec int32 Usec int32 diff --git a/host/host_linux_amd64.go b/host/host_linux_amd64.go index 9a69652f5..1e574482f 100644 --- a/host/host_linux_amd64.go +++ b/host/host_linux_amd64.go @@ -33,10 +33,12 @@ type utmp struct { Addr_v6 [4]int32 X__glibc_reserved [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int64 Usec int64 diff --git a/host/host_linux_arm.go b/host/host_linux_arm.go index e2cf44850..7abbbb8a3 100644 --- a/host/host_linux_arm.go +++ b/host/host_linux_arm.go @@ -33,10 +33,12 @@ type utmp struct { Addr_v6 [4]int32 X__glibc_reserved [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int32 Usec int32 diff --git a/host/host_linux_arm64.go b/host/host_linux_arm64.go index 37dbe5c8c..eebef55cd 100644 --- a/host/host_linux_arm64.go +++ b/host/host_linux_arm64.go @@ -33,10 +33,12 @@ type utmp struct { Addr_v6 [4]int32 X__glibc_reserved [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int64 Usec int64 diff --git a/host/host_linux_mips.go b/host/host_linux_mips.go index b0fca0939..50207e5bc 100644 --- a/host/host_linux_mips.go +++ b/host/host_linux_mips.go @@ -33,10 +33,12 @@ type utmp struct { Addr_v6 [4]int32 X__unused [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int32 Usec int32 diff --git a/host/host_linux_mips64.go b/host/host_linux_mips64.go index b0fca0939..50207e5bc 100644 --- a/host/host_linux_mips64.go +++ b/host/host_linux_mips64.go @@ -33,10 +33,12 @@ type utmp struct { Addr_v6 [4]int32 X__unused [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int32 Usec int32 diff --git a/host/host_linux_mips64le.go b/host/host_linux_mips64le.go index b0fca0939..50207e5bc 100644 --- a/host/host_linux_mips64le.go +++ b/host/host_linux_mips64le.go @@ -33,10 +33,12 @@ type utmp struct { Addr_v6 [4]int32 X__unused [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int32 Usec int32 diff --git a/host/host_linux_mipsle.go b/host/host_linux_mipsle.go index b0fca0939..50207e5bc 100644 --- a/host/host_linux_mipsle.go +++ b/host/host_linux_mipsle.go @@ -33,10 +33,12 @@ type utmp struct { Addr_v6 [4]int32 X__unused [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int32 Usec int32 diff --git a/host/host_linux_ppc64le.go b/host/host_linux_ppc64le.go index d081a0819..51f5bee11 100644 --- a/host/host_linux_ppc64le.go +++ b/host/host_linux_ppc64le.go @@ -1,5 +1,6 @@ -// +build linux -// +build ppc64le +//go:build linux && ppc64le +// +build linux,ppc64le + // Created by cgo -godefs - DO NOT EDIT // cgo -godefs types_linux.go @@ -35,10 +36,12 @@ type utmp struct { Addr_v6 [4]int32 X__glibc_reserved [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int64 Usec int64 diff --git a/host/host_linux_riscv64.go b/host/host_linux_riscv64.go index 79a077d66..bb03a0b39 100644 --- a/host/host_linux_riscv64.go +++ b/host/host_linux_riscv64.go @@ -32,10 +32,12 @@ type utmp struct { Addr_v6 [4]int32 X__glibc_reserved [20]uint8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int64 Usec int64 diff --git a/host/host_linux_s390x.go b/host/host_linux_s390x.go index 083fbf924..6ea432a61 100644 --- a/host/host_linux_s390x.go +++ b/host/host_linux_s390x.go @@ -1,5 +1,6 @@ -// +build linux -// +build s390x +//go:build linux && s390x +// +build linux,s390x + // Created by cgo -godefs - DO NOT EDIT // cgo -godefs types_linux.go @@ -35,10 +36,12 @@ type utmp struct { Addr_v6 [4]int32 X__glibc_reserved [20]int8 } + type exit_status struct { Termination int16 Exit int16 } + type timeval struct { Sec int64 Usec int64 diff --git a/host/host_linux_test.go b/host/host_linux_test.go index 7808eee3d..8c23e5661 100644 --- a/host/host_linux_test.go +++ b/host/host_linux_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package host diff --git a/host/host_openbsd.go b/host/host_openbsd.go index 7e982ff3e..569de4abd 100644 --- a/host/host_openbsd.go +++ b/host/host_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package host diff --git a/host/host_openbsd_386.go b/host/host_openbsd_386.go index af0d855d3..b299d7ae4 100644 --- a/host/host_openbsd_386.go +++ b/host/host_openbsd_386.go @@ -1,5 +1,6 @@ -// +build openbsd -// +build 386 +//go:build openbsd && 386 +// +build openbsd,386 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs host/types_openbsd.go diff --git a/host/host_openbsd_amd64.go b/host/host_openbsd_amd64.go index afe0943e7..2d23b9b71 100644 --- a/host/host_openbsd_amd64.go +++ b/host/host_openbsd_amd64.go @@ -25,6 +25,7 @@ type Utmp struct { Host [256]int8 Time int64 } + type Timeval struct { Sec int64 Usec int64 diff --git a/host/host_openbsd_arm64.go b/host/host_openbsd_arm64.go index 3efc44ecf..20fb42dd7 100644 --- a/host/host_openbsd_arm64.go +++ b/host/host_openbsd_arm64.go @@ -1,5 +1,6 @@ -// +build openbsd -// +build arm64 +//go:build openbsd && arm64 +// +build openbsd,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs host/types_openbsd.go diff --git a/host/host_posix.go b/host/host_posix.go index 1cdf16d6f..89e63781e 100644 --- a/host/host_posix.go +++ b/host/host_posix.go @@ -1,3 +1,4 @@ +//go:build linux || freebsd || openbsd || darwin || solaris // +build linux freebsd openbsd darwin solaris package host diff --git a/host/host_windows.go b/host/host_windows.go index 0ab5e763f..fcd1d5908 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package host diff --git a/host/types_darwin.go b/host/types_darwin.go index b85822788..3378cffe4 100644 --- a/host/types_darwin.go +++ b/host/types_darwin.go @@ -1,4 +1,6 @@ +//go:build ignore // +build ignore + // plus hand editing about timeval /* @@ -13,5 +15,7 @@ package host */ import "C" -type Utmpx C.struct_utmpx -type Timeval C.struct_timeval +type ( + Utmpx C.struct_utmpx + Timeval C.struct_timeval +) diff --git a/host/types_freebsd.go b/host/types_freebsd.go index bbdce0c6a..79154d7ec 100644 --- a/host/types_freebsd.go +++ b/host/types_freebsd.go @@ -1,3 +1,4 @@ +//go:build ignore // +build ignore /* @@ -40,5 +41,7 @@ type ( _C_long_long C.longlong ) -type Utmp C.struct_utmp // for FreeBSD 9.0 compatibility -type Utmpx C.struct_futx +type ( + Utmp C.struct_utmp // for FreeBSD 9.0 compatibility + Utmpx C.struct_futx +) diff --git a/host/types_linux.go b/host/types_linux.go index 8adecb6cf..2b087b1a0 100644 --- a/host/types_linux.go +++ b/host/types_linux.go @@ -1,3 +1,4 @@ +//go:build ignore // +build ignore /* @@ -37,6 +38,8 @@ type ( _C_long_long C.longlong ) -type utmp C.struct_utmp -type exit_status C.struct_exit_status -type timeval C.struct_timeval +type ( + utmp C.struct_utmp + exit_status C.struct_exit_status + timeval C.struct_timeval +) diff --git a/host/types_openbsd.go b/host/types_openbsd.go index 9ebb97ce5..81cdd5336 100644 --- a/host/types_openbsd.go +++ b/host/types_openbsd.go @@ -1,3 +1,4 @@ +//go:build ignore // +build ignore /* @@ -39,5 +40,7 @@ type ( _C_long_long C.longlong ) -type Utmp C.struct_utmp -type Timeval C.struct_timeval +type ( + Utmp C.struct_utmp + Timeval C.struct_timeval +) diff --git a/internal/common/binary.go b/internal/common/binary.go index 9b5dc55b4..446c3597f 100644 --- a/internal/common/binary.go +++ b/internal/common/binary.go @@ -388,8 +388,10 @@ type coder struct { buf []byte } -type decoder coder -type encoder coder +type ( + decoder coder + encoder coder +) func (d *decoder) uint8() uint8 { x := d.buf[0] diff --git a/internal/common/common.go b/internal/common/common.go index 8f57a79c8..435b8d5d7 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -98,7 +98,6 @@ var ErrNotImplementedError = errors.New("not implemented yet") // ReadFile reads contents from a file func ReadFile(filename string) (string, error) { content, err := ioutil.ReadFile(filename) - if err != nil { return "", err } @@ -312,7 +311,7 @@ func PathExists(filename string) bool { return false } -//GetEnv retrieves the environment variable key. If it does not exist it returns the default. +// GetEnv retrieves the environment variable key. If it does not exist it returns the default. func GetEnv(key string, dfault string, combineWith ...string) string { value := os.Getenv(key) if value == "" { diff --git a/internal/common/common_darwin.go b/internal/common/common_darwin.go index be46af3d9..3b2b8e0f4 100644 --- a/internal/common/common_darwin.go +++ b/internal/common/common_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package common diff --git a/internal/common/common_freebsd.go b/internal/common/common_freebsd.go index 85bda0e22..2510f9def 100644 --- a/internal/common/common_freebsd.go +++ b/internal/common/common_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd || openbsd // +build freebsd openbsd package common diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index 734998993..6d44ce4ff 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package common @@ -55,7 +56,6 @@ func NumProcs() (uint64, error) { } func BootTimeWithContext(ctx context.Context) (uint64, error) { - system, role, err := Virtualization() if err != nil { return 0, err @@ -201,7 +201,6 @@ func VirtualizationWithContext(ctx context.Context) (string, string, error) { if PathExists(filepath.Join(filename, "self", "status")) { contents, err := ReadLines(filepath.Join(filename, "self", "status")) if err == nil { - if StringsContains(contents, "s_context:") || StringsContains(contents, "VxID:") { system = "linux-vserver" diff --git a/internal/common/common_openbsd.go b/internal/common/common_openbsd.go index ba73a7eb5..cbd3d28f6 100644 --- a/internal/common/common_openbsd.go +++ b/internal/common/common_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package common diff --git a/internal/common/common_test.go b/internal/common/common_test.go index b0e051c3f..93f33ec39 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -37,6 +37,7 @@ func TestIntToString(t *testing.T) { t.Error("could not convert") } } + func TestByteToString(t *testing.T) { src := []byte{65, 66, 67} dst := ByteToString(src) @@ -63,12 +64,14 @@ func TestMustParseInt32(t *testing.T) { t.Error("could not parse") } } + func TestMustParseUint64(t *testing.T) { ret := mustParseUint64("11111") if ret != uint64(11111) { t.Error("could not parse") } } + func TestMustParseFloat64(t *testing.T) { ret := mustParseFloat64("11111.11") if ret != float64(11111.11) { @@ -79,6 +82,7 @@ func TestMustParseFloat64(t *testing.T) { t.Error("could not parse") } } + func TestStringsContains(t *testing.T) { target, err := ReadLines("common_test.go") if err != nil { diff --git a/internal/common/common_unix.go b/internal/common/common_unix.go index 605202815..a4a953a2f 100644 --- a/internal/common/common_unix.go +++ b/internal/common/common_unix.go @@ -1,3 +1,4 @@ +//go:build linux || freebsd || darwin || openbsd // +build linux freebsd darwin openbsd package common diff --git a/internal/common/common_windows.go b/internal/common/common_windows.go index ba20136da..295b70bfa 100644 --- a/internal/common/common_windows.go +++ b/internal/common/common_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package common @@ -162,7 +163,7 @@ func NewWin32PerformanceCounter(postName, counterName string) (*Win32Performance if err != nil { return nil, err } - var counter = Win32PerformanceCounter{ + counter := Win32PerformanceCounter{ Query: query, PostName: postName, CounterName: counterName, diff --git a/internal/common/sleep.go b/internal/common/sleep.go index ee27e54d4..8c35b1722 100644 --- a/internal/common/sleep.go +++ b/internal/common/sleep.go @@ -8,7 +8,7 @@ import ( // Sleep awaits for provided interval. // Can be interrupted by context cancelation. func Sleep(ctx context.Context, interval time.Duration) error { - var timer = time.NewTimer(interval) + timer := time.NewTimer(interval) select { case <-ctx.Done(): return ctx.Err() diff --git a/internal/common/sleep_test.go b/internal/common/sleep_test.go index 2c846db30..aadc7667c 100644 --- a/internal/common/sleep_test.go +++ b/internal/common/sleep_test.go @@ -11,17 +11,17 @@ import ( func TestSleep(test *testing.T) { const dt = 50 * time.Millisecond - var t = func(name string, ctx context.Context, expected error) { + t := func(name string, ctx context.Context, expected error) { test.Run(name, func(test *testing.T) { - var err = common.Sleep(ctx, dt) + err := common.Sleep(ctx, dt) if !errors.Is(err, expected) { test.Errorf("expected %v, got %v", expected, err) } }) } - var ctx = context.Background() - var canceled, cancel = context.WithCancel(ctx) + ctx := context.Background() + canceled, cancel := context.WithCancel(ctx) cancel() t("background context", ctx, nil) diff --git a/load/load_aix.go b/load/load_aix.go index b3cc8f0bf..e7f9f94df 100644 --- a/load/load_aix.go +++ b/load/load_aix.go @@ -1,3 +1,4 @@ +//go:build aix // +build aix package load diff --git a/load/load_darwin.go b/load/load_darwin.go index a205f2f8a..73e62cab5 100644 --- a/load/load_darwin.go +++ b/load/load_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package load diff --git a/load/load_fallback.go b/load/load_fallback.go index 17126ac4c..3e41fd1ea 100644 --- a/load/load_fallback.go +++ b/load/load_fallback.go @@ -1,3 +1,4 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !windows && !solaris && !aix // +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix package load diff --git a/load/load_freebsd.go b/load/load_freebsd.go index bee8f554d..406980506 100644 --- a/load/load_freebsd.go +++ b/load/load_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd // +build freebsd package load diff --git a/load/load_linux.go b/load/load_linux.go index cfe68d9c5..30bba35e0 100644 --- a/load/load_linux.go +++ b/load/load_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package load diff --git a/load/load_openbsd.go b/load/load_openbsd.go index b299f582f..1d5d611f3 100644 --- a/load/load_openbsd.go +++ b/load/load_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package load diff --git a/load/load_solaris.go b/load/load_solaris.go index ae47dfff3..b9dd273b5 100644 --- a/load/load_solaris.go +++ b/load/load_solaris.go @@ -1,3 +1,4 @@ +//go:build solaris // +build solaris package load diff --git a/load/load_test.go b/load/load_test.go index a9db11b6a..1790fa559 100644 --- a/load/load_test.go +++ b/load/load_test.go @@ -71,7 +71,6 @@ func TestMiscStatString(t *testing.T) { } func BenchmarkLoad(b *testing.B) { - loadAvg := func(t testing.TB) { v, err := Avg() skipIfNotImplementedErr(t, err) diff --git a/load/load_windows.go b/load/load_windows.go index 54688f75b..3ff0c0aa6 100644 --- a/load/load_windows.go +++ b/load/load_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package load diff --git a/mem/mem_aix.go b/mem/mem_aix.go index 312a569d5..9b2196d97 100644 --- a/mem/mem_aix.go +++ b/mem/mem_aix.go @@ -1,3 +1,4 @@ +//go:build aix // +build aix package mem diff --git a/mem/mem_bsd.go b/mem/mem_bsd.go index 88db76209..a0b8be072 100644 --- a/mem/mem_bsd.go +++ b/mem/mem_bsd.go @@ -1,3 +1,4 @@ +//go:build freebsd || openbsd // +build freebsd openbsd package mem diff --git a/mem/mem_bsd_test.go b/mem/mem_bsd_test.go index ad3838e7f..9839a0435 100644 --- a/mem/mem_bsd_test.go +++ b/mem/mem_bsd_test.go @@ -1,3 +1,4 @@ +//go:build freebsd || openbsd // +build freebsd openbsd package mem diff --git a/mem/mem_darwin.go b/mem/mem_darwin.go index 187c52c15..0527dd93c 100644 --- a/mem/mem_darwin.go +++ b/mem/mem_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package mem diff --git a/mem/mem_darwin_cgo.go b/mem/mem_darwin_cgo.go index ade3cecd4..82c38245b 100644 --- a/mem/mem_darwin_cgo.go +++ b/mem/mem_darwin_cgo.go @@ -1,3 +1,4 @@ +//go:build darwin && cgo // +build darwin,cgo package mem diff --git a/mem/mem_darwin_nocgo.go b/mem/mem_darwin_nocgo.go index 2e847cbe4..be926f291 100644 --- a/mem/mem_darwin_nocgo.go +++ b/mem/mem_darwin_nocgo.go @@ -1,3 +1,4 @@ +//go:build darwin && !cgo // +build darwin,!cgo package mem diff --git a/mem/mem_darwin_test.go b/mem/mem_darwin_test.go index dba421ffc..4e0d9a014 100644 --- a/mem/mem_darwin_test.go +++ b/mem/mem_darwin_test.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package mem diff --git a/mem/mem_fallback.go b/mem/mem_fallback.go index 6890f2b98..0b6c528f2 100644 --- a/mem/mem_fallback.go +++ b/mem/mem_fallback.go @@ -1,3 +1,4 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !plan9 && !aix // +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!plan9,!aix package mem diff --git a/mem/mem_freebsd.go b/mem/mem_freebsd.go index ad592136b..44543ef74 100644 --- a/mem/mem_freebsd.go +++ b/mem/mem_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd // +build freebsd package mem diff --git a/mem/mem_linux.go b/mem/mem_linux.go index 08c087b0c..cea5400ee 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package mem @@ -331,7 +332,7 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { Free: uint64(sysinfo.Freeswap) * uint64(sysinfo.Unit), } ret.Used = ret.Total - ret.Free - //check Infinity + // check Infinity if ret.Total != 0 { ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0 } else { @@ -394,7 +395,6 @@ func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint6 fn := common.HostProc("zoneinfo") lines, err := common.ReadLines(fn) - if err != nil { return ret.Free + ret.Cached // fallback under kernel 2.6.13 } @@ -407,7 +407,6 @@ func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint6 if strings.HasPrefix(fields[0], "low") { lowValue, err := strconv.ParseUint(fields[1], 10, 64) - if err != nil { lowValue = 0 } diff --git a/mem/mem_linux_test.go b/mem/mem_linux_test.go index 3448729e7..42e7ff572 100644 --- a/mem/mem_linux_test.go +++ b/mem/mem_linux_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package mem @@ -25,79 +26,83 @@ var virtualMemoryTests = []struct { mockedRootFS string stat *VirtualMemoryStat }{ - {"intelcorei5", &VirtualMemoryStat{ - Total: 16502300672, - Available: 11495358464, - Used: 3437277184, - UsedPercent: 20.82907863769651, - Free: 8783491072, - Active: 4347392000, - Inactive: 2938834944, - Wired: 0, - Laundry: 0, - Buffers: 212496384, - Cached: 4069036032, - WriteBack: 0, - Dirty: 176128, - WriteBackTmp: 0, - Shared: 1222402048, - Slab: 253771776, - Sreclaimable: 186470400, - Sunreclaim: 67301376, - PageTables: 65241088, - SwapCached: 0, - CommitLimit: 16509730816, - CommittedAS: 12360818688, - HighTotal: 0, - HighFree: 0, - LowTotal: 0, - LowFree: 0, - SwapTotal: 8258580480, - SwapFree: 8258580480, - Mapped: 1172627456, - VmallocTotal: 35184372087808, - VmallocUsed: 0, - VmallocChunk: 0, - HugePagesTotal: 0, - HugePagesFree: 0, - HugePageSize: 2097152}, + { + "intelcorei5", &VirtualMemoryStat{ + Total: 16502300672, + Available: 11495358464, + Used: 3437277184, + UsedPercent: 20.82907863769651, + Free: 8783491072, + Active: 4347392000, + Inactive: 2938834944, + Wired: 0, + Laundry: 0, + Buffers: 212496384, + Cached: 4069036032, + WriteBack: 0, + Dirty: 176128, + WriteBackTmp: 0, + Shared: 1222402048, + Slab: 253771776, + Sreclaimable: 186470400, + Sunreclaim: 67301376, + PageTables: 65241088, + SwapCached: 0, + CommitLimit: 16509730816, + CommittedAS: 12360818688, + HighTotal: 0, + HighFree: 0, + LowTotal: 0, + LowFree: 0, + SwapTotal: 8258580480, + SwapFree: 8258580480, + Mapped: 1172627456, + VmallocTotal: 35184372087808, + VmallocUsed: 0, + VmallocChunk: 0, + HugePagesTotal: 0, + HugePagesFree: 0, + HugePageSize: 2097152, + }, }, - {"issue1002", &VirtualMemoryStat{ - Total: 260579328, - Available: 215199744, - Used: 34328576, - UsedPercent: 13.173944481121694, - Free: 124506112, - Active: 108785664, - Inactive: 8581120, - Wired: 0, - Laundry: 0, - Buffers: 4915200, - Cached: 96829440, - WriteBack: 0, - Dirty: 0, - WriteBackTmp: 0, - Shared: 0, - Slab: 9293824, - Sreclaimable: 2764800, - Sunreclaim: 6529024, - PageTables: 405504, - SwapCached: 0, - CommitLimit: 130289664, - CommittedAS: 25567232, - HighTotal: 134217728, - HighFree: 67784704, - LowTotal: 126361600, - LowFree: 56721408, - SwapTotal: 0, - SwapFree: 0, - Mapped: 38793216, - VmallocTotal: 1996488704, - VmallocUsed: 0, - VmallocChunk: 0, - HugePagesTotal: 0, - HugePagesFree: 0, - HugePageSize: 0}, + { + "issue1002", &VirtualMemoryStat{ + Total: 260579328, + Available: 215199744, + Used: 34328576, + UsedPercent: 13.173944481121694, + Free: 124506112, + Active: 108785664, + Inactive: 8581120, + Wired: 0, + Laundry: 0, + Buffers: 4915200, + Cached: 96829440, + WriteBack: 0, + Dirty: 0, + WriteBackTmp: 0, + Shared: 0, + Slab: 9293824, + Sreclaimable: 2764800, + Sunreclaim: 6529024, + PageTables: 405504, + SwapCached: 0, + CommitLimit: 130289664, + CommittedAS: 25567232, + HighTotal: 134217728, + HighFree: 67784704, + LowTotal: 126361600, + LowFree: 56721408, + SwapTotal: 0, + SwapFree: 0, + Mapped: 38793216, + VmallocTotal: 1996488704, + VmallocUsed: 0, + VmallocChunk: 0, + HugePagesTotal: 0, + HugePagesFree: 0, + HugePageSize: 0, + }, }, } diff --git a/mem/mem_openbsd.go b/mem/mem_openbsd.go index 9dc3af1f2..4d9b6cce3 100644 --- a/mem/mem_openbsd.go +++ b/mem/mem_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package mem diff --git a/mem/mem_openbsd_386.go b/mem/mem_openbsd_386.go index 0fa65d9c7..de2b26ca4 100644 --- a/mem/mem_openbsd_386.go +++ b/mem/mem_openbsd_386.go @@ -1,3 +1,4 @@ +//go:build openbsd && 386 // +build openbsd,386 // Code generated by cmd/cgo -godefs; DO NOT EDIT. diff --git a/mem/mem_openbsd_arm64.go b/mem/mem_openbsd_arm64.go index 35f8517b6..3661b16fb 100644 --- a/mem/mem_openbsd_arm64.go +++ b/mem/mem_openbsd_arm64.go @@ -1,3 +1,4 @@ +//go:build openbsd && arm64 // +build openbsd,arm64 // Code generated by cmd/cgo -godefs; DO NOT EDIT. diff --git a/mem/mem_plan9.go b/mem/mem_plan9.go index 8e7c4334e..b5259f844 100644 --- a/mem/mem_plan9.go +++ b/mem/mem_plan9.go @@ -1,3 +1,4 @@ +//go:build plan9 // +build plan9 package mem diff --git a/mem/mem_plan9_test.go b/mem/mem_plan9_test.go index 94097bd9d..b3480ca4f 100644 --- a/mem/mem_plan9_test.go +++ b/mem/mem_plan9_test.go @@ -1,3 +1,4 @@ +//go:build plan9 // +build plan9 package mem @@ -12,14 +13,16 @@ var virtualMemoryTests = []struct { mockedRootFS string stat *VirtualMemoryStat }{ - {"swap", &VirtualMemoryStat{ - Total: 1071185920, - Available: 808370176, - Used: 11436032, - UsedPercent: 1.3949677238843257, - Free: 808370176, - SwapTotal: 655360000, - SwapFree: 655360000}, + { + "swap", &VirtualMemoryStat{ + Total: 1071185920, + Available: 808370176, + Used: 11436032, + UsedPercent: 1.3949677238843257, + Free: 808370176, + SwapTotal: 655360000, + SwapFree: 655360000, + }, }, } @@ -49,10 +52,12 @@ var swapMemoryTests = []struct { mockedRootFS string swap *SwapMemoryStat }{ - {"swap", &SwapMemoryStat{ - Total: 655360000, - Used: 0, - Free: 655360000}, + { + "swap", &SwapMemoryStat{ + Total: 655360000, + Used: 0, + Free: 655360000, + }, }, } diff --git a/mem/mem_solaris.go b/mem/mem_solaris.go index 9f09f82be..7f6a6fd59 100644 --- a/mem/mem_solaris.go +++ b/mem/mem_solaris.go @@ -1,3 +1,4 @@ +//go:build solaris // +build solaris package mem diff --git a/mem/mem_solaris_test.go b/mem/mem_solaris_test.go index 907e49d46..05360203a 100644 --- a/mem/mem_solaris_test.go +++ b/mem/mem_solaris_test.go @@ -1,3 +1,4 @@ +//go:build solaris // +build solaris package mem diff --git a/mem/mem_windows.go b/mem/mem_windows.go index 4d3371327..8c7fb1a13 100644 --- a/mem/mem_windows.go +++ b/mem/mem_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package mem diff --git a/mem/types_openbsd.go b/mem/types_openbsd.go index bb841a428..8e0e412af 100644 --- a/mem/types_openbsd.go +++ b/mem/types_openbsd.go @@ -1,3 +1,4 @@ +//go:build ignore // +build ignore /* diff --git a/net/net_aix.go b/net/net_aix.go index e954d56b2..d89db7ccf 100644 --- a/net/net_aix.go +++ b/net/net_aix.go @@ -1,3 +1,4 @@ +//go:build aix // +build aix package net @@ -102,7 +103,6 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, return getIOCountersAll(iocounters) } return iocounters, nil - } // NetIOCountersByFile is an method which is added just a compatibility for linux. @@ -335,7 +335,6 @@ func parseNetstatA(output string, kind string) ([]ConnectionStat, error) { } return ret, nil - } func Connections(kind string) ([]ConnectionStat, error) { @@ -343,7 +342,6 @@ func Connections(kind string) ([]ConnectionStat, error) { } func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - args := []string{"-na"} switch strings.ToLower(kind) { default: @@ -367,7 +365,6 @@ func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, return nil, err } out, err := invoke.CommandWithContext(ctx, netstat, args...) - if err != nil { return nil, err } @@ -378,7 +375,6 @@ func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, } return ret, nil - } func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { diff --git a/net/net_darwin.go b/net/net_darwin.go index 2327031dd..9badcf6ac 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package net @@ -6,11 +7,12 @@ import ( "context" "errors" "fmt" - "github.com/shirou/gopsutil/v3/internal/common" "os/exec" "regexp" "strconv" "strings" + + "github.com/shirou/gopsutil/v3/internal/common" ) var ( diff --git a/net/net_fallback.go b/net/net_fallback.go index 6220e8340..58325f655 100644 --- a/net/net_fallback.go +++ b/net/net_fallback.go @@ -1,3 +1,4 @@ +//go:build !aix && !darwin && !linux && !freebsd && !openbsd && !windows // +build !aix,!darwin,!linux,!freebsd,!openbsd,!windows package net diff --git a/net/net_freebsd.go b/net/net_freebsd.go index 739f8cc68..0a30423de 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd // +build freebsd package net diff --git a/net/net_linux.go b/net/net_linux.go index b1d1626c0..d856b9e0e 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package net @@ -233,7 +234,6 @@ func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) { maxfile := common.HostProc("sys/net/netfilter/nf_conntrack_max") count, err := common.ReadInts(countfile) - if err != nil { return nil, err } @@ -331,21 +331,25 @@ var kindTCP4 = netConnectionKindType{ sockType: syscall.SOCK_STREAM, filename: "tcp", } + var kindTCP6 = netConnectionKindType{ family: syscall.AF_INET6, sockType: syscall.SOCK_STREAM, filename: "tcp6", } + var kindUDP4 = netConnectionKindType{ family: syscall.AF_INET, sockType: syscall.SOCK_DGRAM, filename: "udp", } + var kindUDP6 = netConnectionKindType{ family: syscall.AF_INET6, sockType: syscall.SOCK_DGRAM, filename: "udp6", } + var kindUNIX = netConnectionKindType{ family: syscall.AF_UNIX, filename: "unix", @@ -747,7 +751,6 @@ func parseIPv6HexString(src []byte) (net.IP, error) { } func processInet(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) { - if strings.HasSuffix(file, "6") && !common.PathExists(file) { // IPv6 not supported, return empty. return []connTmp{}, nil diff --git a/net/net_linux_netlink_test.go b/net/net_linux_netlink_test.go index 67007def7..889719676 100644 --- a/net/net_linux_netlink_test.go +++ b/net/net_linux_netlink_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package net @@ -10,6 +11,7 @@ func BenchmarkGetConnectionsInet(b *testing.B) { Connections("inet") } } + func BenchmarkGetConnectionsAll(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { diff --git a/net/net_openbsd.go b/net/net_openbsd.go index 4e09a66d0..04b9b1d69 100644 --- a/net/net_openbsd.go +++ b/net/net_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package net @@ -298,7 +299,6 @@ func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, return nil, err } out, err := invoke.CommandWithContext(ctx, netstat, args...) - if err != nil { return nil, err } diff --git a/net/net_test.go b/net/net_test.go index ed633cf28..bacca04f8 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -50,7 +50,6 @@ func TestNetProtoCountersStatString(t *testing.T) { if e != fmt.Sprintf("%v", v) { t.Errorf("NetProtoCountersStat string is invalid: %v", v) } - } func TestNetConnectionStatString(t *testing.T) { @@ -64,7 +63,6 @@ func TestNetConnectionStatString(t *testing.T) { if e != fmt.Sprintf("%v", v) { t.Errorf("NetConnectionStat string is invalid: %v", v) } - } func TestNetIOCountersAll(t *testing.T) { @@ -223,7 +221,6 @@ func TestNetConnections(t *testing.T) { t.Errorf("invalid NetConnections: %v", vv) } } - } func TestNetFilterCounters(t *testing.T) { @@ -251,7 +248,6 @@ func TestNetFilterCounters(t *testing.T) { t.Errorf("nf_connTrackMax needs to be greater than zero: %v", vv) } } - } func TestInterfaceStatString(t *testing.T) { diff --git a/net/net_unix.go b/net/net_unix.go index 924cfd115..2fd2224fa 100644 --- a/net/net_unix.go +++ b/net/net_unix.go @@ -1,3 +1,4 @@ +//go:build freebsd || darwin // +build freebsd darwin package net @@ -80,7 +81,6 @@ func ConnectionsPidWithContext(ctx context.Context, kind string, pid int32) ([]C } n, err := parseNetLine(rr) if err != nil { - continue } diff --git a/net/net_windows.go b/net/net_windows.go index d450e2701..f742d8225 100644 --- a/net/net_windows.go +++ b/net/net_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package net @@ -44,16 +45,19 @@ var kindTCP4 = netConnectionKindType{ sockType: syscall.SOCK_STREAM, filename: "tcp", } + var kindTCP6 = netConnectionKindType{ family: syscall.AF_INET6, sockType: syscall.SOCK_STREAM, filename: "tcp6", } + var kindUDP4 = netConnectionKindType{ family: syscall.AF_INET, sockType: syscall.SOCK_DGRAM, filename: "udp", } + var kindUDP6 = netConnectionKindType{ family: syscall.AF_INET6, sockType: syscall.SOCK_DGRAM, @@ -524,9 +528,7 @@ func getUDPConnections(family uint32) ([]ConnectionStat, error) { buf = make([]byte, size) } - var ( - index, step, length int - ) + var index, step, length int stats := make([]ConnectionStat, 0) switch family { @@ -693,8 +695,10 @@ type mibTCP6TableOwnerPid struct { Table [anySize]mibTCP6RowOwnerPid } -type pmibTCPTableOwnerPidAll *mibTCPTableOwnerPid -type pmibTCP6TableOwnerPidAll *mibTCP6TableOwnerPid +type ( + pmibTCPTableOwnerPidAll *mibTCPTableOwnerPid + pmibTCP6TableOwnerPidAll *mibTCP6TableOwnerPid +) // UDP @@ -749,8 +753,10 @@ type mibUDP6TableOwnerPid struct { Table [anySize]mibUDP6RowOwnerPid } -type pmibUDPTableOwnerPid *mibUDPTableOwnerPid -type pmibUDP6TableOwnerPid *mibUDP6TableOwnerPid +type ( + pmibUDPTableOwnerPid *mibUDPTableOwnerPid + pmibUDP6TableOwnerPid *mibUDP6TableOwnerPid +) func decodePort(port uint32) uint16 { return syscall.Ntohs(uint16(port)) diff --git a/net/types_darwin.go b/net/types_darwin.go index 1494b2db1..81aca0119 100644 --- a/net/types_darwin.go +++ b/net/types_darwin.go @@ -1,4 +1,6 @@ +//go:build ignore // +build ignore + // Hand writing: _Ctype_struct___3, 4 /* @@ -42,12 +44,14 @@ type ( _C_long_double C.longlong ) -type Xinpgen C.struct_xinpgen -type Inpcb C.struct_inpcb -type in_addr C.struct_in_addr -type Inpcb_list_entry C.struct__inpcb_list_entry -type Xsocket C.struct_xsocket -type Xsockbuf C.struct_xsockbuf -type Xinpcb C.struct_xinpcb +type ( + Xinpgen C.struct_xinpgen + Inpcb C.struct_inpcb + in_addr C.struct_in_addr + Inpcb_list_entry C.struct__inpcb_list_entry + Xsocket C.struct_xsocket + Xsockbuf C.struct_xsockbuf + Xinpcb C.struct_xinpcb +) -//type u_quad_t C.struct_u_quad_t +// type u_quad_t C.struct_u_quad_t diff --git a/process/process_bsd.go b/process/process_bsd.go index 26b17e901..263829ffa 100644 --- a/process/process_bsd.go +++ b/process/process_bsd.go @@ -1,3 +1,4 @@ +//go:build darwin || freebsd || openbsd // +build darwin freebsd openbsd package process diff --git a/process/process_darwin.go b/process/process_darwin.go index e43dbd875..c1dc918e9 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -1,3 +1,4 @@ +//go:build darwin // +build darwin package process @@ -125,7 +126,7 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { elapsedDurations = append(elapsedDurations, time.Duration(p)) } - var elapsed = time.Duration(elapsedDurations[0]) * time.Second + elapsed := time.Duration(elapsedDurations[0]) * time.Second if len(elapsedDurations) > 1 { elapsed += time.Duration(elapsedDurations[1]) * time.Minute } @@ -305,7 +306,6 @@ func convertCPUTimes(s string) (ret float64, err error) { func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { r, err := callPsWithContext(ctx, "utime,stime", p.Pid, false, false) - if err != nil { return nil, err } diff --git a/process/process_darwin_386.go b/process/process_darwin_386.go index f8e922385..b353e5eac 100644 --- a/process/process_darwin_386.go +++ b/process/process_darwin_386.go @@ -218,10 +218,12 @@ type AuditinfoAddr struct { Asid int32 Flags uint64 } + type AuMask struct { Success uint32 Failure uint32 } + type AuTidAddr struct { Port int32 Type uint32 diff --git a/process/process_darwin_amd64.go b/process/process_darwin_amd64.go index f8e922385..b353e5eac 100644 --- a/process/process_darwin_amd64.go +++ b/process/process_darwin_amd64.go @@ -218,10 +218,12 @@ type AuditinfoAddr struct { Asid int32 Flags uint64 } + type AuMask struct { Success uint32 Failure uint32 } + type AuTidAddr struct { Port int32 Type uint32 diff --git a/process/process_darwin_arm64.go b/process/process_darwin_arm64.go index 92bd4259b..cbd6bdc79 100644 --- a/process/process_darwin_arm64.go +++ b/process/process_darwin_arm64.go @@ -1,5 +1,6 @@ -// +build darwin -// +build arm64 +//go:build darwin && arm64 +// +build darwin,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs process/types_darwin.go diff --git a/process/process_darwin_cgo.go b/process/process_darwin_cgo.go index 9b9d39345..972b74b20 100644 --- a/process/process_darwin_cgo.go +++ b/process/process_darwin_cgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build cgo +//go:build darwin && cgo +// +build darwin,cgo package process @@ -10,6 +10,7 @@ package process // #include // #include import "C" + import ( "bytes" "context" diff --git a/process/process_darwin_nocgo.go b/process/process_darwin_nocgo.go index 91f2fc6a8..1426c97da 100644 --- a/process/process_darwin_nocgo.go +++ b/process/process_darwin_nocgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build !cgo +//go:build darwin && !cgo +// +build darwin,!cgo package process diff --git a/process/process_fallback.go b/process/process_fallback.go index 2638d8c2e..e1c4809d7 100644 --- a/process/process_fallback.go +++ b/process/process_fallback.go @@ -1,3 +1,4 @@ +//go:build !darwin && !linux && !freebsd && !openbsd && !windows && !solaris && !plan9 // +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!plan9 package process @@ -27,8 +28,7 @@ type MemoryMapsStat struct { Swap uint64 `json:"swap"` } -type MemoryInfoExStat struct { -} +type MemoryInfoExStat struct{} func pidsWithContext(ctx context.Context) ([]int32, error) { return nil, common.ErrNotImplementedError diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 63f0136f1..a010383e8 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -1,3 +1,4 @@ +//go:build freebsd // +build freebsd package process diff --git a/process/process_freebsd_arm64.go b/process/process_freebsd_arm64.go index 22679fd4d..effd470a0 100644 --- a/process/process_freebsd_arm64.go +++ b/process/process_freebsd_arm64.go @@ -1,5 +1,6 @@ -// +build freebsd -// +build arm64 +//go:build freebsd && arm64 +// +build freebsd,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs process/types_freebsd.go diff --git a/process/process_linux.go b/process/process_linux.go index 670dffe5e..442b40a21 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package process @@ -251,7 +252,7 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ( case RLIMIT_AS: rs.Used = uint64(p.memInfo.VMS) case RLIMIT_LOCKS: - //TODO we can get the used value from /proc/$pid/locks. But linux doesn't enforce it, so not a high priority. + // TODO we can get the used value from /proc/$pid/locks. But linux doesn't enforce it, so not a high priority. case RLIMIT_SIGPENDING: rs.Used = p.sigInfo.PendingProcess case RLIMIT_NICE: @@ -345,7 +346,6 @@ func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, e return nil, err } return pageFaults, nil - } func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { @@ -548,7 +548,7 @@ func (p *Process) fillFromLimitsWithContext() ([]RlimitStat, error) { // Remove last item from string str = str[:len(str)-1] - //Now last item is a Soft limit + // Now last item is a Soft limit statItem.Soft, err = limitToUint(str[len(str)-1]) if err != nil { return nil, err @@ -556,7 +556,7 @@ func (p *Process) fillFromLimitsWithContext() ([]RlimitStat, error) { // Remove last item from string str = str[:len(str)-1] - //The rest is a stats name + // The rest is a stats name resourceName := strings.Join(str, " ") switch resourceName { case "Max cpu time": diff --git a/process/process_linux_test.go b/process/process_linux_test.go index a374b4cd2..b98707f82 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package process diff --git a/process/process_openbsd.go b/process/process_openbsd.go index 9878fd7e3..cdbdaedc3 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -1,3 +1,4 @@ +//go:build openbsd // +build openbsd package process @@ -80,7 +81,6 @@ func (p *Process) ExeWithContext(ctx context.Context) (string, error) { func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { mib := []int32{CTLKern, KernProcArgs, p.Pid, KernProcArgv} buf, _, err := common.CallSyscall(mib) - if err != nil { return nil, err } @@ -318,7 +318,6 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) { results := []*Process{} buf, length, err := callKernProcSyscall(KernProcAll, 0) - if err != nil { return results, err } diff --git a/process/process_openbsd_386.go b/process/process_openbsd_386.go index b89fb8dc2..f4ed02491 100644 --- a/process/process_openbsd_386.go +++ b/process/process_openbsd_386.go @@ -1,5 +1,6 @@ -// +build openbsd -// +build 386 +//go:build openbsd && 386 +// +build openbsd,386 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs process/types_openbsd.go diff --git a/process/process_openbsd_arm64.go b/process/process_openbsd_arm64.go index 2d97fbc5c..a3291b8ca 100644 --- a/process/process_openbsd_arm64.go +++ b/process/process_openbsd_arm64.go @@ -1,5 +1,6 @@ -// +build openbsd -// +build arm64 +//go:build openbsd && arm64 +// +build openbsd,arm64 + // Code generated by cmd/cgo -godefs; DO NOT EDIT. // cgo -godefs process/types_openbsd.go diff --git a/process/process_plan9.go b/process/process_plan9.go index ff4a5cf4f..70e11e3aa 100644 --- a/process/process_plan9.go +++ b/process/process_plan9.go @@ -1,3 +1,4 @@ +//go:build plan9 // +build plan9 package process @@ -27,8 +28,7 @@ type MemoryMapsStat struct { Swap uint64 `json:"swap"` } -type MemoryInfoExStat struct { -} +type MemoryInfoExStat struct{} func pidsWithContext(ctx context.Context) ([]int32, error) { return nil, common.ErrNotImplementedError diff --git a/process/process_posix.go b/process/process_posix.go index 11cf9f789..88e2bff53 100644 --- a/process/process_posix.go +++ b/process/process_posix.go @@ -1,3 +1,4 @@ +//go:build linux || freebsd || openbsd || darwin || solaris // +build linux freebsd openbsd darwin solaris package process diff --git a/process/process_posix_test.go b/process/process_posix_test.go index a5cacb3b1..201a58c90 100644 --- a/process/process_posix_test.go +++ b/process/process_posix_test.go @@ -1,3 +1,4 @@ +//go:build linux || freebsd // +build linux freebsd package process diff --git a/process/process_race_test.go b/process/process_race_test.go index fd444a896..93c078d50 100644 --- a/process/process_race_test.go +++ b/process/process_race_test.go @@ -1,3 +1,4 @@ +//go:build race // +build race package process diff --git a/process/process_solaris.go b/process/process_solaris.go index 2b695af6c..93a6e01e2 100644 --- a/process/process_solaris.go +++ b/process/process_solaris.go @@ -27,8 +27,7 @@ type MemoryMapsStat struct { Swap uint64 `json:"swap"` } -type MemoryInfoExStat struct { -} +type MemoryInfoExStat struct{} func pidsWithContext(ctx context.Context) ([]int32, error) { return readPidsFromDir(common.HostProc()) diff --git a/process/process_test.go b/process/process_test.go index 07b8fc515..2bb72b8dd 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -65,6 +65,7 @@ func Test_Pids_Fail(t *testing.T) { t.Errorf("wrong getted pid nums: %v/%d", ret, len(ret)) } } + func Test_Pid_exists(t *testing.T) { checkPid := os.Getpid() @@ -93,7 +94,6 @@ func Test_NewProcess(t *testing.T) { t.Errorf("error %v", ret) } } - } func Test_Process_memory_maps(t *testing.T) { @@ -131,6 +131,7 @@ func Test_Process_memory_maps(t *testing.T) { t.Errorf("memory map is empty") } } + func Test_Process_MemoryInfo(t *testing.T) { p := testGetProcess() @@ -316,6 +317,7 @@ func Test_Process_Name(t *testing.T) { t.Errorf("invalid Exe %s", n) } } + func Test_Process_Long_Name_With_Spaces(t *testing.T) { tmpdir, err := ioutil.TempDir("", "") if err != nil { @@ -361,6 +363,7 @@ func Test_Process_Long_Name_With_Spaces(t *testing.T) { } cmd.Process.Kill() } + func Test_Process_Long_Name(t *testing.T) { tmpdir, err := ioutil.TempDir("", "") if err != nil { @@ -406,6 +409,7 @@ func Test_Process_Long_Name(t *testing.T) { } cmd.Process.Kill() } + func Test_Process_Exe(t *testing.T) { p := testGetProcess() diff --git a/process/process_windows.go b/process/process_windows.go index f502720cf..282b0d9dd 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package process @@ -70,11 +71,9 @@ type systemInfo struct { } // Memory_info_ex is different between OSes -type MemoryInfoExStat struct { -} +type MemoryInfoExStat struct{} -type MemoryMapsStat struct { -} +type MemoryMapsStat struct{} // ioCounters is an equivalent representation of IO_COUNTERS in the Windows API. // https://docs.microsoft.com/windows/win32/api/winnt/ns-winnt-io_counters @@ -195,8 +194,10 @@ type winTokenPriviledges struct { Privileges [1]winLUIDAndAttributes } -type winLong int32 -type winDWord uint32 +type ( + winLong int32 + winDWord uint32 +) func init() { var systemInfo systemInfo @@ -261,7 +262,6 @@ func pidsWithContext(ctx context.Context) ([]int32, error) { return ret, nil } - } func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) { @@ -431,7 +431,7 @@ func (p *Process) CwdWithContext(_ context.Context) (string, error) { } } - //if we reach here, we have no cwd + // if we reach here, we have no cwd return "", nil } @@ -1018,14 +1018,14 @@ func is32BitProcess(h windows.Handle) bool { procIs32Bits = true } } else { - //if the OS does not support the call, we fallback into the bitness of the app + // if the OS does not support the call, we fallback into the bitness of the app if unsafe.Sizeof(wow64) == 4 { procIs32Bits = true } } default: - //for other unknown platforms, we rely on process platform + // for other unknown platforms, we rely on process platform if unsafe.Sizeof(processorArchitecture) == 8 { procIs32Bits = false } else { @@ -1159,7 +1159,7 @@ func getProcessCommandLine(pid int32) (string, error) { } } - //if we reach here, we have no command line + // if we reach here, we have no command line return "", nil } diff --git a/process/process_windows_386.go b/process/process_windows_386.go index 1223be648..982287d93 100644 --- a/process/process_windows_386.go +++ b/process/process_windows_386.go @@ -28,7 +28,7 @@ type PROCESS_MEMORY_COUNTERS struct { func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { if is32BitProcess { - //we are on a 32-bit process reading an external 32-bit process + // we are on a 32-bit process reading an external 32-bit process var info processBasicInformation32 ret, _, _ := common.ProcNtQueryInformationProcess.Call( @@ -44,8 +44,8 @@ func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, er return 0, windows.NTStatus(ret) } } else { - //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 + if common.ProcNtWow64QueryInformationProcess64.Find() == nil { // avoid panic var info processBasicInformation64 ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( @@ -83,19 +83,19 @@ func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, si return buffer[:read] } } else { - //reading a 64-bit process from a 32-bit one - if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { //avoid panic + // reading a 64-bit process from a 32-bit one + if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { // avoid panic var read uint64 buffer := make([]byte, size) ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call( uintptr(h), - uintptr(address&0xFFFFFFFF), //the call expects a 64-bit value + uintptr(address&0xFFFFFFFF), // the call expects a 64-bit value uintptr(address>>32), uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), //the call expects a 64-bit value - uintptr(0), //but size is 32-bit so pass zero as the high dword + uintptr(size), // the call expects a 64-bit value + uintptr(0), // but size is 32-bit so pass zero as the high dword uintptr(unsafe.Pointer(&read)), ) if int(ret) >= 0 && read > 0 { @@ -104,6 +104,6 @@ func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, si } } - //if we reach here, an error happened + // if we reach here, an error happened return nil } diff --git a/process/process_windows_amd64.go b/process/process_windows_amd64.go index 79844b52f..74c6212cf 100644 --- a/process/process_windows_amd64.go +++ b/process/process_windows_amd64.go @@ -26,7 +26,7 @@ type PROCESS_MEMORY_COUNTERS struct { func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { if is32BitProcess { - //we are on a 64-bit process reading an external 32-bit process + // we are on a 64-bit process reading an external 32-bit process var wow64 uint ret, _, _ := common.ProcNtQueryInformationProcess.Call( @@ -42,7 +42,7 @@ func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, er return 0, windows.NTStatus(ret) } } else { - //we are on a 64-bit process reading an external 64-bit process + // we are on a 64-bit process reading an external 64-bit process var info processBasicInformation64 ret, _, _ := common.ProcNtQueryInformationProcess.Call( diff --git a/process/types_darwin.go b/process/types_darwin.go index 8be5f582c..3cde887d3 100644 --- a/process/types_darwin.go +++ b/process/types_darwin.go @@ -5,6 +5,7 @@ // Hand Writing // - all pointer in ExternProc to uint64 +//go:build ignore // +build ignore /* @@ -153,9 +154,11 @@ type Posix_cred C.struct_posix_cred type Label C.struct_label -type AuditinfoAddr C.struct_auditinfo_addr -type AuMask C.struct_au_mask -type AuTidAddr C.struct_au_tid_addr +type ( + AuditinfoAddr C.struct_auditinfo_addr + AuMask C.struct_au_mask + AuTidAddr C.struct_au_tid_addr +) // TAILQ(ucred) type UcredQueue C.struct_ucred_queue diff --git a/process/types_freebsd.go b/process/types_freebsd.go index aa7b3462d..658d46166 100644 --- a/process/types_freebsd.go +++ b/process/types_freebsd.go @@ -1,3 +1,4 @@ +//go:build ignore // +build ignore // We still need editing by hands. diff --git a/process/types_openbsd.go b/process/types_openbsd.go index 09ac59028..75f2344a9 100644 --- a/process/types_openbsd.go +++ b/process/types_openbsd.go @@ -1,3 +1,4 @@ +//go:build ignore // +build ignore // We still need editing by hands. diff --git a/winservices/manager.go b/winservices/manager.go index 1f1f9378a..c9957f729 100644 --- a/winservices/manager.go +++ b/winservices/manager.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package winservices diff --git a/winservices/winservices.go b/winservices/winservices.go index 4112b2cc6..93ec0e104 100644 --- a/winservices/winservices.go +++ b/winservices/winservices.go @@ -1,3 +1,4 @@ +//go:build windows // +build windows package winservices From 6bad729ebde2967fc8b6729cdec11d391eae0349 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 23:02:08 +0100 Subject: [PATCH 10/25] enable gofmt linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 29a6dc2e1..0ecc10131 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,7 @@ linters: - durationcheck - errorlint - gci + - gofmt - gosimple - typecheck - unparam From 08a73c90a649cebcb4652b2e9706e229ade5a7ec Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 23:09:23 +0100 Subject: [PATCH 11/25] Delete run-commit.yml --- .github/workflows/run-commit.yml | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 .github/workflows/run-commit.yml diff --git a/.github/workflows/run-commit.yml b/.github/workflows/run-commit.yml deleted file mode 100644 index 5e7941524..000000000 --- a/.github/workflows/run-commit.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Run and commit -on: - workflow_dispatch: - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-go@v2 - with: - go-version: 1.17 - - run: | - go install mvdan.cc/gofumpt@latest - gofumpt -l -w . - - name: Commit changes - uses: EndBug/add-and-commit@v7 - with: - author_name: ${{ github.actor }} - author_email: ${{ github.actor }}@users.noreply.github.com - message: 'gofumpt' From e232c1083c5e1389abe21f297bb615285f306955 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 23:09:58 +0100 Subject: [PATCH 12/25] enable gofumpt linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 0ecc10131..9053da4c0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,6 +6,7 @@ linters: - errorlint - gci - gofmt + - gofumpt - gosimple - typecheck - unparam From 982ee3bc2bc90dfb51b79fa3fd64544b02673f91 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 23:19:22 +0100 Subject: [PATCH 13/25] enable misspell linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 9053da4c0..82d9a31a2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,7 @@ linters: - gofmt - gofumpt - gosimple + - misspell - typecheck - unparam disable: From 4671e649aa7e9674ed2cb91803eb8b38bec1cf21 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 23:49:50 +0100 Subject: [PATCH 14/25] enable revive linter --- .golangci.yml | 8 ++++++++ disk/disk_linux.go | 3 +-- host/types.go | 3 +-- process/process_linux.go | 11 +++++------ 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 82d9a31a2..d4628c2db 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,12 @@ issues: max-same-issues: 0 + exclude-rules: + - linters: + - revive + text: "var-naming" + - linters: + - revive + text: "exported" linters: enable: - durationcheck @@ -9,6 +16,7 @@ linters: - gofumpt - gosimple - misspell + - revive - typecheck - unparam disable: diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 24337d091..5a0cf469e 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -486,9 +486,8 @@ func LabelWithContext(ctx context.Context, name string) (string, error) { dmname, err := ioutil.ReadFile(dmname_filename) if err != nil { return "", err - } else { - return strings.TrimSpace(string(dmname)), nil } + return strings.TrimSpace(string(dmname)), nil } func getFsType(stat unix.Statfs_t) string { diff --git a/host/types.go b/host/types.go index 1eff4755e..c2e7c0bda 100644 --- a/host/types.go +++ b/host/types.go @@ -15,9 +15,8 @@ func (w *Warnings) Add(err error) { func (w *Warnings) Reference() error { if len(w.List) > 0 { return w - } else { - return nil } + return nil } func (w *Warnings) Error() string { diff --git a/process/process_linux.go b/process/process_linux.go index 442b40a21..af8f23296 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -503,13 +503,12 @@ func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) { func limitToUint(val string) (uint64, error) { if val == "unlimited" { return math.MaxUint64, nil - } else { - res, err := strconv.ParseUint(val, 10, 64) - if err != nil { - return 0, err - } - return res, nil } + res, err := strconv.ParseUint(val, 10, 64) + if err != nil { + return 0, err + } + return res, nil } // Get num_fds from /proc/(pid)/limits From 227e7b8b8c53e8b0d5416d1ea11a465ade38caa5 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Wed, 22 Dec 2021 23:59:55 +0100 Subject: [PATCH 15/25] enable nolintlint linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index d4628c2db..50efd1a51 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,6 +16,7 @@ linters: - gofumpt - gosimple - misspell + - nolintlint - revive - typecheck - unparam From 5801744ecc73090d183ebe03720b776dcec2f84f Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 23 Dec 2021 00:10:20 +0100 Subject: [PATCH 16/25] enable asciicheck linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 50efd1a51..a7f8c36e9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,6 +9,7 @@ issues: text: "exported" linters: enable: + - asciicheck - durationcheck - errorlint - gci From 851bffc0f1b2fe99c099190450cb1c20d98cd254 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 23 Dec 2021 00:31:04 +0100 Subject: [PATCH 17/25] enable gosec linter --- .golangci.yml | 4 ++++ internal/common/common_unix.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index a7f8c36e9..3dc09dfd4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,6 +1,9 @@ issues: max-same-issues: 0 exclude-rules: + - linters: + - gosec + text: "G204" - linters: - revive text: "var-naming" @@ -15,6 +18,7 @@ linters: - gci - gofmt - gofumpt + - gosec - gosimple - misspell - nolintlint diff --git a/internal/common/common_unix.go b/internal/common/common_unix.go index a4a953a2f..e0170f939 100644 --- a/internal/common/common_unix.go +++ b/internal/common/common_unix.go @@ -57,7 +57,7 @@ func CallPgrepWithContext(ctx context.Context, invoke Invoker, pid int32) ([]int if len(l) == 0 { continue } - i, err := strconv.Atoi(l) + i, err := strconv.ParseInt(l, 10, 32) if err != nil { continue } From f8c685e7174290ef5c0697bb763390b697a01fea Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 23 Dec 2021 00:46:14 +0100 Subject: [PATCH 18/25] enable caching in workflows --- .github/workflows/build_test.yml | 14 +++++++++++++- .github/workflows/test.yml | 14 +++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index 31559b1f0..ec565ac9b 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -13,6 +13,18 @@ jobs: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 + - id: cache-paths + run: | + echo "::set-output name=cache::$(go env GOCACHE)" + echo "::set-output name=mod-cache::$(go env GOMODCACHE)" + - name: Cache go modules + uses: actions/cache@v2 + with: + path: | + ${{ steps.cache-paths.outputs.cache }} + ${{ steps.cache-paths.outputs.mod-cache }} + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go- - name: Build Test v3 run: | - make build_test \ No newline at end of file + make build_test diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5ee75d66a..0f015a75f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,6 +14,18 @@ jobs: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 + - id: cache-paths + run: | + echo "::set-output name=cache::$(go env GOCACHE)" + echo "::set-output name=mod-cache::$(go env GOMODCACHE)" + - name: Cache go modules + uses: actions/cache@v2 + with: + path: | + ${{ steps.cache-paths.outputs.cache }} + ${{ steps.cache-paths.outputs.mod-cache }} + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: ${{ runner.os }}-go- - name: Test run: | - go test ./... \ No newline at end of file + go test ./... From 5c5245372dcd39261d4e2e79791701096e1dc4f1 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 23 Dec 2021 00:54:07 +0100 Subject: [PATCH 19/25] enable predeclared linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 3dc09dfd4..7dff533e0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,7 @@ linters: - gosimple - misspell - nolintlint + - predeclared - revive - typecheck - unparam From 2252055b935728aa94bf91380e455316a2c8790f Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 23 Dec 2021 08:39:43 +0100 Subject: [PATCH 20/25] enable codecoverage --- .github/workflows/test.yml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0f015a75f..7d21dd664 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,7 @@ jobs: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v2 - - id: cache-paths + - id: go-env run: | echo "::set-output name=cache::$(go env GOCACHE)" echo "::set-output name=mod-cache::$(go env GOMODCACHE)" @@ -22,10 +22,17 @@ jobs: uses: actions/cache@v2 with: path: | - ${{ steps.cache-paths.outputs.cache }} - ${{ steps.cache-paths.outputs.mod-cache }} + ${{ steps.go-env.outputs.cache }} + ${{ steps.go-env.outputs.mod-cache }} key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: ${{ runner.os }}-go- - name: Test run: | - go test ./... + go test -coverprofile='coverage.out' -covermode=atomic ./... + - name: Upload Code Coverage + uses: codecov/codecov-action@v2 + with: + fail_ci_if_error: true + files: coverage.out + flags: ${{ matrix.os }},go-${{ matrix.go-version }} + token: ${{ secrets.CODECOV_TOKEN }} From 803be09d63aaaa9496b3440025d03cf644051edd Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Fri, 24 Dec 2021 21:25:34 +0100 Subject: [PATCH 21/25] enable megacheck linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 7dff533e0..382904d14 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,6 +20,7 @@ linters: - gofumpt - gosec - gosimple + - megacheck - misspell - nolintlint - predeclared From 0f342652adf92145e15ea373df3d0f3f84c939d0 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Fri, 24 Dec 2021 21:32:30 +0100 Subject: [PATCH 22/25] enable nakedret linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 382904d14..ec26c57e3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,7 @@ linters: - gosimple - megacheck - misspell + - nakedret - nolintlint - predeclared - revive From e6e3f9399bbed3ff37a4eb05bc4d3abac9655e96 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Fri, 24 Dec 2021 21:46:59 +0100 Subject: [PATCH 23/25] enable importas linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index ec26c57e3..4a12bb073 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,6 +20,7 @@ linters: - gofumpt - gosec - gosimple + - importas - megacheck - misspell - nakedret From a9e4ce7cf97f154ca483b98924b53b947ebcd450 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Fri, 24 Dec 2021 21:54:45 +0100 Subject: [PATCH 24/25] enable goimports linter --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 4a12bb073..a193ec465 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,6 +18,7 @@ linters: - gci - gofmt - gofumpt + - goimports - gosec - gosimple - importas From fe2fab49384c623c8034582051b2229550cb0d2b Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 27 Dec 2021 18:25:31 +0100 Subject: [PATCH 25/25] Update test.yml --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7d21dd664..82970dbe0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,5 +34,5 @@ jobs: with: fail_ci_if_error: true files: coverage.out - flags: ${{ matrix.os }},go-${{ matrix.go-version }} + flags: ${{ runner.os }},go-${{ matrix.go-version }} token: ${{ secrets.CODECOV_TOKEN }}