diff --git a/defaults.lua b/defaults.lua index 85feb4ad1..245880f1d 100644 --- a/defaults.lua +++ b/defaults.lua @@ -19,7 +19,14 @@ DGLOBALGAMMA = 1.0 -- See comments in djvureader.lua:DJVUReader:select_render_mode() DRENDER_MODE = 0 -- 0 is COLOUR -DGLOBAL_CACHE_SIZE = 1024*1024*10 +-- minimum cache size +DGLOBAL_CACHE_SIZE_MINIMUM = 1024*1024*10 + +-- proportion of system free memory used as global cache +DGLOBAL_CACHE_FREE_PROPORTION = 0.2 + +-- maximum cache size +DGLOBAL_CACHE_SIZE_MAXIMUM = 1024*1024*30 -- background colour in non scroll mode: 8 = gray, 0 = white, 15 = black DBACKGROUND_COLOR = 0 diff --git a/frontend/cache.lua b/frontend/cache.lua index 80721c399..f7487ae05 100644 --- a/frontend/cache.lua +++ b/frontend/cache.lua @@ -1,9 +1,34 @@ --[[ A global LRU cache ]]-- +local function calcFreeMem() + local meminfo = io.open("/proc/meminfo", "r") + local freemem = 0 + if meminfo then + for line in meminfo:lines() do + local free, buffer, cached, n + free, n = line:gsub("^MemFree:%s-(%d+) kB", "%1") + if n ~= 0 then freemem = freemem + tonumber(free)*1024 end + buffer, n = line:gsub("^Buffers:%s-(%d+) kB", "%1") + if n ~= 0 then freemem = freemem + tonumber(buffer)*1024 end + cached, n = line:gsub("^Cached:%s-(%d+) kB", "%1") + if n ~= 0 then freemem = freemem + tonumber(cached)*1024 end + end + meminfo:close() + end + return freemem +end + +local function calcCacheMemSize() + local min = DGLOBAL_CACHE_SIZE_MINIMUM + local max = DGLOBAL_CACHE_SIZE_MAXIMUM + local calc = calcFreeMem()*(DGLOBAL_CACHE_FREE_PROPORTION or 0) + return math.min(max, math.max(min, calc)) +end + local Cache = { -- cache configuration: - max_memsize = DGLOBAL_CACHE_SIZE, + max_memsize = calcCacheMemSize(), -- cache state: current_memsize = 0, -- associative cache