@@ -1215,14 +1215,16 @@ end
12151215"""
12161216 Profile.take_heap_snapshot(io::IOStream, all_one::Bool=false)
12171217 Profile.take_heap_snapshot(filepath::String, all_one::Bool=false)
1218- Profile.take_heap_snapshot(all_one::Bool=false)
1218+ Profile.take_heap_snapshot(all_one::Bool=false; dir::String )
12191219
12201220Write a snapshot of the heap, in the JSON format expected by the Chrome
1221- Devtools Heap Snapshot viewer (.heapsnapshot extension), to a file
1222- (`\$ pid_\$ timestamp.heapsnapshot`) in the current directory, or the given
1223- file path, or IO stream. If `all_one` is true, then report the size of
1224- every object as one so they can be easily counted. Otherwise, report the
1225- actual size.
1221+ Devtools Heap Snapshot viewer (.heapsnapshot extension) to a file
1222+ (`\$ pid_\$ timestamp.heapsnapshot`) in the current directory by default (or tempdir if
1223+ the current directory is unwritable), or in `dir` if given, or the given
1224+ full file path, or IO stream.
1225+
1226+ If `all_one` is true, then report the size of every object as one so they can be easily
1227+ counted. Otherwise, report the actual size.
12261228"""
12271229function take_heap_snapshot (io:: IOStream , all_one:: Bool = false )
12281230 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)
12331235 end
12341236 return filepath
12351237end
1236- function take_heap_snapshot (all_one:: Bool = false )
1237- f = joinpath (tempdir (), " $(getpid ()) _$(time_ns ()) .heapsnapshot" )
1238- return take_heap_snapshot (f, all_one)
1238+ function take_heap_snapshot (all_one:: Bool = false ; dir:: Union{Nothing,S} = nothing ) where {S <: AbstractString }
1239+ fname = " $(getpid ()) _$(time_ns ()) .heapsnapshot"
1240+ if isnothing (dir)
1241+ wd = pwd ()
1242+ fpath = joinpath (wd, fname)
1243+ try
1244+ touch (fpath)
1245+ rm (fpath; force= true )
1246+ catch
1247+ @warn " Cannot write to current directory `$(pwd ()) ` so saving heap snapshot to `$(tempdir ()) `" maxlog= 1 _id= Symbol (wd)
1248+ fpath = joinpath (tempdir (), fname)
1249+ end
1250+ else
1251+ fpath = joinpath (expanduser (dir), fname)
1252+ end
1253+ return take_heap_snapshot (fpath, all_one)
12391254end
12401255
12411256
0 commit comments