Skip to content

Commit 2f8da0a

Browse files
authored
Merge pull request #1205 from mmorel-35/master
enable more linters, report coverage and cache mods
2 parents 0bd6ae1 + fe2fab4 commit 2f8da0a

File tree

160 files changed

+940
-722
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+940
-722
lines changed

.github/workflows/build_test.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ jobs:
1313
go-version: ${{ matrix.go-version }}
1414
- name: Checkout code
1515
uses: actions/checkout@v2
16+
- id: cache-paths
17+
run: |
18+
echo "::set-output name=cache::$(go env GOCACHE)"
19+
echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
20+
- name: Cache go modules
21+
uses: actions/cache@v2
22+
with:
23+
path: |
24+
${{ steps.cache-paths.outputs.cache }}
25+
${{ steps.cache-paths.outputs.mod-cache }}
26+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
27+
restore-keys: ${{ runner.os }}-go-
1628
- name: Build Test v3
1729
run: |
18-
make build_test
30+
make build_test

.github/workflows/test.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ jobs:
1414
go-version: ${{ matrix.go-version }}
1515
- name: Checkout code
1616
uses: actions/checkout@v2
17+
- id: go-env
18+
run: |
19+
echo "::set-output name=cache::$(go env GOCACHE)"
20+
echo "::set-output name=mod-cache::$(go env GOMODCACHE)"
21+
- name: Cache go modules
22+
uses: actions/cache@v2
23+
with:
24+
path: |
25+
${{ steps.go-env.outputs.cache }}
26+
${{ steps.go-env.outputs.mod-cache }}
27+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
28+
restore-keys: ${{ runner.os }}-go-
1729
- name: Test
1830
run: |
19-
go test ./...
31+
go test -coverprofile='coverage.out' -covermode=atomic ./...
32+
- name: Upload Code Coverage
33+
uses: codecov/codecov-action@v2
34+
with:
35+
fail_ci_if_error: true
36+
files: coverage.out
37+
flags: ${{ runner.os }},go-${{ matrix.go-version }}
38+
token: ${{ secrets.CODECOV_TOKEN }}

.golangci.yml

+26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
1+
issues:
2+
max-same-issues: 0
3+
exclude-rules:
4+
- linters:
5+
- gosec
6+
text: "G204"
7+
- linters:
8+
- revive
9+
text: "var-naming"
10+
- linters:
11+
- revive
12+
text: "exported"
113
linters:
214
enable:
15+
- asciicheck
16+
- durationcheck
317
- errorlint
418
- gci
19+
- gofmt
20+
- gofumpt
21+
- goimports
22+
- gosec
523
- gosimple
24+
- importas
25+
- megacheck
26+
- misspell
27+
- nakedret
28+
- nolintlint
29+
- predeclared
30+
- revive
631
- typecheck
32+
- unparam
733
disable:
834
- deadcode
935
- errcheck

_tools/v3migration/v3migration.go

-1
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,4 @@ func main() {
103103
issueRemoveUnusedValue()
104104
}
105105
}
106-
107106
}

cpu/cpu.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ type lastPercent struct {
5151
lastPerCPUTimes []TimesStat
5252
}
5353

54-
var lastCPUPercent lastPercent
55-
var invoke common.Invoker = common.Invoke{}
54+
var (
55+
lastCPUPercent lastPercent
56+
invoke common.Invoker = common.Invoke{}
57+
)
5658

5759
func init() {
5860
lastCPUPercent.Lock()

cpu/cpu_aix.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build aix
12
// +build aix
23

34
package cpu
@@ -36,9 +37,9 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
3637
}
3738
ct := &TimesStat{
3839
CPU: "cpu-total",
39-
Idle: float64(c.IdlePct),
40-
User: float64(c.UserPct),
41-
System: float64(c.KernPct),
40+
Idle: float64(c.IdlePct),
41+
User: float64(c.UserPct),
42+
System: float64(c.KernPct),
4243
Iowait: float64(c.WaitPct),
4344
}
4445
ret = append(ret, *ct)
@@ -56,11 +57,11 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
5657
return nil, err
5758
}
5859
info := InfoStat{
59-
CPU: 0,
60-
Mhz: float64(c.ProcessorHz / 1000000),
60+
CPU: 0,
61+
Mhz: float64(c.ProcessorHz / 1000000),
6162
Cores: int32(c.NCpusCfg),
62-
}
63-
result := []InfoStat{info};
63+
}
64+
result := []InfoStat{info}
6465
return result, nil
6566
}
6667

@@ -71,4 +72,3 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
7172
}
7273
return c.NCpusCfg, nil
7374
}
74-

cpu/cpu_darwin.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build darwin
12
// +build darwin
23

34
package cpu

cpu/cpu_darwin_cgo.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// +build darwin
2-
// +build cgo
1+
//go:build darwin && cgo
2+
// +build darwin,cgo
33

