From e1cd2b6866daa172aa19a87019ef38e3daaa702b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Tue, 19 Mar 2019 13:54:05 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E8=AF=8A=E6=96=AD=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/core/diagnostics.lua | 36 +++++++++-------- server/src/service.lua | 88 ++++++++++++++++++++++------------------- server/src/vm/global.lua | 2 +- server/src/vm/library.lua | 2 +- server/src/vm/source.lua | 3 ++ server/src/vm/value.lua | 4 -- server/src/vm/vm.lua | 27 +++++++------ 7 files changed, 86 insertions(+), 76 deletions(-) (limited to 'server/src') diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 24f7132d..def54a35 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -39,38 +39,40 @@ function mt:searchUnusedLocals(callback) end function mt:searchUndefinedGlobal(callback) + local definedGlobal = {} + local definedValue = {} + for name in pairs(config.config.diagnostics.globals) do + definedGlobal[name] = true + end + local envValue = self.vm.env:getValue() + envValue:eachInfo(function (info) + if info.type == 'set child' then + local value = info[2] + definedValue[value] = true + end + end) self.vm:eachSource(function (source) if not source:get 'global' then return end - local value = source:bindValue() - if not value then + local name = source:getName() + if name == '' then return end - if source:action() ~= 'get' then + if definedGlobal[name] then return end - if value:getLib() then + local value = source:bindValue() + if not value then return end - local name = source:getName() - if name == '' then + if definedValue[value] then return end if type(name) ~= 'string' then return end - if config.config.diagnostics.globals[name] then - return - end - local defined = value:eachInfo(function (info) - if info.type == 'set' then - return true - end - end) - if not defined then - callback(source.start, source.finish, name) - end + callback(source.start, source.finish, name) end) end diff --git a/server/src/service.lua b/server/src/service.lua index 2026bfa9..42f0e1a0 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -8,6 +8,7 @@ local core = require 'core' local lang = require 'language' local updateTimer= require 'timer' local buildVM = require 'vm' +local source = require 'vm.source' local ErrorCodes = { -- Defined by JSON RPC @@ -533,49 +534,56 @@ function mt:_loadProto() end end -function mt:onTick() - self:_loadProto() - self:_doCompileTask() - - if os.clock() - self._clock >= 60 then - self._clock = os.clock() - local count = 0 - for _ in pairs(self._file) do - count = count + 1 +function mt:_testMemory() + if os.clock() - self._clock < 60 then + return + end + self._clock = os.clock() + local cachedVM = 0 + for _ in pairs(self._file) do + cachedVM = cachedVM + 1 + end + local aliveVM = 0 + local deadVM = 0 + for vm in pairs(CachedVM) do + if vm:isRemoved() then + deadVM = deadVM + 1 + else + aliveVM = aliveVM + 1 end - local alive = 0 - local dead = 0 - for vm in pairs(CachedVM) do - if vm:isRemoved() then - dead = dead + 1 - else - alive = alive + 1 - end + end + + local alivedSource = 0 + local deadSource = 0 + for _, id in pairs(source.watch) do + if source.list[id] then + alivedSource = alivedSource + 1 + else + deadSource = deadSource + 1 end - local mem = collectgarbage 'count' - log.debug(('\n\z - State\n\z - Mem: [%.3f]kb\n\z - CachedVM: [%d]\n\z - AlivedVM: [%d]\n\z - DeadVM: [%d]'):format( - mem, - count, - alive, - dead - )) - - --TODO source测试 - --local total = 0 - --local alive = 0 - --for source in pairs(CachedSource) do - -- if not source:isDead() then - -- alive = alive + 1 - -- end - -- total = total + 1 - --end - --log.debug(('CachedSource: %d/%d'):format(alive, total)) end + local mem = collectgarbage 'count' + log.debug(('\n\z + State\n\z + Mem: [%.3f]kb\n\z + CachedVM: [%d]\n\z + AlivedVM: [%d]\n\z + DeadVM: [%d]\n\z + AlivedSrc:[%d]\n\z + DeadSrc: [%d]'):format( + mem, + cachedVM, + aliveVM, + deadVM, + alivedSource, + deadSource + )) +end + +function mt:onTick() + self:_loadProto() + self:_doCompileTask() + self:_testMemory() end function mt:listen() diff --git a/server/src/vm/global.lua b/server/src/vm/global.lua index b0f44901..8af38206 100644 --- a/server/src/vm/global.lua +++ b/server/src/vm/global.lua @@ -14,7 +14,7 @@ return function (lsp) global = t._G for k, v in pairs(t) do global:setChild(k, v) - global:addInfo('set child', sourceMgr.dummy(), k) + global:addInfo('set child', sourceMgr.dummy(), k, v) end end if lsp then diff --git a/server/src/vm/library.lua b/server/src/vm/library.lua index 167ac726..24a17389 100644 --- a/server/src/vm/library.lua +++ b/server/src/vm/library.lua @@ -54,7 +54,7 @@ function buildLibValue(lib) for fName, fLib in pairs(lib.child) do local fValue = buildLibValue(fLib) value:rawSet(fName, fValue) - value:addInfo('set child', sourceMgr.dummy(), fName) + value:addInfo('set child', sourceMgr.dummy(), fName, fValue) end end diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua index 0f167063..810db769 100644 --- a/server/src/vm/source.lua +++ b/server/src/vm/source.lua @@ -4,6 +4,7 @@ mt.uri = '' local Id = 0 local List = {} +local Watch = setmetatable({}, {__mode = 'k'}) function mt:bindLocal(loc, action) if loc then @@ -99,6 +100,7 @@ local function instant(source) Id = Id + 1 source.id = Id List[Id] = source + Watch[source] = Id setmetatable(source, mt) return true end @@ -115,5 +117,6 @@ end return { instant = instant, list = List, + watch = Watch, dummy = dummy, } diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index 9b33afe1..a1ee897b 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -39,10 +39,6 @@ local function create (tp, source, literal) end local function isDeadChild(value, index) - local child = value._child[index] - if not child:getSource() then - return true - end for srcId, info in pairs(value._info) do local src = sourceMgr.list[srcId] if src diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index 2a81372b..48736fe4 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -46,13 +46,13 @@ function mt:buildTable(source) if key.index then local index = self:getIndex(obj) key:set('parent', tbl) - tbl:addInfo('set child', key, index) + tbl:addInfo('set child', key, index, value) tbl:setChild(index, value) else if key.type == 'name' then key:set('parent', tbl) key:set('table index', true) - tbl:addInfo('set child', key, key[1]) + tbl:addInfo('set child', key, key[1], value) tbl:setChild(key[1], value) end end @@ -62,17 +62,18 @@ function mt:buildTable(source) if index == #source then value:eachValue(function (_, v) n = n + 1 - tbl:addInfo('set child', obj, n) + tbl:addInfo('set child', obj, n, v) tbl:setChild(n, v) end) else n = n + 1 - tbl:addInfo('set child', obj, n) - tbl:setChild(n, self:getFirstInMulti(value)) + local v = self:getFirstInMulti(value) + tbl:addInfo('set child', obj, n, v) + tbl:setChild(n, v) end else n = n + 1 - tbl:addInfo('set child', obj, n) + tbl:addInfo('set child', obj, n, value) tbl:setChild(n, value) end -- 处理写了一半的 key = value,把name类的数组元素视为哈希键 @@ -387,7 +388,7 @@ function mt:setName(name, source, value) local ENV = self:loadLocal('_ENV') local ENVValue = ENV:getValue() source:bindValue(value, 'set') - ENVValue:addInfo('set child', source, name) + ENVValue:addInfo('set child', source, name, value) ENVValue:setChild(name, value) source:set('global', true) source:set('parentValue', ENVValue) @@ -786,12 +787,12 @@ function mt:setOne(var, value) if key.type == 'index' then local index = self:getIndex(key) key[1]:set('parent', parent) - parent:addInfo('set child', key[1], index) + parent:addInfo('set child', key[1], index, value) parent:setChild(index, value) elseif key.type == 'name' then local index = key[1] key:set('parent', parent) - parent:addInfo('set child', key, index) + parent:addInfo('set child', key, index, value) parent:setChild(index, value) end key:bindValue(value, 'set') @@ -921,11 +922,11 @@ function mt:doFunction(action) source:set('object', parent) if source.type == 'index' then local index = self:getIndex(source) - parent:addInfo('set child', source[1], index) + parent:addInfo('set child', source[1], index, value) parent:setChild(index, value) elseif source.type == 'name' then local index = source[1] - parent:addInfo('set child', source, index) + parent:addInfo('set child', source, index, value) parent:setChild(index, value) end source:bindValue(value, 'set') @@ -948,11 +949,11 @@ function mt:doFunction(action) self:instantSource(source) if source.type == 'index' then local index = self:getIndex(source) - parent:addInfo('set child', source[1], index) + parent:addInfo('set child', source[1], index, value) parent:setChild(index, value) elseif source.type == 'name' then local index = source[1] - parent:addInfo('set child', source, index) + parent:addInfo('set child', source, index, value) parent:setChild(index, value) end source:bindValue(value, 'set') -- cgit v1.2.3