|
|
|
@ -31,6 +31,7 @@ type Process struct {
|
|
|
|
|
numThreads int32
|
|
|
|
|
memInfo *MemoryInfoStat
|
|
|
|
|
sigInfo *SignalInfoStat
|
|
|
|
|
createTime int64
|
|
|
|
|
|
|
|
|
|
lastCPUTimes *cpu.TimesStat
|
|
|
|
|
lastCPUTime time.Time
|
|
|
|
@ -163,6 +164,7 @@ func NewProcess(pid int32) (*Process, error) {
|
|
|
|
|
if !exists {
|
|
|
|
|
return p, ErrorProcessNotRunning
|
|
|
|
|
}
|
|
|
|
|
go p.CreateTime()
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -222,6 +224,41 @@ func (p *Process) PercentWithContext(ctx context.Context, interval time.Duration
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
p2, err := NewProcess(p.Pid)
|
|
|
|
|
if err == ErrorProcessNotRunning {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64 {
|
|
|
|
|
if delta == 0 {
|
|
|
|
|
return 0
|
|
|
|
|