2017-02-01 16:36:24 +00:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-19 18:33:23 +00:00
|
|
|
"context"
|
2017-02-01 16:36:24 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2020-07-01 12:47:56 +00:00
|
|
|
"path/filepath"
|
2017-02-01 16:36:24 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/cpu"
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
"github.com/shirou/gopsutil/net"
|
2018-10-19 18:33:23 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 16:36:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// copied from sys/sysctl.h
|
|
|
|
const (
|
|
|
|
CTLKern = 1 // "high kernel": proc, limits
|
|
|
|
KernProc = 14 // struct: process entries
|
|
|
|
KernProcPID = 1 // by process id
|
|
|
|
KernProcProc = 8 // only return procs
|
|
|
|
KernProcAll = 0 // everything
|
|
|
|
KernProcPathname = 12 // path to executable
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ClockTicks = 100 // C.sysconf(C._SC_CLK_TCK)
|
|
|
|
)
|
|
|
|
|
|
|
|
type _Ctype_struct___0 struct {
|
|
|
|
Pad uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// MemoryInfoExStat is different between OSes
|
|
|
|
type MemoryInfoExStat struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoryMapsStat struct {
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func pidsWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
var ret []int32
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
pids, err := callPsWithContext(ctx, "pid", 0, false)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pid := range pids {
|
|
|
|
v, err := strconv.Atoi(pid[0])
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
ret = append(ret, int32(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Ppid() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.PpidWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "ppid", p.Pid, false)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
v, err := strconv.Atoi(r[0][0])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return int32(v), err
|
|
|
|
}
|
|
|
|
func (p *Process) Name() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NameWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
name := common.IntToString(k.Proc.P_comm[:])
|
|
|
|
|
|
|
|
if len(name) >= 15 {
|
|
|
|
cmdlineSlice, err := p.CmdlineSliceWithContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(cmdlineSlice) > 0 {
|
|
|
|
extendedName := filepath.Base(cmdlineSlice[0])
|
|
|
|
if strings.HasPrefix(extendedName, p.name) {
|
|
|
|
name = extendedName
|
|
|
|
} else {
|
|
|
|
name = cmdlineSlice[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
return name, nil
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
func (p *Process) Tgid() (int32, error) {
|
|
|
|
return 0, common.ErrNotImplementedError
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) Exe() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ExeWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
// 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) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CmdlineWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "command", p.Pid, false)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Join(r[0], " "), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CmdlineSlice returns the command line arguments of the process as a slice with each
|
|
|
|
// element being an argument. Because of current deficiencies in the way that the command
|
|
|
|
// line arguments are found, single arguments that have spaces in the will actually be
|
|
|
|
// reported as two separate items. In order to do something better CGO would be needed
|
|
|
|
// to use the native darwin functions.
|
|
|
|
func (p *Process) CmdlineSlice() ([]string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CmdlineSliceWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "command", p.Pid, false)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r[0], err
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
r, err := callPsWithContext(ctx, "etime", p.Pid, false)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsedSegments := strings.Split(strings.Replace(r[0][0], "-", ":", 1), ":")
|
|
|
|
var elapsedDurations []time.Duration
|
|
|
|
for i := len(elapsedSegments) - 1; i >= 0; i-- {
|
|
|
|
p, err := strconv.ParseInt(elapsedSegments[i], 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
elapsedDurations = append(elapsedDurations, time.Duration(p))
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
var elapsed = time.Duration(elapsedDurations[0]) * time.Second
|
2017-02-01 16:36:24 +00:00
|
|
|
if len(elapsedDurations) > 1 {
|
|
|
|
elapsed += time.Duration(elapsedDurations[1]) * time.Minute
|
|
|
|
}
|
|
|
|
if len(elapsedDurations) > 2 {
|
|
|
|
elapsed += time.Duration(elapsedDurations[2]) * time.Hour
|
|
|
|
}
|
|
|
|
if len(elapsedDurations) > 3 {
|
|
|
|
elapsed += time.Duration(elapsedDurations[3]) * time.Hour * 24
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now().Add(-elapsed)
|
|
|
|
return start.Unix() * 1000, nil
|
|
|
|
}
|
|
|
|
func (p *Process) Cwd() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CwdWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) Parent() (*Process, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ParentWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
|
|
|
rr, err := common.CallLsofWithContext(ctx, invoke, p.Pid, "-FR")
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, r := range rr {
|
|
|
|
if strings.HasPrefix(r, "p") { // skip if process
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l := string(r)
|
|
|
|
v, err := strconv.Atoi(strings.Replace(l, "R", "", 1))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewProcess(int32(v))
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("could not find parent line")
|
|
|
|
}
|
|
|
|
func (p *Process) Status() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.StatusWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "state", p.Pid, false)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
return r[0][0][0:1], err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
ps, err := exec.LookPath("ps")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
out, err := invoke.CommandWithContext(ctx, ps, "-o", "stat=", "-p", strconv.Itoa(int(pid)))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return strings.IndexByte(string(out), '+') != -1, nil
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
2020-07-01 12:47:56 +00:00
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) Uids() ([]int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.UidsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) UidsWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// See: http://unix.superglobalmegacorp.com/Net2/newsrc/sys/ucred.h.html
|
|
|
|
userEffectiveUID := int32(k.Eproc.Ucred.UID)
|
|
|
|
|
|
|
|
return []int32{userEffectiveUID}, nil
|
|
|
|
}
|
|
|
|
func (p *Process) Gids() ([]int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.GidsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) GidsWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gids := make([]int32, 0, 3)
|
|
|
|
gids = append(gids, int32(k.Eproc.Pcred.P_rgid), int32(k.Eproc.Ucred.Ngroups), int32(k.Eproc.Pcred.P_svgid))
|
|
|
|
|
|
|
|
return gids, nil
|
|
|
|
}
|
|
|
|
func (p *Process) Terminal() (string, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.TerminalWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return "", common.ErrNotImplementedError
|
|
|
|
/*
|
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ttyNr := uint64(k.Eproc.Tdev)
|
|
|
|
termmap, err := getTerminalMap()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return termmap[ttyNr], nil
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
func (p *Process) Nice() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NiceWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NiceWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
k, err := p.getKProc()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int32(k.Proc.P_nice), nil
|
|
|
|
}
|
|
|
|
func (p *Process) IOnice() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.IOniceWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) IOniceWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return 0, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) Rlimit() ([]RlimitStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.RlimitWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) RlimitWithContext(ctx context.Context) ([]RlimitStat, error) {
|
|
|
|
var rlimit []RlimitStat
|
|
|
|
return rlimit, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) RlimitUsage(gatherUsed bool) ([]RlimitStat, error) {
|
|
|
|
return p.RlimitUsageWithContext(context.Background(), gatherUsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) RlimitUsageWithContext(ctx context.Context, gatherUsed bool) ([]RlimitStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
var rlimit []RlimitStat
|
|
|
|
return rlimit, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) IOCounters() (*IOCountersStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.IOCountersWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) IOCountersWithContext(ctx context.Context) (*IOCountersStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) NumCtxSwitches() (*NumCtxSwitchesStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NumCtxSwitchesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitchesStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) NumFDs() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NumFDsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return 0, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) NumThreads() (int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NumThreadsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "utime,stime", p.Pid, true)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int32(len(r)), nil
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
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)
|
2017-02-01 16:36:24 +00:00
|
|
|
return ret, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertCPUTimes(s string) (ret float64, err error) {
|
|
|
|
var t int
|
|
|
|
var _tmp string
|
|
|
|
if strings.Contains(s, ":") {
|
|
|
|
_t := strings.Split(s, ":")
|
2020-07-01 12:47:56 +00:00
|
|
|
switch len(_t) {
|
|
|
|
case 3:
|
|
|
|
hour, err := strconv.Atoi(_t[0])
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
t += hour * 60 * 60 * ClockTicks
|
|
|
|
|
|
|
|
mins, err := strconv.Atoi(_t[1])
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
t += mins * 60 * ClockTicks
|
|
|
|
_tmp = _t[2]
|
|
|
|
case 2:
|
|
|
|
mins, err := strconv.Atoi(_t[0])
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
t += mins * 60 * ClockTicks
|
|
|
|
_tmp = _t[1]
|
|
|
|
case 1, 0:
|
|
|
|
_tmp = s
|
|
|
|
default:
|
|
|
|
return ret, fmt.Errorf("wrong cpu time string")
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_tmp = s
|
|
|
|
}
|
|
|
|
|
|
|
|
_t := strings.Split(_tmp, ".")
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
h, err := strconv.Atoi(_t[0])
|
2020-07-01 12:47:56 +00:00
|
|
|
t += h * ClockTicks
|
2017-02-01 16:36:24 +00:00
|
|
|
h, err = strconv.Atoi(_t[1])
|
|
|
|
t += h
|
|
|
|
return float64(t) / ClockTicks, nil
|
|
|
|
}
|
|
|
|
func (p *Process) Times() (*cpu.TimesStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.TimesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) TimesWithContext(ctx context.Context) (*cpu.TimesStat, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "utime,stime", p.Pid, false)
|
2017-02-01 16:36:24 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
utime, err := convertCPUTimes(r[0][0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stime, err := convertCPUTimes(r[0][1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &cpu.TimesStat{
|
|
|
|
CPU: "cpu",
|
|
|
|
User: utime,
|
|
|
|
System: stime,
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
func (p *Process) CPUAffinity() ([]int32, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.CPUAffinityWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) CPUAffinityWithContext(ctx context.Context) ([]int32, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
func (p *Process) MemoryInfo() (*MemoryInfoStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.MemoryInfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryInfoWithContext(ctx context.Context) (*MemoryInfoStat, error) {
|
|
|
|
r, err := callPsWithContext(ctx, "rss,vsize,pagein", p.Pid, false)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rss, err := strconv.Atoi(r[0][0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vms, err := strconv.Atoi(r[0][1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pagein, err := strconv.Atoi(r[0][2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &MemoryInfoStat{
|
|
|
|
RSS: uint64(rss) * 1024,
|
|
|
|
VMS: uint64(vms) * 1024,
|
|
|
|
Swap: uint64(pagein),
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
func (p *Process) MemoryInfoEx() (*MemoryInfoExStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.MemoryInfoExWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryInfoExWithContext(ctx context.Context) (*MemoryInfoExStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
func (p *Process) PageFaults() (*PageFaultsStat, error) {
|
|
|
|
return p.PageFaultsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) PageFaultsWithContext(ctx context.Context) (*PageFaultsStat, error) {
|
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) Children() ([]*Process, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ChildrenWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
|
|
|
|
pids, err := common.CallPgrepWithContext(ctx, invoke, p.Pid)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := make([]*Process, 0, len(pids))
|
|
|
|
for _, pid := range pids {
|
|
|
|
np, err := NewProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret = append(ret, np)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) OpenFiles() ([]OpenFilesStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.OpenFilesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) OpenFilesWithContext(ctx context.Context) ([]OpenFilesStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Connections() ([]net.ConnectionStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.ConnectionsWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ConnectionsWithContext(ctx context.Context) ([]net.ConnectionStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return net.ConnectionsPid("all", p.Pid)
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:47:56 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) ConnectionsMaxWithContext(ctx context.Context, max int) ([]net.ConnectionStat, error) {
|
|
|
|
return net.ConnectionsPidMax("all", p.Pid, max)
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:36:24 +00:00
|
|
|
func (p *Process) NetIOCounters(pernic bool) ([]net.IOCountersStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.NetIOCountersWithContext(context.Background(), pernic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) NetIOCountersWithContext(ctx context.Context, pernic bool) ([]net.IOCountersStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.MemoryMapsWithContext(context.Background(), grouped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) MemoryMapsWithContext(ctx context.Context, grouped bool) (*[]MemoryMapsStat, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
var ret []MemoryMapsStat
|
|
|
|
return &ret, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func Processes() ([]*Process, error) {
|
|
|
|
return ProcessesWithContext(context.Background())
|
|
|
|
}
|
2017-02-01 16:36:24 +00:00
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
|
|
|
|
out := []*Process{}
|
|
|
|
|
|
|
|
pids, err := PidsWithContext(ctx)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
2018-10-19 18:33:23 +00:00
|
|
|
return out, err
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
for _, pid := range pids {
|
|
|
|
p, err := NewProcess(pid)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
out = append(out, p)
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 18:33:23 +00:00
|
|
|
return out, nil
|
2017-02-01 16:36:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseKinfoProc(buf []byte) (KinfoProc, error) {
|
|
|
|
var k KinfoProc
|
|
|
|
br := bytes.NewReader(buf)
|
|
|
|
|
|
|
|
err := common.Read(br, binary.LittleEndian, &k)
|
|
|
|
if err != nil {
|
|
|
|
return k, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a proc as defined here:
|
|
|
|
// http://unix.superglobalmegacorp.com/Net2/newsrc/sys/kinfo_proc.h.html
|
|
|
|
func (p *Process) getKProc() (*KinfoProc, error) {
|
2018-10-19 18:33:23 +00:00
|
|
|
return p.getKProcWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) getKProcWithContext(ctx context.Context) (*KinfoProc, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
mib := []int32{CTLKern, KernProc, KernProcPID, p.Pid}
|
|
|
|
procK := KinfoProc{}
|
|
|
|
length := uint64(unsafe.Sizeof(procK))
|
|
|
|
buf := make([]byte, length)
|
2018-10-19 18:33:23 +00:00
|
|
|
_, _, syserr := unix.Syscall6(
|
|
|
|
unix.SYS___SYSCTL,
|
2017-02-01 16:36:24 +00:00
|
|
|
uintptr(unsafe.Pointer(&mib[0])),
|
|
|
|
uintptr(len(mib)),
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
|
|
uintptr(unsafe.Pointer(&length)),
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
if syserr != 0 {
|
|
|
|
return nil, syserr
|
|
|
|
}
|
|
|
|
k, err := parseKinfoProc(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &k, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// call ps command.
|
|
|
|
// Return value deletes Header line(you must not input wrong arg).
|
|
|
|
// And splited by Space. Caller have responsibility to manage.
|
|
|
|
// If passed arg pid is 0, get information from all process.
|
2018-10-19 18:33:23 +00:00
|
|
|
func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption bool) ([][]string, error) {
|
2017-02-01 16:36:24 +00:00
|
|
|
bin, err := exec.LookPath("ps")
|
|
|
|
if err != nil {
|
|
|
|
return [][]string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmd []string
|
|
|
|
if pid == 0 { // will get from all processes.
|
|
|
|
cmd = []string{"-ax", "-o", arg}
|
|
|
|
} else if threadOption {
|
|
|
|
cmd = []string{"-x", "-o", arg, "-M", "-p", strconv.Itoa(int(pid))}
|
|
|
|
} else {
|
|
|
|
cmd = []string{"-x", "-o", arg, "-p", strconv.Itoa(int(pid))}
|
|
|
|
}
|
2018-10-19 18:33:23 +00:00
|
|
|
out, err := invoke.CommandWithContext(ctx, bin, cmd...)
|
2017-02-01 16:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return [][]string{}, err
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
|
|
|
|
|
|
var ret [][]string
|
|
|
|
for _, l := range lines[1:] {
|
|
|
|
var lr []string
|
|
|
|
for _, r := range strings.Split(l, " ") {
|
|
|
|
if r == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lr = append(lr, strings.TrimSpace(r))
|
|
|
|
}
|
|
|
|
if len(lr) != 0 {
|
|
|
|
ret = append(ret, lr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|