Skip to content

Commit

Permalink
fix: actually measure memory usage recursively
Browse files Browse the repository at this point in the history
I left in a memory-usage function that wasn't recursive.

v0.9.1
  • Loading branch information
dbohdan committed Dec 21, 2024
1 parent 4a029f4 commit 1b40ee9
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 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.0"
version = "0.9.1"
)

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

func getMemoryUsage(proc *process.Process) (int64, error) {
children, err := proc.Children()
if err != nil && err != process.ErrorNoChildren {
return 0, err
}
procs := []*process.Process{proc}
var total int64

var total uint64
for _, child := range children {
mem, err := child.MemoryInfo()
for len(procs) > 0 {
current := procs[0]
procs = procs[1:]

// If we can't get memory info for a child, skip it.
if err == nil && mem != nil && mem.RSS > 0 {
total += mem.RSS
if current == nil {
continue
}
}

memInfo, err := proc.MemoryInfo()
if err != nil {
return 0, fmt.Errorf("failed to get process memory info: %w", err)
}
if memInfo == nil {
return 0, fmt.Errorf("no memory info available")
children, err := current.Children()
if err != nil && err != process.ErrorNoChildren {
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 += memInfo.RSS
return int64(total), nil
return total, nil
}

func summarize(values []int64, maximum int64, start, end time.Time, memFormat, timeFormat string) string {
Expand Down

0 comments on commit 1b40ee9

Please sign in to comment.