diff --git a/.golangci.yml b/.golangci.yml index a193ec465..b175f43ff 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -13,6 +13,7 @@ issues: linters: enable: - asciicheck + - contextcheck - durationcheck - errorlint - gci diff --git a/cpu/cpu.go b/cpu/cpu.go index 411a063a9..831440d09 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -142,11 +142,11 @@ func Percent(interval time.Duration, percpu bool) ([]float64, error) { func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool) ([]float64, error) { if interval <= 0 { - return percentUsedFromLastCall(percpu) + return percentUsedFromLastCallWithContext(ctx, percpu) } // Get CPU usage at the start of the interval. - cpuTimes1, err := Times(percpu) + cpuTimes1, err := TimesWithContext(ctx, percpu) if err != nil { return nil, err } @@ -156,7 +156,7 @@ func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool } // And at the end of the interval. - cpuTimes2, err := Times(percpu) + cpuTimes2, err := TimesWithContext(ctx, percpu) if err != nil { return nil, err } @@ -165,7 +165,11 @@ func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool } func percentUsedFromLastCall(percpu bool) ([]float64, error) { - cpuTimes, err := Times(percpu) + return percentUsedFromLastCallWithContext(context.Background(), percpu) +} + +func percentUsedFromLastCallWithContext(ctx context.Context, percpu bool) ([]float64, error) { + cpuTimes, err := TimesWithContext(ctx, percpu) if err != nil { return nil, err } diff --git a/docker/docker_linux.go b/docker/docker_linux.go index edcb8e4d4..4c5d1520a 100644 --- a/docker/docker_linux.go +++ b/docker/docker_linux.go @@ -162,11 +162,11 @@ func CgroupCPUUsageDocker(containerid string) (float64, error) { } func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*CgroupCPUStat, error) { - return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker")) + return CgroupCPUWithContext(ctx, containerid, common.HostSys("fs/cgroup/cpuacct/docker")) } func CgroupCPUDockerUsageWithContext(ctx context.Context, containerid string) (float64, error) { - return CgroupCPUUsage(containerid, common.HostSys("fs/cgroup/cpuacct/docker")) + return CgroupCPUUsageWithContext(ctx, containerid, common.HostSys("fs/cgroup/cpuacct/docker")) } func CgroupMem(containerID string, base string) (*CgroupMemStat, error) { @@ -274,7 +274,7 @@ func CgroupMemDocker(containerID string) (*CgroupMemStat, error) { } func CgroupMemDockerWithContext(ctx context.Context, containerID string) (*CgroupMemStat, error) { - return CgroupMem(containerID, common.HostSys("fs/cgroup/memory/docker")) + return CgroupMemWithContext(ctx, containerID, common.HostSys("fs/cgroup/memory/docker")) } // getCgroupFilePath constructs file path to get targeted stats file. diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index ffdc93128..0f7b40c41 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -56,7 +56,7 @@ func NumProcs() (uint64, error) { } func BootTimeWithContext(ctx context.Context) (uint64, error) { - system, role, err := Virtualization() + system, role, err := VirtualizationWithContext(ctx) if err != nil { return 0, err } diff --git a/net/net_linux.go b/net/net_linux.go index 0d909fc0a..81cde8133 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -392,7 +392,7 @@ func Connections(kind string) ([]ConnectionStat, error) { } func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) { - return ConnectionsPid(kind, 0) + return ConnectionsPidWithContext(ctx, kind, 0) } // Return a list of network connections opened returning at most `max` @@ -402,7 +402,7 @@ func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) { } func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) { - return ConnectionsPidMax(kind, 0, max) + return ConnectionsPidMaxWithContext(ctx, kind, 0, max) } // Return a list of network connections opened, omitting `Uids`. @@ -463,7 +463,7 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p var err error var inodes map[string][]inodeMap if pid == 0 { - inodes, err = getProcInodesAll(root, max) + inodes, err = getProcInodesAllWithContext(ctx, root, max) } else { inodes, err = getProcInodes(root, pid, max) if len(inodes) == 0 { @@ -474,10 +474,14 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p if err != nil { return nil, fmt.Errorf("cound not get pid(s), %d: %w", pid, err) } - return statsFromInodes(root, pid, tmap, inodes, skipUids) + return statsFromInodesWithContext(ctx, root, pid, tmap, inodes, skipUids) } func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap, skipUids bool) ([]ConnectionStat, error) { + return statsFromInodesWithContext(context.Background(), root, pid, tmap, inodes, skipUids) +} + +func statsFromInodesWithContext(ctx context.Context, root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap, skipUids bool) ([]ConnectionStat, error) { dupCheckMap := make(map[string]struct{}) var ret []ConnectionStat @@ -493,7 +497,7 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode } switch t.family { case syscall.AF_INET, syscall.AF_INET6: - ls, err = processInet(path, t, inodes, pid) + ls, err = processInetWithContext(ctx, path, t, inodes, pid) case syscall.AF_UNIX: ls, err = processUnix(path, t, inodes, pid) } @@ -666,7 +670,11 @@ func (p *process) fillFromStatus() error { } func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) { - pids, err := Pids() + return getProcInodesAllWithContext(context.Background(), root, max) +} + +func getProcInodesAllWithContext(ctx context.Context, root string, max int) (map[string][]inodeMap, error) { + pids, err := PidsWithContext(ctx) if err != nil { return nil, err } @@ -695,6 +703,10 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) { // "0500000A:0016" -> "10.0.0.5", 22 // "0085002452100113070057A13F025401:0035" -> "2400:8500:1301:1052:a157:7:154:23f", 53 func decodeAddress(family uint32, src string) (Addr, error) { + return decodeAddressWithContext(context.Background(), family, src) +} + +func decodeAddressWithContext(ctx context.Context, family uint32, src string) (Addr, error) { t := strings.Split(src, ":") if len(t) != 2 { return Addr{}, fmt.Errorf("does not contain port, %s", src) @@ -711,9 +723,9 @@ func decodeAddress(family uint32, src string) (Addr, error) { var ip net.IP // Assumes this is little_endian if family == syscall.AF_INET { - ip = net.IP(Reverse(decoded)) + ip = net.IP(ReverseWithContext(ctx, decoded)) } else { // IPv6 - ip, err = parseIPv6HexString(decoded) + ip, err = parseIPv6HexStringWithContext(ctx, decoded) if err != nil { return Addr{}, err } @@ -738,19 +750,27 @@ func ReverseWithContext(ctx context.Context, s []byte) []byte { // parseIPv6HexString parse array of bytes to IPv6 string func parseIPv6HexString(src []byte) (net.IP, error) { + return parseIPv6HexStringWithContext(context.Background(), src) +} + +func parseIPv6HexStringWithContext(ctx context.Context, src []byte) (net.IP, error) { if len(src) != 16 { return nil, fmt.Errorf("invalid IPv6 string") } buf := make([]byte, 0, 16) for i := 0; i < len(src); i += 4 { - r := Reverse(src[i : i+4]) + r := ReverseWithContext(ctx, src[i:i+4]) buf = append(buf, r...) } return net.IP(buf), nil } func processInet(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) { + return processInetWithContext(context.Background(), file, kind, inodes, filterPid) +} + +func processInetWithContext(ctx context.Context, 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 @@ -793,11 +813,11 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in } else { status = "NONE" } - la, err := decodeAddress(kind.family, laddr) + la, err := decodeAddressWithContext(ctx, kind.family, laddr) if err != nil { continue } - ra, err := decodeAddress(kind.family, raddr) + ra, err := decodeAddressWithContext(ctx, kind.family, raddr) if err != nil { continue } diff --git a/process/process_linux.go b/process/process_linux.go index 45de82c47..df6c1401f 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -83,7 +83,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(); err != nil { + if err := p.fillNameWithContext(ctx); err != nil { return "", err } } @@ -92,7 +92,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(); err != nil { + if err := p.fillFromStatusWithContext(ctx); err != nil { return 0, err } } @@ -124,7 +124,7 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { } func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { - err := p.fillFromStatusWithContext() + err := p.fillFromStatusWithContext(ctx) if err != nil { return []string{""}, err } @@ -149,7 +149,7 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { } func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext() + err := p.fillFromStatusWithContext(ctx) if err != nil { return []int32{}, err } @@ -157,7 +157,7 @@ func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext() + err := p.fillFromStatusWithContext(ctx) if err != nil { return []int32{}, err } @@ -165,7 +165,7 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { } func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) { - err := p.fillFromStatusWithContext() + err := p.fillFromStatusWithContext(ctx) if err != nil { return []int32{}, err } @@ -211,7 +211,7 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ( if err != nil { return nil, err } - if err := p.fillFromStatusWithContext(); err != nil { + if err := p.fillFromStatusWithContext(ctx); err != nil { return nil, err } @@ -261,7 +261,7 @@ func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, e } func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) { - err := p.fillFromStatusWithContext() + err := p.fillFromStatusWithContext(ctx) if err != nil { return nil, err } @@ -274,7 +274,7 @@ func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { } func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - err := p.fillFromStatusWithContext() + err := p.fillFromStatusWithContext(ctx) if err != nil { return 0, err } @@ -789,12 +789,12 @@ func (p *Process) fillFromStatmWithContext() (*MemoryInfoStat, *MemoryInfoExStat } // Get name from /proc/(pid)/comm or /proc/(pid)/status -func (p *Process) fillNameWithContext() error { +func (p *Process) fillNameWithContext(ctx context.Context) error { err := p.fillFromCommWithContext() if err == nil && p.name != "" && len(p.name) < 15 { return nil } - return p.fillFromStatusWithContext() + return p.fillFromStatusWithContext(ctx) } // Get name from /proc/(pid)/comm @@ -811,7 +811,11 @@ func (p *Process) fillFromCommWithContext() error { } // Get various status from /proc/(pid)/status -func (p *Process) fillFromStatusWithContext() error { +func (p *Process) fillFromStatus() error { + return p.fillFromStatusWithContext(context.Background()) +} + +func (p *Process) fillFromStatusWithContext(ctx context.Context) error { pid := p.Pid statPath := common.HostProc(strconv.Itoa(int(pid)), "status") contents, err := ioutil.ReadFile(statPath) @@ -832,7 +836,7 @@ func (p *Process) fillFromStatusWithContext() error { case "Name": p.name = strings.Trim(value, " \t") if len(p.name) >= 15 { - cmdlineSlice, err := p.CmdlineSlice() + cmdlineSlice, err := p.CmdlineSliceWithContext(ctx) if err != nil { return err } @@ -1009,6 +1013,10 @@ func (p *Process) fillFromStatusWithContext() error { return nil } +func (p *Process) fillFromTIDStat(tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) { + return p.fillFromTIDStatWithContext(context.Background(), tid) +} + func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) { pid := p.Pid var statPath string diff --git a/process/process_linux_test.go b/process/process_linux_test.go index b98707f82..9003095be 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -4,7 +4,6 @@ package process import ( - "context" "fmt" "io/ioutil" "os" @@ -133,7 +132,7 @@ func Test_fillFromStatusWithContext(t *testing.T) { continue } p, _ := NewProcess(int32(pid)) - if err := p.fillFromStatusWithContext(); err != nil { + if err := p.fillFromStatus(); err != nil { t.Error(err) } } @@ -155,7 +154,7 @@ func Benchmark_fillFromStatusWithContext(b *testing.B) { pid := 1060 p, _ := NewProcess(int32(pid)) for i := 0; i < b.N; i++ { - p.fillFromStatusWithContext() + p.fillFromStatus() } } @@ -175,7 +174,7 @@ func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) { continue } p, _ := NewProcess(int32(pid)) - _, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStatWithContext(context.Background(), -1) + _, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStat(-1) if err != nil { t.Error(err) }