Skip to content

Commit 858cb62

Browse files
Profile: Fix short names (#56627)
1 parent 78fd186 commit 858cb62

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

stdlib/Profile/Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ StyledStrings = "1.11.0"
1010

1111
[extras]
1212
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
13+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
1314
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1415
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1516
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1617

1718
[targets]
18-
test = ["Base64", "Logging", "Serialization", "Test"]
19+
test = ["Base64", "InteractiveUtils", "Logging", "Serialization", "Test"]

stdlib/Profile/src/Profile.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ using StyledStrings: @styled_str
4343

4444
const nmeta = 4 # number of metadata fields per block (threadid, taskid, cpu_cycle_clock, thread_sleeping)
4545

46+
const slash = Sys.iswindows() ? "\\" : "/"
47+
4648
# deprecated functions: use `getdict` instead
4749
lookup(ip::UInt) = lookup(convert(Ptr{Cvoid}, ip))
4850

@@ -537,7 +539,7 @@ function flatten(data::Vector, lidict::LineInfoDict)
537539
end
538540

539541
const SRC_DIR = normpath(joinpath(Sys.BUILD_ROOT_PATH, "src"))
540-
const COMPILER_DIR = "././../usr/share/julia/Compiler/"
542+
const COMPILER_DIR = "../usr/share/julia/Compiler/"
541543

542544
# Take a file-system path and try to form a concise representation of it
543545
# based on the package ecosystem
@@ -554,8 +556,8 @@ function short_path(spath::Symbol, filenamecache::Dict{Symbol, Tuple{String,Stri
554556
elseif startswith(path_norm, lib_dir)
555557
remainder = only(split(path_norm, lib_dir, keepempty=false))
556558
return (isfile(path_norm) ? path_norm : ""), "@julialib", remainder
557-
elseif startswith(path, COMPILER_DIR)
558-
remainder = only(split(path, COMPILER_DIR, keepempty=false))
559+
elseif contains(path, COMPILER_DIR)
560+
remainder = split(path, COMPILER_DIR, keepempty=false)[end]
559561
possible_compiler_path = normpath(joinpath(Sys.BINDIR, Base.DATAROOTDIR, "julia", "Compiler", remainder))
560562
return (isfile(possible_compiler_path) ? possible_compiler_path : ""), "@Compiler", remainder
561563
elseif isabspath(path)
@@ -572,7 +574,7 @@ function short_path(spath::Symbol, filenamecache::Dict{Symbol, Tuple{String,Stri
572574
project_file = joinpath(root, proj)
573575
if Base.isfile_casesensitive(project_file)
574576
pkgid = Base.project_file_name_uuid(project_file, "")
575-
isempty(pkgid.name) && return path # bad Project file
577+
isempty(pkgid.name) && return path, "", path # bad Project file
576578
# return the joined the module name prefix and path suffix
577579
_short_path = path[nextind(path, sizeof(root)):end]
578580
return path, string("@", pkgid.name), _short_path
@@ -944,8 +946,8 @@ function print_flat(io::IO, lilist::Vector{StackFrame},
944946
Base.printstyled(io, pkgname, color=pkgcolor)
945947
file_trunc = ltruncate(file, max(1, wfile))
946948
wpad = wfile - textwidth(pkgname)
947-
if !isempty(pkgname) && !startswith(file_trunc, "/")
948-
Base.print(io, "/")
949+
if !isempty(pkgname) && !startswith(file_trunc, slash)
950+
Base.print(io, slash)
949951
wpad -= 1
950952
end
951953
if isempty(path)
@@ -1048,8 +1050,8 @@ function tree_format(frames::Vector{<:StackFrameTree}, level::Int, cols::Int, ma
10481050
pkgcolor = get!(() -> popfirst!(Base.STACKTRACE_MODULECOLORS), PACKAGE_FIXEDCOLORS, pkgname)
10491051
remaining_path = ltruncate(filename, max(1, widthfile - textwidth(pkgname) - 1))
10501052
linenum = li.line == -1 ? "?" : string(li.line)
1051-
slash = (!isempty(pkgname) && !startswith(remaining_path, "/")) ? "/" : ""
1052-
styled_path = styled"{$pkgcolor:$pkgname}$slash$remaining_path:$linenum"
1053+
_slash = (!isempty(pkgname) && !startswith(remaining_path, slash)) ? slash : ""
1054+
styled_path = styled"{$pkgcolor:$pkgname}$(_slash)$remaining_path:$linenum"
10531055
rich_file = if isempty(path)
10541056
styled_path
10551057
else

stdlib/Profile/test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,24 @@ end
204204
@test getline(values(fdictc)) == getline(values(fdict0)) + 2
205205
end
206206

207+
import InteractiveUtils
208+
209+
@testset "Module short names" begin
210+
Profile.clear()
211+
@profile InteractiveUtils.peakflops()
212+
io = IOBuffer()
213+
ioc = IOContext(io, :displaysize=>(1000,1000))
214+
Profile.print(ioc, C=true)
215+
str = String(take!(io))
216+
slash = Sys.iswindows() ? "\\" : "/"
217+
@test occursin("@Compiler" * slash, str)
218+
@test occursin("@Base" * slash, str)
219+
@test occursin("@InteractiveUtils" * slash, str)
220+
@test occursin("@LinearAlgebra" * slash, str)
221+
@test occursin("@juliasrc" * slash, str)
222+
@test occursin("@julialib" * slash, str)
223+
end
224+
207225
# Profile deadlocking in compilation (debuginfo registration)
208226
let cmd = Base.julia_cmd()
209227
script = """

0 commit comments

Comments
 (0)