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

storage: Only query OS memory periodically #72

Merged
merged 1 commit into from
Nov 14, 2023
Merged
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
storage: Only query OS memory periodically
jpsamaroo committed Nov 9, 2023
commit e8945ba28904b0e0253f92805603bda3dfc2efdb
39 changes: 36 additions & 3 deletions src/storage.jl
Original file line number Diff line number Diff line change
@@ -161,7 +161,7 @@ storage_utilized(s::StorageResource) = storage_capacity(s) - storage_available(s
"Represents CPU RAM."
struct CPURAMResource <: StorageResource end
if Sys.islinux()
function storage_available(::CPURAMResource)
function free_memory()
open("/proc/meminfo", "r") do io
# skip first 2 lines
readline(io)
@@ -173,9 +173,42 @@ function storage_available(::CPURAMResource)
end
else
# FIXME: Sys.free_memory() includes OS caches
storage_available(::CPURAMResource) = Sys.free_memory()
free_memory() = Sys.free_memory()
end
storage_available(::CPURAMResource) = _query_mem_periodically(:available)
storage_capacity(::CPURAMResource) = _query_mem_periodically(:capacity)

mutable struct QueriedMemInfo
value::UInt64
last_ns::UInt64
end
QueriedMemInfo() = QueriedMemInfo(UInt64(0), UInt64(0))
const QUERY_MEM_AVAILABLE = Ref(QueriedMemInfo())
const QUERY_MEM_CAPACITY = Ref(QueriedMemInfo())
const QUERY_MEM_PERIOD = 10 * 1000^2 # 10ms
function _query_mem_periodically(kind::Symbol)
if !(kind in (:available, :capacity))
throw(ArgumentError("Invalid memory query kind: $kind"))
end
mem_bin = if kind == :available
QUERY_MEM_AVAILABLE
elseif kind == :capacity
QUERY_MEM_CAPACITY
end
mem_info = mem_bin[]
now_ns = time_ns()
if mem_info.last_ns < now_ns - QUERY_MEM_PERIOD
if kind == :available
new_mem_info = QueriedMemInfo(free_memory(), now_ns)
elseif kind == :capacity
new_mem_info = QueriedMemInfo(Sys.total_memory(), now_ns)
end
mem_bin[] = new_mem_info
return new_mem_info.value
else
return mem_info.value
end
end
storage_capacity(::CPURAMResource) = Sys.total_memory()

"Represents a filesystem mounted at a given path."
struct FilesystemResource <: StorageResource