2024-02-17 03:48:29 +00:00
// SPDX-License-Identifier: BSD-3-Clause
2014-12-30 22:09:05 +09:00
package process
2014-04-23 12:26:21 +09:00
2014-05-01 18:43:11 +09:00
import (
2017-12-31 15:25:49 +09:00
"context"
2014-05-01 18:43:11 +09:00
"encoding/json"
2018-03-24 15:56:32 +09:00
"errors"
2015-03-04 00:02:09 +09:00
"runtime"
2019-08-22 21:09:10 +02:00
"sort"
2020-10-21 14:52:41 +03:00
"sync"
2015-03-04 00:02:09 +09:00
"time"
2024-02-17 03:48:29 +00:00
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/internal/common"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
2014-05-01 18:43:11 +09:00
)
2018-03-24 15:56:32 +09:00
var (
2019-08-22 21:09:10 +02:00
invoke common . Invoker = common . Invoke { }
2024-09-07 03:10:57 +02:00
ErrorNoChildren = errors . New ( "process does not have children" ) // Deprecated: ErrorNoChildren is never returned by process.Children(), check its returned []*Process slice length instead
2019-08-22 21:09:10 +02:00
ErrorProcessNotRunning = errors . New ( "process does not exist" )
2021-11-06 09:53:56 +00:00
ErrorNotPermitted = errors . New ( "operation not permitted" )
2018-03-24 15:56:32 +09:00
)
2015-09-16 11:58:02 +09:00
2014-04-23 12:26:21 +09:00
type Process struct {
2014-09-19 22:41:29 +09:00
Pid int32 ` json:"pid" `
name string
status string
2016-04-18 11:15:15 +10:00
parent int32
2021-06-24 15:18:00 -07:00
parentMutex sync . RWMutex // for windows ppid cache
2014-08-31 14:46:23 +04:00
numCtxSwitches * NumCtxSwitchesStat
2024-02-17 12:39:18 +00:00
uids [ ] uint32
gids [ ] uint32
2023-02-28 15:05:12 +03:00
groups [ ] uint32
2014-09-19 22:41:29 +09:00
numThreads int32
2014-10-22 19:58:39 +04:00
memInfo * MemoryInfoStat
2017-04-05 12:01:59 -04:00
sigInfo * SignalInfoStat
2019-09-06 13:36:22 +02:00
createTime int64
2015-03-04 00:02:09 +09:00
2016-03-22 23:09:12 +09:00
lastCPUTimes * cpu . TimesStat
2015-03-04 00:02:09 +09:00
lastCPUTime time . Time
2018-01-04 11:30:39 -08:00
tgid int32
2014-04-23 12:26:21 +09:00
}
2021-11-06 09:53:56 +00:00
// Process status
const (
2021-11-09 13:14:54 +00:00
// Running marks a task a running or runnable (on the run queue)
2021-11-06 09:53:56 +00:00
Running = "running"
2022-01-30 22:48:09 +02:00
// Blocked marks a task waiting on a short, uninterruptible operation (usually I/O)
2021-11-09 13:14:54 +00:00
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"
2022-01-30 22:48:09 +02:00
// Sleep marks task waiting for short, interruptible operation
2021-11-09 13:14:54 +00:00
Sleep = "sleep"
// Stop marks a stopped process
Stop = "stop"
// Wait marks an idle interrupt thread (or paging in pre 2.6.xx Linux)
Wait = "wait"
// Zombie marks a defunct process, terminated but not reaped by its parent
Zombie = "zombie"
// Solaris states. See https://github.com/collectd/collectd/blob/1da3305c10c8ff9a63081284cf3d4bb0f6daffd8/src/processes.c#L2115
Daemon = "daemon"
Detached = "detached"
System = "system"
Orphan = "orphan"
UnknownState = ""
2021-11-06 09:53:56 +00:00
)
2014-04-30 15:32:05 +09:00
type OpenFilesStat struct {
2014-04-24 00:38:19 +09:00
Path string ` json:"path" `
2014-04-25 15:09:38 +09:00
Fd uint64 ` json:"fd" `
2014-04-23 12:26:21 +09:00
}
2014-04-30 15:32:05 +09:00
type MemoryInfoStat struct {
2017-04-05 12:01:59 -04:00
RSS uint64 ` json:"rss" ` // bytes
VMS uint64 ` json:"vms" ` // bytes
2019-01-13 17:23:01 +09:00
HWM uint64 ` json:"hwm" ` // bytes
2017-04-05 12:01:59 -04:00
Data uint64 ` json:"data" ` // bytes
Stack uint64 ` json:"stack" ` // bytes
Locked uint64 ` json:"locked" ` // bytes
Swap uint64 ` json:"swap" ` // bytes
}
type SignalInfoStat struct {
PendingProcess uint64 ` json:"pending_process" `
PendingThread uint64 ` json:"pending_thread" `
Blocked uint64 ` json:"blocked" `
Ignored uint64 ` json:"ignored" `
Caught uint64 ` json:"caught" `
2014-04-23 12:26:21 +09:00
}
type RlimitStat struct {
2017-04-05 12:01:59 -04:00
Resource int32 ` json:"resource" `
2021-11-06 09:53:56 +00:00
Soft uint64 ` json:"soft" `
Hard uint64 ` json:"hard" `
2017-04-05 12:01:59 -04:00
Used uint64 ` json:"used" `
2014-04-23 12:26:21 +09:00
}
2014-04-30 16:16:07 +09:00
type IOCountersStat struct {
2024-05-14 00:16:27 +09:00
// ReadCount is a number of read I/O operations such as syscalls.
ReadCount uint64 ` json:"readCount" `
// WriteCount is a number of read I/O operations such as syscalls.
2016-03-23 10:52:46 +09:00
WriteCount uint64 ` json:"writeCount" `
2024-05-14 00:16:27 +09:00
// ReadBytes is a number of all I/O read in bytes. This includes disk I/O on Linux and Windows.
ReadBytes uint64 ` json:"readBytes" `
// WriteBytes is a number of all I/O write in bytes. This includes disk I/O on Linux and Windows.
2016-03-23 10:52:46 +09:00
WriteBytes uint64 ` json:"writeBytes" `
2024-05-14 00:16:27 +09:00
// DiskReadBytes is a number of disk I/O write in bytes. Currently only Linux has this value.
DiskReadBytes uint64 ` json:"diskReadBytes" `
// DiskWriteBytes is a number of disk I/O read in bytes. Currently only Linux has this value.
DiskWriteBytes uint64 ` json:"diskWriteBytes" `
2014-04-23 12:26:21 +09:00
}
2014-04-23 13:24:07 +09:00
2014-05-01 19:10:15 +09:00
type NumCtxSwitchesStat struct {
2014-09-13 00:29:54 +04:00
Voluntary int64 ` json:"voluntary" `
Involuntary int64 ` json:"involuntary" `
2014-05-01 19:10:15 +09:00
}
2019-01-13 17:27:42 +09:00
type PageFaultsStat struct {
MinorFaults uint64 ` json:"minorFaults" `
MajorFaults uint64 ` json:"majorFaults" `
ChildMinorFaults uint64 ` json:"childMinorFaults" `
ChildMajorFaults uint64 ` json:"childMajorFaults" `
}
2017-01-11 19:05:34 +04:00
// Resource limit constants are from /usr/include/x86_64-linux-gnu/bits/resource.h
// from libc6-dev package in Ubuntu 16.10
2017-01-11 00:02:02 +04:00
const (
RLIMIT_CPU int32 = 0
RLIMIT_FSIZE int32 = 1
RLIMIT_DATA int32 = 2
RLIMIT_STACK int32 = 3
RLIMIT_CORE int32 = 4
RLIMIT_RSS int32 = 5
RLIMIT_NPROC int32 = 6
RLIMIT_NOFILE int32 = 7
RLIMIT_MEMLOCK int32 = 8
RLIMIT_AS int32 = 9
RLIMIT_LOCKS int32 = 10
RLIMIT_SIGPENDING int32 = 11
RLIMIT_MSGQUEUE int32 = 12
RLIMIT_NICE int32 = 13
RLIMIT_RTPRIO int32 = 14
RLIMIT_RTTIME int32 = 15
)
2014-05-01 18:43:11 +09:00
func ( p Process ) String ( ) string {
s , _ := json . Marshal ( p )
return string ( s )
}
func ( o OpenFilesStat ) String ( ) string {
s , _ := json . Marshal ( o )
return string ( s )
}
func ( m MemoryInfoStat ) String ( ) string {
s , _ := json . Marshal ( m )
return string ( s )
}
func ( r RlimitStat ) String ( ) string {
s , _ := json . Marshal ( r )
return string ( s )
}
func ( i IOCountersStat ) String ( ) string {
s , _ := json . Marshal ( i )
return string ( s )
}
2014-05-01 19:10:15 +09:00
func ( p NumCtxSwitchesStat ) String ( ) string {
s , _ := json . Marshal ( p )
return string ( s )
}
2024-01-11 00:45:21 +09:00
var enableBootTimeCache bool
// EnableBootTimeCache change cache behavior of BootTime. If true, cache BootTime value. Default is false.
func EnableBootTimeCache ( enable bool ) {
enableBootTimeCache = enable
}
2019-08-22 21:09:10 +02:00
// 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
}
2020-10-10 14:18:22 +03:00
// Processes returns a slice of pointers to Process structs for all
// currently running processes.
func Processes ( ) ( [ ] * Process , error ) {
return ProcessesWithContext ( context . Background ( ) )
}
2019-08-01 21:10:46 +02:00
// 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
// if the process does not exist.
func NewProcess ( pid int32 ) ( * Process , error ) {
2020-10-10 14:18:22 +03:00
return NewProcessWithContext ( context . Background ( ) , pid )
}
func NewProcessWithContext ( ctx context . Context , pid int32 ) ( * Process , error ) {
2020-10-21 14:52:41 +03:00
p := & Process {
2021-06-24 15:18:00 -07:00
Pid : pid ,
2020-10-21 14:52:41 +03:00
}
2019-08-01 21:10:46 +02:00
2020-10-10 14:18:22 +03:00
exists , err := PidExistsWithContext ( ctx , pid )
2019-08-01 21:10:46 +02:00
if err != nil {
return p , err
}
if ! exists {
return p , ErrorProcessNotRunning
}
2020-10-10 14:18:22 +03:00
p . CreateTimeWithContext ( ctx )
2019-08-01 21:10:46 +02:00
return p , nil
}
2014-04-30 15:32:05 +09:00
func PidExists ( pid int32 ) ( bool , error ) {
2017-12-31 15:25:49 +09:00
return PidExistsWithContext ( context . Background ( ) , pid )
}
2018-11-08 20:25:44 +01:00
// Background returns true if the process is in background, false otherwise.
func ( p * Process ) Background ( ) ( bool , error ) {
return p . BackgroundWithContext ( context . Background ( ) )
}
func ( p * Process ) BackgroundWithContext ( ctx context . Context ) ( bool , error ) {
fg , err := p . ForegroundWithContext ( ctx )
if err != nil {
return false , err
}
return ! fg , err
}
2015-03-04 00:02:09 +09:00
// If interval is 0, return difference from last call(non-blocking).
2022-01-30 22:48:09 +02:00
// If interval > 0, wait interval sec and return difference between start and end.
2016-03-22 23:09:12 +09:00
func ( p * Process ) Percent ( interval time . Duration ) ( float64 , error ) {
2017-12-31 15:25:49 +09:00
return p . PercentWithContext ( context . Background ( ) , interval )
}
func ( p * Process ) PercentWithContext ( ctx context . Context , interval time . Duration ) ( float64 , error ) {
2020-10-10 14:18:22 +03:00
cpuTimes , err := p . TimesWithContext ( ctx )
2015-03-04 00:02:09 +09:00
if err != nil {
return 0 , err
}
2016-01-19 21:36:45 -07:00
now := time . Now ( )
2015-03-04 00:02:09 +09:00
if interval > 0 {
p . lastCPUTimes = cpuTimes
2016-01-19 21:36:45 -07:00
p . lastCPUTime = now
2020-09-03 23:01:53 +03:00
if err := common . Sleep ( ctx , interval ) ; err != nil {
return 0 , err
}
2020-10-10 14:18:22 +03:00
cpuTimes , err = p . TimesWithContext ( ctx )
2016-03-09 10:09:27 +09:00
now = time . Now ( )
2015-03-04 00:02:09 +09:00
if err != nil {
return 0 , err
}
} else {
if p . lastCPUTimes == nil {
// invoked first time
2016-01-19 21:36:45 -07:00
p . lastCPUTimes = cpuTimes
p . lastCPUTime = now
2015-03-04 00:02:09 +09:00
return 0 , nil
}
}
2016-01-19 21:36:45 -07:00
numcpu := runtime . NumCPU ( )
delta := ( now . Sub ( p . lastCPUTime ) . Seconds ( ) ) * float64 ( numcpu )
ret := calculatePercent ( p . lastCPUTimes , cpuTimes , delta , numcpu )
2015-03-04 00:02:09 +09:00
p . lastCPUTimes = cpuTimes
2016-01-19 21:36:45 -07:00
p . lastCPUTime = now
2015-03-04 00:02:09 +09:00
return ret , nil
}
2016-01-19 21:36:45 -07:00
2019-09-06 13:36:22 +02:00
// IsRunning returns whether the process is still running or not.
func ( p * Process ) IsRunning ( ) ( bool , error ) {
return p . IsRunningWithContext ( context . Background ( ) )
}
func ( p * Process ) IsRunningWithContext ( ctx context . Context ) ( bool , error ) {
createTime , err := p . CreateTimeWithContext ( ctx )
if err != nil {
return false , err
}
2020-10-10 14:18:22 +03:00
p2 , err := NewProcessWithContext ( ctx , p . Pid )
2021-12-04 22:29:38 +01:00
if errors . Is ( err , ErrorProcessNotRunning ) {
2019-09-06 13:36:22 +02:00
return false , nil
}
createTime2 , err := p2 . CreateTimeWithContext ( ctx )
if err != nil {
return false , err
}
return createTime == createTime2 , nil
}
// CreateTime returns created time of the process in milliseconds since the epoch, in UTC.
func ( p * Process ) CreateTime ( ) ( int64 , error ) {
return p . CreateTimeWithContext ( context . Background ( ) )
}
func ( p * Process ) CreateTimeWithContext ( ctx context . Context ) ( int64 , error ) {
if p . createTime != 0 {
return p . createTime , nil
}
createTime , err := p . createTimeWithContext ( ctx )
p . createTime = createTime
return p . createTime , err
}
2016-03-22 23:09:12 +09:00
func calculatePercent ( t1 , t2 * cpu . TimesStat , delta float64 , numcpu int ) float64 {
2016-01-19 21:36:45 -07:00
if delta == 0 {
return 0
}
2024-08-24 20:46:32 +08:00
// https://github.com/giampaolo/psutil/blob/c034e6692cf736b5e87d14418a8153bb03f6cf42/psutil/__init__.py#L1064
delta_proc := ( t2 . User - t1 . User ) + ( t2 . System - t1 . System )
if delta_proc <= 0 {
return 0
}
2016-01-19 21:36:45 -07:00
overall_percent := ( ( delta_proc / delta ) * 100 ) * float64 ( numcpu )
2019-10-15 14:25:43 +02:00
return overall_percent
2016-01-19 21:36:45 -07:00
}
2016-02-16 19:15:41 +01:00
// MemoryPercent returns how many percent of the total RAM this process uses
func ( p * Process ) MemoryPercent ( ) ( float32 , error ) {
2017-12-31 15:25:49 +09:00
return p . MemoryPercentWithContext ( context . Background ( ) )
}
func ( p * Process ) MemoryPercentWithContext ( ctx context . Context ) ( float32 , error ) {
2020-10-10 14:18:22 +03:00
machineMemory , err := mem . VirtualMemoryWithContext ( ctx )
2016-02-16 19:15:41 +01:00
if err != nil {
return 0 , err
}
total := machineMemory . Total
2020-10-10 14:18:22 +03:00
processMemory , err := p . MemoryInfoWithContext ( ctx )
2016-02-16 19:15:41 +01:00
if err != nil {
return 0 , err
}
used := processMemory . RSS
2019-10-15 14:25:43 +02:00
return ( 100 * float32 ( used ) / float32 ( total ) ) , nil
2016-02-16 19:15:41 +01:00
}
2017-10-04 15:46:49 -04:00
2023-05-09 16:42:07 +08:00
// CPUPercent returns how many percent of the CPU time this process uses
2017-07-23 02:39:35 +05:30
func ( p * Process ) CPUPercent ( ) ( float64 , error ) {
2017-12-31 15:25:49 +09:00
return p . CPUPercentWithContext ( context . Background ( ) )
}
func ( p * Process ) CPUPercentWithContext ( ctx context . Context ) ( float64 , error ) {
2020-10-10 14:18:22 +03:00
crt_time , err := p . createTimeWithContext ( ctx )
2017-10-04 15:46:49 -04:00
if err != nil {
return 0 , err
}
2017-07-23 02:39:35 +05:30
2020-10-10 14:18:22 +03:00
cput , err := p . TimesWithContext ( ctx )
2017-10-04 15:46:49 -04:00
if err != nil {
return 0 , err
}
2017-07-23 02:39:35 +05:30
2017-10-04 15:46:49 -04:00
created := time . Unix ( 0 , crt_time * int64 ( time . Millisecond ) )
totalTime := time . Since ( created ) . Seconds ( )
if totalTime <= 0 {
return 0 , nil
}
2017-07-23 02:39:35 +05:30
2019-10-15 14:25:43 +02:00
return 100 * cput . Total ( ) / totalTime , nil
2017-07-23 02:39:35 +05:30
}
2019-10-24 18:40:27 +09:00
// Groups returns all group IDs(include supplementary groups) of the process as a slice of the int
2023-02-28 15:05:12 +03:00
func ( p * Process ) Groups ( ) ( [ ] uint32 , error ) {
2020-09-03 23:01:53 +03:00
return p . GroupsWithContext ( context . Background ( ) )
2019-10-24 18:40:27 +09:00
}
2020-10-10 14:18:22 +03:00
// Ppid returns Parent Process ID of the process.
func ( p * Process ) Ppid ( ) ( int32 , error ) {
return p . PpidWithContext ( context . Background ( ) )
}
// Name returns name of the process.
func ( p * Process ) Name ( ) ( string , error ) {
return p . NameWithContext ( context . Background ( ) )
}
// Exe returns executable path of the process.
func ( p * Process ) Exe ( ) ( string , error ) {
return p . ExeWithContext ( context . Background ( ) )
}
// 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 ( ) )
}
// 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 ( ) )
}
// Cwd returns current working directory of the process.
func ( p * Process ) Cwd ( ) ( string , error ) {
return p . CwdWithContext ( context . Background ( ) )
}
// Parent returns parent Process of the process.
func ( p * Process ) Parent ( ) ( * Process , error ) {
return p . ParentWithContext ( context . Background ( ) )
}
2022-01-22 18:27:52 +02:00
// 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 )
}
2020-10-10 14:18:22 +03:00
// 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.
2021-11-06 09:53:56 +00:00
func ( p * Process ) Status ( ) ( [ ] string , error ) {
2020-10-10 14:18:22 +03:00
return p . StatusWithContext ( context . Background ( ) )
}
// Foreground returns true if the process is in foreground, false otherwise.
func ( p * Process ) Foreground ( ) ( bool , error ) {
return p . ForegroundWithContext ( context . Background ( ) )
}
// Uids returns user ids of the process as a slice of the int
2024-02-17 12:39:18 +00:00
func ( p * Process ) Uids ( ) ( [ ] uint32 , error ) {
2020-10-10 14:18:22 +03:00
return p . UidsWithContext ( context . Background ( ) )
}
// Gids returns group ids of the process as a slice of the int
2024-02-17 12:39:18 +00:00
func ( p * Process ) Gids ( ) ( [ ] uint32 , error ) {
2020-10-10 14:18:22 +03:00
return p . GidsWithContext ( context . Background ( ) )
}
// Terminal returns a terminal which is associated with the process.
func ( p * Process ) Terminal ( ) ( string , error ) {
return p . TerminalWithContext ( context . Background ( ) )
}
// Nice returns a nice value (priority).
func ( p * Process ) Nice ( ) ( int32 , error ) {
return p . NiceWithContext ( context . Background ( ) )
}
// IOnice returns process I/O nice value (priority).
func ( p * Process ) IOnice ( ) ( int32 , error ) {
return p . IOniceWithContext ( context . Background ( ) )
}
// Rlimit returns Resource Limits.
func ( p * Process ) Rlimit ( ) ( [ ] RlimitStat , error ) {
return p . RlimitWithContext ( context . Background ( ) )
}
// 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 )
}
// IOCounters returns IO Counters.
func ( p * Process ) IOCounters ( ) ( * IOCountersStat , error ) {
return p . IOCountersWithContext ( context . Background ( ) )
}
// NumCtxSwitches returns the number of the context switches of the process.
func ( p * Process ) NumCtxSwitches ( ) ( * NumCtxSwitchesStat , error ) {
return p . NumCtxSwitchesWithContext ( context . Background ( ) )
}
// NumFDs returns the number of File Descriptors used by the process.
func ( p * Process ) NumFDs ( ) ( int32 , error ) {
return p . NumFDsWithContext ( context . Background ( ) )
}
// NumThreads returns the number of threads used by the process.
func ( p * Process ) NumThreads ( ) ( int32 , error ) {
return p . NumThreadsWithContext ( context . Background ( ) )
}
func ( p * Process ) Threads ( ) ( map [ int32 ] * cpu . TimesStat , error ) {
return p . ThreadsWithContext ( context . Background ( ) )
}
// Times returns CPU times of the process.
func ( p * Process ) Times ( ) ( * cpu . TimesStat , error ) {
return p . TimesWithContext ( context . Background ( ) )
}
// CPUAffinity returns CPU affinity of the process.
func ( p * Process ) CPUAffinity ( ) ( [ ] int32 , error ) {
return p . CPUAffinityWithContext ( context . Background ( ) )
}
// MemoryInfo returns generic process memory information,
2021-01-14 19:00:55 +09:00
// such as RSS and VMS.
2020-10-10 14:18:22 +03:00
func ( p * Process ) MemoryInfo ( ) ( * MemoryInfoStat , error ) {
return p . MemoryInfoWithContext ( context . Background ( ) )
}
// MemoryInfoEx returns platform-specific process memory information.
func ( p * Process ) MemoryInfoEx ( ) ( * MemoryInfoExStat , error ) {
return p . MemoryInfoExWithContext ( context . Background ( ) )
}
2023-05-09 16:42:07 +08:00
// PageFaults returns the process's page fault counters.
2020-10-10 14:18:22 +03:00
func ( p * Process ) PageFaults ( ) ( * PageFaultsStat , error ) {
return p . PageFaultsWithContext ( context . Background ( ) )
}
2021-02-04 18:03:52 +01:00
// Children returns the children of the process represented as a slice
// of pointers to Process type.
2020-10-10 14:18:22 +03:00
func ( p * Process ) Children ( ) ( [ ] * Process , error ) {
return p . ChildrenWithContext ( context . Background ( ) )
}
// 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 ( ) )
}
// Connections returns a slice of net.ConnectionStat used by the process.
// This returns all kind of the connection. This means TCP, UDP or UNIX.
func ( p * Process ) Connections ( ) ( [ ] net . ConnectionStat , error ) {
return p . ConnectionsWithContext ( context . Background ( ) )
}
2023-05-09 16:42:07 +08:00
// ConnectionsMax returns a slice of net.ConnectionStat used by the process at most `max`.
2024-08-17 22:41:29 +09:00
func ( p * Process ) ConnectionsMax ( maxConn int ) ( [ ] net . ConnectionStat , error ) {
return p . ConnectionsMaxWithContext ( context . Background ( ) , maxConn )
2020-10-10 14:18:22 +03:00
}
// MemoryMaps get memory maps from /proc/(pid)/smaps
func ( p * Process ) MemoryMaps ( grouped bool ) ( * [ ] MemoryMapsStat , error ) {
return p . MemoryMapsWithContext ( context . Background ( ) , grouped )
}
// Tgid returns thread group id of the process.
func ( p * Process ) Tgid ( ) ( int32 , error ) {
return p . TgidWithContext ( context . Background ( ) )
}
// SendSignal sends a unix.Signal to the process.
2021-11-06 09:53:56 +00:00
func ( p * Process ) SendSignal ( sig Signal ) error {
2020-10-10 14:18:22 +03:00
return p . SendSignalWithContext ( context . Background ( ) , sig )
}
// Suspend sends SIGSTOP to the process.
func ( p * Process ) Suspend ( ) error {
return p . SuspendWithContext ( context . Background ( ) )
}
// Resume sends SIGCONT to the process.
func ( p * Process ) Resume ( ) error {
return p . ResumeWithContext ( context . Background ( ) )
}
// Terminate sends SIGTERM to the process.
func ( p * Process ) Terminate ( ) error {
return p . TerminateWithContext ( context . Background ( ) )
}
// Kill sends SIGKILL to the process.
func ( p * Process ) Kill ( ) error {
return p . KillWithContext ( context . Background ( ) )
}
// Username returns a username of the process.
func ( p * Process ) Username ( ) ( string , error ) {
return p . UsernameWithContext ( context . Background ( ) )
}
2021-07-13 14:57:40 +02:00
// Environ returns the environment variables of the process.
func ( p * Process ) Environ ( ) ( [ ] string , error ) {
return p . EnvironWithContext ( context . Background ( ) )
}
2021-11-06 09:53:56 +00:00
2021-11-09 13:14:54 +00:00
// convertStatusChar as reported by the ps command across different platforms.
2021-11-06 09:53:56 +00:00
func convertStatusChar ( letter string ) string {
2021-11-09 13:14:54 +00:00
// Sources
// Darwin: http://www.mywebuniversity.com/Man_Pages/Darwin/man_ps.html
// FreeBSD: https://www.freebsd.org/cgi/man.cgi?ps
// Linux https://man7.org/linux/man-pages/man1/ps.1.html
// OpenBSD: https://man.openbsd.org/ps.1#state
// Solaris: https://github.com/collectd/collectd/blob/1da3305c10c8ff9a63081284cf3d4bb0f6daffd8/src/processes.c#L2115
2021-11-06 09:53:56 +00:00
switch letter {
2021-11-09 13:14:54 +00:00
case "A" :
return Daemon
case "D" , "U" :
return Blocked
case "E" :
return Detached
case "I" :
return Idle
case "L" :
return Lock
case "O" :
return Orphan
2021-11-06 09:53:56 +00:00
case "R" :
return Running
case "S" :
return Sleep
2021-11-09 13:14:54 +00:00
case "T" , "t" :
// "t" is used by Linux to signal stopped by the debugger during tracing
2021-11-06 09:53:56 +00:00
return Stop
case "W" :
return Wait
2021-11-09 13:14:54 +00:00
case "Y" :
return System
case "Z" :
return Zombie
2021-11-06 09:53:56 +00:00
default :
2021-11-09 13:14:54 +00:00
return UnknownState
2021-11-06 09:53:56 +00:00
}
}