Skip to content

Commit 9a667f1

Browse files
authored
Merge pull request #1210 from mmorel-35/master
enable contextcheck linter
2 parents f6cc356 + b0469a4 commit 9a667f1

File tree

7 files changed

+68
-36
lines changed

7 files changed

+68
-36
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ issues:
1313
linters:
1414
enable:
1515
- asciicheck
16+
- contextcheck
1617
- durationcheck
1718
- errorlint
1819
- gci

cpu/cpu.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ func Percent(interval time.Duration, percpu bool) ([]float64, error) {
142142

143143
func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool) ([]float64, error) {
144144
if interval <= 0 {
145-
return percentUsedFromLastCall(percpu)
145+
return percentUsedFromLastCallWithContext(ctx, percpu)
146146
}
147147

148148
// Get CPU usage at the start of the interval.
149-
cpuTimes1, err := Times(percpu)
149+
cpuTimes1, err := TimesWithContext(ctx, percpu)
150150
if err != nil {
151151
return nil, err
152152
}
@@ -156,7 +156,7 @@ func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool
156156
}
157157

158158
// And at the end of the interval.
159-
cpuTimes2, err := Times(percpu)
159+
cpuTimes2, err := TimesWithContext(ctx, percpu)
160160
if err != nil {
161161
return nil, err
162162
}
@@ -165,7 +165,11 @@ func PercentWithContext(ctx context.Context, interval time.Duration, percpu bool
165165
}
166166

