Skip to content

Commit

Permalink
Move live/garbage stats update to gocore package.
Browse files Browse the repository at this point in the history
  • Loading branch information
randall77 committed Oct 1, 2017
1 parent 80e4e4d commit 5fce4c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
12 changes: 11 additions & 1 deletion gocore/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
func (p *Program) readObjects() {
ptrSize := p.proc.PtrSize()

// Number of objects in p.objects that have been scanned.
// number of objects in p.objects that have been scanned
n := 0
// total size of live objects
var live int64

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

// Goroutine roots
Expand Down Expand Up @@ -95,6 +98,13 @@ func (p *Program) readObjects() {
p.heapInfo[x.Addr.Add(j).Sub(p.arenaStart)/512].firstIdx = i
}
}

// Update stats to include the live/garbage distinction.
alloc := p.Stats().Child("heap").Child("in use spans").Child("alloc")
alloc.Children = []*Stats{
&Stats{"live", live, nil},
&Stats{"garbage", alloc.Size - live, nil},
}
}

// isPtr reports whether the inferior at address a contains a pointer.
Expand Down
18 changes: 6 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func main() {
fmt.Fprintf(os.Stderr, "%s: unknown command %s\n", os.Args[0], cmd)
fmt.Fprintf(os.Stderr, "Run 'corelib help' for usage.\n")
os.Exit(2)

case "overview":
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(t, "arch\t%s\n", p.Arch())
Expand All @@ -96,6 +97,7 @@ func main() {
}
fmt.Fprintf(t, "memory\t%.1f MB\n", float64(total)/(1<<20))
t.Flush()

case "mappings":
t := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
fmt.Fprintf(t, "min\tmax\tperm\tsource\toriginal\t\n")
Expand Down Expand Up @@ -125,6 +127,7 @@ func main() {
fmt.Fprintf(t, "\t\n")
}
t.Flush()

case "goroutines":

for _, g := range c.Goroutines() {
Expand All @@ -144,6 +147,7 @@ func main() {
fmt.Printf(" %016x %016x %s%s\n", f.Min(), f.Max(), f.Func().Name(), adj)
}
}

case "histogram":
// Produce an object histogram (bytes per type).
type bucket struct {
Expand Down Expand Up @@ -188,17 +192,6 @@ func main() {
t.Flush()

case "breakdown":
var total int64
c.ForEachObject(func(x *gocore.Object) bool {
total += x.Size
return true
})
alloc := c.Stats().Child("heap").Child("in use spans").Child("alloc")
alloc.Children = []*gocore.Stats{
&gocore.Stats{"live", total, nil},
&gocore.Stats{"garbage", alloc.Size - total, nil},
}

t := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', tabwriter.AlignRight)
all := c.Stats().Size
var printStat func(*gocore.Stats, string)
Expand All @@ -217,8 +210,8 @@ func main() {
}
printStat(c.Stats(), "")
t.Flush()
case "objgraph":

case "objgraph":
// Dump object graph to output file.
w, err := os.Create("tmp.dot")
if err != nil {
Expand Down Expand Up @@ -273,6 +266,7 @@ func main() {
})
fmt.Fprintf(w, "}")
w.Close()

case "objects":
c.ForEachObject(func(x *gocore.Object) bool {
fmt.Printf("%16x %s\n", x.Addr, typeName(x))
Expand Down

0 comments on commit 5fce4c6

Please sign in to comment.