Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mmap.madvise! #37369

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ Standard library changes
* Change `uuid1` and `uuid4` to use `Random.RandomDevice()` as default random number generator ([#35872]).
* Added `parse(::Type{UUID}, ::AbstractString)` method

#### Mmap
* On Unix systems, the `Mmap.madvise!` function (along with OS-specific `Mmap.MADV_*`
constants) has been added to give advice on handling of memory-mapped arrays. ([#37369])

Deprecated or removed
---------------------

Expand Down
64 changes: 62 additions & 2 deletions stdlib/Mmap/src/Mmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ const MS_SYNC = 4
Forces synchronization between the in-memory version of a memory-mapped `Array` or
[`BitArray`](@ref) and the on-disk version.
"""
function sync!(m::Array{T}, flags::Integer=MS_SYNC) where T
function sync!(m::Array, flags::Integer=MS_SYNC)
offset = rem(UInt(pointer(m)), PAGESIZE)
ptr = pointer(m) - offset
mmaplen = length(m) * sizeof(T) + offset
mmaplen = sizeof(m) + offset
GC.@preserve m @static if Sys.isunix()
systemerror("msync",
ccall(:msync, Cint, (Ptr{Cvoid}, Csize_t, Cint), ptr, mmaplen, flags) != 0)
Expand All @@ -349,4 +349,64 @@ function sync!(m::Array{T}, flags::Integer=MS_SYNC) where T
end
sync!(B::BitArray, flags::Integer=MS_SYNC) = sync!(B.chunks, flags)

@static if Sys.isunix()
const MADV_NORMAL = 0
const MADV_RANDOM = 1
const MADV_SEQUENTIAL = 2
const MADV_WILLNEED = 3
const MADV_DONTNEED = 4
if Sys.islinux()
const MADV_FREE = 8
const MADV_REMOVE = 9
const MADV_DONTFORK = 10
const MADV_DOFORK = 11
const MADV_MERGEABLE = 12
const MADV_UNMERGEABLE = 13
const MADV_HUGEPAGE = 14
const MADV_NOHUGEPAGE = 15
const MADV_DONTDUMP = 16
const MADV_DODUMP = 17
const MADV_WIPEONFORK = 18
const MADV_KEEPONFORK = 19
const MADV_COLD = 20
const MADV_PAGEOUT = 21
const MADV_HWPOISON = 100
const MADV_SOFT_OFFLINE = 101
elseif Sys.isapple()
const MADV_FREE = 5
elseif Sys.isfreebsd() || Sys.isdragonfly()
const MADV_FREE = 5
const MADV_NOSYNC = 6
const MADV_AUTOSYNC = 7
const MADV_NOCORE = 8
const MADV_CORE = 9
if Sys.isfreebsd()
const MADV_PROTECT = 10
else
const MADV_INVAL = 10
const MADV_SETMAP = 11
end
elseif Sys.isopenbsd() || Sys.isnetbsd()
const MADV_SPACEAVAIL = 5
const MADV_FREE = 6
end

"""
Mmap.madvise!(array, flag::Integer = Mmap.MADV_NORMAL)

Advises the kernel on the intended usage of the memory-mapped `array`, with the intent
`flag` being one of the available `MADV_*` constants.
"""
function madvise!(m::Array, flag::Integer=MADV_NORMAL)
offset = rem(UInt(pointer(m)), PAGESIZE)
ptr = pointer(m) - offset
mmaplen = sizeof(m) + offset
GC.@preserve m begin
systemerror("madvise",
ccall(:madvise, Cint, (Ptr{Cvoid}, Csize_t, Cint), ptr, mmaplen, flag) != 0)
end
end
madvise!(B::BitArray, flag::Integer=MADV_NORMAL) = madvise!(B.chunks, flag)
end # Sys.isunix()

end # module
14 changes: 14 additions & 0 deletions stdlib/Mmap/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ n = similar(m, 12)
@test size(n) == (12,)
finalize(m); m = nothing; GC.gc()

if Sys.isunix()
file = tempname()
write(file, rand(Float64, 20))
A = Mmap.mmap(file, Vector{Float64}, 20)
@test Mmap.madvise!(A, Mmap.MADV_WILLNEED) === nothing # checking for no error
finalize(A); A = nothing; GC.gc()

write(file, BitArray(rand(Bool, 20)))
b = Mmap.mmap(file, BitArray, 20)
@test Mmap.madvise!(b, Mmap.MADV_WILLNEED) === nothing
finalize(b); b = nothing; GC.gc()
rm(file)
end

# test #14885
file = tempname()
touch(file)
Expand Down