mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
Merge pull request #754 from Lomanic/issue752
[process] Fix #752 sort PIDS returned by process.Pids()
This commit is contained in:
commit
dad54b96cb
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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())
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user