From c01898fdc1c1681da0ce20027173f5948669ae8f Mon Sep 17 00:00:00 2001 From: David Finkel Date: Wed, 24 Aug 2022 09:43:24 -0400 Subject: [PATCH 1/3] Update go.mod and github-actions Test against go 1.17, 1.18 and 1.19 (current last three versions) --- .github/workflows/go.yml | 4 ++-- go.mod | 10 +++++++--- go.sum | 5 ++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c2510c2..6a97944 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -8,11 +8,11 @@ jobs: strategy: matrix: os: [macOS-latest, ubuntu-latest] - goversion: [1.12, 1.13] + goversion: [1.17, 1.18, 1.19] steps: - name: Set up Go ${{matrix.goversion}} on ${{matrix.os}} - uses: actions/setup-go@v1 + uses: actions/setup-go@v3 with: go-version: ${{matrix.goversion}} id: go diff --git a/go.mod b/go.mod index 3597e47..14209ca 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,17 @@ module github.com/vimeo/procstats -go 1.13 +go 1.18 require ( - github.com/docker/go-units v0.4.0 // indirect github.com/opencontainers/runc v1.0.0-rc9 + golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 +) + +require ( + github.com/docker/go-units v0.4.0 // indirect + github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect github.com/opencontainers/runtime-spec v1.0.1 // indirect github.com/pkg/errors v0.8.1 // indirect github.com/sirupsen/logrus v1.4.2 // indirect github.com/stretchr/testify v1.4.0 // indirect - golang.org/x/sys v0.0.0-20191218084908-4a24b4065292 ) diff --git a/go.sum b/go.sum index 39f3682..7d2c399 100644 --- a/go.sum +++ b/go.sum @@ -21,9 +21,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191218084908-4a24b4065292 h1:Y8q0zsdcgAd+JU8VUA8p8Qv2YhuY9zevDG2ORt5qBUI= -golang.org/x/sys v0.0.0-20191218084908-4a24b4065292/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 h1:TyKJRhyo17yWxOMCTHKWrc5rddHORMlnZ/j57umaUd8= +golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From 3673ed20951d2f2f01853c2f1654a83bd1b42487 Mon Sep 17 00:00:00 2001 From: David Finkel Date: Wed, 24 Aug 2022 09:51:30 -0400 Subject: [PATCH 2/3] cleanup staticcheck lints and gofmt with go1.19 Trivial cleanups: - replace io/ioutil.{Read,Write}File -> os.{Read,Write}File - go fmt ./... - uncapitalize error-messages - gofmt added the new `//go:build` comments that supercedes `// +build` comments. --- cgrouplimits/cgroup_darwin.go | 1 + cgrouplimits/cgroup_linux.go | 21 +++++++++++---------- cgrouplimits/host_linux.go | 7 ++++--- cgrouplimits/host_linux_test.go | 1 + cgrouplimits/host_null.go | 1 + proc_pid_status_linux.go | 7 ++++--- proc_stats_bsd.go | 4 ++-- proc_stats_darwin.go | 4 ++-- proc_stats_linux.go | 8 ++++---- proc_stats_null.go | 1 + pstats.go | 2 +- sysconf_darwin_cgo.go | 4 ++-- sysconf_linux_cgo.go | 4 ++-- sysconf_linux_nocgo.go | 4 ++-- 14 files changed, 38 insertions(+), 31 deletions(-) diff --git a/cgrouplimits/cgroup_darwin.go b/cgrouplimits/cgroup_darwin.go index ec3f6a4..da0bf0f 100644 --- a/cgrouplimits/cgroup_darwin.go +++ b/cgrouplimits/cgroup_darwin.go @@ -1,3 +1,4 @@ +//go:build !linux // +build !linux package cgrouplimits diff --git a/cgrouplimits/cgroup_linux.go b/cgrouplimits/cgroup_linux.go index b7d25d8..8c678c2 100644 --- a/cgrouplimits/cgroup_linux.go +++ b/cgrouplimits/cgroup_linux.go @@ -1,10 +1,11 @@ +//go:build linux // +build linux package cgrouplimits import ( "fmt" - "io/ioutil" + "os" "path/filepath" "strconv" "strings" @@ -27,16 +28,16 @@ const cgroupMemOOMControlFile = "memory.oom_control" func GetCgroupCPULimit() (float64, error) { cpuPath, cgroupFindErr := cgroups.GetOwnCgroupPath("cpu") if cgroupFindErr != nil { - return -1.0, fmt.Errorf("Unable to find cgroup directory: %s", cgroupFindErr) + return -1.0, fmt.Errorf("unable to find cgroup directory: %s", cgroupFindErr) } quotaFilePath := filepath.Join(cpuPath, cgroupCFSQuotaFile) - quotaStr, quotaReadErr := ioutil.ReadFile(quotaFilePath) + quotaStr, quotaReadErr := os.ReadFile(quotaFilePath) if quotaReadErr != nil { return -1.0, fmt.Errorf("failed to read quota file %q: %s", quotaFilePath, quotaReadErr) } enforcePeriodFilePath := filepath.Join(cpuPath, cgroupCFSPeriodFile) - enforcePeriodStr, periodReadErr := ioutil.ReadFile(enforcePeriodFilePath) + enforcePeriodStr, periodReadErr := os.ReadFile(enforcePeriodFilePath) if periodReadErr != nil { return -1.0, fmt.Errorf("failed to read cfs period file %q: %s", enforcePeriodFilePath, periodReadErr) @@ -66,10 +67,10 @@ func GetCgroupCPULimit() (float64, error) { func GetCgroupMemoryLimit() (int64, error) { memPath, cgroupFindErr := cgroups.GetOwnCgroupPath("memory") if cgroupFindErr != nil { - return -1, fmt.Errorf("Unable to find cgroup directory: %s", cgroupFindErr) + return -1, fmt.Errorf("unable to find cgroup directory: %s", cgroupFindErr) } limitFilePath := filepath.Join(memPath, cgroupMemLimitFile) - limitFileContents, limitReadErr := ioutil.ReadFile(limitFilePath) + limitFileContents, limitReadErr := os.ReadFile(limitFilePath) if limitReadErr != nil { return -1, fmt.Errorf("failed to read cgroup memory limit file %q: %s", limitFilePath, limitReadErr) @@ -87,7 +88,7 @@ func GetCgroupMemoryLimit() (int64, error) { func GetCgroupMemoryStats() (MemoryStats, error) { memPath, cgroupFindErr := cgroups.GetOwnCgroupPath("memory") if cgroupFindErr != nil { - return MemoryStats{}, fmt.Errorf("Unable to find cgroup directory: %s", cgroupFindErr) + return MemoryStats{}, fmt.Errorf("unable to find cgroup directory: %s", cgroupFindErr) } mg := fs.MemoryGroup{} st := cgroups.NewStats() @@ -130,7 +131,7 @@ type MemCgroupOOMControl struct { // used in portable applications. func ReadCGroupOOMControl(memCgroupPath string) (MemCgroupOOMControl, error) { oomControlPath := filepath.Join(memCgroupPath, cgroupMemOOMControlFile) - oomControlBytes, oomControlReadErr := ioutil.ReadFile(oomControlPath) + oomControlBytes, oomControlReadErr := os.ReadFile(oomControlPath) if oomControlReadErr != nil { return MemCgroupOOMControl{}, fmt.Errorf( "failed to read contents of %q: %s", @@ -168,12 +169,12 @@ func getCgroupOOMs(memCgroupPath string) (int32, error) { func GetCgroupCPUStats() (CPUStats, error) { cpuPath, cgroupFindErr := cgroups.GetOwnCgroupPath("cpu") if cgroupFindErr != nil { - return CPUStats{}, fmt.Errorf("Unable to find cgroup directory: %s", + return CPUStats{}, fmt.Errorf("unable to find cgroup directory: %s", cgroupFindErr) } cpuAcctPath, cgroupFindErr := cgroups.GetOwnCgroupPath("cpuacct") if cgroupFindErr != nil { - return CPUStats{}, fmt.Errorf("Unable to find cgroup directory: %s", + return CPUStats{}, fmt.Errorf("unable to find cgroup directory: %s", cgroupFindErr) } cg := fs.CpuGroup{} diff --git a/cgrouplimits/host_linux.go b/cgrouplimits/host_linux.go index 23e406b..f52e042 100644 --- a/cgrouplimits/host_linux.go +++ b/cgrouplimits/host_linux.go @@ -1,17 +1,18 @@ +//go:build linux // +build linux package cgrouplimits import ( "fmt" - "io/ioutil" + "os" "github.com/vimeo/procstats/pparser" ) func getMemInfo() (hostMemInfo, error) { const procMemInfo = "/proc/meminfo" - memInfoBytes, procReadErr := ioutil.ReadFile(procMemInfo) + memInfoBytes, procReadErr := os.ReadFile(procMemInfo) if procReadErr != nil { return hostMemInfo{}, fmt.Errorf( "failed to read contents of %q: %s", @@ -30,7 +31,7 @@ func getMemInfo() (hostMemInfo, error) { func getVMStat() (hostVMStat, error) { const procVMStat = "/proc/vmstat" - vmStatBytes, procReadErr := ioutil.ReadFile(procVMStat) + vmStatBytes, procReadErr := os.ReadFile(procVMStat) if procReadErr != nil { return hostVMStat{}, fmt.Errorf( "failed to read contents of %q: %s", diff --git a/cgrouplimits/host_linux_test.go b/cgrouplimits/host_linux_test.go index e22acfa..a23716d 100644 --- a/cgrouplimits/host_linux_test.go +++ b/cgrouplimits/host_linux_test.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package cgrouplimits diff --git a/cgrouplimits/host_null.go b/cgrouplimits/host_null.go index 242ee43..a4336a7 100644 --- a/cgrouplimits/host_null.go +++ b/cgrouplimits/host_null.go @@ -1,3 +1,4 @@ +//go:build !linux // +build !linux package cgrouplimits diff --git a/proc_pid_status_linux.go b/proc_pid_status_linux.go index 3f21cf3..8190f5b 100644 --- a/proc_pid_status_linux.go +++ b/proc_pid_status_linux.go @@ -1,10 +1,11 @@ +//go:build linux // +build linux package procstats import ( "fmt" - "io/ioutil" + "os" "path/filepath" "strconv" @@ -80,7 +81,7 @@ var procPidStatusParser = pparser.NewLineKVFileParser(ProcPidStatus{}, ":") // (ProcessCPUTime, MaxRSS, and RSS) rather than the low-level. func ReadProcStatus(pid int) (*ProcPidStatus, error) { statusPath := filepath.Join("/proc", strconv.Itoa(pid), "status") - contents, err := ioutil.ReadFile(statusPath) + contents, err := os.ReadFile(statusPath) if err != nil { return nil, fmt.Errorf("failed to read file %q: %s", statusPath, err) @@ -117,5 +118,5 @@ func resetMaxRSS(pid int) error { // current resident set size value. // As such, write the value "5" to /proc/$PID/clear_refs to reset the VmHWM value. - return ioutil.WriteFile(refsPath, []byte{'5'}, 0) + return os.WriteFile(refsPath, []byte{'5'}, 0) } diff --git a/proc_stats_bsd.go b/proc_stats_bsd.go index 17363ae..bb76eba 100644 --- a/proc_stats_bsd.go +++ b/proc_stats_bsd.go @@ -1,5 +1,5 @@ -// +build freebsd -// +build cgo +//go:build freebsd && cgo +// +build freebsd,cgo package procstats diff --git a/proc_stats_darwin.go b/proc_stats_darwin.go index a4293de..c1d379b 100644 --- a/proc_stats_darwin.go +++ b/proc_stats_darwin.go @@ -1,5 +1,5 @@ -// +build darwin -// +build cgo +//go:build darwin && cgo +// +build darwin,cgo package procstats diff --git a/proc_stats_linux.go b/proc_stats_linux.go index d56da3d..fc13ed9 100644 --- a/proc_stats_linux.go +++ b/proc_stats_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package procstats @@ -5,7 +6,6 @@ package procstats import ( "bytes" "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -15,7 +15,7 @@ import ( func init() { const self = "/proc/self/stat" - b, err := ioutil.ReadFile(self) + b, err := os.ReadFile(self) if err != nil { panic(fmt.Sprintf("unexpected kernel shenanigans: %v", err)) } @@ -33,7 +33,7 @@ func init() { var ct *CPUTime for i := 0; i < 30 || (*ct == CPUTime{}); i++ { <-t.C - b, err := ioutil.ReadFile(self) + b, err := os.ReadFile(self) if err != nil { panic(fmt.Sprintf("unexpected kernel shenanigans: %v", err)) } @@ -52,7 +52,7 @@ func procFileName(pid int, leafName string) string { func procFileContents(pid int, leafName string) ([]byte, error) { fn := procFileName(pid, leafName) - contents, err := ioutil.ReadFile(fn) + contents, err := os.ReadFile(fn) if err != nil { return nil, fmt.Errorf("failed to read %s with error: %s", leafName, err) } diff --git a/proc_stats_null.go b/proc_stats_null.go index 7b2dadf..0b4e64e 100644 --- a/proc_stats_null.go +++ b/proc_stats_null.go @@ -1,3 +1,4 @@ +//go:build !linux && !cgo // +build !linux,!cgo package procstats diff --git a/pstats.go b/pstats.go index 18a59e0..da55e97 100644 --- a/pstats.go +++ b/pstats.go @@ -8,7 +8,7 @@ import ( // ErrUnimplementedPlatform indicates that this request is not implemented for // this specific platform. -var ErrUnimplementedPlatform = errors.New("Unimplemented for this platform") +var ErrUnimplementedPlatform = errors.New("unimplemented for this platform") // RSS takes a pid and returns the RSS of that process (or an error) // This may return ErrUnimplementedPlatform on non-linux and non-darwin platforms. diff --git a/sysconf_darwin_cgo.go b/sysconf_darwin_cgo.go index 694a3fd..edaebec 100644 --- a/sysconf_darwin_cgo.go +++ b/sysconf_darwin_cgo.go @@ -1,5 +1,5 @@ -// +build darwin -// +build cgo +//go:build darwin && cgo +// +build darwin,cgo package procstats diff --git a/sysconf_linux_cgo.go b/sysconf_linux_cgo.go index 826eb60..d59a99b 100644 --- a/sysconf_linux_cgo.go +++ b/sysconf_linux_cgo.go @@ -1,5 +1,5 @@ -// +build linux -// +build cgo +//go:build linux && cgo +// +build linux,cgo package procstats diff --git a/sysconf_linux_nocgo.go b/sysconf_linux_nocgo.go index f8d90e5..fb6a0cc 100644 --- a/sysconf_linux_nocgo.go +++ b/sysconf_linux_nocgo.go @@ -1,5 +1,5 @@ -// +build linux -// +build !cgo +//go:build linux && !cgo +// +build linux,!cgo package procstats From 11c2b6409058874bc762a200d92bf468591a12f7 Mon Sep 17 00:00:00 2001 From: David Finkel Date: Wed, 24 Aug 2022 13:14:17 -0400 Subject: [PATCH 3/3] add staticcheck github action --- .github/workflows/staticcheck.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/staticcheck.yml diff --git a/.github/workflows/staticcheck.yml b/.github/workflows/staticcheck.yml new file mode 100644 index 0000000..b7133b5 --- /dev/null +++ b/.github/workflows/staticcheck.yml @@ -0,0 +1,14 @@ +name: "StaticCheck" +on: ["push", "pull_request"] + +jobs: + ci: + name: "staticcheck" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 1 + - uses: dominikh/staticcheck-action@v1.1.0 + with: + version: "2022.1.3"