diff --git a/changelog.md b/changelog.md index 0cf094b2b..f87ef3e54 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,10 @@ ## Unreleased +## 3.17.1 +`2026-01-20` +* `FIX` Fixed a CPU usage issue again + ## 3.17.0 `2026-01-19` * `NEW` Support `fun` syntax for inline generic function types in `@field` and `@type` annotations [#1170](https://github.com/LuaLS/lua-language-server/issues/1170) diff --git a/script/brave/brave.lua b/script/brave/brave.lua index 685f8bcf5..26a9b3c9c 100644 --- a/script/brave/brave.lua +++ b/script/brave/brave.lua @@ -1,6 +1,5 @@ -local thread = require 'bee.thread' -local channelMod = require 'bee.channel' -local selectMod = require 'bee.select' +local channel = require 'bee.channel' +local epoll = require 'bee.epoll' local reqPad local resPad @@ -18,8 +17,8 @@ m.queue = {} function m.register(id, taskChName, replyChName) m.id = id - reqPad = channelMod.query(taskChName) - resPad = channelMod.query(replyChName) + reqPad = channel.query(taskChName) + resPad = channel.query(replyChName) assert(reqPad, 'task channel not found: ' .. taskChName) assert(resPad, 'reply channel not found: ' .. replyChName) @@ -53,37 +52,32 @@ end --- 开始找工作 function m.start() - local selector = selectMod.create() - selector:event_add(reqPad:fd(), selectMod.SELECT_READ) + local epfd = assert(epoll.create(16)) + epfd:event_add(reqPad:fd(), epoll.EPOLLIN) m.push('mem', collectgarbage 'count') while true do - -- 使用 select 实现阻塞等待 - local name, id, params - while true do - local ok, n, i, p = reqPad:pop() - if ok then - name, id, params = n, i, p - break + for _, event in epfd:wait() do + if event & epoll.EPOLLIN ~= 0 then + local ok, name, id, params = reqPad:pop() + if ok then + local ability = m.ability[name] + if not ability then + resPad:push(id) + log.error('Brave can not handle this work: ' .. name) + goto CONTINUE + end + local suc, res = xpcall(ability, log.error, params) + if suc then + resPad:push(id, res) + else + resPad:push(id) + end + m.push('mem', collectgarbage 'count') + ::CONTINUE:: + end end - selector:wait(-1) end - - local ability = m.ability[name] - -- TODO - if not ability then - resPad:push(id) - log.error('Brave can not handle this work: ' .. name) - goto CONTINUE - end - local ok, res = xpcall(ability, log.error, params) - if ok then - resPad:push(id, res) - else - resPad:push(id) - end - m.push('mem', collectgarbage 'count') - ::CONTINUE:: end end diff --git a/script/pub/pub.lua b/script/pub/pub.lua index a5fd2058d..d4f962703 100644 --- a/script/pub/pub.lua +++ b/script/pub/pub.lua @@ -133,6 +133,7 @@ function m.pushTask(info) -- 找到空闲 brave,直接推送 brave.busy = true brave.currentTask = info.id + log.debug('push task to brave:', brave.id, info.name, info.id) brave.taskCh:push(info.name, info.id, info.params) m.taskMap[info.id] = info return true @@ -145,13 +146,8 @@ function m.popTask(brave, id, result) log.warn(('Brave pushed unknown task result: # %d => [%d]'):format(brave.id, id)) return end + log.debug('pop task from brave:', brave.id, info.name, id) m.taskMap[id] = nil - if not info.removed then - info.removed = true - if info.callback then - xpcall(info.callback, log.error, result) - end - end -- 任务完成,标记为空闲 brave.busy = false @@ -165,10 +161,18 @@ function m.popTask(brave, id, result) table.remove(queue, i) brave.busy = true brave.currentTask = nextTask.id + log.debug('push task to brave:', brave.id, nextTask.name, nextTask.id) brave.taskCh:push(nextTask.name, nextTask.id, nextTask.params) break end end + + if not info.removed then + info.removed = true + if info.callback then + xpcall(info.callback, log.error, result) + end + end end --- 从勇者处接收报告 diff --git a/script/service/service.lua b/script/service/service.lua index 116bc3bfe..5518a23f5 100644 --- a/script/service/service.lua +++ b/script/service/service.lua @@ -193,8 +193,9 @@ function m.eventLoop() while true do net.update() - local clock = os.clock() - while os.clock() - clock < 0.1 do + log.debug('net update') + local clock = time.monotonic() + while time.monotonic() - clock < 100 do doSomething() end if doSomething() then