Skip to content
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

Prevent stack overflow in Profile #31893

Merged
merged 2 commits into from
May 2, 2019
Merged
Changes from 1 commit
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
45 changes: 26 additions & 19 deletions stdlib/Profile/src/Profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,26 +560,33 @@ function tree!(root::StackFrameTree{T}, all::Vector{UInt64}, lidict::Union{LineI
return root
end

# Print a "branch" starting at a particular level. This gets called recursively.
# Print the stack frame tree starting at a particular root. Uses a worklist to
# avoid stack overflows.
function tree(io::IO, bt::StackFrameTree, level::Int, cols::Int, fmt::ProfileFormat, noisefloor::Int)
level > fmt.maxdepth && return
isempty(bt.down) && return
# Order the line information
nexts = collect(values(bt.down))
lilist = collect(frame.frame for frame in nexts)
counts = collect(frame.count for frame in nexts)
# Generate the string for each line
strs = tree_format(lilist, counts, level, cols)
# Recurse to the next level
for i in liperm(lilist)
down = nexts[i]
count = down.count
count < fmt.mincount && continue
count < noisefloor && continue
str = strs[i]
println(io, isempty(str) ? "$count unknown stackframe" : str)
noisefloor_down = fmt.noisefloor > 0 ? floor(Int, fmt.noisefloor * sqrt(count)) : 0
tree(io, down, level + 1, cols, fmt, noisefloor_down)
worklist = Tuple{StackFrameTree, Int, Int, Union{String, Nothing}}[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
worklist = Tuple{StackFrameTree, Int, Int, Union{String, Nothing}}[
worklist = [(bt, level, noisefloor, "")]

No sense in reducing performance and introducing pointless complexity.

But I don't think this translation is correct: it doesn't seem expected that str would suddenly get appended to the worklist, when it wasn't there before the translation. It looks like this would print all level 0 items, followed by all level 1 items, followed by all level 2 items, and so on. (indeed, running this PR on @profile peakflops(), that's exactly the behavior that I see happen)

(bt, level, noisefloor, nothing)]
while !isempty(worklist)
(bt, level, noisefloor, str) = popfirst!(worklist)
str !== nothing && println(io, str)
level > fmt.maxdepth && continue
isempty(bt.down) && continue
# Order the line information
nexts = collect(values(bt.down))
lilist = collect(frame.frame for frame in nexts)
counts = collect(frame.count for frame in nexts)
# Generate the string for each line
strs = tree_format(lilist, counts, level, cols)
# Recurse to the next level
for i in liperm(lilist)
down = nexts[i]
count = down.count
count < fmt.mincount && continue
count < noisefloor && continue
str = strs[i]
isempty(str) && (str = "$count unknown stackframe")
noisefloor_down = fmt.noisefloor > 0 ? floor(Int, fmt.noisefloor * sqrt(count)) : 0
push!(worklist, (down, level+1, noisefloor_down, str))
end
end
nothing
end
Expand Down