Skip to content

Commit

Permalink
Merge pull request #7529 from JuliaLang/teh/whocalls
Browse files Browse the repository at this point in the history
Add Profile.callers to determine main callers of a particular function
  • Loading branch information
timholy committed Aug 11, 2014
2 parents e24abad + 83934e5 commit eddf0c1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
41 changes: 41 additions & 0 deletions base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ function getdict(data::Vector{Uint})
Dict(uip, [lookup(ip) for ip in uip])
end

function callers(funcname::ByteString, bt::Vector{Uint}, lidict; filename = nothing, linerange = nothing)
if filename == nothing && linerange == nothing
return callersf(li -> li.func == funcname, bt, lidict)
end
filename == nothing && error("If supplying linerange, you must also supply the filename")
if linerange == nothing
return callersf(li -> li.func == funcname && li.file == filename, bt, lidict)
else
return callersf(li -> li.func == funcname && li.file == filename && in(li.line, linerange), bt, lidict)
end
end

callers(funcname::ByteString; kwargs...) = callers(funcname, retrieve()...; kwargs...)
callers(func::Function, bt::Vector{Uint}, lidict; kwargs...) = callers(string(func), bt, lidict; kwargs...)
callers(func::Function; kwargs...) = callers(string(func), retrieve()...; kwargs...)


####
#### Internal interface
####
Expand Down Expand Up @@ -391,6 +408,30 @@ function tree{T<:Unsigned}(io::IO, data::Vector{T}, lidict::Dict, C::Bool, combi
tree(io, bt[keep], counts[keep], lidict, level, combine, cols)
end

function callersf(matchfunc::Function, bt::Vector{Uint}, lidict)
counts = Dict{LineInfo, Int}()
lastmatched = false
for id in bt
if id == 0
lastmatched = false
continue
end
li = lidict[id]
if lastmatched
if haskey(counts, li)
counts[li] += 1
else
counts[li] = 1
end
end
lastmatched = matchfunc(li)
end
k = collect(keys(counts))
v = collect(values(counts))
p = sortperm(v, rev=true)
[(v[i], k[i]) for i in p]
end

# Utilities
function truncto(str::ByteString, w::Int)
ret = str;
Expand Down
11 changes: 11 additions & 0 deletions doc/stdlib/profile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,14 @@ Function reference
values that store the file name, function name, and line
number. This function allows you to save profiling results for
future analysis.

.. function::callers(funcname, [data, lidict], [filename=<filename>], [linerange=<start:stop>]) -> Vector{(count, linfo)}
Given a previous profiling run, determine who called a particular
function. Supplying the filename (and optionally, range of line
numbers over which the function is defined) allows you to
disambiguate an overloaded method. The returned value is a vector
containing a count of the number of calls and line information
about the caller. One can optionally supply backtrace data
obtained from ``retrieve``; otherwise, the current internal profile
buffer is used.

0 comments on commit eddf0c1

Please sign in to comment.