From 4bc9e37b0f5cbb28af8679de511c14ba5ad3d7e0 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 13:35:07 -0700 Subject: [PATCH 01/17] faster file read --- internal/common/common.go | 23 +++++++++++++ internal/common/common_linux.go | 73 +++++++++++++++++++++++------------------ process/process_test.go | 7 ++++ 3 files changed, 71 insertions(+), 32 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index 9bfece3..bad8d1f 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -114,6 +114,29 @@ func ReadLines(filename string) ([]string, error) { return ReadLinesOffsetN(filename, 0, -1) } +// ReadLine reads a file and returns the first occurrence of a line that is prefixed with prefix. +func ReadLine(filename string, prefix string) (string, error) { + f, err := os.Open(filename) + if err != nil { + return "", err + } + defer f.Close() + r := bufio.NewReader(f) + for { + line, err := r.ReadString('\n') + if err != nil { + if err == io.EOF { + break + } + } + if strings.HasPrefix(line, prefix) { + return line, nil + } + } + + return "", nil +} + // ReadLinesOffsetN reads contents from file and splits them by new line. // The offset tells at which line number to start. // The count determines the number of lines to read (starting from offset): diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index b58edbe..607d963 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -62,17 +62,38 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { return 0, err } - statFile := "stat" + useStatFile := true if system == "lxc" && role == "guest" { // if lxc, /proc/uptime is used. - statFile = "uptime" + useStatFile = false } else if system == "docker" && role == "guest" { // also docker, guest - statFile = "uptime" + useStatFile = false } - filename := HostProcWithContext(ctx, statFile) - lines, err := ReadLines(filename) + if useStatFile { + return readBootTimeStat(ctx) + } else { + filename := HostProcWithContext(ctx, "uptime") + lines, err := ReadLines(filename) + if err != nil { + return handleBootTimeFileReadErr(err) + } + if len(lines) != 1 { + return 0, fmt.Errorf("wrong uptime format") + } + f := strings.Fields(lines[0]) + b, err := strconv.ParseFloat(f[0], 64) + if err != nil { + return 0, err + } + currentTime := float64(time.Now().UnixNano()) / float64(time.Second) + t := currentTime - b + return uint64(t), nil + } +} + +func handleBootTimeFileReadErr(err error) (uint64, error) { if os.IsPermission(err) { var info syscall.Sysinfo_t err := syscall.Sysinfo(&info) @@ -81,42 +102,30 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { } currentTime := time.Now().UnixNano() / int64(time.Second) - t := currentTime - int64(info.Uptime) + t := currentTime - info.Uptime return uint64(t), nil } + return 0, err +} + +func readBootTimeStat(ctx context.Context) (uint64, error) { + filename := HostProcWithContext(ctx, "stat") + line, err := ReadLine(filename, "btime") if err != nil { - return 0, err + return handleBootTimeFileReadErr(err) } - - if statFile == "stat" { - for _, line := range lines { - if strings.HasPrefix(line, "btime") { - f := strings.Fields(line) - if len(f) != 2 { - return 0, fmt.Errorf("wrong btime format") - } - b, err := strconv.ParseInt(f[1], 10, 64) - if err != nil { - return 0, err - } - t := uint64(b) - return t, nil - } - } - } else if statFile == "uptime" { - if len(lines) != 1 { - return 0, fmt.Errorf("wrong uptime format") + if strings.HasPrefix(line, "btime") { + f := strings.Fields(line) + if len(f) != 2 { + return 0, fmt.Errorf("wrong btime format") } - f := strings.Fields(lines[0]) - b, err := strconv.ParseFloat(f[0], 64) + b, err := strconv.ParseInt(f[1], 10, 64) if err != nil { return 0, err } - currentTime := float64(time.Now().UnixNano()) / float64(time.Second) - t := currentTime - b - return uint64(t), nil + t := uint64(b) + return t, nil } - return 0, fmt.Errorf("could not find btime") } diff --git a/process/process_test.go b/process/process_test.go index 9281c93..ae26960 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/shirou/gopsutil/v3/internal/common" + "github.com/stretchr/testify/require" ) var mu sync.Mutex @@ -862,3 +863,9 @@ func BenchmarkProcessPpid(b *testing.B) { p.Ppid() } } + +func BenchmarkProcesses(b *testing.B) { + ps, err := Processes() + require.NoError(b, err) + require.Greater(b, len(ps), 0) +} From a7a94986fb9b6deeef51eb25c71a73c1d1c9becc Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:16:28 -0700 Subject: [PATCH 02/17] send results --- process/results.txt | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 process/results.txt diff --git a/process/results.txt b/process/results.txt new file mode 100644 index 0000000..ec9a266 --- /dev/null +++ b/process/results.txt @@ -0,0 +1,108 @@ +on branch read_file_faster: + +/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcesses_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcesses\E$ -test.run ^$ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcesses +BenchmarkProcesses-80 1000000000 0.1756 ns/op +PASS + +on branch master: +/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcessPpid_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcessPpid\E$ -test.run ^$ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcessPpid +BenchmarkProcessPpid-80 8694 126542 ns/op +PASS + +Typical /proc/stat file: +cpu 768944 10288 1072079 11045715258 3486 0 5444 0 0 0 +cpu0 8786 70 6308 137988080 26 0 67 0 0 0 +cpu1 9068 30 6191 138072341 59 0 11 0 0 0 +cpu2 65109 159 171353 137907618 83 0 8 0 0 0 +cpu3 24084 118 40891 137992666 88 0 8 0 0 0 +cpu4 43038 444 109352 137978323 83 0 4 0 0 0 +cpu5 21472 193 40224 138025036 82 0 0 0 0 0 +cpu6 42410 118 123254 137981039 92 0 3 0 0 0 +cpu7 18037 232 33534 138039186 101 0 5 0 0 0 +cpu8 31662 186 72772 138013182 98 0 4 0 0 0 +cpu9 13657 181 17251 138023478 91 0 221 0 0 0 +cpu10 31512 159 74950 138015049 40 0 0 0 0 0 +cpu11 12116 472 16357 138037994 71 0 166 0 0 0 +cpu12 21121 85 43973 138039462 63 0 4 0 0 0 +cpu13 10251 27 10354 138021006 59 0 0 0 0 0 +cpu14 18905 280 40738 138048357 50 0 5 0 0 0 +cpu15 10326 360 10793 138065576 47 0 3 0 0 0 +cpu16 11191 100 13201 138062883 52 0 0 0 0 0 +cpu17 8028 107 7053 138066687 54 0 0 0 0 0 +cpu18 7964 102 7473 138082226 43 0 6 0 0 0 +cpu19 7326 158 5075 138084645 27 0 1 0 0 0 +cpu20 7577 4 6287 138079436 41 0 0 0 0 0 +cpu21 7813 12 6644 138067886 84 0 131 0 0 0 +cpu22 6736 117 5078 138085923 19 0 2 0 0 0 +cpu23 7123 128 5150 138086066 21 0 3 0 0 0 +cpu24 9399 13 6703 138082336 24 0 39 0 0 0 +cpu25 8955 45 6350 138083373 26 0 0 0 0 0 +cpu26 6441 83 4730 138088400 31 0 2 0 0 0 +cpu27 6157 25 4148 138089297 27 0 6 0 0 0 +cpu28 6401 9 4586 138087528 44 0 1 0 0 0 +cpu29 6391 17 4265 138087849 46 0 21 0 0 0 +cpu30 6286 461 3308 138090520 50 0 0 0 0 0 +cpu31 6094 104 3068 138091314 86 0 0 0 0 0 +cpu32 6022 108 3822 138089008 19 0 1 0 0 0 +cpu33 5909 73 3729 138088729 11 0 1 0 0 0 +cpu34 6820 294 3541 138088142 6 0 1 0 0 0 +cpu35 6616 129 3347 138090230 8 0 0 0 0 0 +cpu36 6206 107 4706 138083252 9 0 1 0 0 0 +cpu37 6584 28 2864 138085803 23 0 1 0 0 0 +cpu38 6220 131 3001 138091331 37 0 2 0 0 0 +cpu39 5486 217 2772 138092221 38 0 2 0 0 0 +cpu40 5842 31 2824 138093761 14 0 0 0 0 0 +cpu41 6105 125 2923 138093217 17 0 0 0 0 0 +cpu42 5204 44 3539 138091542 12 0 1 0 0 0 +cpu43 5839 32 3417 138091082 10 0 4 0 0 0 +cpu44 5615 19 3980 138092487 22 0 4 0 0 0 +cpu45 6026 424 4100 138091306 19 0 0 0 0 0 +cpu46 5527 209 3688 138094125 12 0 0 0 0 0 +cpu47 5336 109 3033 138094396 12 0 3 0 0 0 +cpu48 5494 103 2653 138096147 2 0 1 0 0 0 +cpu49 5218 30 2393 138097187 5 0 0 0 0 0 +cpu50 5484 239 2130 138096460 8 0 0 0 0 0 +cpu51 5043 34 2446 138096773 15 0 0 0 0 0 +cpu52 5498 27 2971 138095154 8 0 12 0 0 0 +cpu53 5181 43 2634 138087718 8 0 2 0 0 0 +cpu54 4988 36 2976 138095457 4 0 0 0 0 0 +cpu55 5081 21 2344 138095338 7 0 0 0 0 0 +cpu56 4636 273 1897 138096693 12 0 2 0 0 0 +cpu57 4884 316 1935 138095963 15 0 3 0 0 0 +cpu58 4870 229 1745 138097331 6 0 1 0 0 0 +cpu59 4924 28 1746 138097999 7 0 5 0 0 0 +cpu60 4461 129 1955 138096954 361 0 0 0 0 0 +cpu61 5030 35 1930 138097414 460 0 0 0 0 0 +cpu62 4179 23 2157 138097375 9 0 5 0 0 0 +cpu63 4242 134 2235 138096928 10 0 8 0 0 0 +cpu64 6682 254 2108 138094052 4 0 3 0 0 0 +cpu65 4354 115 1743 138097071 3 0 0 0 0 0 +cpu66 4162 9 2496 138095759 7 0 0 0 0 0 +cpu67 3863 28 1960 138097697 6 0 5 0 0 0 +cpu68 13484 17 17176 137901816 109 0 4614 0 0 0 +cpu69 12515 17 14273 137980618 100 0 4 0 0 0 +cpu70 5085 74 3738 138092386 10 0 0 0 0 0 +cpu71 4792 14 3574 138093476 13 0 0 0 0 0 +cpu72 4943 167 2880 138090318 12 0 2 0 0 0 +cpu73 5333 17 4367 138089338 19 0 0 0 0 0 +cpu74 4909 5 2995 138076914 30 0 2 0 0 0 +cpu75 4817 256 3160 138072268 30 0 0 0 0 0 +cpu76 4146 233 2160 138096352 8 0 1 0 0 0 +cpu77 4598 474 1860 138096155 10 0 3 0 0 0 +cpu78 4733 34 2352 138094484 29 0 4 0 0 0 +cpu79 4488 164 2359 138094227 44 0 2 0 0 0 +intr 1136940775 0 1020438 49587864 0 0 0 210556659 0 0 0 0 0 867076724 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51690 0 0 0 0 0 0 0 1 5604085 970576 914985 910669 70 33120 33768 24240 21358 14764 17850 12776 9795 8765 6098 7307 8012 15687 13640 7797 11437 +ctxt 1918526774 +btime 1691617230 +processes 501797 +procs_running 39 +procs_blocked 0 +softirq 272142382 51064 32640104 523 8408436 152655 0 4696 152439652 131 78445121 From a48fd711371c6f4bf1f4f8d92e5eb791bd6b9a61 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:33:37 -0700 Subject: [PATCH 03/17] Revert "send results" This reverts commit a7a94986fb9b6deeef51eb25c71a73c1d1c9becc. --- process/results.txt | 108 ---------------------------------------------------- 1 file changed, 108 deletions(-) delete mode 100644 process/results.txt diff --git a/process/results.txt b/process/results.txt deleted file mode 100644 index ec9a266..0000000 --- a/process/results.txt +++ /dev/null @@ -1,108 +0,0 @@ -on branch read_file_faster: - -/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcesses_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcesses\E$ -test.run ^$ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcesses -BenchmarkProcesses-80 1000000000 0.1756 ns/op -PASS - -on branch master: -/home/ubuntu/.cache/JetBrains/IntelliJIdea2023.2/tmp/GoLand/___BenchmarkProcessPpid_in_github_com_shirou_gopsutil_v3_process.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkProcessPpid\E$ -test.run ^$ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcessPpid -BenchmarkProcessPpid-80 8694 126542 ns/op -PASS - -Typical /proc/stat file: -cpu 768944 10288 1072079 11045715258 3486 0 5444 0 0 0 -cpu0 8786 70 6308 137988080 26 0 67 0 0 0 -cpu1 9068 30 6191 138072341 59 0 11 0 0 0 -cpu2 65109 159 171353 137907618 83 0 8 0 0 0 -cpu3 24084 118 40891 137992666 88 0 8 0 0 0 -cpu4 43038 444 109352 137978323 83 0 4 0 0 0 -cpu5 21472 193 40224 138025036 82 0 0 0 0 0 -cpu6 42410 118 123254 137981039 92 0 3 0 0 0 -cpu7 18037 232 33534 138039186 101 0 5 0 0 0 -cpu8 31662 186 72772 138013182 98 0 4 0 0 0 -cpu9 13657 181 17251 138023478 91 0 221 0 0 0 -cpu10 31512 159 74950 138015049 40 0 0 0 0 0 -cpu11 12116 472 16357 138037994 71 0 166 0 0 0 -cpu12 21121 85 43973 138039462 63 0 4 0 0 0 -cpu13 10251 27 10354 138021006 59 0 0 0 0 0 -cpu14 18905 280 40738 138048357 50 0 5 0 0 0 -cpu15 10326 360 10793 138065576 47 0 3 0 0 0 -cpu16 11191 100 13201 138062883 52 0 0 0 0 0 -cpu17 8028 107 7053 138066687 54 0 0 0 0 0 -cpu18 7964 102 7473 138082226 43 0 6 0 0 0 -cpu19 7326 158 5075 138084645 27 0 1 0 0 0 -cpu20 7577 4 6287 138079436 41 0 0 0 0 0 -cpu21 7813 12 6644 138067886 84 0 131 0 0 0 -cpu22 6736 117 5078 138085923 19 0 2 0 0 0 -cpu23 7123 128 5150 138086066 21 0 3 0 0 0 -cpu24 9399 13 6703 138082336 24 0 39 0 0 0 -cpu25 8955 45 6350 138083373 26 0 0 0 0 0 -cpu26 6441 83 4730 138088400 31 0 2 0 0 0 -cpu27 6157 25 4148 138089297 27 0 6 0 0 0 -cpu28 6401 9 4586 138087528 44 0 1 0 0 0 -cpu29 6391 17 4265 138087849 46 0 21 0 0 0 -cpu30 6286 461 3308 138090520 50 0 0 0 0 0 -cpu31 6094 104 3068 138091314 86 0 0 0 0 0 -cpu32 6022 108 3822 138089008 19 0 1 0 0 0 -cpu33 5909 73 3729 138088729 11 0 1 0 0 0 -cpu34 6820 294 3541 138088142 6 0 1 0 0 0 -cpu35 6616 129 3347 138090230 8 0 0 0 0 0 -cpu36 6206 107 4706 138083252 9 0 1 0 0 0 -cpu37 6584 28 2864 138085803 23 0 1 0 0 0 -cpu38 6220 131 3001 138091331 37 0 2 0 0 0 -cpu39 5486 217 2772 138092221 38 0 2 0 0 0 -cpu40 5842 31 2824 138093761 14 0 0 0 0 0 -cpu41 6105 125 2923 138093217 17 0 0 0 0 0 -cpu42 5204 44 3539 138091542 12 0 1 0 0 0 -cpu43 5839 32 3417 138091082 10 0 4 0 0 0 -cpu44 5615 19 3980 138092487 22 0 4 0 0 0 -cpu45 6026 424 4100 138091306 19 0 0 0 0 0 -cpu46 5527 209 3688 138094125 12 0 0 0 0 0 -cpu47 5336 109 3033 138094396 12 0 3 0 0 0 -cpu48 5494 103 2653 138096147 2 0 1 0 0 0 -cpu49 5218 30 2393 138097187 5 0 0 0 0 0 -cpu50 5484 239 2130 138096460 8 0 0 0 0 0 -cpu51 5043 34 2446 138096773 15 0 0 0 0 0 -cpu52 5498 27 2971 138095154 8 0 12 0 0 0 -cpu53 5181 43 2634 138087718 8 0 2 0 0 0 -cpu54 4988 36 2976 138095457 4 0 0 0 0 0 -cpu55 5081 21 2344 138095338 7 0 0 0 0 0 -cpu56 4636 273 1897 138096693 12 0 2 0 0 0 -cpu57 4884 316 1935 138095963 15 0 3 0 0 0 -cpu58 4870 229 1745 138097331 6 0 1 0 0 0 -cpu59 4924 28 1746 138097999 7 0 5 0 0 0 -cpu60 4461 129 1955 138096954 361 0 0 0 0 0 -cpu61 5030 35 1930 138097414 460 0 0 0 0 0 -cpu62 4179 23 2157 138097375 9 0 5 0 0 0 -cpu63 4242 134 2235 138096928 10 0 8 0 0 0 -cpu64 6682 254 2108 138094052 4 0 3 0 0 0 -cpu65 4354 115 1743 138097071 3 0 0 0 0 0 -cpu66 4162 9 2496 138095759 7 0 0 0 0 0 -cpu67 3863 28 1960 138097697 6 0 5 0 0 0 -cpu68 13484 17 17176 137901816 109 0 4614 0 0 0 -cpu69 12515 17 14273 137980618 100 0 4 0 0 0 -cpu70 5085 74 3738 138092386 10 0 0 0 0 0 -cpu71 4792 14 3574 138093476 13 0 0 0 0 0 -cpu72 4943 167 2880 138090318 12 0 2 0 0 0 -cpu73 5333 17 4367 138089338 19 0 0 0 0 0 -cpu74 4909 5 2995 138076914 30 0 2 0 0 0 -cpu75 4817 256 3160 138072268 30 0 0 0 0 0 -cpu76 4146 233 2160 138096352 8 0 1 0 0 0 -cpu77 4598 474 1860 138096155 10 0 3 0 0 0 -cpu78 4733 34 2352 138094484 29 0 4 0 0 0 -cpu79 4488 164 2359 138094227 44 0 2 0 0 0 -intr 1136940775 0 1020438 49587864 0 0 0 210556659 0 0 0 0 0 867076724 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 609 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51690 0 0 0 0 0 0 0 1 5604085 970576 914985 910669 70 33120 33768 24240 21358 14764 17850 12776 9795 8765 6098 7307 8012 15687 13640 7797 11437 -ctxt 1918526774 -btime 1691617230 -processes 501797 -procs_running 39 -procs_blocked 0 -softirq 272142382 51064 32640104 523 8408436 152655 0 4696 152439652 131 78445121 From ca71a6db3c7495189bdc01e2235e79175cfd5bd2 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:38:11 -0700 Subject: [PATCH 04/17] lint --- internal/common/common_linux.go | 36 ++++++++++++++++++------------------ process/process_test.go | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index 607d963..a644687 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -73,24 +73,24 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { if useStatFile { return readBootTimeStat(ctx) - } else { - filename := HostProcWithContext(ctx, "uptime") - lines, err := ReadLines(filename) - if err != nil { - return handleBootTimeFileReadErr(err) - } - if len(lines) != 1 { - return 0, fmt.Errorf("wrong uptime format") - } - f := strings.Fields(lines[0]) - b, err := strconv.ParseFloat(f[0], 64) - if err != nil { - return 0, err - } - currentTime := float64(time.Now().UnixNano()) / float64(time.Second) - t := currentTime - b - return uint64(t), nil } + + filename := HostProcWithContext(ctx, "uptime") + lines, err := ReadLines(filename) + if err != nil { + return handleBootTimeFileReadErr(err) + } + if len(lines) != 1 { + return 0, fmt.Errorf("wrong uptime format") + } + f := strings.Fields(lines[0]) + b, err := strconv.ParseFloat(f[0], 64) + if err != nil { + return 0, err + } + currentTime := float64(time.Now().UnixNano()) / float64(time.Second) + t := currentTime - b + return uint64(t), nil } func handleBootTimeFileReadErr(err error) (uint64, error) { @@ -102,7 +102,7 @@ func handleBootTimeFileReadErr(err error) (uint64, error) { } currentTime := time.Now().UnixNano() / int64(time.Second) - t := currentTime - info.Uptime + t := currentTime - int64(info.Uptime) return uint64(t), nil } return 0, err diff --git a/process/process_test.go b/process/process_test.go index ae26960..6dc5695 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -19,9 +19,9 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/shirou/gopsutil/v3/internal/common" - "github.com/stretchr/testify/require" ) var mu sync.Mutex From 4ed0f1436a55c8083fda990321046f12d73c92c0 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 25 Aug 2023 14:39:29 -0700 Subject: [PATCH 05/17] return err if not EOF --- internal/common/common.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/common/common.go b/internal/common/common.go index bad8d1f..7a31d25 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -128,6 +128,7 @@ func ReadLine(filename string, prefix string) (string, error) { if err == io.EOF { break } + return "", err } if strings.HasPrefix(line, prefix) { return line, nil From 6fff1c0e99b0a8b6f9566cfea98353918830f0b0 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Sun, 27 Aug 2023 09:42:51 -0700 Subject: [PATCH 06/17] proper benchmark --- process/process_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/process/process_test.go b/process/process_test.go index 6dc5695..61e2a7a 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -865,7 +865,9 @@ func BenchmarkProcessPpid(b *testing.B) { } func BenchmarkProcesses(b *testing.B) { - ps, err := Processes() - require.NoError(b, err) - require.Greater(b, len(ps), 0) + for i := 0; i < b.N; i++ { + ps, err := Processes() + require.NoError(b, err) + require.Greater(b, len(ps), 0) + } } From ae119d36b8280344151f33a5db5c4a5d58faff89 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Sun, 27 Aug 2023 09:46:08 -0700 Subject: [PATCH 07/17] test results --- process/new.txt | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ process/old.txt | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test_results.txt | 33 ++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 process/new.txt create mode 100644 process/old.txt create mode 100644 test_results.txt diff --git a/process/new.txt b/process/new.txt new file mode 100644 index 0000000..2695a0a --- /dev/null +++ b/process/new.txt @@ -0,0 +1,65 @@ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +Benchmark_fillFromCommWithContext-80 117801 9645 ns/op +Benchmark_fillFromCommWithContext-80 128160 9557 ns/op +Benchmark_fillFromCommWithContext-80 124488 9570 ns/op +Benchmark_fillFromCommWithContext-80 126180 9488 ns/op +Benchmark_fillFromCommWithContext-80 113413 9889 ns/op +Benchmark_fillFromCommWithContext-80 120090 9650 ns/op +Benchmark_fillFromCommWithContext-80 121117 10132 ns/op +Benchmark_fillFromCommWithContext-80 109662 9708 ns/op +Benchmark_fillFromCommWithContext-80 124315 9951 ns/op +Benchmark_fillFromCommWithContext-80 114738 9754 ns/op +Benchmark_fillFromStatusWithContext-80 42361 25613 ns/op +Benchmark_fillFromStatusWithContext-80 45180 26140 ns/op +Benchmark_fillFromStatusWithContext-80 42746 25890 ns/op +Benchmark_fillFromStatusWithContext-80 44743 24767 ns/op +Benchmark_fillFromStatusWithContext-80 47257 26215 ns/op +Benchmark_fillFromStatusWithContext-80 48838 26133 ns/op +Benchmark_fillFromStatusWithContext-80 47120 26333 ns/op +Benchmark_fillFromStatusWithContext-80 44583 26602 ns/op +Benchmark_fillFromStatusWithContext-80 53058 25234 ns/op +Benchmark_fillFromStatusWithContext-80 43609 24786 ns/op +BenchmarkNewProcess-80 9540 125766 ns/op +BenchmarkNewProcess-80 9381 130526 ns/op +BenchmarkNewProcess-80 8694 130677 ns/op +BenchmarkNewProcess-80 10000 126691 ns/op +BenchmarkNewProcess-80 9465 123453 ns/op +BenchmarkNewProcess-80 9014 129650 ns/op +BenchmarkNewProcess-80 9685 127454 ns/op +BenchmarkNewProcess-80 9266 132037 ns/op +BenchmarkNewProcess-80 9447 126732 ns/op +BenchmarkNewProcess-80 9324 128946 ns/op +BenchmarkProcessName-80 405714842 2.906 ns/op +BenchmarkProcessName-80 414845893 2.906 ns/op +BenchmarkProcessName-80 405879571 2.903 ns/op +BenchmarkProcessName-80 405576182 2.902 ns/op +BenchmarkProcessName-80 406102254 2.929 ns/op +BenchmarkProcessName-80 405528814 2.917 ns/op +BenchmarkProcessName-80 406795386 2.904 ns/op +BenchmarkProcessName-80 405236899 2.903 ns/op +BenchmarkProcessName-80 407348631 2.904 ns/op +BenchmarkProcessName-80 406594641 2.900 ns/op +BenchmarkProcessPpid-80 8344 120555 ns/op +BenchmarkProcessPpid-80 10236 120711 ns/op +BenchmarkProcessPpid-80 10000 119398 ns/op +BenchmarkProcessPpid-80 10318 118461 ns/op +BenchmarkProcessPpid-80 10435 116859 ns/op +BenchmarkProcessPpid-80 10386 123064 ns/op +BenchmarkProcessPpid-80 10291 117243 ns/op +BenchmarkProcessPpid-80 10238 119114 ns/op +BenchmarkProcessPpid-80 10484 118057 ns/op +BenchmarkProcessPpid-80 10135 117615 ns/op +BenchmarkProcesses-80 5 218228353 ns/op +BenchmarkProcesses-80 5 215826386 ns/op +BenchmarkProcesses-80 5 219362359 ns/op +BenchmarkProcesses-80 5 215391927 ns/op +BenchmarkProcesses-80 5 209502339 ns/op +BenchmarkProcesses-80 5 217973671 ns/op +BenchmarkProcesses-80 5 211982866 ns/op +BenchmarkProcesses-80 6 207617137 ns/op +BenchmarkProcesses-80 5 217065433 ns/op +BenchmarkProcesses-80 5 211256014 ns/op +PASS +ok github.com/shirou/gopsutil/v3/process 101.689s diff --git a/process/old.txt b/process/old.txt new file mode 100644 index 0000000..0f6d4fe --- /dev/null +++ b/process/old.txt @@ -0,0 +1,65 @@ +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +Benchmark_fillFromCommWithContext-80 119751 9204 ns/op +Benchmark_fillFromCommWithContext-80 124860 9442 ns/op +Benchmark_fillFromCommWithContext-80 130927 9197 ns/op +Benchmark_fillFromCommWithContext-80 118206 9708 ns/op +Benchmark_fillFromCommWithContext-80 116026 9653 ns/op +Benchmark_fillFromCommWithContext-80 129135 9085 ns/op +Benchmark_fillFromCommWithContext-80 119784 9883 ns/op +Benchmark_fillFromCommWithContext-80 125440 9648 ns/op +Benchmark_fillFromCommWithContext-80 124136 9535 ns/op +Benchmark_fillFromCommWithContext-80 123098 9556 ns/op +Benchmark_fillFromStatusWithContext-80 43220 25280 ns/op +Benchmark_fillFromStatusWithContext-80 50610 25287 ns/op +Benchmark_fillFromStatusWithContext-80 48500 24915 ns/op +Benchmark_fillFromStatusWithContext-80 47887 25965 ns/op +Benchmark_fillFromStatusWithContext-80 46794 24535 ns/op +Benchmark_fillFromStatusWithContext-80 44284 24318 ns/op +Benchmark_fillFromStatusWithContext-80 52395 25844 ns/op +Benchmark_fillFromStatusWithContext-80 48452 25524 ns/op +Benchmark_fillFromStatusWithContext-80 47455 26497 ns/op +Benchmark_fillFromStatusWithContext-80 54568 26063 ns/op +BenchmarkNewProcess-80 8568 139806 ns/op +BenchmarkNewProcess-80 8533 140902 ns/op +BenchmarkNewProcess-80 8624 138080 ns/op +BenchmarkNewProcess-80 9368 138218 ns/op +BenchmarkNewProcess-80 8607 133557 ns/op +BenchmarkNewProcess-80 8623 137908 ns/op +BenchmarkNewProcess-80 8538 145054 ns/op +BenchmarkNewProcess-80 8443 147078 ns/op +BenchmarkNewProcess-80 7162 141291 ns/op +BenchmarkNewProcess-80 7294 145403 ns/op +BenchmarkProcessName-80 381517957 3.099 ns/op +BenchmarkProcessName-80 381213841 3.099 ns/op +BenchmarkProcessName-80 382578319 3.101 ns/op +BenchmarkProcessName-80 382308120 3.096 ns/op +BenchmarkProcessName-80 381155623 3.118 ns/op +BenchmarkProcessName-80 382158510 3.099 ns/op +BenchmarkProcessName-80 381464982 3.098 ns/op +BenchmarkProcessName-80 381580702 3.101 ns/op +BenchmarkProcessName-80 381748222 3.098 ns/op +BenchmarkProcessName-80 381223675 3.096 ns/op +BenchmarkProcessPpid-80 9006 132538 ns/op +BenchmarkProcessPpid-80 7534 134648 ns/op +BenchmarkProcessPpid-80 9085 128758 ns/op +BenchmarkProcessPpid-80 9062 133006 ns/op +BenchmarkProcessPpid-80 8802 127635 ns/op +BenchmarkProcessPpid-80 9714 129837 ns/op +BenchmarkProcessPpid-80 9093 132282 ns/op +BenchmarkProcessPpid-80 8434 124920 ns/op +BenchmarkProcessPpid-80 9072 128690 ns/op +BenchmarkProcessPpid-80 9181 133940 ns/op +BenchmarkProcesses-80 5 249461092 ns/op +BenchmarkProcesses-80 4 257841237 ns/op +BenchmarkProcesses-80 4 256107246 ns/op +BenchmarkProcesses-80 4 256276757 ns/op +BenchmarkProcesses-80 4 255431972 ns/op +BenchmarkProcesses-80 5 261030906 ns/op +BenchmarkProcesses-80 5 259028838 ns/op +BenchmarkProcesses-80 4 255359492 ns/op +BenchmarkProcesses-80 4 251292727 ns/op +BenchmarkProcesses-80 5 251119366 ns/op +PASS +ok github.com/shirou/gopsutil/v3/process 103.759s diff --git a/test_results.txt b/test_results.txt new file mode 100644 index 0000000..99a4a97 --- /dev/null +++ b/test_results.txt @@ -0,0 +1,33 @@ +Before: +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcesses +BenchmarkProcesses-80 4 252089573 ns/op +PASS + +Process finished with the exit code 0 + +After: +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process +BenchmarkProcesses +BenchmarkProcesses-80 6 203633386 ns/op +PASS + +Process finished with the exit code 0 + + +goos: linux +goarch: arm64 +pkg: github.com/shirou/gopsutil/v3/process + │ old.txt │ new.txt │ + │ sec/op │ sec/op vs base │ +_fillFromCommWithContext-80 9.546µ ± 4% 9.679µ ± 3% +1.40% (p=0.045 n=10) +_fillFromStatusWithContext-80 25.41µ ± 3% 26.01µ ± 5% ~ (p=0.247 n=10) +NewProcess-80 140.4µ ± 4% 128.2µ ± 2% -8.66% (p=0.000 n=10) +ProcessName-80 3.099n ± 0% 2.904n ± 0% -6.29% (p=0.000 n=10) +ProcessPpid-80 131.1µ ± 3% 118.8µ ± 2% -9.36% (p=0.000 n=10) +Processes-80 255.8m ± 2% 215.6m ± 3% -15.70% (p=0.000 n=10) +geomean 39.03µ 36.59µ -6.25% From 54c31d884d8caa2caa6e1fa4ab81526ebe25f82f Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Sun, 27 Aug 2023 10:01:46 -0700 Subject: [PATCH 08/17] Revert "test results" This reverts commit ae119d36b8280344151f33a5db5c4a5d58faff89. --- process/new.txt | 65 -------------------------------------------------------- process/old.txt | 65 -------------------------------------------------------- test_results.txt | 33 ---------------------------- 3 files changed, 163 deletions(-) delete mode 100644 process/new.txt delete mode 100644 process/old.txt delete mode 100644 test_results.txt diff --git a/process/new.txt b/process/new.txt deleted file mode 100644 index 2695a0a..0000000 --- a/process/new.txt +++ /dev/null @@ -1,65 +0,0 @@ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -Benchmark_fillFromCommWithContext-80 117801 9645 ns/op -Benchmark_fillFromCommWithContext-80 128160 9557 ns/op -Benchmark_fillFromCommWithContext-80 124488 9570 ns/op -Benchmark_fillFromCommWithContext-80 126180 9488 ns/op -Benchmark_fillFromCommWithContext-80 113413 9889 ns/op -Benchmark_fillFromCommWithContext-80 120090 9650 ns/op -Benchmark_fillFromCommWithContext-80 121117 10132 ns/op -Benchmark_fillFromCommWithContext-80 109662 9708 ns/op -Benchmark_fillFromCommWithContext-80 124315 9951 ns/op -Benchmark_fillFromCommWithContext-80 114738 9754 ns/op -Benchmark_fillFromStatusWithContext-80 42361 25613 ns/op -Benchmark_fillFromStatusWithContext-80 45180 26140 ns/op -Benchmark_fillFromStatusWithContext-80 42746 25890 ns/op -Benchmark_fillFromStatusWithContext-80 44743 24767 ns/op -Benchmark_fillFromStatusWithContext-80 47257 26215 ns/op -Benchmark_fillFromStatusWithContext-80 48838 26133 ns/op -Benchmark_fillFromStatusWithContext-80 47120 26333 ns/op -Benchmark_fillFromStatusWithContext-80 44583 26602 ns/op -Benchmark_fillFromStatusWithContext-80 53058 25234 ns/op -Benchmark_fillFromStatusWithContext-80 43609 24786 ns/op -BenchmarkNewProcess-80 9540 125766 ns/op -BenchmarkNewProcess-80 9381 130526 ns/op -BenchmarkNewProcess-80 8694 130677 ns/op -BenchmarkNewProcess-80 10000 126691 ns/op -BenchmarkNewProcess-80 9465 123453 ns/op -BenchmarkNewProcess-80 9014 129650 ns/op -BenchmarkNewProcess-80 9685 127454 ns/op -BenchmarkNewProcess-80 9266 132037 ns/op -BenchmarkNewProcess-80 9447 126732 ns/op -BenchmarkNewProcess-80 9324 128946 ns/op -BenchmarkProcessName-80 405714842 2.906 ns/op -BenchmarkProcessName-80 414845893 2.906 ns/op -BenchmarkProcessName-80 405879571 2.903 ns/op -BenchmarkProcessName-80 405576182 2.902 ns/op -BenchmarkProcessName-80 406102254 2.929 ns/op -BenchmarkProcessName-80 405528814 2.917 ns/op -BenchmarkProcessName-80 406795386 2.904 ns/op -BenchmarkProcessName-80 405236899 2.903 ns/op -BenchmarkProcessName-80 407348631 2.904 ns/op -BenchmarkProcessName-80 406594641 2.900 ns/op -BenchmarkProcessPpid-80 8344 120555 ns/op -BenchmarkProcessPpid-80 10236 120711 ns/op -BenchmarkProcessPpid-80 10000 119398 ns/op -BenchmarkProcessPpid-80 10318 118461 ns/op -BenchmarkProcessPpid-80 10435 116859 ns/op -BenchmarkProcessPpid-80 10386 123064 ns/op -BenchmarkProcessPpid-80 10291 117243 ns/op -BenchmarkProcessPpid-80 10238 119114 ns/op -BenchmarkProcessPpid-80 10484 118057 ns/op -BenchmarkProcessPpid-80 10135 117615 ns/op -BenchmarkProcesses-80 5 218228353 ns/op -BenchmarkProcesses-80 5 215826386 ns/op -BenchmarkProcesses-80 5 219362359 ns/op -BenchmarkProcesses-80 5 215391927 ns/op -BenchmarkProcesses-80 5 209502339 ns/op -BenchmarkProcesses-80 5 217973671 ns/op -BenchmarkProcesses-80 5 211982866 ns/op -BenchmarkProcesses-80 6 207617137 ns/op -BenchmarkProcesses-80 5 217065433 ns/op -BenchmarkProcesses-80 5 211256014 ns/op -PASS -ok github.com/shirou/gopsutil/v3/process 101.689s diff --git a/process/old.txt b/process/old.txt deleted file mode 100644 index 0f6d4fe..0000000 --- a/process/old.txt +++ /dev/null @@ -1,65 +0,0 @@ -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -Benchmark_fillFromCommWithContext-80 119751 9204 ns/op -Benchmark_fillFromCommWithContext-80 124860 9442 ns/op -Benchmark_fillFromCommWithContext-80 130927 9197 ns/op -Benchmark_fillFromCommWithContext-80 118206 9708 ns/op -Benchmark_fillFromCommWithContext-80 116026 9653 ns/op -Benchmark_fillFromCommWithContext-80 129135 9085 ns/op -Benchmark_fillFromCommWithContext-80 119784 9883 ns/op -Benchmark_fillFromCommWithContext-80 125440 9648 ns/op -Benchmark_fillFromCommWithContext-80 124136 9535 ns/op -Benchmark_fillFromCommWithContext-80 123098 9556 ns/op -Benchmark_fillFromStatusWithContext-80 43220 25280 ns/op -Benchmark_fillFromStatusWithContext-80 50610 25287 ns/op -Benchmark_fillFromStatusWithContext-80 48500 24915 ns/op -Benchmark_fillFromStatusWithContext-80 47887 25965 ns/op -Benchmark_fillFromStatusWithContext-80 46794 24535 ns/op -Benchmark_fillFromStatusWithContext-80 44284 24318 ns/op -Benchmark_fillFromStatusWithContext-80 52395 25844 ns/op -Benchmark_fillFromStatusWithContext-80 48452 25524 ns/op -Benchmark_fillFromStatusWithContext-80 47455 26497 ns/op -Benchmark_fillFromStatusWithContext-80 54568 26063 ns/op -BenchmarkNewProcess-80 8568 139806 ns/op -BenchmarkNewProcess-80 8533 140902 ns/op -BenchmarkNewProcess-80 8624 138080 ns/op -BenchmarkNewProcess-80 9368 138218 ns/op -BenchmarkNewProcess-80 8607 133557 ns/op -BenchmarkNewProcess-80 8623 137908 ns/op -BenchmarkNewProcess-80 8538 145054 ns/op -BenchmarkNewProcess-80 8443 147078 ns/op -BenchmarkNewProcess-80 7162 141291 ns/op -BenchmarkNewProcess-80 7294 145403 ns/op -BenchmarkProcessName-80 381517957 3.099 ns/op -BenchmarkProcessName-80 381213841 3.099 ns/op -BenchmarkProcessName-80 382578319 3.101 ns/op -BenchmarkProcessName-80 382308120 3.096 ns/op -BenchmarkProcessName-80 381155623 3.118 ns/op -BenchmarkProcessName-80 382158510 3.099 ns/op -BenchmarkProcessName-80 381464982 3.098 ns/op -BenchmarkProcessName-80 381580702 3.101 ns/op -BenchmarkProcessName-80 381748222 3.098 ns/op -BenchmarkProcessName-80 381223675 3.096 ns/op -BenchmarkProcessPpid-80 9006 132538 ns/op -BenchmarkProcessPpid-80 7534 134648 ns/op -BenchmarkProcessPpid-80 9085 128758 ns/op -BenchmarkProcessPpid-80 9062 133006 ns/op -BenchmarkProcessPpid-80 8802 127635 ns/op -BenchmarkProcessPpid-80 9714 129837 ns/op -BenchmarkProcessPpid-80 9093 132282 ns/op -BenchmarkProcessPpid-80 8434 124920 ns/op -BenchmarkProcessPpid-80 9072 128690 ns/op -BenchmarkProcessPpid-80 9181 133940 ns/op -BenchmarkProcesses-80 5 249461092 ns/op -BenchmarkProcesses-80 4 257841237 ns/op -BenchmarkProcesses-80 4 256107246 ns/op -BenchmarkProcesses-80 4 256276757 ns/op -BenchmarkProcesses-80 4 255431972 ns/op -BenchmarkProcesses-80 5 261030906 ns/op -BenchmarkProcesses-80 5 259028838 ns/op -BenchmarkProcesses-80 4 255359492 ns/op -BenchmarkProcesses-80 4 251292727 ns/op -BenchmarkProcesses-80 5 251119366 ns/op -PASS -ok github.com/shirou/gopsutil/v3/process 103.759s diff --git a/test_results.txt b/test_results.txt deleted file mode 100644 index 99a4a97..0000000 --- a/test_results.txt +++ /dev/null @@ -1,33 +0,0 @@ -Before: -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcesses -BenchmarkProcesses-80 4 252089573 ns/op -PASS - -Process finished with the exit code 0 - -After: -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process -BenchmarkProcesses -BenchmarkProcesses-80 6 203633386 ns/op -PASS - -Process finished with the exit code 0 - - -goos: linux -goarch: arm64 -pkg: github.com/shirou/gopsutil/v3/process - │ old.txt │ new.txt │ - │ sec/op │ sec/op vs base │ -_fillFromCommWithContext-80 9.546µ ± 4% 9.679µ ± 3% +1.40% (p=0.045 n=10) -_fillFromStatusWithContext-80 25.41µ ± 3% 26.01µ ± 5% ~ (p=0.247 n=10) -NewProcess-80 140.4µ ± 4% 128.2µ ± 2% -8.66% (p=0.000 n=10) -ProcessName-80 3.099n ± 0% 2.904n ± 0% -6.29% (p=0.000 n=10) -ProcessPpid-80 131.1µ ± 3% 118.8µ ± 2% -9.36% (p=0.000 n=10) -Processes-80 255.8m ± 2% 215.6m ± 3% -15.70% (p=0.000 n=10) -geomean 39.03µ 36.59µ -6.25% From febdeab87192b3b2ce92b85e18872b7aa820a23c Mon Sep 17 00:00:00 2001 From: shirou Date: Wed, 30 Aug 2023 14:07:47 +0000 Subject: [PATCH 09/17] chore: change CIRCLECI environment variable to CI. --- cpu/cpu_test.go | 8 ++++---- host/host_test.go | 4 ++-- process/process_test.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index 4202ce8..688660a 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -138,8 +138,8 @@ func testCPUPercent(t *testing.T, percpu bool) { if err != nil { t.Errorf("error %v", err) } - // Skip CircleCI which CPU num is different - if os.Getenv("CIRCLECI") != "true" { + // Skip CI which CPU num is different + if os.Getenv("CI") != "true" { if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) { t.Fatalf("wrong number of entries from CPUPercent: %v", v) } @@ -172,8 +172,8 @@ func testCPUPercentLastUsed(t *testing.T, percpu bool) { if err != nil { t.Errorf("error %v", err) } - // Skip CircleCI which CPU num is different - if os.Getenv("CIRCLECI") != "true" { + // Skip CI which CPU num is different + if os.Getenv("CI") != "true" { if (percpu && len(v) != numcpu) || (!percpu && len(v) != 1) { t.Fatalf("wrong number of entries from CPUPercent: %v", v) } diff --git a/host/host_test.go b/host/host_test.go index 29cb9d2..17ced6d 100644 --- a/host/host_test.go +++ b/host/host_test.go @@ -33,7 +33,7 @@ func TestHostInfo(t *testing.T) { } func TestUptime(t *testing.T) { - if os.Getenv("CIRCLECI") == "true" { + if os.Getenv("CI") == "true" { t.Skip("Skip CI") } @@ -48,7 +48,7 @@ func TestUptime(t *testing.T) { } func TestBoot_time(t *testing.T) { - if os.Getenv("CIRCLECI") == "true" { + if os.Getenv("CI") == "true" { t.Skip("Skip CI") } v, err := BootTime() diff --git a/process/process_test.go b/process/process_test.go index 61e2a7a..fc44963 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -502,7 +502,7 @@ func Test_Process_CpuPercentLoop(t *testing.T) { } func Test_Process_CreateTime(t *testing.T) { - if os.Getenv("CIRCLECI") == "true" { + if os.Getenv("CI") == "true" { t.Skip("Skip CI") } From 7f4efa53585d34fa0938f1cbb0025120ea4183b8 Mon Sep 17 00:00:00 2001 From: Kevin Conaway Date: Thu, 31 Aug 2023 13:19:58 -0400 Subject: [PATCH 10/17] Add support for reading AnonHugePages from /proc/meminfo This commit adds support for reading the `AnonHugePages` field from `/proc/meminfo`. The values in this field allow monitoring the [THP](https://www.kernel.org/doc/Documentation/vm/transhuge.txt) usage by systems that use this type of memory --- mem/mem.go | 2 ++ mem/mem_linux.go | 6 ++++++ mem/mem_linux_test.go | 10 ++++++++++ mem/mem_test.go | 2 +- mem/testdata/linux/virtualmemory/anonhugepages/proc/meminfo | 4 ++++ 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 mem/testdata/linux/virtualmemory/anonhugepages/proc/meminfo diff --git a/mem/mem.go b/mem/mem.go index ff960da..edaf268 100644 --- a/mem/mem.go +++ b/mem/mem.go @@ -50,6 +50,7 @@ type VirtualMemoryStat struct { // https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html // https://www.kernel.org/doc/Documentation/filesystems/proc.txt // https://www.kernel.org/doc/Documentation/vm/overcommit-accounting + // https://www.kernel.org/doc/Documentation/vm/transhuge.txt Buffers uint64 `json:"buffers"` Cached uint64 `json:"cached"` WriteBack uint64 `json:"writeBack"` @@ -78,6 +79,7 @@ type VirtualMemoryStat struct { HugePagesRsvd uint64 `json:"hugePagesRsvd"` HugePagesSurp uint64 `json:"hugePagesSurp"` HugePageSize uint64 `json:"hugePageSize"` + AnonHugePages uint64 `json:"anonHugePages"` } type SwapMemoryStat struct { diff --git a/mem/mem_linux.go b/mem/mem_linux.go index 9353317..214a91e 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -311,6 +311,12 @@ func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *Virtu return ret, retEx, err } ret.HugePageSize = t * 1024 + case "AnonHugePages": + t, err := strconv.ParseUint(value, 10, 64) + if err != nil { + return ret, retEx, err + } + ret.AnonHugePages = t * 1024 } } diff --git a/mem/mem_linux_test.go b/mem/mem_linux_test.go index d830fbf..6b6fb78 100644 --- a/mem/mem_linux_test.go +++ b/mem/mem_linux_test.go @@ -108,6 +108,16 @@ var virtualMemoryTests = []struct { HugePageSize: 0, }, }, + { + "anonhugepages", &VirtualMemoryStat{ + Total: 260799420 * 1024, + Available: 127880216 * 1024, + Free: 119443248 * 1024, + AnonHugePages: 50409472 * 1024, + Used: 144748720128, + UsedPercent: 54.20110673559013, + }, + }, } func TestVirtualMemoryLinux(t *testing.T) { diff --git a/mem/mem_test.go b/mem/mem_test.go index 3be5656..79ddb0f 100644 --- a/mem/mem_test.go +++ b/mem/mem_test.go @@ -89,7 +89,7 @@ func TestVirtualMemoryStat_String(t *testing.T) { Free: 40, } t.Log(v) - e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"buffers":0,"cached":0,"writeBack":0,"dirty":0,"writeBackTmp":0,"shared":0,"slab":0,"sreclaimable":0,"sunreclaim":0,"pageTables":0,"swapCached":0,"commitLimit":0,"committedAS":0,"highTotal":0,"highFree":0,"lowTotal":0,"lowFree":0,"swapTotal":0,"swapFree":0,"mapped":0,"vmallocTotal":0,"vmallocUsed":0,"vmallocChunk":0,"hugePagesTotal":0,"hugePagesFree":0,"hugePagesRsvd":0,"hugePagesSurp":0,"hugePageSize":0}` + e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"buffers":0,"cached":0,"writeBack":0,"dirty":0,"writeBackTmp":0,"shared":0,"slab":0,"sreclaimable":0,"sunreclaim":0,"pageTables":0,"swapCached":0,"commitLimit":0,"committedAS":0,"highTotal":0,"highFree":0,"lowTotal":0,"lowFree":0,"swapTotal":0,"swapFree":0,"mapped":0,"vmallocTotal":0,"vmallocUsed":0,"vmallocChunk":0,"hugePagesTotal":0,"hugePagesFree":0,"hugePagesRsvd":0,"hugePagesSurp":0,"hugePageSize":0,"anonHugePages":0}` if e != fmt.Sprintf("%v", v) { t.Errorf("VirtualMemoryStat string is invalid: %v", v) } diff --git a/mem/testdata/linux/virtualmemory/anonhugepages/proc/meminfo b/mem/testdata/linux/virtualmemory/anonhugepages/proc/meminfo new file mode 100644 index 0000000..d158c67 --- /dev/null +++ b/mem/testdata/linux/virtualmemory/anonhugepages/proc/meminfo @@ -0,0 +1,4 @@ +MemTotal: 260799420 kB +MemFree: 119443248 kB +MemAvailable: 127880216 kB +AnonHugePages: 50409472 kB \ No newline at end of file From 85d37b91b38a03e13d02fca11f7ab3cdf47345a3 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 4 Sep 2023 01:53:34 +0000 Subject: [PATCH 11/17] chore(deps): bump golang.org/x/sys from 0.11.0 to 0.12.0 Bumps [golang.org/x/sys](https://github.com/golang/sys) from 0.11.0 to 0.12.0. - [Commits](https://github.com/golang/sys/compare/v0.11.0...v0.12.0) --- updated-dependencies: - dependency-name: golang.org/x/sys dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 8fc781e..78cc4af 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/stretchr/testify v1.8.4 github.com/tklauser/go-sysconf v0.3.12 github.com/yusufpapurcu/wmi v1.2.3 - golang.org/x/sys v0.11.0 + golang.org/x/sys v0.12.0 ) retract v3.22.11 diff --git a/go.sum b/go.sum index 914427d..e558c9c 100644 --- a/go.sum +++ b/go.sum @@ -32,8 +32,9 @@ github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From eadf3f7a33b5d05e95ee2049c60ee848a982ecc8 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 5 Sep 2023 01:50:24 +0000 Subject: [PATCH 12/17] chore(deps): bump actions/checkout from 3.6.0 to 4.0.0 Bumps [actions/checkout](https://github.com/actions/checkout) from 3.6.0 to 4.0.0. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/f43a0e5ff2bd294095638e18286ca9a3d1956744...3df4ab11eba7bda6032a0b82a6bb43b11571feac) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/build_test.yml | 2 +- .github/workflows/lint.yml | 2 +- .github/workflows/release.yml | 2 +- .github/workflows/sbom_generator.yml | 2 +- .github/workflows/shellcheck.yml | 2 +- .github/workflows/test.yml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index ce255f9..5aa2677 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -26,7 +26,7 @@ jobs: with: go-version: ${{ matrix.go-version }} - name: Checkout code - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 - id: cache-paths run: | echo "::set-output name=cache::$(go env GOCACHE)" diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5a9ebca..91feb39 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,7 +20,7 @@ jobs: with: go-version: 1.17 - name: Checkout repository - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 - name: Setup golangci-lint uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc # v3.7.0 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index efd3bae..6bdd103 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,6 +10,6 @@ jobs: release: runs-on: ubuntu-latest steps: - - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 - name: Release run: make release diff --git a/.github/workflows/sbom_generator.yml b/.github/workflows/sbom_generator.yml index 97565c1..3bd0a5d 100644 --- a/.github/workflows/sbom_generator.yml +++ b/.github/workflows/sbom_generator.yml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 - uses: advanced-security/sbom-generator-action@375dee8e6144d9fd0ec1f5667b4f6fb4faacefed # v0.0.1 id: sbom diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.yml index b700ca8..2c2198f 100644 --- a/.github/workflows/shellcheck.yml +++ b/.github/workflows/shellcheck.yml @@ -8,6 +8,6 @@ jobs: name: Shellcheck runs-on: ubuntu-latest steps: - - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 - name: Run ShellCheck uses: ludeeus/action-shellcheck@00cae500b08a931fb5698e11e79bfbd38e612a38 # v2.0.0 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dacda8b..6055a1b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,7 +27,7 @@ jobs: with: go-version: ${{ matrix.go-version }} - name: Checkout code - uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3.6.0 + uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 - id: go-env run: | echo "::set-output name=cache::$(go env GOCACHE)" From 2d2033ed7dede52f74ffd228845d8fd06d439d4d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 7 Sep 2023 01:12:39 +0000 Subject: [PATCH 13/17] chore(deps): bump actions/upload-artifact from 3.1.2 to 3.1.3 Bumps [actions/upload-artifact](https://github.com/actions/upload-artifact) from 3.1.2 to 3.1.3. - [Release notes](https://github.com/actions/upload-artifact/releases) - [Commits](https://github.com/actions/upload-artifact/compare/0b7f8abb1508181956e8e162db84b466c27e18ce...a8a3f3ad30e3422c9c7b888a15615d19a852ae32) --- updated-dependencies: - dependency-name: actions/upload-artifact dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/sbom_generator.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sbom_generator.yml b/.github/workflows/sbom_generator.yml index 97565c1..f2db27d 100644 --- a/.github/workflows/sbom_generator.yml +++ b/.github/workflows/sbom_generator.yml @@ -19,7 +19,7 @@ jobs: id: sbom env: GITHUB_TOKEN: ${{ github.token }} - - uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2 + - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3 with: path: ${{steps.sbom.outputs.fileName }} name: "SBOM" From 1d4b8c3989e0da46526c4e05fbf74456098f1c15 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 8 Sep 2023 01:37:26 +0000 Subject: [PATCH 14/17] chore(deps): bump actions/cache from 3.3.1 to 3.3.2 Bumps [actions/cache](https://github.com/actions/cache) from 3.3.1 to 3.3.2. - [Release notes](https://github.com/actions/cache/releases) - [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md) - [Commits](https://github.com/actions/cache/compare/88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8...704facf57e6136b1bc63b828d79edcd491f0ee84) --- updated-dependencies: - dependency-name: actions/cache dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/build_test.yml | 2 +- .github/workflows/test.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build_test.yml b/.github/workflows/build_test.yml index ce255f9..10a3959 100644 --- a/.github/workflows/build_test.yml +++ b/.github/workflows/build_test.yml @@ -32,7 +32,7 @@ jobs: echo "::set-output name=cache::$(go env GOCACHE)" echo "::set-output name=mod-cache::$(go env GOMODCACHE)" - name: Cache go modules - uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1 + uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 with: path: | ${{ steps.cache-paths.outputs.cache }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dacda8b..67f2081 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,7 +33,7 @@ jobs: echo "::set-output name=cache::$(go env GOCACHE)" echo "::set-output name=mod-cache::$(go env GOMODCACHE)" - name: Cache go modules - uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1 + uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 with: path: | ${{ steps.go-env.outputs.cache }} From 0665cafa1b8eb4b284231dd2b488f80227d99279 Mon Sep 17 00:00:00 2001 From: shirou Date: Fri, 8 Sep 2023 17:05:14 +0000 Subject: [PATCH 15/17] chore: replace deprecated ioutil package to os and io --- cpu/cpu_solaris_test.go | 6 +++--- disk/disk_linux.go | 9 ++++----- host/host_darwin.go | 4 ++-- host/host_freebsd.go | 6 +++--- host/host_linux.go | 16 ++++++++-------- host/host_openbsd.go | 4 ++-- host/host_solaris.go | 2 +- internal/common/common.go | 5 ++--- load/load_linux.go | 6 +++--- net/net_linux.go | 7 +++---- process/process_linux.go | 21 ++++++++++----------- process/process_linux_test.go | 2 +- process/process_solaris.go | 7 +++---- process/process_test.go | 3 ++- 14 files changed, 47 insertions(+), 51 deletions(-) diff --git a/cpu/cpu_solaris_test.go b/cpu/cpu_solaris_test.go index 508aad5..dd9362c 100644 --- a/cpu/cpu_solaris_test.go +++ b/cpu/cpu_solaris_test.go @@ -1,7 +1,7 @@ package cpu import ( - "io/ioutil" + "os" "path/filepath" "reflect" "sort" @@ -49,7 +49,7 @@ func TestParseISAInfo(t *testing.T) { } for _, tc := range cases { - content, err := ioutil.ReadFile(filepath.Join("testdata", "solaris", tc.filename)) + content, err := os.ReadFile(filepath.Join("testdata", "solaris", tc.filename)) if err != nil { t.Errorf("cannot read test case: %s", err) } @@ -138,7 +138,7 @@ func TestParseProcessorInfo(t *testing.T) { } for _, tc := range cases { - content, err := ioutil.ReadFile(filepath.Join("testdata", "solaris", tc.filename)) + content, err := os.ReadFile(filepath.Join("testdata", "solaris", tc.filename)) if err != nil { t.Errorf("cannot read test case: %s", err) } diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 5015c34..c06516c 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -9,7 +9,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" "path" "path/filepath" @@ -497,7 +496,7 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) { // Try to get the serial from udev data udevDataPath := common.HostRunWithContext(ctx, fmt.Sprintf("udev/data/b%d:%d", major, minor)) - if udevdata, err := ioutil.ReadFile(udevDataPath); err == nil { + if udevdata, err := os.ReadFile(udevDataPath); err == nil { scanner := bufio.NewScanner(bytes.NewReader(udevdata)) for scanner.Scan() { values := strings.Split(scanner.Text(), "=") @@ -510,8 +509,8 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) { // Try to get the serial from sysfs, look at the disk device (minor 0) directly // because if it is a partition it is not going to contain any device information devicePath := common.HostSysWithContext(ctx, fmt.Sprintf("dev/block/%d:0/device", major)) - model, _ := ioutil.ReadFile(filepath.Join(devicePath, "model")) - serial, _ := ioutil.ReadFile(filepath.Join(devicePath, "serial")) + model, _ := os.ReadFile(filepath.Join(devicePath, "model")) + serial, _ := os.ReadFile(filepath.Join(devicePath, "serial")) if len(model) > 0 && len(serial) > 0 { return fmt.Sprintf("%s_%s", string(model), string(serial)), nil } @@ -526,7 +525,7 @@ func LabelWithContext(ctx context.Context, name string) (string, error) { return "", nil } - dmname, err := ioutil.ReadFile(dmname_filename) + dmname, err := os.ReadFile(dmname_filename) if err != nil { return "", err } diff --git a/host/host_darwin.go b/host/host_darwin.go index 1be2e85..f045d4f 100644 --- a/host/host_darwin.go +++ b/host/host_darwin.go @@ -8,7 +8,7 @@ import ( "context" "encoding/binary" "errors" - "io/ioutil" + "io" "os" "strings" "unsafe" @@ -59,7 +59,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) { } defer file.Close() - buf, err := ioutil.ReadAll(file) + buf, err := io.ReadAll(file) if err != nil { return ret, err } diff --git a/host/host_freebsd.go b/host/host_freebsd.go index 2c9aa9d..9a5382d 100644 --- a/host/host_freebsd.go +++ b/host/host_freebsd.go @@ -7,7 +7,7 @@ import ( "bytes" "context" "encoding/binary" - "io/ioutil" + "io" "math" "os" "strings" @@ -54,7 +54,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) { } defer file.Close() - buf, err := ioutil.ReadAll(file) + buf, err := io.ReadAll(file) if err != nil { return ret, err } @@ -111,7 +111,7 @@ func getUsersFromUtmp(utmpfile string) ([]UserStat, error) { } defer file.Close() - buf, err := ioutil.ReadAll(file) + buf, err := io.ReadAll(file) if err != nil { return ret, err } diff --git a/host/host_linux.go b/host/host_linux.go index e6ac63a..f9d7995 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -8,7 +8,7 @@ import ( "context" "encoding/binary" "fmt" - "io/ioutil" + "io" "os" "path/filepath" "regexp" @@ -91,7 +91,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) { } defer file.Close() - buf, err := ioutil.ReadAll(file) + buf, err := io.ReadAll(file) if err != nil { return nil, err } @@ -411,13 +411,13 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err } for _, file := range files { // Get the name of the temperature you are reading - name, err := ioutil.ReadFile(filepath.Join(file, "type")) + name, err := os.ReadFile(filepath.Join(file, "type")) if err != nil { warns.Add(err) continue } // Get the temperature reading - current, err := ioutil.ReadFile(filepath.Join(file, "temp")) + current, err := os.ReadFile(filepath.Join(file, "temp")) if err != nil { warns.Add(err) continue @@ -461,13 +461,13 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err // Get the label of the temperature you are reading label := "" - if raw, _ = ioutil.ReadFile(basepath + "_label"); len(raw) != 0 { + if raw, _ = os.ReadFile(basepath + "_label"); len(raw) != 0 { // Format the label from "Core 0" to "core_0" label = strings.Join(strings.Split(strings.TrimSpace(strings.ToLower(string(raw))), " "), "_") } // Get the name of the temperature you are reading - if raw, err = ioutil.ReadFile(filepath.Join(directory, "name")); err != nil { + if raw, err = os.ReadFile(filepath.Join(directory, "name")); err != nil { warns.Add(err) continue } @@ -479,7 +479,7 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err } // Get the temperature reading - if raw, err = ioutil.ReadFile(file); err != nil { + if raw, err = os.ReadFile(file); err != nil { warns.Add(err) continue } @@ -513,7 +513,7 @@ func optionalValueReadFromFile(filename string) float64 { return 0 } - if raw, err = ioutil.ReadFile(filename); err != nil { + if raw, err = os.ReadFile(filename); err != nil { return 0 } diff --git a/host/host_openbsd.go b/host/host_openbsd.go index 569de4a..325015c 100644 --- a/host/host_openbsd.go +++ b/host/host_openbsd.go @@ -7,7 +7,7 @@ import ( "bytes" "context" "encoding/binary" - "io/ioutil" + "io" "os" "strings" "unsafe" @@ -65,7 +65,7 @@ func UsersWithContext(ctx context.Context) ([]UserStat, error) { } defer file.Close() - buf, err := ioutil.ReadAll(file) + buf, err := io.ReadAll(file) if err != nil { return ret, err } diff --git a/host/host_solaris.go b/host/host_solaris.go index 7d3625a..e0661b0 100644 --- a/host/host_solaris.go +++ b/host/host_solaris.go @@ -138,7 +138,7 @@ func VirtualizationWithContext(ctx context.Context) (string, string, error) { // Find distribution name from /etc/release func parseReleaseFile() (string, error) { - b, err := ioutil.ReadFile("/etc/release") + b, err := os.ReadFile("/etc/release") if err != nil { return "", err } diff --git a/internal/common/common.go b/internal/common/common.go index 7a31d25..99ed6a5 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -14,7 +14,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/url" "os" "os/exec" @@ -87,7 +86,7 @@ func (i FakeInvoke) Command(name string, arg ...string) ([]byte, error) { fpath += "_" + i.Suffix } if PathExists(fpath) { - return ioutil.ReadFile(fpath) + return os.ReadFile(fpath) } return []byte{}, fmt.Errorf("could not find testdata: %s", fpath) } @@ -100,7 +99,7 @@ var ErrNotImplementedError = errors.New("not implemented yet") // ReadFile reads contents from a file func ReadFile(filename string) (string, error) { - content, err := ioutil.ReadFile(filename) + content, err := os.ReadFile(filename) if err != nil { return "", err } diff --git a/load/load_linux.go b/load/load_linux.go index 0298c8b..06bceeb 100644 --- a/load/load_linux.go +++ b/load/load_linux.go @@ -5,7 +5,7 @@ package load import ( "context" - "io/ioutil" + "os" "strconv" "strings" "syscall" @@ -76,7 +76,7 @@ func Misc() (*MiscStat, error) { func MiscWithContext(ctx context.Context) (*MiscStat, error) { filename := common.HostProcWithContext(ctx, "stat") - out, err := ioutil.ReadFile(filename) + out, err := os.ReadFile(filename) if err != nil { return nil, err } @@ -126,7 +126,7 @@ func getProcsTotal(ctx context.Context) (int64, error) { func readLoadAvgFromFile(ctx context.Context) ([]string, error) { loadavgFilename := common.HostProcWithContext(ctx, "loadavg") - line, err := ioutil.ReadFile(loadavgFilename) + line, err := os.ReadFile(loadavgFilename) if err != nil { return nil, err } diff --git a/net/net_linux.go b/net/net_linux.go index de0ea73..6e8ce67 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -10,7 +10,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "os" "strconv" @@ -643,7 +642,7 @@ func (p *process) getUids(ctx context.Context) ([]int32, error) { func (p *process) fillFromStatus(ctx context.Context) error { pid := p.Pid statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "status") - contents, err := ioutil.ReadFile(statPath) + contents, err := os.ReadFile(statPath) if err != nil { return err } @@ -784,7 +783,7 @@ func processInetWithContext(ctx context.Context, file string, kind netConnection // This minimizes duplicates in the returned connections // For more info: // https://github.com/shirou/gopsutil/pull/361 - contents, err := ioutil.ReadFile(file) + contents, err := os.ReadFile(file) if err != nil { return nil, err } @@ -845,7 +844,7 @@ func processUnix(file string, kind netConnectionKindType, inodes map[string][]in // This minimizes duplicates in the returned connections // For more info: // https://github.com/shirou/gopsutil/pull/361 - contents, err := ioutil.ReadFile(file) + contents, err := os.ReadFile(file) if err != nil { return nil, err } diff --git a/process/process_linux.go b/process/process_linux.go index 37cb7ca..f7989cd 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -9,7 +9,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "math" "os" "path/filepath" @@ -136,7 +135,7 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) { // see https://github.com/shirou/gopsutil/issues/596#issuecomment-432707831 for implementation details pid := p.Pid statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "stat") - contents, err := ioutil.ReadFile(statPath) + contents, err := os.ReadFile(statPath) if err != nil { return false, err } @@ -391,7 +390,7 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M smapsPath = smapsRollupPath } } - contents, err := ioutil.ReadFile(smapsPath) + contents, err := os.ReadFile(smapsPath) if err != nil { return nil, err } @@ -484,7 +483,7 @@ func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]M func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error) { environPath := common.HostProcWithContext(ctx, strconv.Itoa(int(p.Pid)), "environ") - environContent, err := ioutil.ReadFile(environPath) + environContent, err := os.ReadFile(environPath) if err != nil { return nil, err } @@ -668,7 +667,7 @@ func (p *Process) fillFromExeWithContext(ctx context.Context) (string, error) { func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error) { pid := p.Pid cmdPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) + cmdline, err := os.ReadFile(cmdPath) if err != nil { return "", err } @@ -682,7 +681,7 @@ func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string, error) { pid := p.Pid cmdPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) + cmdline, err := os.ReadFile(cmdPath) if err != nil { return nil, err } @@ -705,7 +704,7 @@ func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, error) { pid := p.Pid ioPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "io") - ioline, err := ioutil.ReadFile(ioPath) + ioline, err := os.ReadFile(ioPath) if err != nil { return nil, err } @@ -741,7 +740,7 @@ func (p *Process) fillFromIOWithContext(ctx context.Context) (*IOCountersStat, e func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat, *MemoryInfoExStat, error) { pid := p.Pid memPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "statm") - contents, err := ioutil.ReadFile(memPath) + contents, err := os.ReadFile(memPath) if err != nil { return nil, nil, err } @@ -802,7 +801,7 @@ func (p *Process) fillNameWithContext(ctx context.Context) error { func (p *Process) fillFromCommWithContext(ctx context.Context) error { pid := p.Pid statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "comm") - contents, err := ioutil.ReadFile(statPath) + contents, err := os.ReadFile(statPath) if err != nil { return err } @@ -819,7 +818,7 @@ func (p *Process) fillFromStatus() error { func (p *Process) fillFromStatusWithContext(ctx context.Context) error { pid := p.Pid statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "status") - contents, err := ioutil.ReadFile(statPath) + contents, err := os.ReadFile(statPath) if err != nil { return err } @@ -1026,7 +1025,7 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui statPath = common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "task", strconv.Itoa(int(tid)), "stat") } - contents, err := ioutil.ReadFile(statPath) + contents, err := os.ReadFile(statPath) if err != nil { return 0, 0, nil, 0, 0, 0, nil, err } diff --git a/process/process_linux_test.go b/process/process_linux_test.go index e8c2e83..c200114 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -72,7 +72,7 @@ func Test_Process_splitProcStat_fromFile(t *testing.T) { if _, err := os.Stat(statFile); err != nil { continue } - contents, err := ioutil.ReadFile(statFile) + contents, err := os.ReadFile(statFile) assert.NoError(t, err) pidStr := strconv.Itoa(int(pid)) diff --git a/process/process_solaris.go b/process/process_solaris.go index ad1c3cf..dd4bd47 100644 --- a/process/process_solaris.go +++ b/process/process_solaris.go @@ -3,7 +3,6 @@ package process import ( "bytes" "context" - "io/ioutil" "os" "strconv" "strings" @@ -232,7 +231,7 @@ func (p *Process) fillFromPathAOutWithContext(ctx context.Context) (string, erro func (p *Process) fillFromExecnameWithContext(ctx context.Context) (string, error) { pid := p.Pid execNamePath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "execname") - exe, err := ioutil.ReadFile(execNamePath) + exe, err := os.ReadFile(execNamePath) if err != nil { return "", err } @@ -242,7 +241,7 @@ func (p *Process) fillFromExecnameWithContext(ctx context.Context) (string, erro func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error) { pid := p.Pid cmdPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) + cmdline, err := os.ReadFile(cmdPath) if err != nil { return "", err } @@ -259,7 +258,7 @@ func (p *Process) fillFromCmdlineWithContext(ctx context.Context) (string, error func (p *Process) fillSliceFromCmdlineWithContext(ctx context.Context) ([]string, error) { pid := p.Pid cmdPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "cmdline") - cmdline, err := ioutil.ReadFile(cmdPath) + cmdline, err := os.ReadFile(cmdPath) if err != nil { return nil, err } diff --git a/process/process_test.go b/process/process_test.go index fc44963..fcb11fa 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -4,6 +4,7 @@ import ( "bufio" "errors" "fmt" + "io" "io/ioutil" "net" "os" @@ -571,7 +572,7 @@ func Test_Connections(t *testing.T) { defer conn.Close() serverEstablished <- struct{}{} - _, err = ioutil.ReadAll(conn) + _, err = io.ReadAll(conn) if err != nil { panic(err) } From c806740b348abc3b0a5abb0aa181cf1982b7acc4 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Tue, 12 Sep 2023 14:14:04 +0200 Subject: [PATCH 16/17] ci(lint): ensure io/ioutil replacement (#1525) * ci(lint): enure ioutil replacement Signed-off-by: Matthieu MOREL * Update host_solaris.go * Update process_linux_test.go * Update net_linux_test.go * Update net_linux_test.go * Update process_test.go * Update process_linux_test.go * Update process_linux_test.go --------- Signed-off-by: Matthieu MOREL --- .github/workflows/lint.yml | 1 + .golangci.yml | 47 +++++++++++++++++++++++++------------------ host/host_solaris.go | 3 +-- net/net_linux_test.go | 5 ++--- process/process_linux_test.go | 9 ++++----- process/process_test.go | 9 ++++----- 6 files changed, 39 insertions(+), 35 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 91feb39..245f7e9 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -19,6 +19,7 @@ jobs: uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 with: go-version: 1.17 + cache: false - name: Checkout repository uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 - name: Setup golangci-lint diff --git a/.golangci.yml b/.golangci.yml index 4d163db..170626b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,21 +1,21 @@ issues: max-same-issues: 0 - exclude-rules: - - linters: - - gosec - text: "G204" - - linters: - - revive - text: "var-naming" - - linters: - - revive - text: "exported" - - linters: - - revive - text: "empty-block" - - linters: - - revive - text: "unused-parameter" + exclude-rules: + - linters: + - gosec + text: "G204" + - linters: + - revive + text: "var-naming" + - linters: + - revive + text: "exported" + - linters: + - revive + text: "empty-block" + - linters: + - revive + text: "unused-parameter" linters: enable: - asciicheck @@ -26,6 +26,7 @@ linters: - gofmt - gofumpt - goimports + - gomodguard - gosec - gosimple - importas @@ -46,10 +47,16 @@ linters: - structcheck - unused - varcheck - linters-settings: gci: sections: - - standard - - default - - prefix(github.com/shirou) + - standard + - default + - prefix(github.com/shirou) + gomodguard: + blocked: + modules: + - io/ioutil: + recommandations: + - io + - os diff --git a/host/host_solaris.go b/host/host_solaris.go index e0661b0..fef67f8 100644 --- a/host/host_solaris.go +++ b/host/host_solaris.go @@ -7,7 +7,6 @@ import ( "encoding/csv" "fmt" "io" - "io/ioutil" "os" "regexp" "strconv" @@ -60,7 +59,7 @@ func HostIDWithContext(ctx context.Context) (string, error) { // Count number of processes based on the number of entries in /proc func numProcs(ctx context.Context) (uint64, error) { - dirs, err := ioutil.ReadDir("/proc") + dirs, err := os.ReadDir("/proc") if err != nil { return 0, err } diff --git a/net/net_linux_test.go b/net/net_linux_test.go index f1b7fba..eae0e71 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -3,7 +3,6 @@ package net import ( "context" "fmt" - "io/ioutil" "net" "os" "strings" @@ -17,7 +16,7 @@ import ( func TestIOCountersByFileParsing(t *testing.T) { // Prpare a temporary file, which will be read during the test - tmpfile, err := ioutil.TempFile("", "proc_dev_net") + tmpfile, err := os.CreateTemp("", "proc_dev_net") defer os.Remove(tmpfile.Name()) // clean up assert.Nil(t, err, "Temporary file creation failed: ", err) @@ -195,7 +194,7 @@ func TestReverse(t *testing.T) { } func TestConntrackStatFileParsing(t *testing.T) { - tmpfile, err := ioutil.TempFile("", "proc_net_stat_conntrack") + tmpfile, err := os.CreateTemp("", "proc_net_stat_conntrack") defer os.Remove(tmpfile.Name()) assert.Nil(t, err, "Temporary file creation failed: ", err) diff --git a/process/process_linux_test.go b/process/process_linux_test.go index c200114..87df812 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -6,7 +6,6 @@ package process import ( "context" "fmt" - "io/ioutil" "os" "strconv" "strings" @@ -58,7 +57,7 @@ func Test_Process_splitProcStat(t *testing.T) { } func Test_Process_splitProcStat_fromFile(t *testing.T) { - pids, err := ioutil.ReadDir("testdata/linux/") + pids, err := os.ReadDir("testdata/linux/") if err != nil { t.Error(err) } @@ -94,7 +93,7 @@ func Test_Process_splitProcStat_fromFile(t *testing.T) { } func Test_fillFromCommWithContext(t *testing.T) { - pids, err := ioutil.ReadDir("testdata/linux/") + pids, err := os.ReadDir("testdata/linux/") if err != nil { t.Error(err) } @@ -115,7 +114,7 @@ func Test_fillFromCommWithContext(t *testing.T) { } func Test_fillFromStatusWithContext(t *testing.T) { - pids, err := ioutil.ReadDir("testdata/linux/") + pids, err := os.ReadDir("testdata/linux/") if err != nil { t.Error(err) } @@ -154,7 +153,7 @@ func Benchmark_fillFromStatusWithContext(b *testing.B) { } func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) { - pids, err := ioutil.ReadDir("testdata/lx_brandz/") + pids, err := os.ReadDir("testdata/lx_brandz/") if err != nil { t.Error(err) } diff --git a/process/process_test.go b/process/process_test.go index fcb11fa..51f7d9c 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "os" "os/exec" @@ -303,7 +302,7 @@ func Test_Process_Name(t *testing.T) { } func Test_Process_Long_Name_With_Spaces(t *testing.T) { - tmpdir, err := ioutil.TempDir("", "") + tmpdir, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("unable to create temp dir %v", err) } @@ -349,7 +348,7 @@ func Test_Process_Long_Name_With_Spaces(t *testing.T) { } func Test_Process_Long_Name(t *testing.T) { - tmpdir, err := ioutil.TempDir("", "") + tmpdir, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("unable to create temp dir %v", err) } @@ -406,7 +405,7 @@ func Test_Process_Name_Against_Python(t *testing.T) { t.Skipf("psutil not found for %s: %s", py3Path, out) } - tmpdir, err := ioutil.TempDir("", "") + tmpdir, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("unable to create temp dir %v", err) } @@ -775,7 +774,7 @@ func Test_IsRunning(t *testing.T) { } func Test_Process_Environ(t *testing.T) { - tmpdir, err := ioutil.TempDir("", "") + tmpdir, err := os.MkdirTemp("", "") if err != nil { t.Fatalf("unable to create temp dir %v", err) } From 03d43fb5a25e9c62cc3662c11e05c2bb7c00d651 Mon Sep 17 00:00:00 2001 From: TossPig Date: Thu, 14 Sep 2023 05:05:59 +0800 Subject: [PATCH 17/17] [load][windows] Fix DATA RACE in load. Avg() fixed #1526 --- load/load_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/load/load_windows.go b/load/load_windows.go index b484838..8f53efa 100644 --- a/load/load_windows.go +++ b/load/load_windows.go @@ -45,8 +45,8 @@ func loadAvgGoroutine(ctx context.Context) { f := func() { currentLoad, err = counter.GetValue() - loadErr = err loadAvgMutex.Lock() + loadErr = err loadAvg1M = loadAvg1M*loadAvgFactor1M + currentLoad*(1-loadAvgFactor1M) loadAvg5M = loadAvg5M*loadAvgFactor5M + currentLoad*(1-loadAvgFactor5M) loadAvg15M = loadAvg15M*loadAvgFactor15M + currentLoad*(1-loadAvgFactor15M)