44
package cpu
55

@@ -108,5 +108,4 @@ func allCPUTimes() ([]TimesStat, error) {
108108
}
109109

110110
return []TimesStat{c}, nil
111-
112111
}

cpu/cpu_darwin_nocgo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// +build darwin
2-
// +build !cgo
1+
//go:build darwin && !cgo
2+
// +build darwin,!cgo
33

44
package cpu
55

cpu/cpu_dragonfly.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import (
1515
"golang.org/x/sys/unix"
1616
)
1717

18-
var ClocksPerSec = float64(128)
19-
var cpuMatch = regexp.MustCompile(`^CPU:`)
20-
var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
21-
var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
22-
var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
23-
var cpuEnd = regexp.MustCompile(`^Trying to mount root`)
24-
var cpuTimesSize int
25-
var emptyTimes cpuTimes
18+
var (
19+
ClocksPerSec = float64(128)
20+
cpuMatch = regexp.MustCompile(`^CPU:`)
21+
originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
22+
featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
23+
featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
24+
cpuEnd = regexp.MustCompile(`^Trying to mount root`)
25+
cpuTimesSize int
26+
emptyTimes cpuTimes
27+
)
2628

2729
func init() {
2830
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)

cpu/cpu_fallback.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !darwin && !linux && !freebsd && !openbsd && !solaris && !windows && !dragonfly && !plan9 && !aix
12
// +build !darwin,!linux,!freebsd,!openbsd,!solaris,!windows,!dragonfly,!plan9,!aix
23

34
package cpu

cpu/cpu_freebsd.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ import (
1515
"golang.org/x/sys/unix"
1616
)
1717

18-
var ClocksPerSec = float64(128)
19-
var cpuMatch = regexp.MustCompile(`^CPU:`)
20-
var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
21-
var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
22-
var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
23-
var cpuEnd = regexp.MustCompile(`^Trying to mount root`)
24-
var cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`)
25-
var cpuTimesSize int
26-
var emptyTimes cpuTimes
18+
var (
19+
ClocksPerSec = float64(128)
20+
cpuMatch = regexp.MustCompile(`^CPU:`)
21+
originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
22+
featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
23+
featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
24+
cpuEnd = regexp.MustCompile(`^Trying to mount root`)
25+
cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`)
26+
cpuTimesSize int
27+
emptyTimes cpuTimes
28+
)
2729

2830
func init() {
2931
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)

cpu/cpu_freebsd_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestParseDmesgBoot(t *testing.T) {
1313
t.SkipNow()
1414
}
1515

