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

Optimize memory queries #81

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/MemPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ approx_size(f::FileRef) = f.size

include("io.jl")
include("lock.jl")
include("clock.jl")
include("datastore.jl")

"""
Expand Down
59 changes: 59 additions & 0 deletions src/clock.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
##
# This file is a part of MemPool.jl. License is MIT
#
# Based upon https://github.com/google/benchmark, which is licensed under Apache v2:
# https://github.com/google/benchmark/blob/master/LICENSE
#
# In compliance with the Apache v2 license, here are the original copyright notices:
# Copyright 2015 Google Inc. All rights reserved.
##

struct TimeSpec
tv_sec::UInt64 # time_t
tv_nsec::UInt64
end

maketime(ts) = ts.tv_sec * UInt(1e9) + ts.tv_nsec

# From bits/times.h on a Linux system
# Check if those are the same on BSD
if Sys.islinux()
const CLOCK_MONOTONIC = Cint(1)
const CLOCK_PROCESS_CPUTIME_ID = Cint(2)
const CLOCK_THREAD_CPUTIME_ID = Cint(3)
const CLOCK_MONOTONIC_COARSE = Cint(6)
elseif Sys.isfreebsd() # atleast on FreeBSD 11.1
const CLOCK_MONOTONIC = Cint(4)
const CLOCK_PROCESS_CPUTIME_ID = Cint(14)
elseif Sys.isapple() # Version 10.12 required
const CLOCK_MONOTONIC = Cint(6)
const CLOCK_PROCESS_CPUTIME_ID = Cint(12)
end

@static if Sys.isunix()
@inline function clock_gettime(cid)
ts = Ref{TimeSpec}()
ccall(:clock_gettime, Cint, (Cint, Ref{TimeSpec}), cid, ts)
return ts[]
end

@inline function realtime()
maketime(clock_gettime(CLOCK_MONOTONIC))

Check warning on line 41 in src/clock.jl

View check run for this annotation

Codecov / codecov/patch

src/clock.jl#L40-L41

Added lines #L40 - L41 were not covered by tests
end

@inline function cputime()
maketime(clock_gettime(CLOCK_PROCESS_CPUTIME_ID))

Check warning on line 45 in src/clock.jl

View check run for this annotation

Codecov / codecov/patch

src/clock.jl#L44-L45

Added lines #L44 - L45 were not covered by tests
end
end

@static if Sys.islinux()
@inline function cputhreadtime()
maketime(clock_gettime(CLOCK_THREAD_CPUTIME_ID))

Check warning on line 51 in src/clock.jl

View check run for this annotation

Codecov / codecov/patch

src/clock.jl#L50-L51

Added lines #L50 - L51 were not covered by tests
end
@inline function fasttime()
maketime(clock_gettime(CLOCK_MONOTONIC_COARSE))
end
else
@inline cputhreadtime() = time_ns() # HACK
@inline fasttime() = time_ns() # HACK

Check warning on line 58 in src/clock.jl

View check run for this annotation

Codecov / codecov/patch

src/clock.jl#L57-L58

Added lines #L57 - L58 were not covered by tests
end
7 changes: 6 additions & 1 deletion src/datastore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@
function ensure_memory_reserved(size::Integer=0; max_sweeps::Integer=5)
sat_sub(x::T, y::T) where T = x < y ? zero(T) : x-y

# 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[]
return
end

# Check whether the OS is running tight on memory
sweep_ctr = 0
while true
Expand All @@ -396,7 +401,7 @@
end || break

# We need more memory! Let's encourage the GC to clear some memory...
sweep_start = time_ns()
sweep_start = fasttime()

Check warning on line 404 in src/datastore.jl

View check run for this annotation

Codecov / codecov/patch

src/datastore.jl#L404

Added line #L404 was not covered by tests
mem_used = with(QUERY_MEM_OVERRIDE => true) do
storage_utilized(CPURAMResource())
end
Expand Down
6 changes: 3 additions & 3 deletions src/storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ 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
const QUERY_MEM_PERIOD = Ref(10 * 1000^2) # 10ms
const QUERY_MEM_OVERRIDE = ScopedValue(false)
function _query_mem_periodically(kind::Symbol)
if !(kind in (:available, :capacity))
Expand All @@ -197,8 +197,8 @@ function _query_mem_periodically(kind::Symbol)
QUERY_MEM_CAPACITY
end
mem_info = mem_bin[]
now_ns = time_ns()
if QUERY_MEM_OVERRIDE[] || mem_info.last_ns < now_ns - QUERY_MEM_PERIOD
now_ns = fasttime()
if QUERY_MEM_OVERRIDE[] || mem_info.last_ns < now_ns - QUERY_MEM_PERIOD[]
if kind == :available
new_mem_info = QueriedMemInfo(free_memory(), now_ns)
elseif kind == :capacity
Expand Down
Loading