diff --git a/.golangci.yml b/.golangci.yml index 0a2a4c3..4bb1a74 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 ea40244..563a78e 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 c981d99..cfe68d9 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 50fb2dc..08c087b 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 5987131..670dffe 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 afeaaee..a374b4c 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() } }