From 7ad3836ad39d22dec7213bb5f4bb7e4377b3357e Mon Sep 17 00:00:00 2001 From: Leonid Podolny Date: Fri, 29 Sep 2017 15:34:43 -0400 Subject: [PATCH] Implement Threads() for Linux --- process/process_darwin.go | 4 ++-- process/process_fallback.go | 2 +- process/process_freebsd.go | 4 ++-- process/process_linux.go | 12 ++++++++---- process/process_openbsd.go | 4 ++-- process/process_windows.go | 4 ++-- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/process/process_darwin.go b/process/process_darwin.go index 7de9add..1a9ed94 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -268,8 +268,8 @@ func (p *Process) NumThreads() (int32, error) { } return int32(len(r)), nil } -func (p *Process) Threads() (map[string]string, error) { - ret := make(map[string]string, 0) +func (p *Process) Threads() ([]int32, error) { + ret := make([]int32, 0) return ret, common.ErrNotImplementedError } diff --git a/process/process_fallback.go b/process/process_fallback.go index bf09875..7c7d71d 100644 --- a/process/process_fallback.go +++ b/process/process_fallback.go @@ -95,7 +95,7 @@ func (p *Process) NumFDs() (int32, error) { func (p *Process) NumThreads() (int32, error) { return 0, common.ErrNotImplementedError } -func (p *Process) Threads() (map[string]string, error) { +func (p *Process) Threads() ([]int32, error) { return nil, common.ErrNotImplementedError } func (p *Process) Times() (*cpu.TimesStat, error) { diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 4f40e12..a6b978d 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -204,8 +204,8 @@ func (p *Process) NumThreads() (int32, error) { return k.Numthreads, nil } -func (p *Process) Threads() (map[string]string, error) { - ret := make(map[string]string, 0) +func (p *Process) Threads() ([]int32, error) { + ret := make([]int32, 0) return ret, common.ErrNotImplementedError } func (p *Process) Times() (*cpu.TimesStat, error) { diff --git a/process/process_linux.go b/process/process_linux.go index 65cc41b..dcf4d7a 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -293,9 +293,9 @@ func (p *Process) NumThreads() (int32, error) { // Threads returns a map of threads // // Notice: Not implemented yet. always returns empty map. -func (p *Process) Threads() (map[string]string, error) { - ret := make(map[string]string, 0) - return ret, nil +func (p *Process) Threads() ([]int32, error) { + taskPath := common.HostProc(strconv.Itoa(int(p.Pid)), "task") + return readPidsFromDir(taskPath) } // Times returns CPU times of the process. @@ -991,9 +991,13 @@ func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32, // Pids returns a slice of process ID list which are running now. func Pids() ([]int32, error) { + return readPidsFromDir(common.HostProc()) +} + +func readPidsFromDir(path string) ([]int32, error) { var ret []int32 - d, err := os.Open(common.HostProc()) + d, err := os.Open(path) if err != nil { return nil, err } diff --git a/process/process_openbsd.go b/process/process_openbsd.go index 3ca3857..b8365d5 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -194,8 +194,8 @@ func (p *Process) NumThreads() (int32, error) { /* not supported, just return 1 */ return 1, nil } -func (p *Process) Threads() (map[string]string, error) { - ret := make(map[string]string, 0) +func (p *Process) Threads() ([]int32, error) { + ret := make([]int32, 0) return ret, common.ErrNotImplementedError } func (p *Process) Times() (*cpu.TimesStat, error) { diff --git a/process/process_windows.go b/process/process_windows.go index d94dcbc..ff55274 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -273,8 +273,8 @@ func (p *Process) NumThreads() (int32, error) { } return int32(dst[0].ThreadCount), nil } -func (p *Process) Threads() (map[string]string, error) { - ret := make(map[string]string, 0) +func (p *Process) Threads() ([]int32, error) { + ret := make([]int32, 0) return ret, common.ErrNotImplementedError } func (p *Process) Times() (*cpu.TimesStat, error) {