mirror of
https://github.com/jimeh/rbheap.git
synced 2026-02-19 04:46:40 +00:00
Break core structs down into obj and leak packages
This commit is contained in:
104
obj/dump.go
Normal file
104
obj/dump.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package obj
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func NewDump(file string) *Dump {
|
||||
return &Dump{File: file}
|
||||
}
|
||||
|
||||
// Dump contains all relevant data for a single heap dump.
|
||||
type Dump struct {
|
||||
File string
|
||||
Index []*string
|
||||
Entries map[string]*Entry
|
||||
}
|
||||
|
||||
// Process processes the heap dump
|
||||
func (s *Dump) Process() error {
|
||||
file, err := os.Open(s.File)
|
||||
defer file.Close()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.Entries = map[string]*Entry{}
|
||||
|
||||
var offset int64 = -1
|
||||
reader := bufio.NewReader(file)
|
||||
for {
|
||||
offset++
|
||||
line, err := reader.ReadBytes(byte('\n'))
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
entry, err := NewEntry(line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
entry.Offset = offset
|
||||
s.Entries[entry.Index] = entry
|
||||
s.Index = append(s.Index, &entry.Index)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Dump) PrintEntryAddress(indexes []*string) {
|
||||
for _, index := range indexes {
|
||||
if entry, ok := s.Entries[*index]; ok {
|
||||
fmt.Println(entry.Address())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Dump) PrintEntryJSON(indexes []*string) error {
|
||||
file, err := os.Open(s.File)
|
||||
defer file.Close()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
offsets := s.sortedOffsets(indexes)
|
||||
var current int64
|
||||
var offset int64 = -1
|
||||
reader := bufio.NewReader(file)
|
||||
for {
|
||||
offset++
|
||||
line, err := reader.ReadBytes(byte('\n'))
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if offset == offsets[current] {
|
||||
current++
|
||||
fmt.Print(string(line))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Dump) sortedOffsets(indexes []*string) []int64 {
|
||||
var res []int64
|
||||
|
||||
for _, index := range indexes {
|
||||
res = append(res, s.Entries[*index].Offset)
|
||||
}
|
||||
sort.Slice(res, func(i, j int) bool { return res[i] < res[j] })
|
||||
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user