From 904a5f1ebe3358c8b0e7866964778fc63482120c Mon Sep 17 00:00:00 2001 From: chrox Date: Sun, 19 Jan 2014 11:40:34 +0800 Subject: [PATCH] calculate maximum global cache size from free RAM In reflowing scroll mode with 2 pages hinting, 4 full page blitbuffers and koptcontexts should stay well in cache in the most demanding cases, with two pages shown on screen and two pages rendered in background. Since blitbuffer size is halved the size of page, we need cache size to be 6 times an average reflowed page size. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For Kobo Aura HD which has a resolution of 1440×1080, a reflowed page could become 1080×4800. So 30MB of cache is demanded for this case. This PR implements dynamic cache size allocating according to size of system free memory. By default it will use 20 percent of free RAM with a clip specified by DGLOBAL_CACHE_SIZE_MINIMUM and DGLOBAL_CACHE_SIZE_MAXIMUM which are 10MB and 30MB respectively by default. --- defaults.lua | 9 ++++++++- frontend/cache.lua | 27 ++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) 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