@ -64,11 +64,6 @@ func (m MemoryMapsStat) String() string {
return string ( s )
}
// Ppid returns Parent Process ID of the process.
func ( p * Process ) Ppid ( ) ( int32 , error ) {
return p . PpidWithContext ( context . Background ( ) )
}
func ( p * Process ) PpidWithContext ( ctx context . Context ) ( int32 , error ) {
_ , ppid , _ , _ , _ , _ , _ , err := p . fillFromStatWithContext ( ctx )
if err != nil {
@ -77,11 +72,6 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
return ppid , nil
}
// Name returns name of the process.
func ( p * Process ) Name ( ) ( string , error ) {
return p . NameWithContext ( context . Background ( ) )
}
func ( p * Process ) NameWithContext ( ctx context . Context ) ( string , error ) {
if p . name == "" {
if err := p . fillFromStatusWithContext ( ctx ) ; err != nil {
@ -91,41 +81,23 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return p . name , nil
}
// Tgid returns tgid, a Linux-synonym for user-space Pid
func ( p * Process ) Tgid ( ) ( int32 , error ) {
func ( p * Process ) TgidWithContext ( ctx context . Context ) ( int32 , error ) {
if p . tgid == 0 {
if err := p . fillFromStatusWithContext ( c on te xt. Background ( ) ) ; err != nil {
if err := p . fillFromStatusWithContext ( c tx) ; err != nil {
return 0 , err
}
}
return p . tgid , nil
}
// Exe returns executable path of the process.
func ( p * Process ) Exe ( ) ( string , error ) {
return p . ExeWithContext ( context . Background ( ) )
}
func ( p * Process ) ExeWithContext ( ctx context . Context ) ( string , error ) {
return p . fillFromExeWithContext ( ctx )
}
// Cmdline returns the command line arguments of the process as a string with
// each argument separated by 0x20 ascii character.
func ( p * Process ) Cmdline ( ) ( string , error ) {
return p . CmdlineWithContext ( context . Background ( ) )
}
func ( p * Process ) CmdlineWithContext ( ctx context . Context ) ( string , error ) {
return p . fillFromCmdlineWithContext ( ctx )
}
// CmdlineSlice returns the command line arguments of the process as a slice with each
// element being an argument.
func ( p * Process ) CmdlineSlice ( ) ( [ ] string , error ) {
return p . CmdlineSliceWithContext ( context . Background ( ) )
}
func ( p * Process ) CmdlineSliceWithContext ( ctx context . Context ) ( [ ] string , error ) {
return p . fillSliceFromCmdlineWithContext ( ctx )
}
@ -138,20 +110,10 @@ func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
return createTime , nil
}
// Cwd returns current working directory of the process.
func ( p * Process ) Cwd ( ) ( string , error ) {
return p . CwdWithContext ( context . Background ( ) )
}
func ( p * Process ) CwdWithContext ( ctx context . Context ) ( string , error ) {
return p . fillFromCwdWithContext ( ctx )
}
// Parent returns parent Process of the process.
func ( p * Process ) Parent ( ) ( * Process , error ) {
return p . ParentWithContext ( context . Background ( ) )
}
func ( p * Process ) ParentWithContext ( ctx context . Context ) ( * Process , error ) {
err := p . fillFromStatusWithContext ( ctx )
if err != nil {
@ -160,16 +122,7 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
if p . parent == 0 {
return nil , fmt . Errorf ( "wrong number of parents" )
}
return NewProcess ( p . parent )
}
// Status returns the process status.
// Return value could be one of these.
// R: Running S: Sleep T: Stop I: Idle
// Z: Zombie W: Wait L: Lock
// The character is same within all supported platforms.
func ( p * Process ) Status ( ) ( string , error ) {
return p . StatusWithContext ( context . Background ( ) )
return NewProcessWithContext ( ctx , p . parent )
}
func ( p * Process ) StatusWithContext ( ctx context . Context ) ( string , error ) {
@ -180,11 +133,6 @@ func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
return p . status , nil
}
// Foreground returns true if the process is in foreground, false otherwise.
func ( p * Process ) Foreground ( ) ( bool , error ) {
return p . ForegroundWithContext ( context . Background ( ) )
}
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
@ -202,11 +150,6 @@ func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
return pgid == tpgid , nil
}
// Uids returns user ids of the process as a slice of the int
func ( p * Process ) Uids ( ) ( [ ] int32 , error ) {
return p . UidsWithContext ( context . Background ( ) )
}
func ( p * Process ) UidsWithContext ( ctx context . Context ) ( [ ] int32 , error ) {
err := p . fillFromStatusWithContext ( ctx )
if err != nil {
@ -215,11 +158,6 @@ func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
return p . uids , nil
}
// Gids returns group ids of the process as a slice of the int
func ( p * Process ) Gids ( ) ( [ ] int32 , error ) {
return p . GidsWithContext ( context . Background ( ) )
}
func ( p * Process ) GidsWithContext ( ctx context . Context ) ( [ ] int32 , error ) {
err := p . fillFromStatusWithContext ( ctx )
if err != nil {
@ -236,11 +174,6 @@ func (p *Process) GroupsWithContext(ctx context.Context) ([]int32, error) {
return p . groups , nil
}
// Terminal returns a terminal which is associated with the process.
func ( p * Process ) Terminal ( ) ( string , error ) {
return p . TerminalWithContext ( context . Background ( ) )
}
func ( p * Process ) TerminalWithContext ( ctx context . Context ) ( string , error ) {
t , _ , _ , _ , _ , _ , _ , err := p . fillFromStatWithContext ( ctx )
if err != nil {
@ -254,12 +187,6 @@ func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
return terminal , nil
}
// Nice returns a nice value (priority).
// Notice: gopsutil can not set nice value.
func ( p * Process ) Nice ( ) ( int32 , error ) {
return p . NiceWithContext ( context . Background ( ) )
}
func ( p * Process ) NiceWithContext ( ctx context . Context ) ( int32 , error ) {
_ , _ , _ , _ , _ , nice , _ , err := p . fillFromStatWithContext ( ctx )
if err != nil {
@ -268,29 +195,12 @@ func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
return nice , nil
}
// IOnice returns process I/O nice value (priority).
func ( p * Process ) IOnice ( ) ( int32 , error ) {
return p . IOniceWithContext ( context . Background ( ) )
}
func ( p * Process ) IOniceWithContext ( ctx context . Context ) ( int32 , error ) {
return 0 , common . ErrNotImplementedError
}
// Rlimit returns Resource Limits.
func ( p * Process ) Rlimit ( ) ( [ ] RlimitStat , error ) {
return p . RlimitWithContext ( context . Background ( ) )
}
func ( p * Process ) RlimitWithContext ( ctx context . Context ) ( [ ] RlimitStat , error ) {
return p . RlimitUsage ( false )
}
// RlimitUsage returns Resource Limits.
// If gatherUsed is true, the currently used value will be gathered and added
// to the resulting RlimitStat.
func ( p * Process ) RlimitUsage ( gatherUsed bool ) ( [ ] RlimitStat , error ) {
return p . RlimitUsageWithContext ( context . Background ( ) , gatherUsed )
return p . RlimitUsageWithContext ( ctx , false )
}
func ( p * Process ) RlimitUsageWithContext ( ctx context . Context , gatherUsed bool ) ( [ ] RlimitStat , error ) {
@ -311,7 +221,7 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) (
rs := & rlimits [ i ]
switch rs . Resource {
case RLIMIT_CPU :
times , err := p . Times ( )
times , err := p . Times WithContext ( ctx )
if err != nil {
return nil , err
}
@ -323,7 +233,7 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) (
case RLIMIT_RSS :
rs . Used = uint64 ( p . memInfo . RSS )
case RLIMIT_NOFILE :
n , err := p . NumFDs ( )
n , err := p . NumFDs WithContext ( ctx )
if err != nil {
return nil , err
}
@ -348,20 +258,10 @@ func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) (
return rlimits , err
}
// IOCounters returns IO Counters.
func ( p * Process ) IOCounters ( ) ( * IOCountersStat , error ) {
return p . IOCountersWithContext ( context . Background ( ) )
}
func ( p * Process ) IOCountersWithContext ( ctx context . Context ) ( * IOCountersStat , error ) {
return p . fillFromIOWithContext ( ctx )
}
// NumCtxSwitches returns the number of the context switches of the process.
func ( p * Process ) NumCtxSwitches ( ) ( * NumCtxSwitchesStat , error ) {
return p . NumCtxSwitchesWithContext ( context . Background ( ) )
}
func ( p * Process ) NumCtxSwitchesWithContext ( ctx context . Context ) ( * NumCtxSwitchesStat , error ) {
err := p . fillFromStatusWithContext ( ctx )
if err != nil {
@ -370,21 +270,11 @@ func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitche
return p . numCtxSwitches , nil
}
// NumFDs returns the number of File Descriptors used by the process.
func ( p * Process ) NumFDs ( ) ( int32 , error ) {
return p . NumFDsWithContext ( context . Background ( ) )
}
func ( p * Process ) NumFDsWithContext ( ctx context . Context ) ( int32 , error ) {
_ , fnames , err := p . fillFromfdListWithContext ( ctx )
return int32 ( len ( fnames ) ) , err
}
// NumThreads returns the number of threads used by the process.
func ( p * Process ) NumThreads ( ) ( int32 , error ) {
return p . NumThreadsWithContext ( context . Background ( ) )
}
func ( p * Process ) NumThreadsWithContext ( ctx context . Context ) ( int32 , error ) {
err := p . fillFromStatusWithContext ( ctx )
if err != nil {
@ -393,10 +283,6 @@ func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
return p . numThreads , nil
}
func ( p * Process ) Threads ( ) ( map [ int32 ] * cpu . TimesStat , error ) {
return p . ThreadsWithContext ( context . Background ( ) )
}
func ( p * Process ) ThreadsWithContext ( ctx context . Context ) ( map [ int32 ] * cpu . TimesStat , error ) {
ret := make ( map [ int32 ] * cpu . TimesStat )
taskPath := common . HostProc ( strconv . Itoa ( int ( p . Pid ) ) , "task" )
@ -417,11 +303,6 @@ func (p *Process) ThreadsWithContext(ctx context.Context) (map[int32]*cpu.TimesS
return ret , nil
}
// Times returns CPU times of the process.
func ( p * Process ) Times ( ) ( * cpu . TimesStat , error ) {
return p . TimesWithContext ( context . Background ( ) )
}
func ( p * Process ) TimesWithContext ( ctx context . Context ) ( * cpu . TimesStat , error ) {
_ , _ , cpuTimes , _ , _ , _ , _ , err := p . fillFromStatWithContext ( ctx )
if err != nil {
@ -430,22 +311,10 @@ func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error)
return cpuTimes , nil
}
// CPUAffinity returns CPU affinity of the process.
//
// Notice: Not implemented yet.
func ( p * Process ) CPUAffinity ( ) ( [ ] int32 , error ) {
return p . CPUAffinityWithContext ( context . Background ( ) )
}
func ( p * Process ) CPUAffinityWithContext ( ctx context . Context ) ( [ ] int32 , error ) {
return nil , common . ErrNotImplementedError
}
// MemoryInfo returns platform in-dependend memory information, such as RSS, VMS and Swap
func ( p * Process ) MemoryInfo ( ) ( * MemoryInfoStat , error ) {
return p . MemoryInfoWithContext ( context . Background ( ) )
}
func ( p * Process ) MemoryInfoWithContext ( ctx context . Context ) ( * MemoryInfoStat , error ) {
meminfo , _ , err := p . fillFromStatmWithContext ( ctx )
if err != nil {
@ -454,11 +323,6 @@ func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, e
return meminfo , nil
}
// MemoryInfoEx returns platform dependend memory information.
func ( p * Process ) MemoryInfoEx ( ) ( * MemoryInfoExStat , error ) {
return p . MemoryInfoExWithContext ( context . Background ( ) )
}
func ( p * Process ) MemoryInfoExWithContext ( ctx context . Context ) ( * MemoryInfoExStat , error ) {
_ , memInfoEx , err := p . fillFromStatmWithContext ( ctx )
if err != nil {
@ -467,11 +331,6 @@ func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExSta
return memInfoEx , nil
}
// PageFaultsInfo returns the process's page fault counters
func ( p * Process ) PageFaults ( ) ( * PageFaultsStat , error ) {
return p . PageFaultsWithContext ( context . Background ( ) )
}
func ( p * Process ) PageFaultsWithContext ( ctx context . Context ) ( * PageFaultsStat , error ) {
_ , _ , _ , _ , _ , _ , pageFaults , err := p . fillFromStatWithContext ( ctx )
if err != nil {
@ -481,11 +340,6 @@ func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, e
}
// Children returns a slice of Process of the process.
func ( p * Process ) Children ( ) ( [ ] * Process , error ) {
return p . ChildrenWithContext ( context . Background ( ) )
}
func ( p * Process ) ChildrenWithContext ( ctx context . Context ) ( [ ] * Process , error ) {
pids , err := common . CallPgrepWithContext ( ctx , invoke , p . Pid )
if err != nil {
@ -496,7 +350,7 @@ func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
}
ret := make ( [ ] * Process , 0 , len ( pids ) )
for _ , pid := range pids {
np , err := NewProcess ( pid )
np , err := NewProcess WithContext ( ctx , pid )
if err != nil {
return nil , err
}
@ -505,12 +359,6 @@ func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
return ret , nil
}
// OpenFiles returns a slice of OpenFilesStat opend by the process.
// OpenFilesStat includes a file path and file descriptor.
func ( p * Process ) OpenFiles ( ) ( [ ] OpenFilesStat , error ) {
return p . OpenFilesWithContext ( context . Background ( ) )
}
func ( p * Process ) OpenFilesWithContext ( ctx context . Context ) ( [ ] OpenFilesStat , error ) {
_ , ofs , err := p . fillFromfdWithContext ( ctx )
if err != nil {
@ -524,38 +372,17 @@ func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, er
return ret , nil
}
// Connections returns a slice of net.ConnectionStat used by the process.
// This returns all kind of the connection. This measn TCP, UDP or UNIX.
func ( p * Process ) Connections ( ) ( [ ] net . ConnectionStat , error ) {
return p . ConnectionsWithContext ( context . Background ( ) )
}
func ( p * Process ) ConnectionsWithContext ( ctx context . Context ) ( [ ] net . ConnectionStat , error ) {
return net . ConnectionsPid ( "all" , p . Pid )
}
// Connections returns a slice of net.ConnectionStat used by the process at most `max`
func ( p * Process ) ConnectionsMax ( max int ) ( [ ] net . ConnectionStat , error ) {
return p . ConnectionsMaxWithContext ( context . Background ( ) , max )
return net . ConnectionsPidWithContext ( ctx , "all" , p . Pid )
}
func ( p * Process ) ConnectionsMaxWithContext ( ctx context . Context , max int ) ( [ ] net . ConnectionStat , error ) {
return net . ConnectionsPidMax ( "all" , p . Pid , max )
}
// NetIOCounters returns NetIOCounters of the process.
func ( p * Process ) NetIOCounters ( pernic bool ) ( [ ] net . IOCountersStat , error ) {
return p . NetIOCountersWithContext ( context . Background ( ) , pernic )
return net . ConnectionsPidMaxWithContext ( ctx , "all" , p . Pid , max )
}
func ( p * Process ) NetIOCountersWithContext ( ctx context . Context , pernic bool ) ( [ ] net . IOCountersStat , error ) {
filename := common . HostProc ( strconv . Itoa ( int ( p . Pid ) ) , "net/dev" )
return net . IOCountersByFile ( pernic , filename )
}
// MemoryMaps get memory maps from /proc/(pid)/smaps
func ( p * Process ) MemoryMaps ( grouped bool ) ( * [ ] MemoryMapsStat , error ) {
return p . MemoryMapsWithContext ( context . Background ( ) , grouped )
return net . IOCountersByFileWithContext ( ctx , pernic , filename )
}
func ( p * Process ) MemoryMapsWithContext ( ctx context . Context , grouped bool ) ( * [ ] MemoryMapsStat , error ) {
@ -1181,7 +1008,7 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (ui
// docs). Note: I am assuming at least Linux 2.6.18
iotime , err := strconv . ParseFloat ( fields [ i + 40 ] , 64 )
if err != nil {
iotime = 0 // Ancient linux version, most likely
iotime = 0 // Ancient linux version, most likely
}
cpuTimes := & cpu . TimesStat {
@ -1249,12 +1076,6 @@ func pidsWithContext(ctx context.Context) ([]int32, error) {
return readPidsFromDir ( common . HostProc ( ) )
}
// Process returns a slice of pointers to Process structs for all
// currently running processes.
func Processes ( ) ( [ ] * Process , error ) {
return ProcessesWithContext ( context . Background ( ) )
}
func ProcessesWithContext ( ctx context . Context ) ( [ ] * Process , error ) {
out := [ ] * Process { }
@ -1264,7 +1085,7 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
}
for _ , pid := range pids {
p , err := NewProcess ( pid )
p , err := NewProcess WithContext ( ctx , pid )
if err != nil {
continue
}