Skip to content

Commit

Permalink
Half-finished attempt at allowing older, non-meta profiles too
Browse files Browse the repository at this point in the history
I'm not sure it's worth the complexity
  • Loading branch information
NHDaly committed Oct 20, 2023
1 parent 46b02bf commit 7b1f044
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
30 changes: 21 additions & 9 deletions src/PProf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,16 @@ function pprof(data::Union{Nothing, Vector{UInt}} = nothing,
keep_frames::Union{Nothing, AbstractString} = nothing,
ui_relative_percentages::Bool = true,
)
has_meta = false
if data === nothing
data = if isdefined(Profile, :has_meta)
copy(Profile.fetch(include_meta = true))
has_meta = true
else
copy(Profile.fetch())
end
elseif isdefined(Profile, :has_meta)
@assert Profile.has_meta(data) "PProf expects `Profile.fetch(include_meta=true)`."
has_meta = Profile.has_meta(data)
end
lookup = lidict
if lookup === nothing
Expand Down Expand Up @@ -147,27 +149,28 @@ function pprof(data::Union{Nothing, Vector{UInt}} = nothing,
# start decoding backtraces
location_id = Vector{eltype(data)}()

# All samples get the same value for the CPU profile.
value = [
1, # events
]

idx = length(data)
value = nothing
meta = nothing
while idx > 0
if Profile.is_block_end(data, idx)
if value !== nothing
@assert meta !== nothing
if has_meta && Profile.is_block_end(data, idx)
if meta !== nothing
# Finish last block
push!(samples, Sample(;location_id = reverse!(location_id), value = value, label = meta))
location_id = Vector{eltype(data)}()
end

# read metadata
# Consume all of the metadata entries in the buffer, and then position the IP
# at the idx for the actual ip.
thread_sleeping = data[idx - Profile.META_OFFSET_SLEEPSTATE] - 1 # "Sleeping" is recorded as 1 or 2, to avoid 0s, which indicate end-of-block.
cpu_cycle_clock = data[idx - Profile.META_OFFSET_CPUCYCLECLOCK]
taskid = data[idx - Profile.META_OFFSET_TASKID]
threadid = data[idx - Profile.META_OFFSET_THREADID]

value = [
1, # events
]
meta = Label[
Label!("thread_sleeping", thread_sleeping != 0),
Label!("cycle_clock", cpu_cycle_clock, "nanoseconds"),
Expand All @@ -176,6 +179,15 @@ function pprof(data::Union{Nothing, Vector{UInt}} = nothing,
]
idx -= (Profile.nmeta + 2) # skip all the metas, plus the 2 nulls that end a block.
continue
elseif !has_meta && data[idx] == 0
# ip == 0x0 is the sentinel value for finishing a backtrace (when meta is disabled), therefore finising a sample
# On some platforms, we sometimes get two 0s in a row for some reason...
if idx > 1 && data[idx-1] == 0
idx -= 1
end
# Finish last block
push!(samples, Sample(;location_id = reverse!(location_id), value = value, label = meta))
location_id = Vector{eltype(data)}()
end
ip = data[idx]
idx -= 1
Expand Down
9 changes: 5 additions & 4 deletions test/PProf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ end
end
sleep(2)
end
for i in 1:2
for i in 1:3
if i == 1
data = Profile.fetch(include_meta = true)
args = (data,)
elseif i == 2
# Ensure we are backwards compatible with older, non-meta profiles
data = Profile.fetch(include_meta = false)
args = (data,)
else
data,lidict = Profile.retrieve(include_meta = true)
args = (data, lidict)
Expand All @@ -94,9 +98,6 @@ end
@test length(with_c.location) > length(without_c.location)
@test length(with_c.var"#function") > length(without_c.var"#function")
end

# Must have meta.
@test_throws AssertionError pprof(Profile.fetch(include_meta = false))
end

@testset "full_signatures" begin
Expand Down

0 comments on commit 7b1f044

Please sign in to comment.