diff options
author | sumneko <sumneko@hotmail.com> | 2019-04-12 15:30:28 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-04-12 15:30:28 +0800 |
commit | 9297dd189945e8019c4598ad3eb082059ab4a3e7 (patch) | |
tree | 80ad977e95d5af0068db23d2b8016ba6cb444c7e /server/src | |
parent | 2d0a488eb5a871d6f70b3c6288c9c650b6689655 (diff) | |
download | lua-language-server-9297dd189945e8019c4598ad3eb082059ab4a3e7.zip |
统计并回收子线程的内存
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/async/async.lua | 25 | ||||
-rw-r--r-- | server/src/async/proto.lua | 2 | ||||
-rw-r--r-- | server/src/service.lua | 18 |
3 files changed, 42 insertions, 3 deletions
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: <Unknown>'):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 |