Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 7dc0414

Browse files
committed
Ensure we have at least one mapping in pprof
1 parent 6fe96f4 commit 7dc0414

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

pkg/phlaredb/head_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ func TestHeadIngestRealProfiles(t *testing.T) {
335335
"testdata/profile",
336336
"testdata/profile_uncompressed",
337337
"testdata/profile_python",
338+
"testdata/profile_java",
338339
}
339340

340341
head := newTestHead(t)

pkg/phlaredb/testdata/profile_java

17.7 KB
Binary file not shown.

pkg/pprof/pprof.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ func (s *sortedSample) Swap(i, j int) {
189189
s.hashes[i], s.hashes[j] = s.hashes[j], s.hashes[i]
190190
}
191191

192-
var (
193-
currentTime = time.Now
194-
)
192+
var currentTime = time.Now
195193

196194
// Normalize normalizes the profile by:
197195
// - Removing all duplicate samples (summing their values).
@@ -207,6 +205,8 @@ func (p *Profile) Normalize() {
207205
p.TimeNanos = currentTime().UnixNano()
208206
}
209207

208+
p.ensureHasMapping()
209+
210210
// first we sort the samples location ids.
211211
hashes := p.hasher.Hashes(p.Sample)
212212

@@ -251,6 +251,29 @@ func (p *Profile) Normalize() {
251251
p.clearSampleReferences(removedSamples)
252252
}
253253

254+
// ensureHasMapping ensures all locations have at least a mapping.
255+
func (p *Profile) ensureHasMapping() {
256+
var mId uint64
257+
for _, m := range p.Mapping {
258+
if mId < m.Id {
259+
mId = m.Id
260+
}
261+
}
262+
var fake *profilev1.Mapping
263+
for _, l := range p.Location {
264+
if l.MappingId == 0 {
265+
if fake == nil {
266+
fake = &profilev1.Mapping{
267+
Id: mId + 1,
268+
MemoryLimit: ^uint64(0),
269+
}
270+
p.Mapping = append(p.Mapping, fake)
271+
}
272+
l.MappingId = fake.Id
273+
}
274+
}
275+
}
276+
254277
func (p *Profile) clearSampleReferences(samples []*profilev1.Sample) {
255278
if len(samples) == 0 {
256279
return

pkg/pprof/pprof_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ func TestNormalizeProfile(t *testing.T) {
6868
Sample: []*profilev1.Sample{
6969
{LocationId: []uint64{2, 3}, Value: []int64{0, 1}, Label: []*profilev1.Label{}},
7070
},
71-
Mapping: []*profilev1.Mapping{},
71+
Mapping: []*profilev1.Mapping{{
72+
Id: 1,
73+
MemoryLimit: ^uint64(0),
74+
}},
7275
Location: []*profilev1.Location{
73-
{Id: 2, Line: []*profilev1.Line{{FunctionId: 2, Line: 1}, {FunctionId: 3, Line: 3}}},
74-
{Id: 3, Line: []*profilev1.Line{{FunctionId: 3, Line: 1}, {FunctionId: 4, Line: 3}}},
76+
{Id: 2, MappingId: 1, Line: []*profilev1.Line{{FunctionId: 2, Line: 1}, {FunctionId: 3, Line: 3}}},
77+
{Id: 3, MappingId: 1, Line: []*profilev1.Line{{FunctionId: 3, Line: 1}, {FunctionId: 4, Line: 3}}},
7578
},
7679
Function: []*profilev1.Function{
7780
{Id: 2, Name: 6, SystemName: 7, Filename: 8, StartLine: 1},
@@ -137,6 +140,19 @@ func TestRemoveDuplicateSampleStacktraces(t *testing.T) {
137140
require.Equal(t, total-duplicate, len(p.Sample), "unexpected total samples")
138141
}
139142

143+
func TestEmptyMappingJava(t *testing.T) {
144+
p, err := OpenFile("testdata/profile_java")
145+
require.NoError(t, err)
146+
require.Len(t, p.Mapping, 0)
147+
148+
p.Normalize()
149+
require.Len(t, p.Mapping, 1)
150+
151+
for _, loc := range p.Location {
152+
require.Equal(t, loc.MappingId, uint64(1))
153+
}
154+
}
155+
140156
func countSampleDuplicates(p *Profile) int {
141157
hashes := p.hasher.Hashes(p.Sample)
142158
uniq := map[uint64][]*profilev1.Sample{}

pkg/pprof/testdata/profile_java

17.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)