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

Optimizations to approx_size and ensure_memory_reserved #88

Merged
merged 4 commits into from
Aug 1, 2024
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
8 changes: 6 additions & 2 deletions src/MemPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ include("datastore.jl")

Returns the size of `d` in bytes used for accounting in MemPool datastore.
"""
function approx_size(@nospecialize(d))
Base.summarysize(d) # note: this is accurate but expensive
function approx_size(d::T) where T
if Base.datatype_pointerfree(T)
return sizeof(d)
else
Base.summarysize(d) # note: this is accurate but expensive
end
end

function approx_size(d::Union{Base.BitInteger, Float16, Float32, Float64})
Expand Down
8 changes: 4 additions & 4 deletions src/datastore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ const MEM_RESERVE_LOCK = Threads.ReentrantLock()
const MEM_RESERVE_SWEEPS = Ref{Int}(3)

"""
When called, ensures that at least `MEM_RESERVED[] + size` bytes are available
When called, ensures that at least `MEM_RESERVED[]` bytes are available
to the OS. If there is not enough memory available, then a variety of calls to
the GC are performed to free up memory until either the reservation limit is
satisfied, or `max_sweeps` number of cycles have elapsed.
Expand All @@ -396,15 +396,15 @@ function ensure_memory_reserved(size::Integer=0; max_sweeps::Integer=MEM_RESERVE
max_sweeps == 0 && return

# Do a quick (cached) check, to optimize for many calls to this function when memory isn't tight
if Int(storage_available(CPURAMResource())) - size >= MEM_RESERVED[]
if Int(storage_available(CPURAMResource())) >= MEM_RESERVED[]
return
end

# Check whether the OS is running tight on memory
sweep_ctr = 0
while true
with(QUERY_MEM_OVERRIDE => true) do
Int(storage_available(CPURAMResource())) - size < MEM_RESERVED[]
Int(storage_available(CPURAMResource())) < MEM_RESERVED[]
end || break

# We need more memory! Let's encourage the GC to clear some memory...
Expand All @@ -425,7 +425,7 @@ function ensure_memory_reserved(size::Integer=0; max_sweeps::Integer=MEM_RESERVE
yield()

# Wait for send queue to clear
while SEND_QUEUE.processing
while SEND_QUEUE.processing || !isempty(SEND_QUEUE.queue)
yield()
end

Expand Down
11 changes: 5 additions & 6 deletions src/storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,11 @@ struct CPURAMResource <: StorageResource end
if Sys.islinux()
function free_memory()
open("/proc/meminfo", "r") do io
# skip first 2 lines
readline(io)
readline(io)
line = readline(io)
free = match(r"MemAvailable:\s*([0-9]*)\s.*", line).captures[1]
parse(UInt64, free) * 1024
# TODO: Cache in TLS
buf = zeros(UInt8, 128)
readbytes!(io, buf)
free = match(r"MemAvailable:\s*([0-9]*)\s.*", String(buf)).captures[1]
return parse(UInt64, free) * 1024
end
end
else
Expand Down
Loading