Skip to content

Commit

Permalink
fix(getMemoryUsage): count RSS of first process
Browse files Browse the repository at this point in the history
Exit with an error
when we fail to get the memory info of a descendant process.
We currently ignore the error.

v0.9.2
  • Loading branch information
dbohdan committed Dec 26, 2024
1 parent 92bcfd1 commit c374a72
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const (
defaultWait = -1
sparklineLowMaximum = 10000
usageDivisor = 1 << 20 // Report memory usage in binary megabytes.
version = "0.9.1"
version = "0.9.2"
)

var sparklineTicks = []rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
Expand Down Expand Up @@ -536,33 +536,30 @@ func getOutput(path string) (*os.File, error) {
}

func getMemoryUsage(proc *process.Process) (int64, error) {
procs := []*process.Process{proc}
queue := []*process.Process{proc}
var total int64

for len(procs) > 0 {
current := procs[0]
procs = procs[1:]
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]

if current == nil {
continue
}

children, err := current.Children()
if err != nil && err != process.ErrorNoChildren {
mem, err := current.MemoryInfo()
if err != nil {
return 0, err
}
procs = append(procs, children...)

for _, child := range children {
mem, err := child.MemoryInfo()

// If we can't get memory info for a child, skip it.
if err != nil || mem == nil {
continue
}
total += int64(mem.RSS)

total += int64(mem.RSS)
children, err := current.Children()
if err != nil && err != process.ErrorNoChildren {
return 0, err
}

queue = append(queue, children...)
}

return total, nil
Expand Down

0 comments on commit c374a72

Please sign in to comment.