1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-29 13:49:21 +08:00

Merge pull request #428 from Leonid99/threads

Implement Threads() in Linux
This commit is contained in:
shirou 2017-10-05 08:03:32 +09:00 committed by GitHub
commit fa0e9fd921
7 changed files with 64 additions and 17 deletions

View File

@ -268,8 +268,8 @@ func (p *Process) NumThreads() (int32, error) {
} }
return int32(len(r)), nil return int32(len(r)), nil
} }
func (p *Process) Threads() (map[string]string, error) { func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
ret := make(map[string]string, 0) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }

View File

@ -95,7 +95,7 @@ func (p *Process) NumFDs() (int32, error) {
func (p *Process) NumThreads() (int32, error) { func (p *Process) NumThreads() (int32, error) {
return 0, common.ErrNotImplementedError return 0, common.ErrNotImplementedError
} }
func (p *Process) Threads() (map[string]string, error) { func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
return nil, common.ErrNotImplementedError return nil, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {

View File

@ -204,8 +204,8 @@ func (p *Process) NumThreads() (int32, error) {
return k.Numthreads, nil return k.Numthreads, nil
} }
func (p *Process) Threads() (map[string]string, error) { func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
ret := make(map[string]string, 0) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {

View File

@ -290,11 +290,23 @@ func (p *Process) NumThreads() (int32, error) {
return p.numThreads, nil return p.numThreads, nil
} }
// Threads returns a map of threads func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
// ret := make(map[int32]*cpu.TimesStat)
// Notice: Not implemented yet. always returns empty map. taskPath := common.HostProc(strconv.Itoa(int(p.Pid)), "task")
func (p *Process) Threads() (map[string]string, error) {
ret := make(map[string]string, 0) tids, err := readPidsFromDir(taskPath)
if err != nil {
return nil, err
}
for _, tid := range tids {
_, _, cpuTimes, _, _, _, err := p.fillFromTIDStat(tid)
if err != nil {
return nil, err
}
ret[tid] = cpuTimes
}
return ret, nil return ret, nil
} }
@ -922,9 +934,16 @@ func (p *Process) fillFromStatus() error {
return nil return nil
} }
func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromTIDStat(tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) {
pid := p.Pid pid := p.Pid
statPath := common.HostProc(strconv.Itoa(int(pid)), "stat") var statPath string
if tid == -1 {
statPath = common.HostProc(strconv.Itoa(int(pid)), "stat")
} else {
statPath = common.HostProc(strconv.Itoa(int(pid)), "task", strconv.Itoa(int(tid)), "stat")
}
contents, err := ioutil.ReadFile(statPath) contents, err := ioutil.ReadFile(statPath)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return "", 0, nil, 0, 0, 0, err
@ -989,11 +1008,19 @@ func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32,
return terminal, int32(ppid), cpuTimes, createTime, uint32(rtpriority), nice, nil return terminal, int32(ppid), cpuTimes, createTime, uint32(rtpriority), nice, nil
} }
func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromTIDStat(-1)
}
// Pids returns a slice of process ID list which are running now. // Pids returns a slice of process ID list which are running now.
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
return readPidsFromDir(common.HostProc())
}
func readPidsFromDir(path string) ([]int32, error) {
var ret []int32 var ret []int32
d, err := os.Open(common.HostProc()) d, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -194,8 +194,8 @@ func (p *Process) NumThreads() (int32, error) {
/* not supported, just return 1 */ /* not supported, just return 1 */
return 1, nil return 1, nil
} }
func (p *Process) Threads() (map[string]string, error) { func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
ret := make(map[string]string, 0) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {

View File

@ -219,6 +219,26 @@ func Test_Process_NumThread(t *testing.T) {
} }
} }
func Test_Process_Threads(t *testing.T) {
p := testGetProcess()
n, err := p.NumThreads()
if err != nil {
t.Errorf("geting NumThread error %v", err)
}
if n < 0 {
t.Errorf("invalid NumThread: %d", n)
}
ts, err := p.Threads()
if err != nil {
t.Errorf("geting Threads error %v", err)
}
if len(ts) != int(n) {
t.Errorf("unexpected number of threads: %v vs %v", len(ts), n)
}
}
func Test_Process_Name(t *testing.T) { func Test_Process_Name(t *testing.T) {
p := testGetProcess() p := testGetProcess()

View File

@ -273,8 +273,8 @@ func (p *Process) NumThreads() (int32, error) {
} }
return int32(dst[0].ThreadCount), nil return int32(dst[0].ThreadCount), nil
} }
func (p *Process) Threads() (map[string]string, error) { func (p *Process) Threads() (map[int32]*cpu.TimesStat, error) {
ret := make(map[string]string, 0) ret := make(map[int32]*cpu.TimesStat)
return ret, common.ErrNotImplementedError return ret, common.ErrNotImplementedError
} }
func (p *Process) Times() (*cpu.TimesStat, error) { func (p *Process) Times() (*cpu.TimesStat, error) {