Skip to content

Commit

Permalink
Merge pull request #12516 from JuliaLang/teh/profile_printing
Browse files Browse the repository at this point in the history
Add maxdepth and sortedby options to Profile.print (fixes #12510)
  • Loading branch information
timholy committed Aug 10, 2015
2 parents ebd7230 + 68446af commit 5c9d178
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
28 changes: 18 additions & 10 deletions base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ end

clear() = ccall(:jl_profile_clear_data, Void, ())

function print{T<:Unsigned}(io::IO, data::Vector{T} = fetch(), lidict::Dict = getdict(data); format = :tree, C = false, combine = true, cols = Base.tty_size()[2])
function print{T<:Unsigned}(io::IO, data::Vector{T} = fetch(), lidict::Dict = getdict(data); format = :tree, C = false, combine = true, cols = Base.tty_size()[2], maxdepth::Int = typemax(Int), sortedby::Symbol = :filefuncline)
if format == :tree
tree(io, data, lidict, C, combine, cols)
tree(io, data, lidict, C, combine, cols, maxdepth)
elseif format == :flat
flat(io, data, lidict, C, combine, cols)
flat(io, data, lidict, C, combine, cols, sortedby)
else
throw(ArgumentError("output format $(repr(format)) not recognized"))
end
Expand Down Expand Up @@ -202,7 +202,7 @@ function parse_flat(iplist, n, lidict, C::Bool)
lilist, n
end

function flat{T<:Unsigned}(io::IO, data::Vector{T}, lidict::Dict, C::Bool, combine::Bool, cols::Integer)
function flat{T<:Unsigned}(io::IO, data::Vector{T}, lidict::Dict, C::Bool, combine::Bool, cols::Integer, sortedby)
if !C
data = purgeC(data, lidict)
end
Expand All @@ -212,10 +212,10 @@ function flat{T<:Unsigned}(io::IO, data::Vector{T}, lidict::Dict, C::Bool, combi
return
end
lilist, n = parse_flat(iplist, n, lidict, C)
print_flat(io, lilist, n, combine, cols)
print_flat(io, lilist, n, combine, cols, sortedby)
end

function print_flat(io::IO, lilist::Vector{LineInfo}, n::Vector{Int}, combine::Bool, cols::Integer)
function print_flat(io::IO, lilist::Vector{LineInfo}, n::Vector{Int}, combine::Bool, cols::Integer, sortedby)
p = liperm(lilist)
lilist = lilist[p]
n = n[p]
Expand All @@ -233,6 +233,11 @@ function print_flat(io::IO, lilist::Vector{LineInfo}, n::Vector{Int}, combine::B
n = n[keep]
lilist = lilist[keep]
end
if sortedby == :count
p = sortperm(n)
n = n[p]
lilist = lilist[p]
end
wcounts = max(6, ndigits(maximum(n)))
maxline = 0
maxfile = 0
Expand Down Expand Up @@ -330,7 +335,10 @@ function tree_format(lilist::Vector{LineInfo}, counts::Vector{Int}, level::Int,
end

# Print a "branch" starting at a particular level. This gets called recursively.
function tree{T<:Unsigned}(io::IO, bt::Vector{Vector{T}}, counts::Vector{Int}, lidict::Dict, level::Int, combine::Bool, cols::Integer)
function tree{T<:Unsigned}(io::IO, bt::Vector{Vector{T}}, counts::Vector{Int}, lidict::Dict, level::Int, combine::Bool, cols::Integer, maxdepth)
if level > maxdepth
return
end
# Organize backtraces into groups that are identical up to this level
if combine
# Combine based on the line information
Expand Down Expand Up @@ -401,12 +409,12 @@ function tree{T<:Unsigned}(io::IO, bt::Vector{Vector{T}}, counts::Vector{Int}, l
keep = len[idx] .> level+1
if any(keep)
idx = idx[keep]
tree(io, bt[idx], counts[idx], lidict, level+1, combine, cols)
tree(io, bt[idx], counts[idx], lidict, level+1, combine, cols, maxdepth)
end
end
end

function tree{T<:Unsigned}(io::IO, data::Vector{T}, lidict::Dict, C::Bool, combine::Bool, cols::Integer)
function tree{T<:Unsigned}(io::IO, data::Vector{T}, lidict::Dict, C::Bool, combine::Bool, cols::Integer, maxdepth)
if !C
data = purgeC(data, lidict)
end
Expand All @@ -418,7 +426,7 @@ function tree{T<:Unsigned}(io::IO, data::Vector{T}, lidict::Dict, C::Bool, combi
level = 0
len = Int[length(x) for x in bt]
keep = len .> 0
tree(io, bt[keep], counts[keep], lidict, level, combine, cols)
tree(io, bt[keep], counts[keep], lidict, level, combine, cols, maxdepth)
end

function callersf(matchfunc::Function, bt::Vector{UInt}, lidict)
Expand Down
6 changes: 5 additions & 1 deletion doc/manual/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Options for controlling the display of profile results
:func:`Profile.print` has more options than we've described so far.
Let's see the full declaration::

function print(io::IO = STDOUT, data = fetch(); format = :tree, C = false, combine = true, cols = tty_cols())
function print(io::IO = STDOUT, data = fetch(); format = :tree, C = false, combine = true, cols = tty_cols(), maxdepth = typemax(Int), sortedby = :filefuncline)

Let's discuss these arguments in order:

Expand Down Expand Up @@ -267,6 +267,10 @@ Let's discuss these arguments in order:
Profile.print(s,cols = 500)
close(s)

- ``maxdepth`` can be used to limit the size of the output in
``:tree`` format (it nests only up to level ``maxdepth``)
- ``sortedby = :count`` sorts the ``:flat`` format in order of
increasing counts

Configuration
-------------
Expand Down
11 changes: 10 additions & 1 deletion test/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ let iobuf = IOBuffer()
Profile.print(iobuf, format=:tree, C=true)
str = takebuf_string(iobuf)
@test !isempty(str)
truncate(iobuf, 0)
Profile.print(iobuf, format=:tree, maxdepth=2)
str = takebuf_string(iobuf)
@test !isempty(str)
truncate(iobuf, 0)
Profile.print(iobuf, format=:flat, C=true)
str = takebuf_string(iobuf)
@test !isempty(str)
truncate(iobuf, 0)
Profile.print(iobuf)
Profile.print(iobuf, format=:flat)
@test !isempty(takebuf_string(iobuf))
truncate(iobuf, 0)
Profile.print(iobuf, format=:flat, sortedby=:count)
@test !isempty(takebuf_string(iobuf))
Profile.clear()
@test isempty(Profile.fetch())
end

0 comments on commit 5c9d178

Please sign in to comment.