Skip to content

Commit 3abe453

Browse files
color stdlibs in Profile.print()
1 parent 0ef8a91 commit 3abe453

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

stdlib/Profile/src/Profile.jl

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ end
503503

504504
# Take a file-system path and try to form a concise representation of it
505505
# based on the package ecosystem
506-
function short_path(spath::Symbol, filenamecache::Dict{Symbol, String})
506+
function short_path(spath::Symbol, filenamecache::Dict{Symbol, Tuple{String,String}})
507507
return get!(filenamecache, spath) do
508508
path = Base.fixup_stdlib_path(string(spath))
509509
if isabspath(path)
@@ -523,19 +523,19 @@ function short_path(spath::Symbol, filenamecache::Dict{Symbol, String})
523523
isempty(pkgid.name) && return path # bad Project file
524524
# return the joined the module name prefix and path suffix
525525
path = path[nextind(path, sizeof(root)):end]
526-
return string("@", pkgid.name, path)
526+
return string("@", pkgid.name), path
527527
end
528528
end
529529
end
530530
end
531-
return path
531+
return "", path
532532
elseif isfile(joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "base", path))
533533
# do the same mechanic for Base (or Core/Compiler) files as above,
534534
# but they start from a relative path
535-
return joinpath("@Base", normpath(path))
535+
return "@Base", normpath(path)
536536
else
537537
# for non-existent relative paths (such as "REPL[1]"), just consider simplifying them
538-
return normpath(path) # drop leading "./"
538+
return "", normpath(path) # drop leading "./"
539539
end
540540
end
541541
end
@@ -766,7 +766,7 @@ function flat(io::IO, data::Vector{UInt64}, lidict::Union{LineInfoDict, LineInfo
766766
m = m[keep]
767767
end
768768
util_perc = (1 - (nsleeping / totalshots)) * 100
769-
filenamemap = Dict{Symbol,String}()
769+
filenamemap = Dict{Symbol,Tuple{String,String}}()
770770
if isempty(lilist)
771771
if is_subsection
772772
Base.print(io, "Total snapshots: ")
@@ -790,7 +790,7 @@ end
790790

791791
function print_flat(io::IO, lilist::Vector{StackFrame},
792792
n::Vector{Int}, m::Vector{Int},
793-
cols::Int, filenamemap::Dict{Symbol,String},
793+
cols::Int, filenamemap::Dict{Symbol,Tuple{String,String}},
794794
fmt::ProfileFormat)
795795
if fmt.sortedby === :count
796796
p = sortperm(n)
@@ -802,7 +802,7 @@ function print_flat(io::IO, lilist::Vector{StackFrame},
802802
lilist = lilist[p]
803803
n = n[p]
804804
m = m[p]
805-
filenames = String[short_path(li.file, filenamemap) for li in lilist]
805+
filenames = String[joinpath(short_path(li.file, filenamemap)...) for li in lilist]
806806
funcnames = String[string(li.func) for li in lilist]
807807
wcounts = max(6, ndigits(maximum(n)))
808808
wself = max(9, ndigits(maximum(m)))
@@ -889,7 +889,10 @@ function indent(depth::Int)
889889
return indent
890890
end
891891

892-
function tree_format(frames::Vector{<:StackFrameTree}, level::Int, cols::Int, maxes, filenamemap::Dict{Symbol,String}, showpointer::Bool)
892+
# mimics Stacktraces
893+
const PACKAGE_FIXEDCOLORS = Dict{String, Any}("@Base" => :light_black, "@Core" => :light_black)
894+
895+
function tree_format(frames::Vector{<:StackFrameTree}, level::Int, cols::Int, maxes, filenamemap::Dict{Symbol,Tuple{String,String}}, showpointer::Bool)
893896
nindent = min(cols>>1, level)
894897
ndigoverhead = ndigits(maxes.overhead)
895898
ndigcounts = ndigits(maxes.count)
@@ -924,16 +927,20 @@ function tree_format(frames::Vector{<:StackFrameTree}, level::Int, cols::Int, ma
924927
else
925928
fname = string(li.func)
926929
end
927-
filename = short_path(li.file, filenamemap)
930+
pkgname, filename = short_path(li.file, filenamemap)
928931
if showpointer
929932
fname = string(
930933
"0x",
931934
string(li.pointer, base = 16, pad = 2*sizeof(Ptr{Cvoid})),
932935
" ",
933936
fname)
934937
end
938+
pkgcolor = get!(() -> popfirst!(Base.STACKTRACE_MODULECOLORS), PACKAGE_FIXEDCOLORS, pkgname)
939+
remaining_path = rtruncto(filename, widthfile - length(pkgname) - 1)
935940
strs[i] = string(stroverhead, "", base, strcount, " ",
936-
rtruncto(filename, widthfile),
941+
sprint((io,x) -> printstyled(io, x; color=pkgcolor), pkgname; context=stdout),
942+
startswith(remaining_path, "/") ? "" : "/",
943+
remaining_path,
937944
":",
938945
li.line == -1 ? "?" : string(li.line),
939946
"; ",
@@ -1101,7 +1108,7 @@ end
11011108
# avoid stack overflows.
11021109
function print_tree(io::IO, bt::StackFrameTree{T}, cols::Int, fmt::ProfileFormat, is_subsection::Bool) where T
11031110
maxes = maxstats(bt)
1104-
filenamemap = Dict{Symbol,String}()
1111+
filenamemap = Dict{Symbol,Tuple{String,String}}()
11051112
worklist = [(bt, 0, 0, "")]
11061113
if !is_subsection
11071114
Base.print(io, "Overhead ╎ [+additional indent] Count File:Line; Function\n")

0 commit comments

Comments
 (0)