Skip to content

Commit

Permalink
Fixup the backwards compatiblity change: support non-meta profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
NHDaly committed Oct 20, 2023
1 parent 1fc51c9 commit 2d72715
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
24 changes: 16 additions & 8 deletions src/PProf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ function pprof(data::Union{Nothing, Vector{UInt}} = nothing,
has_meta = false
if data === nothing
data = if isdefined(Profile, :has_meta)
copy(Profile.fetch(include_meta = true))
has_meta = true
copy(Profile.fetch(include_meta = true))
else
copy(Profile.fetch())
end
Expand Down Expand Up @@ -149,11 +149,13 @@ 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.
# All samples get the same value for CPU profiles.
value = [
1, # events
]

lastwaszero = false # (Legacy: used when has_meta = false)

idx = length(data)
meta = nothing
while idx > 0
Expand All @@ -179,18 +181,24 @@ 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
elseif !has_meta && idx != length(data) && data[idx] == 0
# Avoid creating empty samples
# 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
if lastwaszero
@assert length(location_id) == 0
else
# Finish last block
push!(samples, Sample(;location_id = reverse!(location_id), value = value))
location_id = Vector{eltype(data)}()
lastwaszero = true
end
# Finish last block
push!(samples, Sample(;location_id = reverse!(location_id), value = value, label = meta))
location_id = Vector{eltype(data)}()
idx -= 1
continue
end
ip = data[idx]
idx -= 1
lastwaszero = false

# A backtrace consists of a set of IP (Instruction Pointers), each IP points
# a single line of code and `litrace` has the necessary information to decode
Expand Down
8 changes: 6 additions & 2 deletions test/PProf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ end
end
sleep(2)
end
for i in 1:3
@testset for i in 1:4
if i == 1
data = Profile.fetch(include_meta = true)
args = (data,)
elseif i == 2
data,lidict = Profile.retrieve(include_meta = true)
args = (data, lidict)
elseif i == 3
# 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)
# Ensure we are backwards compatible with older, non-meta profiles
data,lidict = Profile.retrieve(include_meta = false)
args = (data, lidict)
end

Expand Down

0 comments on commit 2d72715

Please sign in to comment.