diff --git a/process/process.go b/process/process.go index a7d04f4..c674b3d 100644 --- a/process/process.go +++ b/process/process.go @@ -6,6 +6,7 @@ import ( "errors" "math" "runtime" + "sort" "time" "github.com/shirou/gopsutil/cpu" @@ -14,9 +15,9 @@ import ( ) var ( - invoke common.Invoker = common.Invoke{} - ErrorNoChildren = errors.New("process does not have children") - ErrorProcessNotRunning = errors.New("process does not exist") + invoke common.Invoker = common.Invoke{} + ErrorNoChildren = errors.New("process does not have children") + ErrorProcessNotRunning = errors.New("process does not exist") ) type Process struct { @@ -137,6 +138,17 @@ func (p NumCtxSwitchesStat) String() string { return string(s) } +// Pids returns a slice of process ID list which are running now. +func Pids() ([]int32, error) { + return PidsWithContext(context.Background()) +} + +func PidsWithContext(ctx context.Context) ([]int32, error) { + pids, err := pidsWithContext(ctx) + sort.Slice(pids, func(i, j int) bool { return pids[i] < pids[j] }) + return pids, err +} + // NewProcess creates a new Process instance, it only stores the pid and // checks that the process exists. Other method on Process can be used // to get more information about the process. An error will be returned diff --git a/process/process_darwin.go b/process/process_darwin.go index ad7354b..98a5566 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -44,11 +44,7 @@ type MemoryInfoExStat struct { type MemoryMapsStat struct { } -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { +func pidsWithContext(ctx context.Context) ([]int32, error) { var ret []int32 pids, err := callPsWithContext(ctx, "pid", 0, false) diff --git a/process/process_fallback.go b/process/process_fallback.go index 76f7caa..3201efd 100644 --- a/process/process_fallback.go +++ b/process/process_fallback.go @@ -28,11 +28,7 @@ type MemoryMapsStat struct { type MemoryInfoExStat struct { } -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { +func pidsWithContext(ctx context.Context) ([]int32, error) { return []int32{}, common.ErrNotImplementedError } diff --git a/process/process_freebsd.go b/process/process_freebsd.go index d606615..0d4bbfc 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -23,11 +23,7 @@ type MemoryInfoExStat struct { type MemoryMapsStat struct { } -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { +func pidsWithContext(ctx context.Context) ([]int32, error) { var ret []int32 procs, err := Processes() if err != nil { diff --git a/process/process_linux.go b/process/process_linux.go index 9d9b09e..062b0ae 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -1280,12 +1280,7 @@ func (p *Process) fillFromStatWithContext(ctx context.Context) (uint64, int32, * return p.fillFromTIDStat(-1) } -// Pids returns a slice of process ID list which are running now. -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { +func pidsWithContext(ctx context.Context) ([]int32, error) { return readPidsFromDir(common.HostProc()) } diff --git a/process/process_openbsd.go b/process/process_openbsd.go index f95e750..56e3500 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -26,11 +26,7 @@ type MemoryInfoExStat struct { type MemoryMapsStat struct { } -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { +func pidsWithContext(ctx context.Context) ([]int32, error) { var ret []int32 procs, err := Processes() if err != nil { diff --git a/process/process_windows.go b/process/process_windows.go index 7a6a9de..f78c9be 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -150,11 +150,7 @@ func init() { 0) } -func Pids() ([]int32, error) { - return PidsWithContext(context.Background()) -} - -func PidsWithContext(ctx context.Context) ([]int32, error) { +func pidsWithContext(ctx context.Context) ([]int32, error) { // inspired by https://gist.github.com/henkman/3083408 // and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329 var ret []int32