@@ -1215,14 +1215,16 @@ end
1215
1215
"""
1216
1216
Profile.take_heap_snapshot(io::IOStream, all_one::Bool=false)
1217
1217
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 )
1219
1219
1220
1220
Write 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.
1226
1228
"""
1227
1229
function take_heap_snapshot (io:: IOStream , all_one:: Bool = false )
1228
1230
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)
1233
1235
end
1234
1236
return filepath
1235
1237
end
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)
1239
1254
end
1240
1255
1241
1256
0 commit comments