Skip to content

Commit

Permalink
Merge pull request #1205 from mmorel-35/master
Browse files Browse the repository at this point in the history
enable more linters, report coverage and cache mods
  • Loading branch information
shirou authored Dec 29, 2021
2 parents 0bd6ae1 + fe2fab4 commit 2f8da0a
Show file tree
Hide file tree
Showing 160 changed files with 940 additions and 722 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
make build_test
21 changes: 20 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- id: go-env
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.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: ${{ runner.os }},go-${{ matrix.go-version }}
token: ${{ secrets.CODECOV_TOKEN }}
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
issues:
max-same-issues: 0
exclude-rules:
- linters:
- gosec
text: "G204"
- linters:
- revive
text: "var-naming"
- linters:
- revive
text: "exported"
linters:
enable:
- asciicheck
- durationcheck
- errorlint
- gci
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- importas
- megacheck
- misspell
- nakedret
- nolintlint
- predeclared
- revive
- typecheck
- unparam
disable:
- deadcode
- errcheck
Expand Down
1 change: 0 additions & 1 deletion _tools/v3migration/v3migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,4 @@ func main() {
issueRemoveUnusedValue()
}
}

}
6 changes: 4 additions & 2 deletions cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 8 additions & 8 deletions cpu/cpu_aix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build aix
// +build aix

package cpu
Expand Down Expand Up @@ -36,9 +37,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)
Expand All @@ -56,11 +57,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
}

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

1 change: 1 addition & 0 deletions cpu/cpu_darwin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin
// +build darwin

package cpu
Expand Down
5 changes: 2 additions & 3 deletions cpu/cpu_darwin_cgo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build darwin
// +build cgo
//go:build darwin && cgo
// +build darwin,cgo

package cpu

Expand Down Expand Up @@ -108,5 +108,4 @@ func allCPUTimes() ([]TimesStat, error) {
}

return []TimesStat{c}, nil

}
4 changes: 2 additions & 2 deletions cpu/cpu_darwin_nocgo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build darwin
// +build !cgo
//go:build darwin && !cgo
// +build darwin,!cgo

package cpu

Expand Down
18 changes: 10 additions & 8 deletions cpu/cpu_dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cpu/cpu_fallback.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 11 additions & 9 deletions cpu/cpu_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cpu/cpu_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestParseDmesgBoot(t *testing.T) {
t.SkipNow()
}

var cpuTests = []struct {
cpuTests := []struct {
file string
cpuNum int
cores int32
Expand Down
22 changes: 8 additions & 14 deletions cpu/cpu_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

package cpu
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -63,7 +64,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
Expand All @@ -82,17 +83,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.
Expand Down Expand Up @@ -127,10 +127,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}
Expand Down Expand Up @@ -224,10 +221,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
Expand Down Expand Up @@ -345,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
Expand Down
3 changes: 2 additions & 1 deletion cpu/cpu_openbsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build openbsd
// +build openbsd

package cpu
Expand Down Expand Up @@ -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)}
Expand Down
1 change: 1 addition & 0 deletions cpu/cpu_plan9.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build plan9
// +build plan9

package cpu
Expand Down
1 change: 1 addition & 0 deletions cpu/cpu_plan9_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build plan9
// +build plan9

package cpu
Expand Down
4 changes: 2 additions & 2 deletions cpu/cpu_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
24 changes: 16 additions & 8 deletions cpu/cpu_solaris_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
}

Expand Down
Loading

0 comments on commit 2f8da0a

Please sign in to comment.