Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
[Profnsched] lost jobs correction
Browse files Browse the repository at this point in the history
Modified to replace # when we ant to count items in an array.
Code currently not cleaned after correction.
  • Loading branch information
Coethium committed Oct 29, 2016
1 parent 7ddebdd commit 8a78752
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
10 changes: 9 additions & 1 deletion mods/profnsched/after.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ dofile(minetest.get_modpath("profnsched").."/queue.lua")
local jobs = {}
local scheduler = scheduler

function getnjobs()
return #jobs
end

-- For minetest.after replacement
local function check_expired_jobs()
local time = core.get_us_time()
local tjobs = {}
for i,job in pairs(jobs) do
if time >= job.expire then
scheduler.add(1, job)
jobs[i] = nil
--jobs[i] = nil
else
tjobs[#tjobs+1] = job
end
end
jobs = tjobs
scheduler.asap(4, check_expired_jobs)
end
scheduler.asap(4, check_expired_jobs)
Expand Down
13 changes: 8 additions & 5 deletions mods/profnsched/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,26 @@ old_globalstep(function(dtime)
local njb = scheduler.waitingjobs()
local jbdone = 0
for class,q in ipairs(scheduler.queue) do
local grp = q.groups[q.first]
for i,job in pairs(grp) do
--local grp = q.cur
for i,job in pairs(q.cur) do
tbegin = core.get_us_time()
core.set_last_run_mod(job.mod_name)
job.func_code(unpack(job.arg))
jbdone = jbdone+1
current_durations[#current_durations+1] = {job.mod_name, job.func_id, core.get_us_time()-tbegin}
grp[i] = nil
q.cur[i] = nil
if class > 1 and ((core.get_us_time()-launch_dtime) > tick_dtime) then --class 1 fully processed even on overload
break
end
end
if ((core.get_us_time()-launch_dtime) > tick_dtime) then
break
end
end
end
--minetest.debug("[Profnsched] "..scheduler.waitingjobs()+getnjobs())
scheduler.shift()

--minetest.debug("[Profnsched] "..scheduler.waitingjobs()+getnjobs())

local elapsed = (core.get_us_time()-launch_dtime)

-- update all durations
Expand All @@ -161,6 +163,7 @@ old_globalstep(function(dtime)
if active and dump_delay == 0 then
minetest.log("[Profnsched] Overload ! "..mathfloor(elapsed)/1000 .."ms")
dump_durations(current_durations)
--scheduler.fulldebug()
end
else
if active and dump_delay == 0 then
Expand Down
58 changes: 38 additions & 20 deletions mods/profnsched/queue.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
scheduler = {}
scheduler.queue = {[1]={first=1, last=2, groups={[1]={}, [2]={}}}}
-- scheduler.queue = {[1]={first=1, last=2, groups={[1]={}, [2]={}}}}
scheduler.queue = {[1]={cur={}, nxt={}, ncur=1, nnxt=1}}

function scheduler.add(priority, job)
-- get asked class
local class = scheduler.queue[priority]
local p = priority
while not class do -- create all classes under 'priority'
scheduler.queue[p] = {first=1, last=2, groups={[1]={}, [2]={}}}
--scheduler.queue[p] = {first=1, last=2, groups={[1]={}, [2]={}}}
scheduler.queue[p] = {cur={}, nxt={}, ncur=1, nnxt=1}
p = p-1
class = scheduler.queue[p]
end
class = scheduler.queue[priority]
class.nxt[class.nnxt] = job
class.nnxt = class.nnxt+1
-- get last group
local grp = class.groups[class.last]
--local grp = class.groups[class.last]
-- add job into last group
grp[#grp+1] = job
--grp[#grp+1] = job
end

function scheduler.asap(priority, func)
Expand All @@ -32,9 +36,27 @@ end

function scheduler.shift()
local nb = scheduler.waitingjobs()
local tsrc = nil
local tdst = nil
local qnext = nil
for class,q in ipairs(scheduler.queue) do
q.cur = q.nxt
q.nxt = {}
q.ncur = q.nnxt
q.nnxt = 1

--local tnext = class+1
qnext = scheduler.queue[class+1]
if qnext then
local src = qnext.cur
for i,j in pairs(src) do
q.cur[q.ncur] = j
q.ncur = q.ncur+1
src[i] = nil
end
qnext.cur = {}
qnext.ncur = 1
end
end
--[[
q.groups[q.first] = q.groups[q.last]
q.groups[q.last] = {}
local tnext = class+1
Expand All @@ -47,36 +69,32 @@ function scheduler.shift()
tsrc[i] = nil
end
end
end
end]]
if nb ~= scheduler.waitingjobs() then --This should never happen, left because it was used during debug phase
mdebug("ERROR, This should never happen ! Lost jobs, some mod may not work from now, please restart the server.")
end
--
end

function scheduler.fulldebug()
minetest.log("[Profnsched]"..table.getn(scheduler.queue).." classes")
minetest.log("[Profnsched]"..#scheduler.queue.." classes")
for class,q in pairs(scheduler.queue) do
minetest.log("[Profnsched] class "..class..":")
minetest.log("[Profnsched] "..q.first.." "..q.last.." ("..q.last-q.first+1 .." groups)")
for i,grp in pairs(q.groups) do
local n = 0
for j,jb in pairs(grp) do
n = n+1
end
minetest.log("[Profnsched] group "..i..", "..n.." jobs")
end
minetest.log("[Profnsched] current "..q.ncur)
minetest.log("[Profnsched] next "..q.nnxt)
end
minetest.log("[Profnsched] end")
end


function scheduler.waitingjobs()
local n = 0
for class, q in pairs(scheduler.queue) do
for i,grp in pairs(q.groups) do
for j,jb in pairs(grp) do
n = n+1
end
for i,grp in pairs(q.cur) do
n = n+1
end
for i,grp in pairs(q.nxt) do
n = n+1
end
end
return n
Expand Down

2 comments on commit 8a78752

@BetterToAutomateTheWorld
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peux-tu faire un retour en local voir si tout est fonctionnel ?
Je suis entrain de merger le serveur, je n'aurais qu'a activer dans la world.mt le mod si celui-ci est prêt :)

@Coethium
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dans l'issue #514 j'indique avoir testé pendant environ 5mn (construction, minage, déplacements, dégats, décès, ...) mais en effet je demande si qq'un d'autre peut tester sur son ordi puisque sur le mien on avait constaté que ce n'était pas représentatif (je n'ai jamais vu le bug).

Please sign in to comment.