Add mtos cache in ram to boost performance

master
Alexander Weber 2018-03-22 14:38:43 +01:00
parent 42266a4929
commit 76c5e6b2d4
5 changed files with 83 additions and 3 deletions

3
API.md
View File

@ -207,7 +207,8 @@ Can be used for non-data and/or system tasks. For usual data store please use th
- `bdev:get_hard_disk()` hdd store - a table with all app-related storage partitions, if hard disk capatibility exists
- `bdev:get_removable_disk()` removable data object (drive)
- `bdev:get_cloud_disk(storage_name)` - Get named cloud storage
- `bdev:sync()` - Write/store all opened and maybe changed data
- `bdev:sync()` - Write/store all opened and maybe changed data (cached)
- `bdev:sync_cloud()` - Write/store all opened and maybe changed data in cloud
### Storage methods
- `get_boot_disk()` - Check which device can be booted. possible return value is "hdd" or "removable"

View File

@ -58,5 +58,6 @@ laptop.register_app("removable", {
end
end
mtos.bdev:sync()
laptop.mtos_cache:free(mtos.pos)
end,
})

View File

@ -178,7 +178,10 @@ function bdev:sync()
end
self.removable_disk.inv:set_stack("main", 1, self.removable_disk.stack)
end
end
-- Save all data if used
function bdev:sync_cloud()
-- Modmeta (Cloud)
if self.cloud_disk then
for store, value in pairs(self.cloud_disk) do

View File

@ -2,6 +2,7 @@
laptop.node_config = {}
local function on_construct(pos)
laptop.mtos_cache:free(pos)
local mtos = laptop.os_get(pos)
local node = minetest.get_node(pos)
local hwdef = laptop.node_config[node.name]
@ -15,6 +16,10 @@ local function on_construct(pos)
end
end
local function after_destruct(pos, oldnode)
laptop.mtos_cache:free(pos)
end
local function on_punch(pos, node, puncher)
local mtos = laptop.os_get(pos)
@ -36,6 +41,7 @@ local function on_punch(pos, node, puncher)
-- reload OS
slot:reload(punch_item)
mtos.bdev:sync()
laptop.mtos_cache:free(pos)
for k,v in pairs(laptop.os_get(mtos.pos)) do
mtos[k] = v
end
@ -186,6 +192,7 @@ function laptop.register_hardware(name, hwdef)
-- def.preserve_metadata = preserve_metadata TODO: if MT-0.5 stable
def.on_punch = on_punch
def.on_construct = on_construct
def.after_destruct = after_destruct
def.on_receive_fields = on_receive_fields
def.allow_metadata_inventory_move = allow_metadata_inventory_move
def.allow_metadata_inventory_put = allow_metadata_inventory_put

View File

@ -64,6 +64,65 @@ laptop.supported_textcolors = {
}
-----------------------------------------------------
-- Operating System cache
-----------------------------------------------------
local mtos_cache = {
list = {},
is_running = false
}
laptop.mtos_cache = mtos_cache
function mtos_cache:get(pos)
local hash = minetest.hash_node_position(pos)
return self.list[hash]
end
function mtos_cache:set(pos, mtos)
local hash = minetest.hash_node_position(pos)
mtos.last_access = os.time()
self.list[hash] = mtos
self:check_free_step()
end
function mtos_cache:free(pos)
local hash = minetest.hash_node_position(pos)
self.list[hash] = nil
end
function mtos_cache:check_free_step()
-- do not start twice
if self.is_running then
return
end
local function check_free(mtos_cache)
local current_time
for hash, mtos in pairs(mtos_cache.list) do
current_time = current_time or os.time()
if mtos.last_access +5 < current_time then -- 5 seconds cache
mtos.bdev:sync()
mtos_cache:free(mtos.pos)
end
end
mtos_cache.is_running = false
mtos_cache:check_free_step()
end
if next(self.list) then
self.is_running = true
minetest.after(1, check_free, self)
end
end
-- Sync all on shutdown
minetest.register_on_shutdown(function()
for hash, mtos in pairs(mtos_cache.list) do
mtos.bdev:sync()
mtos_cache:free(mtos.pos)
end
end)
-----------------------------------------------------
-- Operating System class
-----------------------------------------------------
@ -89,6 +148,7 @@ end
-- Power on the system and start the launcher
function os_class:power_on(new_node_name)
self.bdev:free_ram_disk()
mtos_cache:free(self.pos)
-- update current instance with reinitialized data
for k,v in pairs(laptop.os_get(self.pos)) do
self[k] = v
@ -292,7 +352,8 @@ function os_class:pass_to_app(method, reshow, sender, ...)
end
function os_class:save()
self.bdev:sync()
self.bdev:sync_cloud()
mtos_cache:set(self.pos, self)
end
-- Use parameter and launch the select_file dialog
@ -315,7 +376,14 @@ end
-- Get Operating system object
-----------------------------------------------------
function laptop.os_get(pos)
local self = setmetatable({}, os_class)
-- get OS-object from cache
local self = mtos_cache:get(pos)
if self then
return self
end
-- Create new if not in cache
self = setmetatable({}, os_class)
self.__index = os_class
self.pos = pos
self.node = minetest.get_node(pos)