16-
var cpuTests = []struct {
16+
cpuTests := []struct {
1717
file string
1818
cpuNum int
1919
cores int32

cpu/cpu_linux.go

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
package cpu
@@ -30,7 +31,7 @@ func Times(percpu bool) ([]TimesStat, error) {
3031

3132
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
3233
filename := common.HostProc("stat")
33-
var lines = []string{}
34+
lines := []string{}
3435
if percpu {
3536
statlines, err := common.ReadLines(filename)
3637
if err != nil || len(statlines) < 2 {
@@ -63,7 +64,7 @@ func sysCPUPath(cpu int32, relPath string) string {
6364
return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
6465
}
6566

66-
func finishCPUInfo(c *InfoStat) error {
67+
func finishCPUInfo(c *InfoStat) {
6768
var lines []string
6869
var err error
6970
var value float64
@@ -82,17 +83,16 @@ func finishCPUInfo(c *InfoStat) error {
8283
// if we encounter errors below such as there are no cpuinfo_max_freq file,
8384
// we just ignore. so let Mhz is 0.
8485
if err != nil || len(lines) == 0 {
85-
return nil
86+
return
8687
}
8788
value, err = strconv.ParseFloat(lines[0], 64)
8889
if err != nil {
89-
return nil
90+
return
9091
}
9192
c.Mhz = value / 1000.0 // value is in kHz
9293
if c.Mhz > 9999 {
9394
c.Mhz = c.Mhz / 1000.0 // value in Hz
9495
}
95-
return nil
9696
}
9797

9898
// CPUInfo on linux will return 1 item per physical thread.
@@ -127,10 +127,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
127127
processorName = value
128128
case "processor":
129129
if c.CPU >= 0 {
130-
err := finishCPUInfo(&c)
131-
if err != nil {
132-
return ret, err
133-
}
130+
finishCPUInfo(&c)
134131
ret = append(ret, c)
135132
}
136133
c = InfoStat{Cores: 1, ModelName: processorName}
@@ -224,10 +221,7 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
224221
}
225222
}
226223
if c.CPU >= 0 {
227-
err := finishCPUInfo(&c)
228-
if err != nil {
229-
return ret, err
230-
}
224+
finishCPUInfo(&c)
231225
ret = append(ret, c)
232226
}
233227
return ret, nil
@@ -345,7 +339,7 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
345339
}
346340
// physical cores
347341
// https://github.com/giampaolo/psutil/blob/8415355c8badc9c94418b19bdf26e622f06f0cce/psutil/_pslinux.py#L615-L628
348-
var threadSiblingsLists = make(map[string]bool)
342+
threadSiblingsLists := make(map[string]bool)
349343
// These 2 files are the same but */core_cpus_list is newer while */thread_siblings_list is deprecated and may disappear in the future.
350344
// https://www.kernel.org/doc/Documentation/admin-guide/cputopology.rst
351345
// https://github.com/giampaolo/psutil/pull/1727#issuecomment-707624964

cpu/cpu_openbsd.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build openbsd
12
// +build openbsd
23

34
package cpu
@@ -108,7 +109,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
108109
j *= 2
109110
}
110111

111-
var cpuTimes = make([]int32, cpUStates)
112+
cpuTimes := make([]int32, cpUStates)
112113
var mib []int32
113114
if percpu {
114115
mib = []int32{ctlKern, kernCptime2, int32(j)}

cpu/cpu_plan9.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build plan9
12
// +build plan9
23

34
package cpu

cpu/cpu_plan9_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build plan9
12
// +build plan9
23

34
package cpu

cpu/cpu_solaris.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func init() {
2424
}
2525
}
2626

27-
//sum all values in a float64 map with float64 keys
27+
// sum all values in a float64 map with float64 keys
2828
func msum(x map[float64]float64) float64 {
2929
total := 0.0
3030
for _, y := range x {
@@ -47,7 +47,7 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
4747
user := make(map[float64]float64)
4848
kern := make(map[float64]float64)
4949
iowt := make(map[float64]float64)
50-
//swap := make(map[float64]float64)
50+
// swap := make(map[float64]float64)
5151
kstatSysOut, err := invoke.CommandWithContext(ctx, kstatSys, "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/")
5252
if err != nil {
5353
return nil, fmt.Errorf("cannot execute kstat: %s", err)

cpu/cpu_solaris_test.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,36 @@ func TestParseISAInfo(t *testing.T) {
1515
}{
1616
{
1717
"1cpu_1core_isainfo.txt",
18-
[]string{"rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx",
18+
[]string{
19+
"rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx",
1920
"avx", "xsave", "pclmulqdq", "aes", "movbe", "sse4.2", "sse4.1", "ssse3", "popcnt",
2021
"tscp", "cx16", "sse3", "sse2", "sse", "fxsr", "mmx", "cmov", "amd_sysc", "cx8",
21-
"tsc", "fpu"},
22+
"tsc", "fpu",
23+
},
2224
},
2325
{
2426
"2cpu_1core_isainfo.txt",
25-
[]string{"rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx",
27+
[]string{
28+
"rdseed", "adx", "avx2", "fma", "bmi2", "bmi1", "rdrand", "f16c", "vmx",
2629
"avx", "xsave", "pclmulqdq", "aes", "movbe", "sse4.2", "sse4.1", "ssse3", "popcnt",
2730
"tscp", "cx16", "sse3", "sse2", "sse", "fxsr", "mmx", "cmov", "amd_sysc", "cx8",
28-
"tsc", "fpu"},
31+
"tsc", "fpu",
32+
},
2933
},
3034
{
3135
"2cpu_8core_isainfo.txt",
32-
[]string{"vmx", "avx", "xsave", "pclmulqdq", "aes", "sse4.2", "sse4.1", "ssse3", "popcnt",
36+
[]string{
37+
"vmx", "avx", "xsave", "pclmulqdq", "aes", "sse4.2", "sse4.1", "ssse3", "popcnt",
3338
"tscp", "cx16", "sse3", "sse2", "sse", "fxsr", "mmx", "cmov", "amd_sysc", "cx8",
34-
"tsc", "fpu"},
39+
"tsc", "fpu",
40+
},
3541
},
3642
{
3743
"2cpu_12core_isainfo.txt",
38-
[]string{"amd_svm", "amd_lzcnt", "popcnt", "amd_sse4a", "tscp", "ahf", "cx16", "sse3", "sse2",
39-
"sse", "fxsr", "amd_3dnowx", "amd_3dnow", "amd_mmx", "mmx", "cmov", "amd_sysc", "cx8", "tsc", "fpu"},
44+
[]string{
45+
"amd_svm", "amd_lzcnt", "popcnt", "amd_sse4a", "tscp", "ahf", "cx16", "sse3", "sse2",
46+
"sse", "fxsr", "amd_3dnowx", "amd_3dnow", "amd_mmx", "mmx", "cmov", "amd_sysc", "cx8", "tsc", "fpu",
47+
},
4048
},
4149
}
4250

0 commit comments

Comments
 (0)