Skip to content

Commit 5fce4c6

Browse files
committed
Move live/garbage stats update to gocore package.
1 parent 80e4e4d commit 5fce4c6

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

gocore/object.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import (
1111
func (p *Program) readObjects() {
1212
ptrSize := p.proc.PtrSize()
1313

14-
// Number of objects in p.objects that have been scanned.
14+
// number of objects in p.objects that have been scanned
1515
n := 0
16+
// total size of live objects
17+
var live int64
1618

1719
// Function to call when we find a new pointer.
1820
add := func(x core.Address) {
@@ -37,6 +39,7 @@ func (p *Program) readObjects() {
3739
}
3840
s.mark |= uint64(1) << b
3941
p.objects = append(p.objects, Object{Addr: x, Size: s.size})
42+
live += s.size
4043
}
4144

4245
// Goroutine roots
@@ -95,6 +98,13 @@ func (p *Program) readObjects() {
9598
p.heapInfo[x.Addr.Add(j).Sub(p.arenaStart)/512].firstIdx = i
9699
}
97100
}
101+
102+
// Update stats to include the live/garbage distinction.
103+
alloc := p.Stats().Child("heap").Child("in use spans").Child("alloc")
104+
alloc.Children = []*Stats{
105+
&Stats{"live", live, nil},
106+
&Stats{"garbage", alloc.Size - live, nil},
107+
}
98108
}
99109

100110
// isPtr reports whether the inferior at address a contains a pointer.

main.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func main() {
8686
fmt.Fprintf(os.Stderr, "%s: unknown command %s\n", os.Args[0], cmd)
8787
fmt.Fprintf(os.Stderr, "Run 'corelib help' for usage.\n")
8888
os.Exit(2)
89+
8990
case "overview":
9091
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
9192
fmt.Fprintf(t, "arch\t%s\n", p.Arch())
@@ -96,6 +97,7 @@ func main() {
9697
}
9798
fmt.Fprintf(t, "memory\t%.1f MB\n", float64(total)/(1<<20))
9899
t.Flush()
100+
99101
case "mappings":
100102
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
101103
fmt.Fprintf(t, "min\tmax\tperm\tsource\toriginal\t\n")
@@ -125,6 +127,7 @@ func main() {
125127
fmt.Fprintf(t, "\t\n")
126128
}
127129
t.Flush()
130+
128131
case "goroutines":
129132

130133
for _, g := range c.Goroutines() {
@@ -144,6 +147,7 @@ func main() {
144147
fmt.Printf(" %016x %016x %s%s\n", f.Min(), f.Max(), f.Func().Name(), adj)
145148
}
146149
}
150+
147151
case "histogram":
148152
// Produce an object histogram (bytes per type).
149153
type bucket struct {
@@ -188,17 +192,6 @@ func main() {
188192
t.Flush()
189193

190194
case "breakdown":
191-
var total int64
192-
c.ForEachObject(func(x *gocore.Object) bool {
193-
total += x.Size
194-
return true
195-
})
196-
alloc := c.Stats().Child("heap").Child("in use spans").Child("alloc")
197-
alloc.Children = []*gocore.Stats{
198-
&gocore.Stats{"live", total, nil},
199-
&gocore.Stats{"garbage", alloc.Size - total, nil},
200-
}
201-
202195
t := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', tabwriter.AlignRight)
203196
all := c.Stats().Size
204197
var printStat func(*gocore.Stats, string)
@@ -217,8 +210,8 @@ func main() {
217210
}
218211
printStat(c.Stats(), "")
219212
t.Flush()
220-
case "objgraph":
221213

214+
case "objgraph":
222215
// Dump object graph to output file.
223216
w, err := os.Create("tmp.dot")
224217
if err != nil {
@@ -273,6 +266,7 @@ func main() {
273266
})
274267
fmt.Fprintf(w, "}")
275268
w.Close()
269+
276270
case "objects":
277271
c.ForEachObject(func(x *gocore.Object) bool {
278272
fmt.Printf("%16x %s\n", x.Addr, typeName(x))

0 commit comments

Comments
 (0)