167167
func percentUsedFromLastCall(percpu bool) ([]float64, error) {
168-
cpuTimes, err := Times(percpu)
168+
return percentUsedFromLastCallWithContext(context.Background(), percpu)
169+
}
170+
171+
func percentUsedFromLastCallWithContext(ctx context.Context, percpu bool) ([]float64, error) {
172+
cpuTimes, err := TimesWithContext(ctx, percpu)
169173
if err != nil {
170174
return nil, err
171175
}

docker/docker_linux.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ func CgroupCPUUsageDocker(containerid string) (float64, error) {
162162
}
163163

164164
func CgroupCPUDockerWithContext(ctx context.Context, containerid string) (*CgroupCPUStat, error) {
165-
return CgroupCPU(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
165+
return CgroupCPUWithContext(ctx, containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
166166
}
167167

168168
func CgroupCPUDockerUsageWithContext(ctx context.Context, containerid string) (float64, error) {
169-
return CgroupCPUUsage(containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
169+
return CgroupCPUUsageWithContext(ctx, containerid, common.HostSys("fs/cgroup/cpuacct/docker"))
170170
}
171171

172172
func CgroupMem(containerID string, base string) (*CgroupMemStat, error) {
@@ -274,7 +274,7 @@ func CgroupMemDocker(containerID string) (*CgroupMemStat, error) {
274274
}
275275

276276
func CgroupMemDockerWithContext(ctx context.Context, containerID string) (*CgroupMemStat, error) {
277-
return CgroupMem(containerID, common.HostSys("fs/cgroup/memory/docker"))
277+
return CgroupMemWithContext(ctx, containerID, common.HostSys("fs/cgroup/memory/docker"))
278278
}
279279

280280
// getCgroupFilePath constructs file path to get targeted stats file.

internal/common/common_linux.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NumProcs() (uint64, error) {
5656
}
5757

5858
func BootTimeWithContext(ctx context.Context) (uint64, error) {
59-
system, role, err := Virtualization()
59+
system, role, err := VirtualizationWithContext(ctx)
6060
if err != nil {
6161
return 0, err
6262
}

net/net_linux.go

+31-11
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func Connections(kind string) ([]ConnectionStat, error) {
392392
}
393393

394394
func ConnectionsWithContext(ctx context.Context, kind string) ([]ConnectionStat, error) {
395-
return ConnectionsPid(kind, 0)
395+
return ConnectionsPidWithContext(ctx, kind, 0)
396396
}
397397

398398
// Return a list of network connections opened returning at most `max`
@@ -402,7 +402,7 @@ func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
402402
}
403403

404404
func ConnectionsMaxWithContext(ctx context.Context, kind string, max int) ([]ConnectionStat, error) {
405-
return ConnectionsPidMax(kind, 0, max)
405+
return ConnectionsPidMaxWithContext(ctx, kind, 0, max)
406406
}
407407

408408
// Return a list of network connections opened, omitting `Uids`.
@@ -463,7 +463,7 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
463463
var err error
464464
var inodes map[string][]inodeMap
465465
if pid == 0 {
466-
inodes, err = getProcInodesAll(root, max)
466+
inodes, err = getProcInodesAllWithContext(ctx, root, max)
467467
} else {
468468
inodes, err = getProcInodes(root, pid, max)
469469
if len(inodes) == 0 {
@@ -474,10 +474,14 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
474474
if err != nil {
475475
return nil, fmt.Errorf("cound not get pid(s), %d: %w", pid, err)
476476
}
477-
return statsFromInodes(root, pid, tmap, inodes, skipUids)
477+
return statsFromInodesWithContext(ctx, root, pid, tmap, inodes, skipUids)
478478
}
479479

480480
func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap, skipUids bool) ([]ConnectionStat, error) {
481+
return statsFromInodesWithContext(context.Background(), root, pid, tmap, inodes, skipUids)
482+
}
483+
484+
func statsFromInodesWithContext(ctx context.Context, root string, pid int32, tmap []netConnectionKindType, inodes map[string][]inodeMap, skipUids bool) ([]ConnectionStat, error) {
481485
dupCheckMap := make(map[string]struct{})
482486
var ret []ConnectionStat
483487

@@ -493,7 +497,7 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode
493497
}
494498
switch t.family {
495499
case syscall.AF_INET, syscall.AF_INET6:
496-
ls, err = processInet(path, t, inodes, pid)
500+
ls, err = processInetWithContext(ctx, path, t, inodes, pid)
497501
case syscall.AF_UNIX:
498502
ls, err = processUnix(path, t, inodes, pid)
499503
}
@@ -666,7 +670,11 @@ func (p *process) fillFromStatus() error {
666670
}
667671

668672
func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
669-
pids, err := Pids()
673+
return getProcInodesAllWithContext(context.Background(), root, max)
674+
}
675+
676+
func getProcInodesAllWithContext(ctx context.Context, root string, max int) (map[string][]inodeMap, error) {
677+
pids, err := PidsWithContext(ctx)
670678
if err != nil {
671679
return nil, err
672680
}
@@ -695,6 +703,10 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
695703
// "0500000A:0016" -> "10.0.0.5", 22
696704
// "0085002452100113070057A13F025401:0035" -> "2400:8500:1301:1052:a157:7:154:23f", 53
697705
func decodeAddress(family uint32, src string) (Addr, error) {
706+
return decodeAddressWithContext(context.Background(), family, src)
707+
}
708+
709+
func decodeAddressWithContext(ctx context.Context, family uint32, src string) (Addr, error) {
698710
t := strings.Split(src, ":")
699711
if len(t) != 2 {
700712
return Addr{}, fmt.Errorf("does not contain port, %s", src)
@@ -711,9 +723,9 @@ func decodeAddress(family uint32, src string) (Addr, error) {
711723
var ip net.IP
712724
// Assumes this is little_endian
713725
if family == syscall.AF_INET {
714-
ip = net.IP(Reverse(decoded))
726+
ip = net.IP(ReverseWithContext(ctx, decoded))
715727
} else { // IPv6
716-
ip, err = parseIPv6HexString(decoded)
728+
ip, err = parseIPv6HexStringWithContext(ctx, decoded)
717729
if err != nil {
718730
return Addr{}, err
719731
}
@@ -738,19 +750,27 @@ func ReverseWithContext(ctx context.Context, s []byte) []byte {
738750

739751
// parseIPv6HexString parse array of bytes to IPv6 string
740752
func parseIPv6HexString(src []byte) (net.IP, error) {
753+
return parseIPv6HexStringWithContext(context.Background(), src)
754+
}
755+
756+
func parseIPv6HexStringWithContext(ctx context.Context, src []byte) (net.IP, error) {
741757
if len(src) != 16 {
742758
return nil, fmt.Errorf("invalid IPv6 string")
743759
}
744760

745761
buf := make([]byte, 0, 16)
746762
for i := 0; i < len(src); i += 4 {
747-
r := Reverse(src[i : i+4])
763+
r := ReverseWithContext(ctx, src[i:i+4])
748764
buf = append(buf, r...)
749765
}
750766
return net.IP(buf), nil
751767
}
752768

753769
func processInet(file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
770+
return processInetWithContext(context.Background(), file, kind, inodes, filterPid)
771+
}
772+
773+
func processInetWithContext(ctx context.Context, file string, kind netConnectionKindType, inodes map[string][]inodeMap, filterPid int32) ([]connTmp, error) {
754774
if strings.HasSuffix(file, "6") && !common.PathExists(file) {
755775
// IPv6 not supported, return empty.
756776
return []connTmp{}, nil
@@ -793,11 +813,11 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
793813
} else {
794814
status = "NONE"
795815
}
796-
la, err := decodeAddress(kind.family, laddr)
816+
la, err := decodeAddressWithContext(ctx, kind.family, laddr)
797817
if err != nil {
798818
continue
799819
}
800-
ra, err := decodeAddress(kind.family, raddr)
820+
ra, err := decodeAddressWithContext(ctx, kind.family, raddr)
801821
if err != nil {
802822
continue
803823
}

process/process_linux.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
8383

8484
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
8585
if p.name == "" {
86-
if err := p.fillNameWithContext(); err != nil {
86+
if err := p.fillNameWithContext(ctx); err != nil {
8787
return "", err
8888
}
8989
}
@@ -92,7 +92,7 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
9292

9393
func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
9494
if p.tgid == 0 {
95-
if err := p.fillFromStatusWithContext(); err != nil {
95+
if err := p.fillFromStatusWithContext(ctx); err != nil {
9696
return 0, err
9797
}
9898
}
@@ -124,7 +124,7 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
124124
}
125125

126126
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
127-
err := p.fillFromStatusWithContext()
127+
err := p.fillFromStatusWithContext(ctx)
128128
if err != nil {
129129
return []string{""}, err
130130
}
@@ -149,23 +149,23 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
149149
}
150150

151151
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
152-
err := p.fillFromStatusWithContext()
152+
err := p.fillFromStatusWithContext(ctx)
153153
if err != nil {
154154
return []int32{}, err
155155
}
156156
return p.uids, nil
157157
}
158158

159159
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
160-
err := p.fillFromStatusWithContext()
160+
err := p.fillFromStatusWithContext(ctx)
161161
if err != nil {
162162
return []int32{}, err
163163
}
164164
return p.gids, nil
165165
}
166166

167167
func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) {
168-
err := p.fillFromStatusWithContext()
168+
err := p.fillFromStatusWithContext(ctx)
169169
if err != nil {
170170
return []int32{}, err
171171
}
@@ -211,7 +211,7 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) (
211211
if err != nil {
212212
return nil, err
213213
}
214-
if err := p.fillFromStatusWithContext(); err != nil {
214+
if err := p.fillFromStatusWithContext(ctx); err != nil {
215215
return nil, err
216216
}
217217

@@ -261,7 +261,7 @@ func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, e
261261
}
262262

263263
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
264-
err := p.fillFromStatusWithContext()
264+
err := p.fillFromStatusWithContext(ctx)
265265
if err != nil {
266266
return nil, err
267267
}
@@ -274,7 +274,7 @@ func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
274274
}
275275

276276
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
277-
err := p.fillFromStatusWithContext()
277+
err := p.fillFromStatusWithContext(ctx)
278278
if err != nil {
279279
return 0, err
280280
}
@@ -789,12 +789,12 @@ func (p *Process) fillFromStatmWithContext() (*MemoryInfoStat, *MemoryInfoExStat
789789
}
790790

791791
// Get name from /proc/(pid)/comm or /proc/(pid)/status
792-
func (p *Process) fillNameWithContext() error {
792+
func (p *Process) fillNameWithContext(ctx context.Context) error {
793793
err := p.fillFromCommWithContext()
794794
if err == nil && p.name != "" && len(p.name) < 15 {
795795
return nil
796796
}
797-
return p.fillFromStatusWithContext()
797+
return p.fillFromStatusWithContext(ctx)
798798
}
799799

800800
// Get name from /proc/(pid)/comm
@@ -811,7 +811,11 @@ func (p *Process) fillFromCommWithContext() error {
811811
}
812812

813813
// Get various status from /proc/(pid)/status
814-
func (p *Process) fillFromStatusWithContext() error {
814+
func (p *Process) fillFromStatus() error {
815+
return p.fillFromStatusWithContext(context.Background())
816+
}
817+
818+
func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
815819
pid := p.Pid
816820
statPath := common.HostProc(strconv.Itoa(int(pid)), "status")
817821
contents, err := ioutil.ReadFile(statPath)
@@ -832,7 +836,7 @@ func (p *Process) fillFromStatusWithContext() error {
832836
case "Name":
833837
p.name = strings.Trim(value, " \t")
834838
if len(p.name) >= 15 {
835-
cmdlineSlice, err := p.CmdlineSlice()
839+
cmdlineSlice, err := p.CmdlineSliceWithContext(ctx)
836840
if err != nil {
837841
return err
838842
}
@@ -1009,6 +1013,10 @@ func (p *Process) fillFromStatusWithContext() error {
10091013
return nil
10101014
}
10111015

1016+
func (p *Process) fillFromTIDStat(tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) {
1017+
return p.fillFromTIDStatWithContext(context.Background(), tid)
1018+
}
1019+
10121020
func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, *PageFaultsStat, error) {
10131021
pid := p.Pid
10141022
var statPath string

process/process_linux_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package process
55

66
import (
7-
"context"
87
"fmt"
98
"io/ioutil"
109
"os"
@@ -133,7 +132,7 @@ func Test_fillFromStatusWithContext(t *testing.T) {
133132
continue
134133
}
135134
p, _ := NewProcess(int32(pid))
136-
if err := p.fillFromStatusWithContext(); err != nil {
135+
if err := p.fillFromStatus(); err != nil {
137136
t.Error(err)
138137
}
139138
}
@@ -155,7 +154,7 @@ func Benchmark_fillFromStatusWithContext(b *testing.B) {
155154
pid := 1060
156155
p, _ := NewProcess(int32(pid))
157156
for i := 0; i < b.N; i++ {
158-
p.fillFromStatusWithContext()
157+
p.fillFromStatus()
159158
}
160159
}
161160

@@ -175,7 +174,7 @@ func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) {
175174
continue
176175
}
177176
p, _ := NewProcess(int32(pid))
178-
_, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStatWithContext(context.Background(), -1)
177+
_, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStat(-1)
179178
if err != nil {
180179
t.Error(err)
181180
}

0 commit comments

Comments
 (0)