Skip to content

Commit

Permalink
Use the raw_lines field in stackprof for more precise lines
Browse files Browse the repository at this point in the history
Since tmm1/stackprof#213 the actual source line within
the method, not just the method start line, should be available.
  • Loading branch information
dalehamel committed Oct 11, 2024
1 parent d69f3d0 commit fc98015
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
45 changes: 45 additions & 0 deletions src/import/stackprof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,48 @@ describe('importCpuProfileWithProperWeights', () => {
}
})
})

describe('reads correct line numbers', () => {
test('falls back to method start line if raw lines not available', async () => {
// this was recorded with an older version of stackprof that does not have the raw_lines field
const profileFile = readFileSync('./sample/profiles/stackprof/simple-cpu-stackprof.json')
const profileGroup = await importProfileGroupFromText(
'simple-cpu-stackprof.json',
profileFile.toString(),
)
expect(profileGroup).not.toBeNull()

if (profileGroup) {
const profile = profileGroup.profiles[profileGroup.indexToView]
expect(profile).not.toBeNull()

if (profile) {
expect(
profile.getGroupedCalltreeRoot().children[0].children[0].children[0].frame.line,
).toBe(37)
}
}
})
test('uses the most precise iseq line if raw_lines is available', async () => {
const profileFile = readFileSync('./sample/profiles/stackprof/wall_with_raw_lines.json')
const profileGroup = await importProfileGroupFromText(
'wall_with_raw_lines.json',
profileFile.toString(),
)
expect(profileGroup).not.toBeNull()

if (profileGroup) {
const profile = profileGroup.profiles[profileGroup.indexToView]
expect(profile).not.toBeNull()

if (profile) {
// note that line 5 of simple.rb is *inside* the function body, proving this works as previously it would only ever show the start line
// {"col": undefined, "file": ".../sample/programs/ruby/simple.rb", "key": 4826695720, "line": 5, "name": "Object#a", "selfWeight": 0, "totalWeight": 6231645}
expect(
profile.getGroupedCalltreeRoot().children[0].children[0].children[0].children[0].frame
.line,
).toBe(5)
}
}
})
})
5 changes: 4 additions & 1 deletion src/import/stackprof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export interface StackprofProfile {
frames: {[number: string]: StackprofFrame}
mode: string
raw: number[]
raw_lines: number[]
raw_timestamp_deltas: number[]
samples: number
interval: number
}

export function importFromStackprof(stackprofProfile: StackprofProfile): Profile {
const {frames, mode, raw, raw_timestamp_deltas, interval} = stackprofProfile
const {frames, mode, raw, raw_lines, raw_timestamp_deltas, interval} = stackprofProfile
const profile = new StackListProfileBuilder()
profile.setValueFormatter(new TimeFormatter('microseconds')) // default to time format unless we're in object mode

Expand All @@ -33,13 +34,15 @@ export function importFromStackprof(stackprofProfile: StackprofProfile): Profile
let stack: FrameInfo[] = []
for (let j = 0; j < stackHeight; j++) {
const id = raw[i++]
const lineNo = raw_lines ? raw_lines[i - 1] : frames[id].line
let frameName = frames[id].name
if (frameName == null) {
frameName = '(unknown)'
}
const frame = {
key: id,
...frames[id],
line: lineNo,
name: frameName,
}
stack.push(frame)
Expand Down

0 comments on commit fc98015

Please sign in to comment.