Skip to content

Commit

Permalink
Handle 0 references for locations,mappings,functions (#3217)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonswine authored Apr 15, 2024
1 parent 4cb01d6 commit 77f3e43
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/pprof/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,27 @@ func RewriteStrings(p *profilev1.Profile, n []uint32) {
func RewriteFunctions(p *profilev1.Profile, n []uint32) {
for _, loc := range p.Location {
for _, line := range loc.Line {
line.FunctionId = uint64(n[line.FunctionId-1]) + 1
if line.FunctionId > 0 {
line.FunctionId = uint64(n[line.FunctionId-1]) + 1
}
}
}
}

func RewriteMappings(p *profilev1.Profile, n []uint32) {
for _, loc := range p.Location {
loc.MappingId = uint64(n[loc.MappingId-1]) + 1
if loc.MappingId > 0 {
loc.MappingId = uint64(n[loc.MappingId-1]) + 1
}
}
}

func RewriteLocations(p *profilev1.Profile, n []uint32) {
for _, s := range p.Sample {
for i, loc := range s.LocationId {
s.LocationId[i] = uint64(n[loc-1]) + 1
if loc > 0 {
s.LocationId[i] = uint64(n[loc-1]) + 1
}
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/pprof/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,51 @@ func Test_Merge_Self(t *testing.T) {
testhelper.EqualProto(t, p.Profile, m.Profile())
}

func Test_Merge_ZeroReferences(t *testing.T) {
p, err := OpenFile("testdata/go.cpu.labels.pprof")
require.NoError(t, err)

t.Run("mappingID=0", func(t *testing.T) {
before := p.Location[10]
p.Location[10].MappingId = 0
defer func() {
p.Location[10] = before
}()

var m ProfileMerge
require.NoError(t, m.Merge(p.Profile))

testhelper.EqualProto(t, p.Profile, m.Profile())
})

t.Run("locationID=0", func(t *testing.T) {
before := p.Sample[10].LocationId[0]
p.Sample[10].LocationId[0] = 0
defer func() {
p.Sample[10].LocationId[0] = before
}()

var m ProfileMerge
require.NoError(t, m.Merge(p.Profile))

testhelper.EqualProto(t, p.Profile, m.Profile())
})

t.Run("functionID=0", func(t *testing.T) {
before := p.Location[10].Line[0].FunctionId
p.Location[10].Line[0].FunctionId = 0
defer func() {
p.Location[10].Line[0].FunctionId = before
}()

var m ProfileMerge
require.NoError(t, m.Merge(p.Profile))

testhelper.EqualProto(t, p.Profile, m.Profile())
})

}

func Test_Merge_Halves(t *testing.T) {
p, err := OpenFile("testdata/go.cpu.labels.pprof")
require.NoError(t, err)
Expand Down

0 comments on commit 77f3e43

Please sign in to comment.