Skip to content

Commit

Permalink
Hotspot tests: mock process memory reading (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z authored Jul 11, 2024
1 parent 8ac4aa5 commit 0e596c3
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions interpreter/hotspot/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@
package hotspot

import (
"bytes"
"encoding/binary"
"os"
"strings"
"testing"
"unsafe"

"github.com/elastic/go-freelru"

"github.com/elastic/otel-profiling-agent/libpf"
"github.com/elastic/otel-profiling-agent/lpm"
"github.com/elastic/otel-profiling-agent/remotememory"
"github.com/elastic/otel-profiling-agent/util"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestJavaSymbolExtraction(t *testing.T) {
rm := remotememory.NewProcessVirtualMemory(util.PID(os.Getpid()))
id := hotspotData{}
vmd, _ := id.GetOrInit(func() (hotspotVMData, error) {
vmd := hotspotVMData{}
Expand All @@ -34,6 +29,11 @@ func TestJavaSymbolExtraction(t *testing.T) {
return vmd, nil
})

maxLength := 1024
sym := make([]byte, vmd.vmStructs.Symbol.Body+uint(maxLength))
rd := bytes.NewReader(sym)
rm := remotememory.RemoteMemory{ReaderAt: rd}

addrToSymbol, err := freelru.New[libpf.Address, string](2, libpf.Address.Hash32)
require.NoError(t, err, "symbol cache failed")

Expand All @@ -44,14 +44,12 @@ func TestJavaSymbolExtraction(t *testing.T) {
prefixes: libpf.Set[lpm.Prefix]{},
stubs: map[libpf.Address]StubRoutine{},
}
maxLength := 1024
sym := make([]byte, vmd.vmStructs.Symbol.Body+uint(maxLength))

str := strings.Repeat("a", maxLength)
copy(sym[vmd.vmStructs.Symbol.Body:], str)
for i := 0; i <= maxLength; i++ {
binary.LittleEndian.PutUint16(sym[vmd.vmStructs.Symbol.Length:], uint16(i))
address := libpf.Address(uintptr(unsafe.Pointer(&sym[0])))
got := ii.getSymbol(address)
got := ii.getSymbol(0)
assert.Equal(t, str[:i], got, "symbol length %d mismatched read", i)
ii.addrToSymbol.Purge()
}
Expand Down

0 comments on commit 0e596c3

Please sign in to comment.