Skip to content

Commit 17ca9e1

Browse files
committed
opt: using go-ps to implement pstree
1 parent a381477 commit 17ca9e1

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.14
44

55
require (
66
github.com/howeyc/fsnotify v0.9.0
7+
github.com/mitchellh/go-ps v1.0.0
78
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
8-
github.com/sbinet/pstree v0.3.0
99
github.com/silenceper/log v0.0.0-20171204144354-e5ac7fa8a76a
1010
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
1111
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1O
33
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
44
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
55
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
6+
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
7+
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
68
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
79
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
8-
github.com/sbinet/pstree v0.3.0 h1:XZLdGCbAg/wg8TzoW/060WtRoAQdECnsTrn+RYgyJoQ=
9-
github.com/sbinet/pstree v0.3.0/go.mod h1:G208WfJOi4oxq4++w97Y4AeuydVuoOz7tPKCEm8y1oE=
1010
github.com/silenceper/log v0.0.0-20171204144354-e5ac7fa8a76a h1:COf2KvPmardI1M8p2fhHsXlFS2EXSQygbGgcDYBI9Wc=
1111
github.com/silenceper/log v0.0.0-20171204144354-e5ac7fa8a76a/go.mod h1:nyN/YUSK3CgJjtNzm6dVTkcou+RYXNMP+XLSlzQu0m0=
1212
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=

gowatch.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/howeyc/fsnotify"
16-
"github.com/sbinet/pstree"
16+
"github.com/mitchellh/go-ps"
1717
"github.com/silenceper/log"
1818
)
1919

@@ -185,7 +185,7 @@ func Kill() {
185185
func killAllProcesses(pid int) (err error) {
186186
hasAllKilled := make(chan bool)
187187
go func() {
188-
pids, err := getAllProcesses(pid)
188+
pids, err := psTree(pid)
189189
if err != nil {
190190
log.Fatalf("getting all sub processes error: %v\n", err)
191191
return
@@ -232,28 +232,42 @@ func killProcess(pid int) (err error) {
232232
return
233233
}
234234

235-
func getAllProcesses(pid int) (pids []int, err error) {
236-
tree, err := pstree.New()
235+
// implement pstree based on the cross-platform ps utility in go, go-ps
236+
func psTree(rootPid int) (res []int, err error) {
237+
pidOfInterest := map[int]struct{}{rootPid: {}}
238+
pss, err := ps.Processes()
237239
if err != nil {
238-
log.Fatalf("error: %v\n", err)
240+
fmt.Println("ERROR: ", err)
239241
return
240242
}
241243

242-
traverseChildren(pid, tree, &pids)
243-
// we must kill the sub processes from smallest to largest
244-
sort.Ints(pids)
245-
return
246-
}
244+
// we must sort the ps by ppid && pid first, otherwise we probably will miss some sub-processes
245+
// of the root process during for-range searching
246+
sort.Slice(pss, func(i, j int) bool {
247+
ppidLess := pss[i].PPid() < pss[j].PPid()
248+
pidLess := pss[i].PPid() == pss[j].PPid() && pss[i].Pid() < pss[j].Pid()
249+
250+
return ppidLess || pidLess
251+
})
252+
253+
for _, ps := range pss {
254+
ppid := ps.PPid()
255+
if _, exists := pidOfInterest[ppid]; exists {
256+
pidOfInterest[ps.Pid()] = struct{}{}
257+
}
258+
}
247259

248-
func traverseChildren(pid int, tree *pstree.Tree, curPids *[]int) {
249-
for _, cid := range tree.Procs[pid].Children {
250-
*curPids = append(*curPids, cid)
251-
traverseChildren(cid, tree, curPids)
260+
for pid, _ := range pidOfInterest {
261+
if pid != rootPid {
262+
res = append(res, pid)
263+
}
252264
}
265+
266+
return
253267
}
254268

255269
func waitForProcess(pid int, hasAllKilled chan bool) (err error) {
256-
pids, _ := getAllProcesses(pid)
270+
pids, _ := psTree(pid)
257271
if len(pids) == 0 {
258272
hasAllKilled <- true
259273
return

0 commit comments

Comments
 (0)