@@ -290,11 +290,23 @@ func (p *Process) NumThreads() (int32, error) {
290
290
return p .numThreads , nil
291
291
}
292
292
293
- // Threads returns a map of threads
294
- //
295
- // Notice: Not implemented yet. always returns empty map.
296
- func (p * Process ) Threads () (map [string ]string , error ) {
297
- ret := make (map [string ]string , 0 )
293
+ func (p * Process ) Threads () (map [int32 ]* cpu.TimesStat , error ) {
294
+ ret := make (map [int32 ]* cpu.TimesStat )
295
+ taskPath := common .HostProc (strconv .Itoa (int (p .Pid )), "task" )
296
+
297
+ tids , err := readPidsFromDir (taskPath )
298
+ if err != nil {
299
+ return nil , err
300
+ }
301
+
302
+ for _ , tid := range tids {
303
+ _ , _ , cpuTimes , _ , _ , _ , err := p .fillFromTIDStat (tid )
304
+ if err != nil {
305
+ return nil , err
306
+ }
307
+ ret [tid ] = cpuTimes
308
+ }
309
+
298
310
return ret , nil
299
311
}
300
312
@@ -922,9 +934,16 @@ func (p *Process) fillFromStatus() error {
922
934
return nil
923
935
}
924
936
925
- func (p * Process ) fillFromStat ( ) (string , int32 , * cpu.TimesStat , int64 , uint32 , int32 , error ) {
937
+ func (p * Process ) fillFromTIDStat ( tid int32 ) (string , int32 , * cpu.TimesStat , int64 , uint32 , int32 , error ) {
926
938
pid := p .Pid
927
- statPath := common .HostProc (strconv .Itoa (int (pid )), "stat" )
939
+ var statPath string
940
+
941
+ if tid == - 1 {
942
+ statPath = common .HostProc (strconv .Itoa (int (pid )), "stat" )
943
+ } else {
944
+ statPath = common .HostProc (strconv .Itoa (int (pid )), "task" , strconv .Itoa (int (tid )), "stat" )
945
+ }
946
+
928
947
contents , err := ioutil .ReadFile (statPath )
929
948
if err != nil {
930
949
return "" , 0 , nil , 0 , 0 , 0 , err
@@ -989,11 +1008,19 @@ func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32,
989
1008
return terminal , int32 (ppid ), cpuTimes , createTime , uint32 (rtpriority ), nice , nil
990
1009
}
991
1010
1011
+ func (p * Process ) fillFromStat () (string , int32 , * cpu.TimesStat , int64 , uint32 , int32 , error ) {
1012
+ return p .fillFromTIDStat (- 1 )
1013
+ }
1014
+
992
1015
// Pids returns a slice of process ID list which are running now.
993
1016
func Pids () ([]int32 , error ) {
1017
+ return readPidsFromDir (common .HostProc ())
1018
+ }
1019
+
1020
+ func readPidsFromDir (path string ) ([]int32 , error ) {
994
1021
var ret []int32
995
1022
996
- d , err := os .Open (common . HostProc () )
1023
+ d , err := os .Open (path )
997
1024
if err != nil {
998
1025
return nil , err
999
1026
}
0 commit comments