From 9297dd189945e8019c4598ad3eb082059ab4a3e7 Mon Sep 17 00:00:00 2001 From: sumneko Date: Fri, 12 Apr 2019 15:30:28 +0800 Subject: =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E5=B9=B6=E5=9B=9E=E6=94=B6=E5=AD=90?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E7=9A=84=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/async/async.lua | 25 ++++++++++++++++++++++++- server/src/async/proto.lua | 2 ++ server/src/service.lua | 18 ++++++++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) (limited to 'server/src') diff --git a/server/src/async/async.lua b/server/src/async/async.lua index b385aac8..3401c346 100644 --- a/server/src/async/async.lua +++ b/server/src/async/async.lua @@ -1,23 +1,30 @@ local thread = require 'bee.thread' local errlog = thread.channel 'errlog' + local TaskId = 0 local IdlePool = {} local RunningList = {} +local GCInfo = {} + +thread.newchannel 'gc' local function createTask() TaskId = TaskId + 1 + GCInfo[TaskId] = false local id = TaskId local requestName = 'request' .. tostring(id) local responseName = 'response' .. tostring(id) thread.newchannel(requestName) thread.newchannel(responseName) local buf = ([[ +ID = %d package.cpath = %q package.path = %q local thread = require 'bee.thread' local request = thread.channel(%q) local response = thread.channel(%q) local errlog = thread.channel 'errlog' +local gc = thread.channel 'gc' local function task() local dump, arg = request:bpop() @@ -25,6 +32,7 @@ local function task() IN = request, OUT = response, ERR = errlog, + GC = gc, }, { __index = _ENV }) local f, err = load(dump, '=task', 't', env) if not f then @@ -40,8 +48,10 @@ while true do if not ok then errlog:push(result) end + collectgarbage() + gc:push(ID, collectgarbage 'count') end -]]):format(package.cpath, package.path, requestName, responseName) +]]):format(id, package.cpath, package.path, requestName, responseName) log.debug('Create thread, id: ', id) return { id = id, @@ -87,6 +97,17 @@ local function callback(id, running) end end +local function checkGC() + local gc = thread.channel 'gc' + while true do + local ok, id, count = gc:pop() + if not ok then + break + end + GCInfo[id] = count + end +end + local function onTick() local ok, msg = errlog:pop() if ok then @@ -95,9 +116,11 @@ local function onTick() for id, running in pairs(RunningList) do callback(id, running) end + checkGC() end return { onTick = onTick, run = run, + info = GCInfo, } diff --git a/server/src/async/proto.lua b/server/src/async/proto.lua index ef0b3300..bc2714af 100644 --- a/server/src/async/proto.lua +++ b/server/src/async/proto.lua @@ -53,4 +53,6 @@ end while true do readProto() + collectgarbage() + GC:push(ID, collectgarbage 'count') end diff --git a/server/src/service.lua b/server/src/service.lua index ade3b2eb..f14b0a9b 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -750,9 +750,20 @@ function mt:_testMemory() end local mem = collectgarbage 'count' + local threadInfo = async.info + local threadBuf = {} + for i, count in ipairs(threadInfo) do + if count then + threadBuf[i] = ('#%03d Mem: [%.3f]kb'):format(i, count) + else + threadBuf[i] = ('#%03d Mem: '):format(i) + end + end + log.debug(('\n\z State\n\z - Mem: [%.3f]kb\n\z + Main Mem: [%.3f]kb\n\z + %s\n\z -------------------\n\z CachedVM: [%d]\n\z AlivedVM: [%d]\n\z @@ -769,6 +780,7 @@ function mt:_testMemory() TotalLoc: [%d]\n\z TotalVal: [%d]\n\z'):format( mem, + table.concat(threadBuf, '\n'), cachedVM, aliveVM, @@ -791,7 +803,9 @@ end function mt:onTick() self:_loadProto() self:_doCompileTask() - if os.clock() - self._clock >= 60 and not self:isWaitingCompile() then + if (os.clock() - self._clock >= 60 and not self:isWaitingCompile()) + or (os.clock() - self._clock >= 300) + then self._clock = os.clock() self:_testMemory() end -- cgit v1.2.3