Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"math"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -202,27 +203,27 @@ func elapsedTime(p *process.Process) (string, error) {
return fmtEtimeDuration(etime), nil
}

// pstree contains a mapping between the PPIDs and the child processes.
var pstree map[int][]goprocess.P

// displayProcessTree displays a tree of all the running Go processes.
func displayProcessTree() {
ps := goprocess.FindAll()
pstree = make(map[int][]goprocess.P)
sort.Slice(ps, func(i, j int) bool {
return ps[i].PPID < ps[j].PPID
})
pstree := make(map[int][]goprocess.P, len(ps))
for _, p := range ps {
pstree[p.PPID] = append(pstree[p.PPID], p)
}
tree := treeprint.New()
tree.SetValue("...")
seen := map[int]bool{}
for _, p := range ps {
constructProcessTree(p.PPID, p, seen, tree)
constructProcessTree(p.PPID, p, pstree, seen, tree)
}
fmt.Println(tree.String())
fmt.Print(tree.String())
}

// constructProcessTree constructs the process tree in a depth-first fashion.
func constructProcessTree(ppid int, process goprocess.P, seen map[int]bool, tree treeprint.Tree) {
func constructProcessTree(ppid int, process goprocess.P, pstree map[int][]goprocess.P, seen map[int]bool, tree treeprint.Tree) {
if seen[ppid] {
return
}
Expand All @@ -239,7 +240,7 @@ func constructProcessTree(ppid int, process goprocess.P, seen map[int]bool, tree
}
for index := range pstree[ppid] {
process := pstree[ppid][index]
constructProcessTree(process.PID, process, seen, tree)
constructProcessTree(process.PID, process, pstree, seen, tree)
}
}

Expand Down