[process] Fix #752 sort PIDS returned by process.Pids()

pull/754/head
Lomanic 6 years ago
parent 58dc3b28aa
commit 3eefe64948

@ -6,6 +6,7 @@ import (
"errors" "errors"
"math" "math"
"runtime" "runtime"
"sort"
"time" "time"
"github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/cpu"
@ -14,9 +15,9 @@ import (
) )
var ( var (
invoke common.Invoker = common.Invoke{} invoke common.Invoker = common.Invoke{}
ErrorNoChildren = errors.New("process does not have children") ErrorNoChildren = errors.New("process does not have children")
ErrorProcessNotRunning = errors.New("process does not exist") ErrorProcessNotRunning = errors.New("process does not exist")
) )
type Process struct { type Process struct {
@ -137,6 +138,17 @@ func (p NumCtxSwitchesStat) String() string {
return string(s) 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 // NewProcess creates a new Process instance, it only stores the pid and
// checks that the process exists. Other method on Process can be used // checks that the process exists. Other method on Process can be used
// to get more information about the process. An error will be returned // to get more information about the process. An error will be returned

@ -44,11 +44,7 @@ type MemoryInfoExStat struct {
type MemoryMapsStat struct { type MemoryMapsStat struct {
} }
func Pids() ([]int32, error) { func pidsWithContext(ctx context.Context) ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
var ret []int32 var ret []int32
pids, err := callPsWithContext(ctx, "pid", 0, false) pids, err := callPsWithContext(ctx, "pid", 0, false)

@ -28,11 +28,7 @@ type MemoryMapsStat struct {
type MemoryInfoExStat struct { type MemoryInfoExStat struct {
} }
func Pids() ([]int32, error) { func pidsWithContext(ctx context.Context) ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
return []int32{}, common.ErrNotImplementedError return []int32{}, common.ErrNotImplementedError
} }

@ -23,11 +23,7 @@ type MemoryInfoExStat struct {
type MemoryMapsStat struct { type MemoryMapsStat struct {
} }
func Pids() ([]int32, error) { func pidsWithContext(ctx context.Context) ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
var ret []int32 var ret []int32
procs, err := Processes() procs, err := Processes()
if err != nil { if err != nil {

@ -1280,12 +1280,7 @@ func (p *Process) fillFromStatWithContext(ctx context.Context) (uint64, int32, *
return p.fillFromTIDStat(-1) return p.fillFromTIDStat(-1)
} }
// Pids returns a slice of process ID list which are running now. func pidsWithContext(ctx context.Context) ([]int32, error) {
func Pids() ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
return readPidsFromDir(common.HostProc()) return readPidsFromDir(common.HostProc())
} }

@ -26,11 +26,7 @@ type MemoryInfoExStat struct {
type MemoryMapsStat struct { type MemoryMapsStat struct {
} }
func Pids() ([]int32, error) { func pidsWithContext(ctx context.Context) ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
var ret []int32 var ret []int32
procs, err := Processes() procs, err := Processes()
if err != nil { if err != nil {

@ -150,11 +150,7 @@ func init() {
0) 0)
} }
func Pids() ([]int32, error) { func pidsWithContext(ctx context.Context) ([]int32, error) {
return PidsWithContext(context.Background())
}
func PidsWithContext(ctx context.Context) ([]int32, error) {
// inspired by https://gist.github.com/henkman/3083408 // inspired by https://gist.github.com/henkman/3083408
// and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329 // and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329
var ret []int32 var ret []int32

Loading…
Cancel
Save