mirror of
https://github.com/jimeh/rbheap.git
synced 2026-02-19 12:56:46 +00:00
Initial commit
This commit is contained in:
70
heap_dump.go
Normal file
70
heap_dump.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
json "encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func NewHeapDump(file string) (*HeapDump, error) {
|
||||
heapDump := HeapDump{File: file}
|
||||
err := heapDump.Process()
|
||||
return &heapDump, err
|
||||
}
|
||||
|
||||
// HeapDump contains all relevant data for a single heap dump.
|
||||
type HeapDump struct {
|
||||
File string
|
||||
Index []string
|
||||
Items map[string]*HeapItem
|
||||
}
|
||||
|
||||
// Process processes the heap dump
|
||||
func (s *HeapDump) Process() error {
|
||||
file, err := os.Open(s.File)
|
||||
defer file.Close()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.Items = map[string]*HeapItem{}
|
||||
|
||||
reader := bufio.NewReader(file)
|
||||
for {
|
||||
line, err := reader.ReadBytes(byte('\n'))
|
||||
s.ProcessLine(line)
|
||||
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err != io.EOF {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HeapDump) ProcessLine(line []byte) error {
|
||||
item, err := s.Parse(line)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(item.Address) > 0 {
|
||||
index := item.Address + ":" + item.Type
|
||||
s.Items[index] = item
|
||||
s.Index = append(s.Index, index)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *HeapDump) Parse(itemJSON []byte) (*HeapItem, error) {
|
||||
var i HeapItem
|
||||
err := json.Unmarshal(itemJSON, &i)
|
||||
|
||||
return &i, err
|
||||
}
|
||||
Reference in New Issue
Block a user