From 358576db26d0d6f4a78963652d18ee17e3d8ed40 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 11 Jul 2023 16:32:11 -0400 Subject: [PATCH 1/2] expand heapsnapshot tests --- stdlib/Profile/test/runtests.jl | 45 ++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/stdlib/Profile/test/runtests.jl b/stdlib/Profile/test/runtests.jl index eccfeea846a23..63f9a262f7905 100644 --- a/stdlib/Profile/test/runtests.jl +++ b/stdlib/Profile/test/runtests.jl @@ -269,19 +269,46 @@ end end @testset "HeapSnapshot" begin - tmpdir = mktempdir() - fname = cd(tmpdir) do - read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot())"`, String) + @testset "default save" begin + tmpdir = mktempdir() + fname = cd(tmpdir) do + read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot())"`, String) + end + @test isfile(fname) + open(fname) do fs + @test readline(fs) != "" + end + rm(fname) + rm(tmpdir, force = true, recursive = true) end - @test isfile(fname) - - open(fname) do fs - @test readline(fs) != "" + @testset "default save when current directory is readonly" begin + tmpdir = mktempdir() + fname = cd(tmpdir) do + chmod(tmpdir, 0o555) + read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot())"`, String) + end + @test !occursin(tmpdir, fname) # should not be in the given dir + @test isfile(fname) + open(fname) do fs + @test readline(fs) != "" + end + chmod(tmpdir, 0o777) + rm(fname) + rm(tmpdir, force = true, recursive = true) end - rm(fname) - rm(tmpdir, force = true, recursive = true) + @testset "save with custom dir" begin + tmpdir = mktempdir() + fname = read(`$(Base.julia_cmd()) --startup-file=no -e "using Profile; print(Profile.take_heap_snapshot(dir=$(repr(tmpdir))))"`, String) + @test occursin(tmpdir, fname) + @test isfile(fname) + open(fname) do fs + @test readline(fs) != "" + end + rm(fname) + rm(tmpdir, force = true, recursive = true) + end end include("allocs.jl") From 3d49df36f72a383ec39bfcb247cd0d2824b7b181 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 14 Jul 2023 21:15:09 -0400 Subject: [PATCH 2/2] work around windows slow filesystem --- stdlib/Profile/src/Profile.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 206d2957a91e5..847f41fe43a89 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -1254,9 +1254,10 @@ function take_heap_snapshot(all_one::Bool=false; dir::Union{Nothing,S}=nothing) if isnothing(dir) wd = pwd() fpath = joinpath(wd, fname) + fpath_test = fpath * ".test" # use a different test file as windows can fail to save immediately after rm-ing a file try - touch(fpath) - rm(fpath; force=true) + touch(fpath_test) + rm(fpath_test; 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)