Skip to content

Commit

Permalink
various performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
randall77 committed Oct 2, 2017
1 parent b1e8b4b commit 33b0e68
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
58 changes: 43 additions & 15 deletions core/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ func (p *Process) readCore(core *os.File) error {
switch e.Class {
case elf.ELFCLASS32:
p.ptrSize = 4
p.logPtrSize = 2
case elf.ELFCLASS64:
p.ptrSize = 8
p.logPtrSize = 3
default:
return fmt.Errorf("unknown elf class %s\n", e.Class)
}
Expand Down Expand Up @@ -440,39 +442,65 @@ func (p *Process) ReadAt(b []byte, a Address) {

// ReadUint8 returns a uint8 read from address a of the inferior.
func (p *Process) ReadUint8(a Address) uint8 {
var buf [1]byte
p.ReadAt(buf[:], a)
return buf[0]
m := p.findMapping(a)
if m == nil {
panic(fmt.Errorf("address %x is not mapped in the core file", a))
}
return m.contents[a.Sub(m.min)]
}

// ReadUint16 returns a uint16 read from address a of the inferior.
func (p *Process) ReadUint16(a Address) uint16 {
var buf [2]byte
p.ReadAt(buf[:], a)
m := p.findMapping(a)
if m == nil {
panic(fmt.Errorf("address %x is not mapped in the core file", a))
}
b := m.contents[a.Sub(m.min):]
if len(b) < 2 {
var buf [2]byte
b = buf[:]
p.ReadAt(b, a)
}
if p.littleEndian {
return binary.LittleEndian.Uint16(buf[:])
return binary.LittleEndian.Uint16(b)
}
return binary.BigEndian.Uint16(buf[:])
return binary.BigEndian.Uint16(b)
}

// ReadUint32 returns a uint32 read from address a of the inferior.
func (p *Process) ReadUint32(a Address) uint32 {
var buf [4]byte
p.ReadAt(buf[:], a)
m := p.findMapping(a)
if m == nil {
panic(fmt.Errorf("address %x is not mapped in the core file", a))
}
b := m.contents[a.Sub(m.min):]
if len(b) < 4 {
var buf [4]byte
b = buf[:]
p.ReadAt(b, a)
}
if p.littleEndian {
return binary.LittleEndian.Uint32(buf[:])
return binary.LittleEndian.Uint32(b)
}
return binary.BigEndian.Uint32(buf[:])
return binary.BigEndian.Uint32(b)
}

// ReadUint64 returns a uint64 read from address a of the inferior.
func (p *Process) ReadUint64(a Address) uint64 {
var buf [8]byte
p.ReadAt(buf[:], a)
m := p.findMapping(a)
if m == nil {
panic(fmt.Errorf("address %x is not mapped in the core file", a))
}
b := m.contents[a.Sub(m.min):]
if len(b) < 8 {
var buf [8]byte
b = buf[:]
p.ReadAt(b, a)
}
if p.littleEndian {
return binary.LittleEndian.Uint64(buf[:])
return binary.LittleEndian.Uint64(b)
}
return binary.BigEndian.Uint64(buf[:])
return binary.BigEndian.Uint64(b)
}

// ReadInt8 returns an int8 read from address a of the inferior.
Expand Down
4 changes: 4 additions & 0 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Process struct {
threads []*Thread // os threads (TODO: map from pid?)
arch string // amd64, ...
ptrSize int64 // 4 or 8
logPtrSize uint // 2 or 3
byteOrder binary.ByteOrder //
littleEndian bool // redundant with byteOrder
syms map[string]Address // symbols (could be empty if executable is stripped)
Expand Down Expand Up @@ -98,6 +99,9 @@ func (p *Process) Arch() string {
func (p *Process) PtrSize() int64 {
return p.ptrSize
}
func (p *Process) LogPtrSize() uint {
return p.logPtrSize
}

func (p *Process) ByteOrder() binary.ByteOrder {
return p.byteOrder
Expand Down
4 changes: 2 additions & 2 deletions gocore/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ func (p *Program) readObjects() {
func (p *Program) isPtr(a core.Address) bool {
// Convert arena offset in words to bitmap offset in bits.
off := a.Sub(p.arenaStart)
off /= p.proc.PtrSize()
off >>= p.proc.LogPtrSize()

// Find bit in bitmap. It goes backwards from the end.
// Each byte contains pointer/nonpointer bits for 4 words in its low nybble.
return p.proc.ReadUint8(p.bitmapEnd.Add(-off/4-1))>>uint(off%4)&1 != 0
return p.proc.ReadUint8(p.bitmapEnd.Add(-off>>2-1))>>uint(off&3)&1 != 0
}

// FindObject finds the object containing a. Returns that object and the offset within
Expand Down
2 changes: 1 addition & 1 deletion gocore/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,5 @@ type heapInfo struct {
base core.Address // start of the span containing this heap region
size int64 // size of objects in the span
mark uint64 // 64 mark bits, one for every 8 bytes
firstIdx int // the index of the first object that starts in this region
firstIdx int // the index of the first object that starts in this region (-1 if none)
}

0 comments on commit 33b0e68

Please sign in to comment.