From 1b5757b7d1c6be6459523db0f63344f4c9690b6b Mon Sep 17 00:00:00 2001 From: YangKeao Date: Tue, 21 Dec 2021 16:14:07 +0800 Subject: [PATCH 01/18] fix unstable boot time because of float conversion Signed-off-by: YangKeao --- internal/common/common_linux.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index 7349989..d2d3d64 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux package common @@ -100,8 +101,9 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) { if err != nil { return 0, err } - t := uint64(time.Now().Unix()) - uint64(b) - return t, nil + currentTime := float64(time.Now().UnixNano()) / float64(time.Second) + t := currentTime - b + return uint64(t), nil } return 0, fmt.Errorf("could not find btime") From d826e14e2792247ad0b4c34c56f0c07d0870fdaa Mon Sep 17 00:00:00 2001 From: Lomanic <5020919+Lomanic@users.noreply.github.com> Date: Sat, 1 Jan 2022 17:10:26 +0100 Subject: [PATCH 02/18] [net][linux] Fix #1198 "f.ReadDir undefined" on Go 1.15 by redefining a custom readDir according to go version Using os.File.Readdir pre Go 1.16 and os.File.ReadDir post Go 1.16 --- net/net_linux.go | 2 +- net/net_linux_111.go | 12 ++++++++++++ net/net_linux_116.go | 12 ++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 net/net_linux_111.go create mode 100644 net/net_linux_116.go diff --git a/net/net_linux.go b/net/net_linux.go index d856b9e..d323781 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -549,7 +549,7 @@ func getProcInodes(root string, pid int32, max int) (map[string][]inodeMap, erro return ret, err } defer f.Close() - dirEntries, err := f.ReadDir(max) + dirEntries, err := readDir(f, max) if err != nil { return ret, err } diff --git a/net/net_linux_111.go b/net/net_linux_111.go new file mode 100644 index 0000000..bd5c958 --- /dev/null +++ b/net/net_linux_111.go @@ -0,0 +1,12 @@ +//go:build !go1.16 +// +build !go1.16 + +package net + +import ( + "os" +) + +func readDir(f *os.File, max int) ([]os.FileInfo, error) { + return f.Readdir(max) +} diff --git a/net/net_linux_116.go b/net/net_linux_116.go new file mode 100644 index 0000000..a45072e --- /dev/null +++ b/net/net_linux_116.go @@ -0,0 +1,12 @@ +//go:build go1.16 +// +build go1.16 + +package net + +import ( + "os" +) + +func readDir(f *os.File, max int) ([]os.DirEntry, error) { + return f.ReadDir(max) +} From df68a56e2da3ac07305c29067862b4ad004a22c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 5 Jan 2022 00:38:50 +0200 Subject: [PATCH 03/18] [disk][freebsd] implement SerialNumberWithContext --- disk/disk_freebsd.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/disk/disk_freebsd.go b/disk/disk_freebsd.go index e74c186..dd3eba1 100644 --- a/disk/disk_freebsd.go +++ b/disk/disk_freebsd.go @@ -4,10 +4,14 @@ package disk import ( + "bufio" "bytes" "context" "encoding/binary" + "fmt" + "os/exec" "strconv" + "strings" "golang.org/x/sys/unix" @@ -163,7 +167,22 @@ func getFsType(stat unix.Statfs_t) string { } func SerialNumberWithContext(ctx context.Context, name string) (string, error) { - return "", common.ErrNotImplementedError + geom, err := exec.LookPath("geom") + if err != nil { + return "", fmt.Errorf("find geom: %w", err) + } + geomOut, err := invoke.CommandWithContext(ctx, geom, "disk", "list", name) + if err != nil { + return "", fmt.Errorf("exec geom: %w", err) + } + s := bufio.NewScanner(bytes.NewReader(geomOut)) + for s.Scan() { + flds := strings.Fields(s.Text()) + if len(flds) == 2 && flds[0] == "ident:" { + return flds[1], nil + } + } + return "", nil } func LabelWithContext(ctx context.Context, name string) (string, error) { From 41e2595443d376da01233445687bd890a3a2bcb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 5 Jan 2022 01:20:20 +0200 Subject: [PATCH 04/18] [process][freebsd] implement createTimeWithContext --- process/process_freebsd.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/process/process_freebsd.go b/process/process_freebsd.go index a010383..383ec0f 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -111,7 +111,11 @@ func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) } func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - return 0, common.ErrNotImplementedError + k, err := p.getKProc() + if err != nil { + return 0, err + } + return k.Start.Sec*1000 + k.Start.Usec/1000, nil } func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { From 01cf95a92d692ca5b14ce3a1c0335d9ea70f9c30 Mon Sep 17 00:00:00 2001 From: Lomanic <5020919+Lomanic@users.noreply.github.com> Date: Wed, 5 Jan 2022 01:02:27 +0100 Subject: [PATCH 05/18] [process][windows] Fix #1216 fix compilation on arm/arm64 go build ./... with GOARCH=arm64 is otherwise failing with the following: process\process_windows.go:412:11: undefined: readProcessMemory process\process_windows.go:425:11: undefined: readProcessMemory process\process_windows.go:896:32: undefined: PROCESS_MEMORY_COUNTERS process\process_windows.go:897:10: undefined: PROCESS_MEMORY_COUNTERS process\process_windows.go:910:50: undefined: PROCESS_MEMORY_COUNTERS process\process_windows.go:950:21: undefined: queryPebAddress process\process_windows.go:955:9: undefined: readProcessMemory process\process_windows.go:961:8: undefined: readProcessMemory process\process_windows.go:969:21: undefined: queryPebAddress process\process_windows.go:974:9: undefined: readProcessMemory process\process_windows.go:974:9: too many errors --- process/process_windows_32bit.go | 109 +++++++++++++++++++++++++++++++++++++++ process/process_windows_386.go | 109 --------------------------------------- process/process_windows_64bit.go | 79 ++++++++++++++++++++++++++++ process/process_windows_amd64.go | 79 ---------------------------- 4 files changed, 188 insertions(+), 188 deletions(-) create mode 100644 process/process_windows_32bit.go delete mode 100644 process/process_windows_386.go create mode 100644 process/process_windows_64bit.go delete mode 100644 process/process_windows_amd64.go diff --git a/process/process_windows_32bit.go b/process/process_windows_32bit.go new file mode 100644 index 0000000..982287d --- /dev/null +++ b/process/process_windows_32bit.go @@ -0,0 +1,109 @@ +//go:build (windows && 386) || (windows && arm) +// +build windows,386 windows,arm + +package process + +import ( + "errors" + "syscall" + "unsafe" + + "golang.org/x/sys/windows" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +type PROCESS_MEMORY_COUNTERS struct { + CB uint32 + PageFaultCount uint32 + PeakWorkingSetSize uint32 + WorkingSetSize uint32 + QuotaPeakPagedPoolUsage uint32 + QuotaPagedPoolUsage uint32 + QuotaPeakNonPagedPoolUsage uint32 + QuotaNonPagedPoolUsage uint32 + PagefileUsage uint32 + PeakPagefileUsage uint32 +} + +func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { + if is32BitProcess { + // we are on a 32-bit process reading an external 32-bit process + var info processBasicInformation32 + + ret, _, _ := common.ProcNtQueryInformationProcess.Call( + uintptr(procHandle), + uintptr(common.ProcessBasicInformation), + uintptr(unsafe.Pointer(&info)), + uintptr(unsafe.Sizeof(info)), + uintptr(0), + ) + if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { + return uint64(info.PebBaseAddress), nil + } else { + return 0, windows.NTStatus(ret) + } + } else { + // we are on a 32-bit process reading an external 64-bit process + if common.ProcNtWow64QueryInformationProcess64.Find() == nil { // avoid panic + var info processBasicInformation64 + + ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( + uintptr(procHandle), + uintptr(common.ProcessBasicInformation), + uintptr(unsafe.Pointer(&info)), + uintptr(unsafe.Sizeof(info)), + uintptr(0), + ) + if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { + return info.PebBaseAddress, nil + } else { + return 0, windows.NTStatus(ret) + } + } else { + return 0, errors.New("can't find API to query 64 bit process from 32 bit") + } + } +} + +func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte { + if is32BitProcess { + var read uint + + buffer := make([]byte, size) + + ret, _, _ := common.ProcNtReadVirtualMemory.Call( + uintptr(h), + uintptr(address), + uintptr(unsafe.Pointer(&buffer[0])), + uintptr(size), + uintptr(unsafe.Pointer(&read)), + ) + if int(ret) >= 0 && read > 0 { + return buffer[:read] + } + } else { + // reading a 64-bit process from a 32-bit one + if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { // avoid panic + var read uint64 + + buffer := make([]byte, size) + + ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call( + uintptr(h), + uintptr(address&0xFFFFFFFF), // the call expects a 64-bit value + uintptr(address>>32), + uintptr(unsafe.Pointer(&buffer[0])), + uintptr(size), // the call expects a 64-bit value + uintptr(0), // but size is 32-bit so pass zero as the high dword + uintptr(unsafe.Pointer(&read)), + ) + if int(ret) >= 0 && read > 0 { + return buffer[:uint(read)] + } + } + } + + // if we reach here, an error happened + return nil +} diff --git a/process/process_windows_386.go b/process/process_windows_386.go deleted file mode 100644 index 982287d..0000000 --- a/process/process_windows_386.go +++ /dev/null @@ -1,109 +0,0 @@ -//go:build (windows && 386) || (windows && arm) -// +build windows,386 windows,arm - -package process - -import ( - "errors" - "syscall" - "unsafe" - - "golang.org/x/sys/windows" - - "github.com/shirou/gopsutil/v3/internal/common" -) - -type PROCESS_MEMORY_COUNTERS struct { - CB uint32 - PageFaultCount uint32 - PeakWorkingSetSize uint32 - WorkingSetSize uint32 - QuotaPeakPagedPoolUsage uint32 - QuotaPagedPoolUsage uint32 - QuotaPeakNonPagedPoolUsage uint32 - QuotaNonPagedPoolUsage uint32 - PagefileUsage uint32 - PeakPagefileUsage uint32 -} - -func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { - if is32BitProcess { - // we are on a 32-bit process reading an external 32-bit process - var info processBasicInformation32 - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return uint64(info.PebBaseAddress), nil - } else { - return 0, windows.NTStatus(ret) - } - } else { - // we are on a 32-bit process reading an external 64-bit process - if common.ProcNtWow64QueryInformationProcess64.Find() == nil { // avoid panic - var info processBasicInformation64 - - ret, _, _ := common.ProcNtWow64QueryInformationProcess64.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return info.PebBaseAddress, nil - } else { - return 0, windows.NTStatus(ret) - } - } else { - return 0, errors.New("can't find API to query 64 bit process from 32 bit") - } - } -} - -func readProcessMemory(h syscall.Handle, is32BitProcess bool, address uint64, size uint) []byte { - if is32BitProcess { - var read uint - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtReadVirtualMemory.Call( - uintptr(h), - uintptr(address), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:read] - } - } else { - // reading a 64-bit process from a 32-bit one - if common.ProcNtWow64ReadVirtualMemory64.Find() == nil { // avoid panic - var read uint64 - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtWow64ReadVirtualMemory64.Call( - uintptr(h), - uintptr(address&0xFFFFFFFF), // the call expects a 64-bit value - uintptr(address>>32), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), // the call expects a 64-bit value - uintptr(0), // but size is 32-bit so pass zero as the high dword - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:uint(read)] - } - } - } - - // if we reach here, an error happened - return nil -} diff --git a/process/process_windows_64bit.go b/process/process_windows_64bit.go new file mode 100644 index 0000000..74c6212 --- /dev/null +++ b/process/process_windows_64bit.go @@ -0,0 +1,79 @@ +//go:build (windows && amd64) || (windows && arm64) +// +build windows,amd64 windows,arm64 + +package process + +import ( + "syscall" + "unsafe" + + "github.com/shirou/gopsutil/v3/internal/common" + "golang.org/x/sys/windows" +) + +type PROCESS_MEMORY_COUNTERS struct { + CB uint32 + PageFaultCount uint32 + PeakWorkingSetSize uint64 + WorkingSetSize uint64 + QuotaPeakPagedPoolUsage uint64 + QuotaPagedPoolUsage uint64 + QuotaPeakNonPagedPoolUsage uint64 + QuotaNonPagedPoolUsage uint64 + PagefileUsage uint64 + PeakPagefileUsage uint64 +} + +func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { + if is32BitProcess { + // we are on a 64-bit process reading an external 32-bit process + var wow64 uint + + ret, _, _ := common.ProcNtQueryInformationProcess.Call( + uintptr(procHandle), + uintptr(common.ProcessWow64Information), + uintptr(unsafe.Pointer(&wow64)), + uintptr(unsafe.Sizeof(wow64)), + uintptr(0), + ) + if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { + return uint64(wow64), nil + } else { + return 0, windows.NTStatus(ret) + } + } else { + // we are on a 64-bit process reading an external 64-bit process + var info processBasicInformation64 + + ret, _, _ := common.ProcNtQueryInformationProcess.Call( + uintptr(procHandle), + uintptr(common.ProcessBasicInformation), + uintptr(unsafe.Pointer(&info)), + uintptr(unsafe.Sizeof(info)), + uintptr(0), + ) + if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { + return info.PebBaseAddress, nil + } else { + return 0, windows.NTStatus(ret) + } + } +} + +func readProcessMemory(procHandle syscall.Handle, _ bool, address uint64, size uint) []byte { + var read uint + + buffer := make([]byte, size) + + ret, _, _ := common.ProcNtReadVirtualMemory.Call( + uintptr(procHandle), + uintptr(address), + uintptr(unsafe.Pointer(&buffer[0])), + uintptr(size), + uintptr(unsafe.Pointer(&read)), + ) + if int(ret) >= 0 && read > 0 { + return buffer[:read] + } + return nil +} diff --git a/process/process_windows_amd64.go b/process/process_windows_amd64.go deleted file mode 100644 index 74c6212..0000000 --- a/process/process_windows_amd64.go +++ /dev/null @@ -1,79 +0,0 @@ -//go:build (windows && amd64) || (windows && arm64) -// +build windows,amd64 windows,arm64 - -package process - -import ( - "syscall" - "unsafe" - - "github.com/shirou/gopsutil/v3/internal/common" - "golang.org/x/sys/windows" -) - -type PROCESS_MEMORY_COUNTERS struct { - CB uint32 - PageFaultCount uint32 - PeakWorkingSetSize uint64 - WorkingSetSize uint64 - QuotaPeakPagedPoolUsage uint64 - QuotaPagedPoolUsage uint64 - QuotaPeakNonPagedPoolUsage uint64 - QuotaNonPagedPoolUsage uint64 - PagefileUsage uint64 - PeakPagefileUsage uint64 -} - -func queryPebAddress(procHandle syscall.Handle, is32BitProcess bool) (uint64, error) { - if is32BitProcess { - // we are on a 64-bit process reading an external 32-bit process - var wow64 uint - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(procHandle), - uintptr(common.ProcessWow64Information), - uintptr(unsafe.Pointer(&wow64)), - uintptr(unsafe.Sizeof(wow64)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return uint64(wow64), nil - } else { - return 0, windows.NTStatus(ret) - } - } else { - // we are on a 64-bit process reading an external 64-bit process - var info processBasicInformation64 - - ret, _, _ := common.ProcNtQueryInformationProcess.Call( - uintptr(procHandle), - uintptr(common.ProcessBasicInformation), - uintptr(unsafe.Pointer(&info)), - uintptr(unsafe.Sizeof(info)), - uintptr(0), - ) - if status := windows.NTStatus(ret); status == windows.STATUS_SUCCESS { - return info.PebBaseAddress, nil - } else { - return 0, windows.NTStatus(ret) - } - } -} - -func readProcessMemory(procHandle syscall.Handle, _ bool, address uint64, size uint) []byte { - var read uint - - buffer := make([]byte, size) - - ret, _, _ := common.ProcNtReadVirtualMemory.Call( - uintptr(procHandle), - uintptr(address), - uintptr(unsafe.Pointer(&buffer[0])), - uintptr(size), - uintptr(unsafe.Pointer(&read)), - ) - if int(ret) >= 0 && read > 0 { - return buffer[:read] - } - return nil -} From dcac9d9b010d2e0979a079b00b691240ab7c655d Mon Sep 17 00:00:00 2001 From: shirou Date: Thu, 6 Jan 2022 22:16:57 +0900 Subject: [PATCH 06/18] [process][windows] fix release handle --- process/process_windows.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/process/process_windows.go b/process/process_windows.go index 282b0d9..d348946 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -703,6 +703,9 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er 0, true, windows.DUPLICATE_SAME_ACCESS) != nil { continue } + // release the new handle + defer windows.CloseHandle(windows.Handle(file)) + fileType, _ := windows.GetFileType(windows.Handle(file)) if fileType != windows.FILE_TYPE_DISK { continue From 0d33df272b00d0ed46d947d3f24426ae6e88e4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 6 Jan 2022 23:16:03 +0200 Subject: [PATCH 07/18] [disk][freebsd] return empty serial on `(null)` geom disk ident --- disk/disk_freebsd.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/disk/disk_freebsd.go b/disk/disk_freebsd.go index dd3eba1..9f95c62 100644 --- a/disk/disk_freebsd.go +++ b/disk/disk_freebsd.go @@ -176,13 +176,17 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) { return "", fmt.Errorf("exec geom: %w", err) } s := bufio.NewScanner(bytes.NewReader(geomOut)) + serial := "" for s.Scan() { flds := strings.Fields(s.Text()) if len(flds) == 2 && flds[0] == "ident:" { - return flds[1], nil + if flds[1] != "(null)" { + serial = flds[1] + } + break } } - return "", nil + return serial, nil } func LabelWithContext(ctx context.Context, name string) (string, error) { From 59e366d674f89bf63b73e598f1cc5d28399b1f42 Mon Sep 17 00:00:00 2001 From: shirou Date: Fri, 7 Jan 2022 08:12:07 +0000 Subject: [PATCH 08/18] remove codecov --- .github/workflows/test.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 82970db..7ecac03 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,10 +29,3 @@ jobs: - name: Test run: | 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 }} From d92d114f901e028be252aa5cd8c724924a212be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Mon, 10 Jan 2022 23:38:29 +0200 Subject: [PATCH 09/18] [load][solaris] implement AvgWithContext --- README.md | 2 +- load/load_solaris.go | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f66413b..ad8be35 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ will provide useful information. - CacheSize - Flags (ex: "fpu vme de pse tsc msr pae mce cx8 ...") - Microcode -- load/LoadAvg() (linux, freebsd) +- load/Avg() (linux, freebsd, solaris) - Load1 - Load5 - Load15 diff --git a/load/load_solaris.go b/load/load_solaris.go index b9dd273..aa6cd1e 100644 --- a/load/load_solaris.go +++ b/load/load_solaris.go @@ -4,11 +4,12 @@ package load import ( + "bufio" + "bytes" "context" "os/exec" + "strconv" "strings" - - "github.com/shirou/gopsutil/v3/internal/common" ) func Avg() (*AvgStat, error) { @@ -16,7 +17,45 @@ func Avg() (*AvgStat, error) { } func AvgWithContext(ctx context.Context) (*AvgStat, error) { - return nil, common.ErrNotImplementedError + kstat, err := exec.LookPath("kstat") + if err != nil { + return nil, err + } + + out, err := invoke.CommandWithContext(ctx, kstat, "-p", "unix:0:system_misc:avenrun_*") + if err != nil { + return nil, err + } + + avg := &AvgStat{} + scanner := bufio.NewScanner(bytes.NewReader(out)) + for scanner.Scan() { + flds := strings.Fields(scanner.Text()) + if len(flds) < 2 { + continue + } + var tgt *float64 + switch { + case strings.HasSuffix(flds[0], ":avenrun_1min"): + tgt = &avg.Load1 + case strings.HasSuffix(flds[0], ":avenrun_5min"): + tgt = &avg.Load5 + case strings.HasSuffix(flds[0], ":avenrun_15min"): + tgt = &avg.Load15 + default: + continue + } + v, err := strconv.ParseInt(flds[1], 10, 64) + if err != nil { + return nil, err + } + *tgt = float64(v) / (1 << 8) + } + if err = scanner.Err(); err != nil { + return nil, err + } + + return avg, nil } func Misc() (*MiscStat, error) { From 31c410026df0cdce67c37eaabe91538c55e5bcd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Mon, 10 Jan 2022 23:53:35 +0200 Subject: [PATCH 10/18] [disk][freebsd] handle SerialNumberWithContext scanner errors --- disk/disk_freebsd.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/disk/disk_freebsd.go b/disk/disk_freebsd.go index 9f95c62..8f4ede5 100644 --- a/disk/disk_freebsd.go +++ b/disk/disk_freebsd.go @@ -186,6 +186,9 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) { break } } + if err = s.Err(); err != nil { + return "", err + } return serial, nil } From b9b3dbe67a5cd51eb1d60708851f9ae04e289c7b Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Thu, 13 Jan 2022 10:49:17 +0100 Subject: [PATCH 11/18] Avoid ps command and use KProc on MacOS --- go.mod | 2 +- go.sum | 4 +-- process/process_darwin.go | 80 ++++++++++------------------------------------- 3 files changed, 19 insertions(+), 67 deletions(-) diff --git a/go.mod b/go.mod index f19cec5..1bb369e 100644 --- a/go.mod +++ b/go.mod @@ -9,5 +9,5 @@ require ( github.com/stretchr/testify v1.7.0 github.com/tklauser/go-sysconf v0.3.9 github.com/yusufpapurcu/wmi v1.2.2 - golang.org/x/sys v0.0.0-20211013075003-97ac67df715c + golang.org/x/sys v0.0.0-20220111092808-5a964db01320 ) diff --git a/go.sum b/go.sum index 50b5a10..ac1a981 100644 --- a/go.sum +++ b/go.sum @@ -26,8 +26,8 @@ github.com/yusufpapurcu/wmi v1.2.2/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.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211013075003-97ac67df715c h1:taxlMj0D/1sOAuv/CbSD+MMDof2vbyPTqz5FNYKpXt8= -golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY= +golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 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= diff --git a/process/process_darwin.go b/process/process_darwin.go index c1dc918..3f02058 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -10,7 +10,6 @@ import ( "path/filepath" "strconv" "strings" - "time" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/internal/common" @@ -46,34 +45,25 @@ type _Ctype_struct___0 struct { func pidsWithContext(ctx context.Context) ([]int32, error) { var ret []int32 - pids, err := callPsWithContext(ctx, "pid", 0, false, false) + kprocs, err := unix.SysctlKinfoProcSlice("kern.proc.all") if err != nil { return ret, err } - for _, pid := range pids { - v, err := strconv.Atoi(pid[0]) - if err != nil { - return ret, err - } - ret = append(ret, int32(v)) + for _, proc := range kprocs { + ret = append(ret, int32(proc.Proc.P_pid)) } return ret, nil } func (p *Process) PpidWithContext(ctx context.Context) (int32, error) { - r, err := callPsWithContext(ctx, "ppid", p.Pid, false, false) - if err != nil { - return 0, err - } - - v, err := strconv.Atoi(r[0][0]) + k, err := p.getKProc() if err != nil { return 0, err } - return int32(v), err + return k.Eproc.Ppid, nil } func (p *Process) NameWithContext(ctx context.Context) (string, error) { @@ -81,7 +71,8 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) { if err != nil { return "", err } - name := common.IntToString(k.Proc.P_comm[:]) + + name := common.ByteToString(k.Proc.P_comm[:]) if len(name) >= 15 { cmdName, err := p.cmdNameWithContext(ctx) @@ -111,51 +102,21 @@ func (p *Process) cmdNameWithContext(ctx context.Context) ([]string, error) { } func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { - r, err := callPsWithContext(ctx, "etime", p.Pid, false, false) + k, err := p.getKProc() if err != nil { return 0, err } - elapsedSegments := strings.Split(strings.Replace(r[0][0], "-", ":", 1), ":") - var elapsedDurations []time.Duration - for i := len(elapsedSegments) - 1; i >= 0; i-- { - p, err := strconv.ParseInt(elapsedSegments[i], 10, 0) - if err != nil { - return 0, err - } - elapsedDurations = append(elapsedDurations, time.Duration(p)) - } - - elapsed := time.Duration(elapsedDurations[0]) * time.Second - if len(elapsedDurations) > 1 { - elapsed += time.Duration(elapsedDurations[1]) * time.Minute - } - if len(elapsedDurations) > 2 { - elapsed += time.Duration(elapsedDurations[2]) * time.Hour - } - if len(elapsedDurations) > 3 { - elapsed += time.Duration(elapsedDurations[3]) * time.Hour * 24 - } - - start := time.Now().Add(-elapsed) - return start.Unix() * 1000, nil + return k.Proc.P_starttime.Sec*1000 + int64(k.Proc.P_starttime.Usec)/1000, nil } func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") + ppid, err := p.PpidWithContext(ctx) if err != nil { return nil, err } - for _, line := range out { - if len(line) >= 1 && line[0] == 'R' { - v, err := strconv.Atoi(line[1:]) - if err != nil { - return nil, err - } - return NewProcessWithContext(ctx, int32(v)) - } - } - return nil, fmt.Errorf("could not find parent line") + + return NewProcessWithContext(ctx, ppid) } func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { @@ -188,7 +149,7 @@ func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) { } // See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html - userEffectiveUID := int32(k.Eproc.Ucred.UID) + userEffectiveUID := int32(k.Eproc.Ucred.Uid) return []int32{userEffectiveUID}, nil } @@ -200,7 +161,7 @@ func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) { } gids := make([]int32, 0, 3) - gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Ucred.Ngroups), int32(k.Eproc.Pcred.P_svgid)) + gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Pcred.P_svgid)) return gids, nil } @@ -399,17 +360,8 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) { // Returns a proc as defined here: // http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html -func (p *Process) getKProc() (*KinfoProc, error) { - buf, err := unix.SysctlRaw("kern.proc.pid", int(p.Pid)) - if err != nil { - return nil, err - } - k, err := parseKinfoProc(buf) - if err != nil { - return nil, err - } - - return &k, nil +func (p *Process) getKProc() (*unix.KinfoProc, error) { + return unix.SysctlKinfoProc("kern.proc.pid", int(p.Pid)) } // call ps command. From 60eae48e6aa75012c62f2926a43d64bf40d88a8d Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Thu, 13 Jan 2022 13:27:05 +0100 Subject: [PATCH 12/18] Drop test for Darwin Pids() --- process/process_test.go | 20 -------------------- process/testdata/darwin/ps-ax-opid_fail | 10 ---------- 2 files changed, 30 deletions(-) delete mode 100644 process/testdata/darwin/ps-ax-opid_fail diff --git a/process/process_test.go b/process/process_test.go index 2bb72b8..786eb51 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -46,26 +46,6 @@ func Test_Pids(t *testing.T) { } } -func Test_Pids_Fail(t *testing.T) { - if runtime.GOOS != "darwin" { - t.Skip("darwin only") - } - - mu.Lock() - defer mu.Unlock() - - invoke = common.FakeInvoke{Suffix: "fail"} - ret, err := Pids() - skipIfNotImplementedErr(t, err) - invoke = common.Invoke{} - if err != nil { - t.Errorf("error %v", err) - } - if len(ret) != 9 { - t.Errorf("wrong getted pid nums: %v/%d", ret, len(ret)) - } -} - func Test_Pid_exists(t *testing.T) { checkPid := os.Getpid() diff --git a/process/testdata/darwin/ps-ax-opid_fail b/process/testdata/darwin/ps-ax-opid_fail deleted file mode 100644 index fce59ef..0000000 --- a/process/testdata/darwin/ps-ax-opid_fail +++ /dev/null @@ -1,10 +0,0 @@ - PID - 245 - 247 - 248 - 249 - 254 - 262 - 264 - 265 - 267 From 43e50e1d7d027c76e89ade0efb93e8f7e8a0ca84 Mon Sep 17 00:00:00 2001 From: Pierre Fersing Date: Thu, 13 Jan 2022 17:55:01 +0100 Subject: [PATCH 13/18] Reduce call to ps for process package on darwin --- process/process_darwin.go | 72 +------------------------------- process/process_darwin_cgo.go | 91 ++++++++++++++++++++++++++++++++++++++--- process/process_darwin_nocgo.go | 71 ++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+), 76 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 3f02058..9d3bf19 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -11,7 +11,6 @@ import ( "strconv" "strings" - "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/net" "github.com/tklauser/go-sysconf" @@ -80,11 +79,11 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) { return "", err } if len(cmdName) > 0 { - extendedName := filepath.Base(cmdName[0]) + extendedName := filepath.Base(cmdName) if strings.HasPrefix(extendedName, p.name) { name = extendedName } else { - name = cmdName[0] + name = cmdName } } } @@ -92,15 +91,6 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) { return name, nil } -// cmdNameWithContext returns the command name (including spaces) without any arguments -func (p *Process) cmdNameWithContext(ctx context.Context) ([]string, error) { - r, err := callPsWithContext(ctx, "command", p.Pid, false, true) - if err != nil { - return nil, err - } - return r[0], err -} - func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { k, err := p.getKProc() if err != nil { @@ -211,14 +201,6 @@ func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, e return nil, common.ErrNotImplementedError } -func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { - r, err := callPsWithContext(ctx, "utime,stime", p.Pid, true, false) - if err != nil { - return 0, err - } - return int32(len(r)), nil -} - func convertCPUTimes(s string) (ret float64, err error) { var t int var _tmp string @@ -265,56 +247,6 @@ func convertCPUTimes(s string) (ret float64, err error) { return float64(t) / float64(clockTicks), nil } -func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { - r, err := callPsWithContext(ctx, "utime,stime", p.Pid, false, false) - if err != nil { - return nil, err - } - - utime, err := convertCPUTimes(r[0][0]) - if err != nil { - return nil, err - } - stime, err := convertCPUTimes(r[0][1]) - if err != nil { - return nil, err - } - - ret := &cpu.TimesStat{ - CPU: "cpu", - User: utime, - System: stime, - } - return ret, nil -} - -func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { - r, err := callPsWithContext(ctx, "rss,vsize,pagein", p.Pid, false, false) - if err != nil { - return nil, err - } - rss, err := strconv.Atoi(r[0][0]) - if err != nil { - return nil, err - } - vms, err := strconv.Atoi(r[0][1]) - if err != nil { - return nil, err - } - pagein, err := strconv.Atoi(r[0][2]) - if err != nil { - return nil, err - } - - ret := &MemoryInfoStat{ - RSS: uint64(rss) * 1024, - VMS: uint64(vms) * 1024, - Swap: uint64(pagein), - } - - return ret, nil -} - func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid) if err != nil { diff --git a/process/process_darwin_cgo.go b/process/process_darwin_cgo.go index 972b74b..2ac413f 100644 --- a/process/process_darwin_cgo.go +++ b/process/process_darwin_cgo.go @@ -9,6 +9,7 @@ package process // #include // #include // #include +// #include import "C" import ( @@ -18,12 +19,18 @@ import ( "strings" "syscall" "unsafe" + + "github.com/shirou/gopsutil/v3/cpu" ) -var argMax int +var ( + argMax int + timescaleToNanoSeconds float64 +) func init() { argMax = getArgMax() + timescaleToNanoSeconds = getTimeScaleToNanoSeconds() } func getArgMax() int { @@ -39,6 +46,14 @@ func getArgMax() int { return 0 } +func getTimeScaleToNanoSeconds() float64 { + var timeBaseInfo C.struct_mach_timebase_info + + C.mach_timebase_info(&timeBaseInfo) + + return float64(timeBaseInfo.numer) / float64(timeBaseInfo.denom) +} + func (p *Process) ExeWithContext(ctx context.Context) (string, error) { var c C.char // need a var for unsafe.Sizeof need a var const bufsize = C.PROC_PIDPATHINFO_MAXSIZE * unsafe.Sizeof(c) @@ -82,7 +97,7 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return C.GoString(&vpi.pvi_cdir.vip_path[0]), err } -func procArgs(pid int32) (*[]byte, int, error) { +func procArgs(pid int32) ([]byte, int, error) { var ( mib = [...]C.int{C.CTL_KERN, C.KERN_PROCARGS2, C.int(pid)} size C.size_t = C.ulong(argMax) @@ -91,23 +106,27 @@ func procArgs(pid int32) (*[]byte, int, error) { ) procargs := (*C.char)(C.malloc(C.ulong(argMax))) defer C.free(unsafe.Pointer(procargs)) - retval := C.sysctl(&mib[0], 3, unsafe.Pointer(procargs), &size, C.NULL, 0) + retval, err := C.sysctl(&mib[0], 3, unsafe.Pointer(procargs), &size, C.NULL, 0) if retval == 0 { C.memcpy(unsafe.Pointer(&nargs), unsafe.Pointer(procargs), C.sizeof_int) result = C.GoBytes(unsafe.Pointer(procargs), C.int(size)) // fmt.Printf("size: %d %d\n%s\n", size, nargs, hex.Dump(result)) - return &result, int(nargs), nil + return result, int(nargs), nil } - return nil, 0, fmt.Errorf("error: %d", retval) + return nil, 0, err } func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) { + return p.cmdlineSliceWithContext(ctx, true) +} + +func (p *Process) cmdlineSliceWithContext(ctx context.Context, fallback bool) ([]string, error) { pargs, nargs, err := procArgs(p.Pid) if err != nil { return nil, err } // The first bytes hold the nargs int, skip it. - args := bytes.Split((*pargs)[C.sizeof_int:], []byte{0}) + args := bytes.Split((pargs)[C.sizeof_int:], []byte{0}) var argStr string // The first element is the actual binary/command path. // command := args[0] @@ -131,6 +150,20 @@ func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) return argSlice, err } +// cmdNameWithContext returns the command name (including spaces) without any arguments +func (p *Process) cmdNameWithContext(ctx context.Context) (string, error) { + r, err := p.cmdlineSliceWithContext(ctx, false) + if err != nil { + return "", err + } + + if len(r) == 0 { + return "", nil + } + + return r[0], err +} + func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { r, err := p.CmdlineSliceWithContext(ctx) if err != nil { @@ -138,3 +171,49 @@ func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { } return strings.Join(r, " "), err } + +func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { + const tiSize = C.sizeof_struct_proc_taskinfo + ti := (*C.struct_proc_taskinfo)(C.malloc(tiSize)) + + _, err := C.proc_pidinfo(C.int(p.Pid), C.PROC_PIDTASKINFO, 0, unsafe.Pointer(ti), tiSize) + if err != nil { + return 0, err + } + + return int32(ti.pti_threadnum), nil +} + +func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { + const tiSize = C.sizeof_struct_proc_taskinfo + ti := (*C.struct_proc_taskinfo)(C.malloc(tiSize)) + + _, err := C.proc_pidinfo(C.int(p.Pid), C.PROC_PIDTASKINFO, 0, unsafe.Pointer(ti), tiSize) + if err != nil { + return nil, err + } + + ret := &cpu.TimesStat{ + CPU: "cpu", + User: float64(ti.pti_total_user) * timescaleToNanoSeconds / 1e9, + System: float64(ti.pti_total_system) * timescaleToNanoSeconds / 1e9, + } + return ret, nil +} + +func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { + const tiSize = C.sizeof_struct_proc_taskinfo + ti := (*C.struct_proc_taskinfo)(C.malloc(tiSize)) + + _, err := C.proc_pidinfo(C.int(p.Pid), C.PROC_PIDTASKINFO, 0, unsafe.Pointer(ti), tiSize) + if err != nil { + return nil, err + } + + ret := &MemoryInfoStat{ + RSS: uint64(ti.pti_resident_size), + VMS: uint64(ti.pti_virtual_size), + Swap: uint64(ti.pti_pageins), + } + return ret, nil +} diff --git a/process/process_darwin_nocgo.go b/process/process_darwin_nocgo.go index 1426c97..3d696b7 100644 --- a/process/process_darwin_nocgo.go +++ b/process/process_darwin_nocgo.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" + "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/internal/common" ) @@ -47,6 +48,18 @@ func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) { return strings.Join(r[0], " "), err } +func (p *Process) cmdNameWithContext(ctx context.Context) (string, error) { + r, err := callPsWithContext(ctx, "command", p.Pid, false, true) + if err != nil { + return "", err + } + if len(r) > 0 && len(r[0]) > 0 { + return r[0][0], err + } + + return "", err +} + // CmdlineSliceWithContext returns the command line arguments of the process as a slice with each // element being an argument. Because of current deficiencies in the way that the command // line arguments are found, single arguments that have spaces in the will actually be @@ -59,3 +72,61 @@ func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) } return r[0], err } + +func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { + r, err := callPsWithContext(ctx, "utime,stime", p.Pid, true, false) + if err != nil { + return 0, err + } + return int32(len(r)), nil +} + +func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) { + r, err := callPsWithContext(ctx, "utime,stime", p.Pid, false, false) + if err != nil { + return nil, err + } + + utime, err := convertCPUTimes(r[0][0]) + if err != nil { + return nil, err + } + stime, err := convertCPUTimes(r[0][1]) + if err != nil { + return nil, err + } + + ret := &cpu.TimesStat{ + CPU: "cpu", + User: utime, + System: stime, + } + return ret, nil +} + +func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) { + r, err := callPsWithContext(ctx, "rss,vsize,pagein", p.Pid, false, false) + if err != nil { + return nil, err + } + rss, err := strconv.Atoi(r[0][0]) + if err != nil { + return nil, err + } + vms, err := strconv.Atoi(r[0][1]) + if err != nil { + return nil, err + } + pagein, err := strconv.Atoi(r[0][2]) + if err != nil { + return nil, err + } + + ret := &MemoryInfoStat{ + RSS: uint64(rss) * 1024, + VMS: uint64(vms) * 1024, + Swap: uint64(pagein), + } + + return ret, nil +} From 35fbe3850592fe247bcd0faef727d783b652df64 Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 15 Jan 2022 14:09:57 +0000 Subject: [PATCH 14/18] [process][linux] Fix error handling on Children. If pgrep returns error, `CallPgrepWithContext` always returns empty pids. So this Children always returns ErrorNoChildren. This PR fixes that handling. --- process/process_linux.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/process/process_linux.go b/process/process_linux.go index af8f232..e425720 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -351,11 +351,11 @@ func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, e func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) { pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid) if err != nil { - if len(pids) == 0 { - return nil, ErrorNoChildren - } return nil, err } + if len(pids) == 0 { + return nil, ErrorNoChildren + } ret := make([]*Process, 0, len(pids)) for _, pid := range pids { np, err := NewProcessWithContext(ctx, pid) From 001699bed7b3e9cf92b88fb1befb89f8caad9912 Mon Sep 17 00:00:00 2001 From: Lammert Hellinga Date: Wed, 19 Jan 2022 22:37:51 +0100 Subject: [PATCH 15/18] Let almalinux be part of the rhel family --- host/host_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host/host_linux.go b/host/host_linux.go index 29ad9ab..def9a67 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -283,7 +283,7 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil family = "debian" case "fedora": family = "fedora" - case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm", "rocky": + case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm", "rocky", "almalinux": family = "rhel" case "suse", "opensuse", "opensuse-leap", "opensuse-tumbleweed", "opensuse-tumbleweed-kubic", "sles", "sled", "caasp": family = "suse" From 5d930b5b8460b8e0bbf1e42a73acb9bb9ee55393 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 20 Jan 2022 01:22:32 +0000 Subject: [PATCH 16/18] Bump github.com/google/go-cmp from 0.5.6 to 0.5.7 Bumps [github.com/google/go-cmp](https://github.com/google/go-cmp) from 0.5.6 to 0.5.7. - [Release notes](https://github.com/google/go-cmp/releases) - [Commits](https://github.com/google/go-cmp/compare/v0.5.6...v0.5.7) --- updated-dependencies: - dependency-name: github.com/google/go-cmp dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index f19cec5..0811f96 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/shirou/gopsutil/v3 go 1.15 require ( - github.com/google/go-cmp v0.5.6 + github.com/google/go-cmp v0.5.7 github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c github.com/stretchr/testify v1.7.0 diff --git a/go.sum b/go.sum index 50b5a10..1084387 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= From 0306525d788076dbc4f3c594930acb3391620f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 22 Jan 2022 18:27:52 +0200 Subject: [PATCH 17/18] [process] implement ParentWithContext using PpidWithContext Removes need for redundant ParentWithContext implementations. It had led to it being unsupported on FreeBSD and OpenBSD even though PpidWithContext was available for them, and different implementations for getting the parent info used in ParentWithContext and PpidWithContext on Darwin and Linux. --- process/process.go | 9 +++++++++ process/process_darwin.go | 17 ----------------- process/process_fallback.go | 4 ---- process/process_freebsd.go | 4 ---- process/process_linux.go | 11 ----------- process/process_openbsd.go | 4 ---- process/process_plan9.go | 4 ---- process/process_solaris.go | 4 ---- process/process_windows.go | 9 --------- 9 files changed, 9 insertions(+), 57 deletions(-) diff --git a/process/process.go b/process/process.go index bcfd616..9e6542e 100644 --- a/process/process.go +++ b/process/process.go @@ -402,6 +402,15 @@ func (p *Process) Parent() (*Process, error) { return p.ParentWithContext(context.Background()) } +// ParentWithContext returns parent Process of the process. +func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { + ppid, err := p.PpidWithContext(ctx) + if err != nil { + return nil, err + } + return NewProcessWithContext(ctx, ppid) +} + // Status returns the process status. // Return value could be one of these. // R: Running S: Sleep T: Stop I: Idle diff --git a/process/process_darwin.go b/process/process_darwin.go index c1dc918..a700037 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -141,23 +141,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { return start.Unix() * 1000, nil } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - out, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR") - if err != nil { - return nil, err - } - for _, line := range out { - if len(line) >= 1 && line[0] == 'R' { - v, err := strconv.Atoi(line[1:]) - if err != nil { - return nil, err - } - return NewProcessWithContext(ctx, int32(v)) - } - } - return nil, fmt.Errorf("could not find parent line") -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { r, err := callPsWithContext(ctx, "state", p.Pid, false, false) if err != nil { diff --git a/process/process_fallback.go b/process/process_fallback.go index e1c4809..1a5d0c4 100644 --- a/process/process_fallback.go +++ b/process/process_fallback.go @@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return "", common.ErrNotImplementedError } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError } diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 383ec0f..444af35 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -118,10 +118,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { return k.Start.Sec*1000 + k.Start.Usec/1000, nil } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { k, err := p.getKProc() if err != nil { diff --git a/process/process_linux.go b/process/process_linux.go index af8f232..322ea0c 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -123,17 +123,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return p.fillFromCwdWithContext() } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - err := p.fillFromStatusWithContext() - if err != nil { - return nil, err - } - if p.parent == 0 { - return nil, fmt.Errorf("wrong number of parents") - } - return NewProcessWithContext(ctx, p.parent) -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { err := p.fillFromStatusWithContext() if err != nil { diff --git a/process/process_openbsd.go b/process/process_openbsd.go index cdbdaed..77aa804 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -142,10 +142,6 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) { return 0, common.ErrNotImplementedError } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { k, err := p.getKProc() if err != nil { diff --git a/process/process_plan9.go b/process/process_plan9.go index 70e11e3..bc4bc06 100644 --- a/process/process_plan9.go +++ b/process/process_plan9.go @@ -74,10 +74,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return "", common.ErrNotImplementedError } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError } diff --git a/process/process_solaris.go b/process/process_solaris.go index 93a6e01..4f10a67 100644 --- a/process/process_solaris.go +++ b/process/process_solaris.go @@ -88,10 +88,6 @@ func (p *Process) CwdWithContext(ctx context.Context) (string, error) { return p.fillFromPathCwdWithContext(ctx) } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - return nil, common.ErrNotImplementedError -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError } diff --git a/process/process_windows.go b/process/process_windows.go index d348946..2a3a0c4 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -435,15 +435,6 @@ func (p *Process) CwdWithContext(_ context.Context) (string, error) { return "", nil } -func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) { - ppid, err := p.PpidWithContext(ctx) - if err != nil { - return nil, fmt.Errorf("could not get ParentProcessID: %s", err) - } - - return NewProcessWithContext(ctx, ppid) -} - func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) { return []string{""}, common.ErrNotImplementedError } From 1e56c6f421ca758fd760bca7093c64c3605720d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 30 Jan 2022 22:48:09 +0200 Subject: [PATCH 18/18] Spelling and grammar fixes --- LICENSE | 2 +- README.md | 6 +++--- docker/docker_linux.go | 6 +++--- docker/docker_notlinux.go | 4 ++-- internal/common/common.go | 2 +- load/load_darwin.go | 2 +- load/load_linux.go | 2 +- mem/mem_linux.go | 6 +++--- net/net_aix.go | 2 +- net/net_darwin.go | 2 +- net/net_freebsd.go | 2 +- net/net_linux.go | 8 ++++---- net/net_openbsd.go | 2 +- net/net_windows.go | 2 +- process/process.go | 6 +++--- process/process_linux.go | 2 +- process/process_windows.go | 12 ++++++------ 17 files changed, 34 insertions(+), 34 deletions(-) diff --git a/LICENSE b/LICENSE index da71a5e..6f06adc 100644 --- a/LICENSE +++ b/LICENSE @@ -28,7 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ------- -internal/common/binary.go in the gopsutil is copied and modifid from golang/encoding/binary.go. +internal/common/binary.go in the gopsutil is copied and modified from golang/encoding/binary.go. diff --git a/README.md b/README.md index ad8be35..191f3c7 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ challenge is porting all psutil functions on some architectures. ## v3 migration -from v3.20.10, gopsutil becomes v3 which breaks backwards compatiblity. +from v3.20.10, gopsutil becomes v3 which breaks backwards compatibility. See [v3Changes.md](_tools/v3migration/v3Changes.md) more detail changes. ## Tag semantics @@ -21,7 +21,7 @@ for example, v2.17.04 means - 17: release year, 2017 - 04: release month -gopsutil aims to keep backwards-compatiblity until major version change. +gopsutil aims to keep backwards compatibility until major version change. Tagged at every end of month, but if there are only a few commits, it can be skipped. @@ -229,7 +229,7 @@ Some code is ported from Ohai. many thanks. |**HostInfo** | | | | | | | |hostname |x |x |x |x |x |x | |uptime |x |x |x |x | |x | -|proces |x |x |x | | |x | +|process |x |x |x | | |x | |os |x |x |x |x |x |x | |platform |x |x |x |x | |x | |platformfamily |x |x |x |x | |x | diff --git a/docker/docker_linux.go b/docker/docker_linux.go index 0c07312..edcb8e4 100644 --- a/docker/docker_linux.go +++ b/docker/docker_linux.go @@ -57,7 +57,7 @@ func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) { return ret, nil } -// GetDockerIDList returnes a list of DockerID. +// GetDockerIDList returns a list of DockerID. // This requires certain permission. func GetDockerIDList() ([]string, error) { return GetDockerIDListWithContext(context.Background()) @@ -86,7 +86,7 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) { return ret, nil } -// CgroupCPU returnes specified cgroup id CPU status. +// CgroupCPU returns specified cgroup id CPU status. // containerID is same as docker id if you use docker. // If you use container via systemd.slice, you could use // containerID = docker-.scope and base=/sys/fs/cgroup/cpuacct/system.slice/ @@ -94,7 +94,7 @@ func CgroupCPU(containerID string, base string) (*CgroupCPUStat, error) { return CgroupCPUWithContext(context.Background(), containerID, base) } -// CgroupCPUUsage returnes specified cgroup id CPU usage. +// CgroupCPUUsage returns specified cgroup id CPU usage. // containerID is same as docker id if you use docker. // If you use container via systemd.slice, you could use // containerID = docker-.scope and base=/sys/fs/cgroup/cpuacct/system.slice/ diff --git a/docker/docker_notlinux.go b/docker/docker_notlinux.go index df33512..2bd9111 100644 --- a/docker/docker_notlinux.go +++ b/docker/docker_notlinux.go @@ -19,7 +19,7 @@ func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) { return nil, ErrDockerNotAvailable } -// GetDockerIDList returnes a list of DockerID. +// GetDockerIDList returns a list of DockerID. // This requires certain permission. func GetDockerIDList() ([]string, error) { return GetDockerIDListWithContext(context.Background()) @@ -29,7 +29,7 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) { return nil, ErrDockerNotAvailable } -// CgroupCPU returnes specified cgroup id CPU status. +// CgroupCPU returns specified cgroup id CPU status. // containerid is same as docker id if you use docker. // If you use container via systemd.slice, you could use // containerid = docker-.scope and base=/sys/fs/cgroup/cpuacct/system.slice/ diff --git a/internal/common/common.go b/internal/common/common.go index 9bfb366..4f6df9a 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -64,7 +64,7 @@ func (i Invoke) CommandWithContext(ctx context.Context, name string, arg ...stri type FakeInvoke struct { Suffix string // Suffix species expected file name suffix such as "fail" - Error error // If Error specfied, return the error. + Error error // If Error specified, return the error. } // Command in FakeInvoke returns from expected file if exists. diff --git a/load/load_darwin.go b/load/load_darwin.go index 73e62ca..e0df3c9 100644 --- a/load/load_darwin.go +++ b/load/load_darwin.go @@ -39,7 +39,7 @@ func AvgWithContext(ctx context.Context) (*AvgStat, error) { return ret, nil } -// Misc returnes miscellaneous host-wide statistics. +// Misc returns miscellaneous host-wide statistics. // darwin use ps command to get process running/blocked count. // Almost same as FreeBSD implementation, but state is different. // U means 'Uninterruptible Sleep'. diff --git a/load/load_linux.go b/load/load_linux.go index 30bba35..debf073 100644 --- a/load/load_linux.go +++ b/load/load_linux.go @@ -68,7 +68,7 @@ func fileAvgWithContext() (*AvgStat, error) { return ret, nil } -// Misc returnes miscellaneous host-wide statistics. +// Misc returns miscellaneous host-wide statistics. // Note: the name should be changed near future. func Misc() (*MiscStat, error) { return MiscWithContext(context.Background()) diff --git a/mem/mem_linux.go b/mem/mem_linux.go index cea5400..9a5967c 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -305,7 +305,7 @@ func fillFromMeminfoWithContext() (*VirtualMemoryStat, *VirtualMemoryExStat, err if !memavail { if activeFile && inactiveFile && sReclaimable { - ret.Available = calcuateAvailVmem(ret, retEx) + ret.Available = calculateAvailVmem(ret, retEx) } else { ret.Available = ret.Cached + ret.Free } @@ -387,10 +387,10 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) { return ret, nil } -// calcuateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide +// calculateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide // "MemAvailable:" column. It reimplements an algorithm from the link below // https://github.com/giampaolo/psutil/pull/890 -func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 { +func calculateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 { var watermarkLow uint64 fn := common.HostProc("zoneinfo") diff --git a/net/net_aix.go b/net/net_aix.go index d89db7c..84e3609 100644 --- a/net/net_aix.go +++ b/net/net_aix.go @@ -105,7 +105,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, return iocounters, nil } -// NetIOCountersByFile is an method which is added just a compatibility for linux. +// IOCountersByFile exists just for compatibility with Linux. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { return IOCountersByFileWithContext(context.Background(), pernic, filename) } diff --git a/net/net_darwin.go b/net/net_darwin.go index 9badcf6..ec9ba41 100644 --- a/net/net_darwin.go +++ b/net/net_darwin.go @@ -257,7 +257,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, return ret, nil } -// NetIOCountersByFile is an method which is added just a compatibility for linux. +// IOCountersByFile exists just for compatibility with Linux. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { return IOCountersByFileWithContext(context.Background(), pernic, filename) } diff --git a/net/net_freebsd.go b/net/net_freebsd.go index 0a30423..50fe93a 100644 --- a/net/net_freebsd.go +++ b/net/net_freebsd.go @@ -95,7 +95,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, return ret, nil } -// NetIOCountersByFile is an method which is added just a compatibility for linux. +// IOCountersByFile exists just for compatibility with Linux. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { return IOCountersByFileWithContext(context.Background(), pernic, filename) } diff --git a/net/net_linux.go b/net/net_linux.go index d323781..0d909fc 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -40,7 +40,7 @@ const ( // Conntrack Column numbers ctSEARCH_RESTART ) -// NetIOCounters returnes network I/O statistics for every network +// NetIOCounters returns network I/O statistics for every network // interface installed on the system. If pernic argument is false, // return only sum of all information (which name is 'all'). If true, // every network interface installed on the system is returned @@ -188,7 +188,7 @@ func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoC line := lines[i] r := strings.IndexRune(line, ':') if r == -1 { - return nil, errors.New(filename + " is not fomatted correctly, expected ':'.") + return nil, errors.New(filename + " is not formatted correctly, expected ':'.") } proto := strings.ToLower(line[:r]) if !protos[proto] { @@ -204,7 +204,7 @@ func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoC i++ statValues := strings.Split(lines[i][r+2:], " ") if len(statNames) != len(statValues) { - return nil, errors.New(filename + " is not fomatted correctly, expected same number of columns.") + return nil, errors.New(filename + " is not formatted correctly, expected same number of columns.") } stat := ProtoCountersStat{ Protocol: proto, @@ -539,7 +539,7 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode return ret, nil } -// getProcInodes returnes fd of the pid. +// getProcInodes returns fd of the pid. func getProcInodes(root string, pid int32, max int) (map[string][]inodeMap, error) { ret := make(map[string][]inodeMap) diff --git a/net/net_openbsd.go b/net/net_openbsd.go index 04b9b1d..5f066a0 100644 --- a/net/net_openbsd.go +++ b/net/net_openbsd.go @@ -139,7 +139,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, return ret, nil } -// NetIOCountersByFile is an method which is added just a compatibility for linux. +// IOCountersByFile exists just for compatibility with Linux. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { return IOCountersByFileWithContext(context.Background(), pernic, filename) } diff --git a/net/net_windows.go b/net/net_windows.go index f742d82..731c8f9 100644 --- a/net/net_windows.go +++ b/net/net_windows.go @@ -200,7 +200,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, return counters, nil } -// NetIOCountersByFile is an method which is added just a compatibility for linux. +// IOCountersByFile exists just for compatibility with Linux. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) { return IOCountersByFileWithContext(context.Background(), pernic, filename) } diff --git a/process/process.go b/process/process.go index 9e6542e..0ca26c2 100644 --- a/process/process.go +++ b/process/process.go @@ -47,13 +47,13 @@ type Process struct { const ( // Running marks a task a running or runnable (on the run queue) Running = "running" - // Blocked marks a task waiting on a short, uninterruptable operation (usually IO) + // Blocked marks a task waiting on a short, uninterruptible operation (usually I/O) Blocked = "blocked" // Idle marks a task sleeping for more than about 20 seconds Idle = "idle" // Lock marks a task waiting to acquire a lock Lock = "lock" - // Sleep marks task waiting for short, interruptable operation + // Sleep marks task waiting for short, interruptible operation Sleep = "sleep" // Stop marks a stopped process Stop = "stop" @@ -230,7 +230,7 @@ func (p *Process) BackgroundWithContext(ctx context.Context) (bool, error) { } // If interval is 0, return difference from last call(non-blocking). -// If interval > 0, wait interval sec and return diffrence between start and end. +// If interval > 0, wait interval sec and return difference between start and end. func (p *Process) Percent(interval time.Duration) (float64, error) { return p.PercentWithContext(context.Background(), interval) } diff --git a/process/process_linux.go b/process/process_linux.go index ef95333..8b7d3fd 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -526,7 +526,7 @@ func (p *Process) fillFromLimitsWithContext() ([]RlimitStat, error) { // Assert that last item is a Hard limit statItem.Hard, err = limitToUint(str[len(str)-1]) if err != nil { - // On error remove last item an try once again since it can be unit or header line + // On error remove last item and try once again since it can be unit or header line str = str[:len(str)-1] statItem.Hard, err = limitToUint(str[len(str)-1]) if err != nil { diff --git a/process/process_windows.go b/process/process_windows.go index 2a3a0c4..d7bb53e 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -189,7 +189,7 @@ type winLUIDAndAttributes struct { } // TOKEN_PRIVILEGES -type winTokenPriviledges struct { +type winTokenPrivileges struct { PrivilegeCount winDWord Privileges [1]winLUIDAndAttributes } @@ -218,23 +218,23 @@ func init() { } defer token.Close() - tokenPriviledges := winTokenPriviledges{PrivilegeCount: 1} + tokenPrivileges := winTokenPrivileges{PrivilegeCount: 1} lpName := syscall.StringToUTF16("SeDebugPrivilege") ret, _, _ := procLookupPrivilegeValue.Call( 0, uintptr(unsafe.Pointer(&lpName[0])), - uintptr(unsafe.Pointer(&tokenPriviledges.Privileges[0].Luid))) + uintptr(unsafe.Pointer(&tokenPrivileges.Privileges[0].Luid))) if ret == 0 { return } - tokenPriviledges.Privileges[0].Attributes = 0x00000002 // SE_PRIVILEGE_ENABLED + tokenPrivileges.Privileges[0].Attributes = 0x00000002 // SE_PRIVILEGE_ENABLED procAdjustTokenPrivileges.Call( uintptr(token), 0, - uintptr(unsafe.Pointer(&tokenPriviledges)), - uintptr(unsafe.Sizeof(tokenPriviledges)), + uintptr(unsafe.Pointer(&tokenPrivileges)), + uintptr(unsafe.Sizeof(tokenPrivileges)), 0, 0) }