From 56b38af5ed1feeac60e87f1105ad24166c637991 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sat, 8 Jul 2023 15:58:10 -0400 Subject: [PATCH 1/3] Revert "Use tempdir() to store heap snapshot files instead of abspatch ~= rootdir (#50026)" This reverts commit 877b368d78f9fbb6bb698f40f6e9c725ef057bd0. --- stdlib/Profile/src/Profile.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index f0323d0334b1a..71bbfc70ee937 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -1234,7 +1234,7 @@ function take_heap_snapshot(filepath::String, all_one::Bool=false) return filepath end function take_heap_snapshot(all_one::Bool=false) - f = joinpath(tempdir(), "$(getpid())_$(time_ns()).heapsnapshot") + f = abspath("$(getpid())_$(time_ns()).heapsnapshot") return take_heap_snapshot(f, all_one) end From 45c6b12b46b202f185dd2b645a430dfe9350c9b4 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sat, 8 Jul 2023 16:28:29 -0400 Subject: [PATCH 2/3] fix and add custom dir option for heap snapshot --- stdlib/Profile/src/Profile.jl | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 71bbfc70ee937..2dcd98c97ee09 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -1215,14 +1215,16 @@ end """ Profile.take_heap_snapshot(io::IOStream, all_one::Bool=false) Profile.take_heap_snapshot(filepath::String, all_one::Bool=false) - Profile.take_heap_snapshot(all_one::Bool=false) + Profile.take_heap_snapshot(all_one::Bool=false; dir::String) Write a snapshot of the heap, in the JSON format expected by the Chrome -Devtools Heap Snapshot viewer (.heapsnapshot extension), to a file -(`\$pid_\$timestamp.heapsnapshot`) in the current directory, or the given -file path, or IO stream. If `all_one` is true, then report the size of -every object as one so they can be easily counted. Otherwise, report the -actual size. +Devtools Heap Snapshot viewer (.heapsnapshot extension) to a file +(`\$pid_\$timestamp.heapsnapshot`) in the current directory by default (or tempdir if +the current directory is unwritable), or in `dir` if given, or the given +full file path, or IO stream. + +If `all_one` is true, then report the size of every object as one so they can be easily +counted. Otherwise, report the actual size. """ function take_heap_snapshot(io::IOStream, all_one::Bool=false) Base.@_lock_ios(io, ccall(:jl_gc_take_heap_snapshot, Cvoid, (Ptr{Cvoid}, Cchar), io.handle, Cchar(all_one))) @@ -1233,9 +1235,22 @@ function take_heap_snapshot(filepath::String, all_one::Bool=false) end return filepath end -function take_heap_snapshot(all_one::Bool=false) - f = abspath("$(getpid())_$(time_ns()).heapsnapshot") - return take_heap_snapshot(f, all_one) +function take_heap_snapshot(all_one::Bool=false; dir::Union{Nothing,S}=nothing) where {S <: AbstractString} + fname = "$(getpid())_$(time_ns()).heapsnapshot" + if isnothing(dir) + wd = pwd() + fpath = joinpath(wd, fname) + try + touch(fpath) + rm(fpath; force=true) + catch + @warn "Cannot write to current directory `$(pwd())` so saving heap snapshot to `tempdir()`" maxlog=1 _id=Symbol(wd) + fpath = joinpath(tempdir(), fname) + end + else + fpath = joinpath(expanduser(dir), fname) + end + return take_heap_snapshot(fpath, all_one) end From 9d9402fefc54168b81c340120176f57e42156a08 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 11 Jul 2023 06:24:40 -0400 Subject: [PATCH 3/3] Update stdlib/Profile/src/Profile.jl Co-authored-by: Valentin Churavy --- stdlib/Profile/src/Profile.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 2dcd98c97ee09..c37cdd0af0368 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -1244,7 +1244,7 @@ function take_heap_snapshot(all_one::Bool=false; dir::Union{Nothing,S}=nothing) touch(fpath) rm(fpath; force=true) catch - @warn "Cannot write to current directory `$(pwd())` so saving heap snapshot to `tempdir()`" maxlog=1 _id=Symbol(wd) + @warn "Cannot write to current directory `$(pwd())` so saving heap snapshot to `$(tempdir())`" maxlog=1 _id=Symbol(wd) fpath = joinpath(tempdir(), fname) end else