Skip to content

Stackprof: weight on-cpu samples by period rather than timestamp delta #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sample/profiles/stackprof/simple-cpu-stackprof.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions sample/programs/ruby/simple.rb
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ def a
for i in 0..100 do
b
c
e
end
end

@@ -28,8 +29,13 @@ def d
prod
end

profile = StackProf.run(mode: :wall, raw: true) do
def e
sleep 0.05
end

mode = (ARGV[0] || :wall).to_sym
profile = StackProf.run(mode: mode, raw: true) do
a
end

puts JSON.generate(profile)
puts JSON.generate(profile)
448 changes: 448 additions & 0 deletions src/import/__snapshots__/stackprof.test.ts.snap

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions src/import/stackprof.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFileSync } from 'fs';
import { importProfileGroupFromText } from '.'
import {checkProfileSnapshot} from '../lib/test-utils'

test('importFromStackprof', async () => {
@@ -11,3 +13,26 @@ test('importFromStackprof object mode', async () => {
test('importFromStackprof when a profile has a frame with no name', async () => {
await checkProfileSnapshot('./sample/profiles/stackprof/stackprof-last-frame-no-name.json')
})

describe('importCpuProfileWithProperWeights', () => {
test('importFromStackprof cpu mode snapshot', async () => {
await checkProfileSnapshot('./sample/profiles/stackprof/simple-cpu-stackprof.json')
})

test('uses samples count for weight when importing cpu profile', async () => {

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.getWeightUnit()).toBe('microseconds')
expect(profile.getTotalWeight()).toBe(489000)
}
}
})
})
38 changes: 19 additions & 19 deletions src/import/stackprof.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://github.com/tmm1/stackprof

import {Profile, FrameInfo, StackListProfileBuilder} from '../lib/profile'
import {TimeFormatter} from '../lib/value-formatters'
import {RawValueFormatter, TimeFormatter} from '../lib/value-formatters'

interface StackprofFrame {
name?: string
@@ -15,14 +15,13 @@ export interface StackprofProfile {
raw: number[]
raw_timestamp_deltas: number[]
samples: number
interval: number
}

export function importFromStackprof(stackprofProfile: StackprofProfile): Profile {
const {frames, mode, raw, raw_timestamp_deltas, samples} = stackprofProfile
const objectMode = mode == 'object'

const size = objectMode ? samples : stackprofProfile.raw_timestamp_deltas.reduce((a, b) => a + b, 0)
const profile = new StackListProfileBuilder(size)
const {frames, mode, raw, raw_timestamp_deltas, interval} = stackprofProfile
const profile = new StackListProfileBuilder()
profile.setValueFormatter(new TimeFormatter('microseconds')) // default to time format unless we're in object mode

let sampleIndex = 0

@@ -50,23 +49,24 @@ export function importFromStackprof(stackprofProfile: StackprofProfile): Profile
}
const nSamples = raw[i++]

if (objectMode) {
profile.appendSampleWithWeight(stack, nSamples)
} else {
let sampleDuration = 0
for (let j = 0; j < nSamples; j++) {
sampleDuration += raw_timestamp_deltas[sampleIndex++]
}

profile.appendSampleWithWeight(stack, sampleDuration)
switch (mode) {
case 'object':
profile.appendSampleWithWeight(stack, nSamples)
profile.setValueFormatter(new RawValueFormatter())
break
case 'cpu':
profile.appendSampleWithWeight(stack, nSamples * interval)
break
default:
let sampleDuration = 0
for (let j = 0; j < nSamples; j++) {
sampleDuration += raw_timestamp_deltas[sampleIndex++]
}
profile.appendSampleWithWeight(stack, sampleDuration)
}

prevStack = stack
}

if (!objectMode) {
profile.setValueFormatter(new TimeFormatter('microseconds'))
}

return profile.build()
}