1
0
mirror of https://github.com/20kdc/OC-KittenOS.git synced 2024-11-08 11:38:07 +11:00
OC-KittenOS/code/apps/app-taskmgr.lua
20kdc 7bde8fee55 Finish lowering memory use, R1
Since this is after the technical "release", version numbers have been bumped to 1.

Changes before this commit for R1:
 Kernel memory usage reduction schemes, with some security fixes.
 Still need to deal w/ proxies (see later)
Changes in this commit:
 Some various little things in apps
 CLAW inet actually works now on 192K
 sys-icecap no longer uses the event/neoux combination,
  and now handles Everest disappearance as a mass-close,
  but still handles Everest not being around on window create.
 So it still handles every situation that matters.
 neoux no longer handles everest crash protection.
 Security policy and filedialog obviously don't use neoux anymore.
 Kernel now only guarantees parsing, not event-loop, by executeAsync
 This is safer and allows app-launcher to get rid of NeoUX by
  any means necessary.
 wrapMeta cache now exists, and proxies get wrapMeta'd to deal with
  various low-priority security shenanigans.
 This is a *stopgap* until I work out how to force OCEmu to give me
  totally accurate boot-time memory figures, so I can create the
  ultimate lowmem proxy. I'm calling it "puppet". FG knows why.
2018-03-30 13:37:02 +01:00

133 lines
3.2 KiB
Lua

-- This is released into the public domain.
-- No warranty is provided, implied or otherwise.
-- app-taskmgr: Task manager
-- a-hello : simple test program for Everest.
local everest = neo.requireAccess("x.neo.pub.window", "main window")
local kill = neo.requestAccess("k.kill")
local sW, sH = 20, 8
local headlines = 2
local window = everest(sW, sH)
local lastIdleTimeTime = os.uptime()
local lastIdleTime = neo.totalIdleTime()
local cpuPercent = 100
local lastCPUTimeRecord = {}
local cpuCause = "(none)"
local camY = 1
-- elements have {pid, text}
local consistentProcList = {}
local function drawLine(n)
local red = false
local idx = (camY + n) - (headlines + 1)
local stat = ("~"):rep(sW)
if n == 1 then
-- x.everest redraw. Since this window only has one line...
local usage = math.floor((os.totalMemory() - os.freeMemory()) / 1024)
stat = usage .. "/" .. math.floor(os.totalMemory() / 1024) .. "K, CPU " .. cpuPercent .. "%"
red = true
elseif n == 2 then
stat = "MAX:" .. cpuCause
red = true
elseif consistentProcList[idx] then
if idx == camY then
stat = ">" .. consistentProcList[idx][2]
else
stat = " " .. consistentProcList[idx][2]
end
end
stat = unicode.safeTextFormat(stat)
while unicode.len(stat) < sW do stat = stat .. " " end
if red then
window.span(1, n, unicode.sub(stat, 1, sW), 0xFFFFFF, 0)
else
window.span(1, n, unicode.sub(stat, 1, sW), 0x000000, 0xFFFFFF)
end
end
local function updateConsistentProcList(pt, lp)
local tbl = {}
local tbl2 = {}
local tbl3 = {}
for _, v in ipairs(pt) do
table.insert(tbl, v[1])
tbl2[v[1]] = v[2] .. "/" .. v[1] .. " " .. tostring(lp[v[1]]) .. "%"
end
table.sort(tbl)
for k, v in ipairs(tbl) do
tbl3[k] = {v, tbl2[v]}
end
consistentProcList = tbl3
end
local p = os.uptime()
neo.scheduleTimer(p)
while true do
local n = {coroutine.yield()}
if n[1] == "x.neo.pub.window" then
if n[3] == "line" then
drawLine(n[4])
end
if n[3] == "close" then
return
end
if n[3] == "key" then
if n[6] then
if n[4] == 8 then
if consistentProcList[camY] then
kill(consistentProcList[camY][1])
end
end
if n[5] == 200 then
camY = camY - 1
if camY < 1 then camY = 1 end
for i = (headlines + 1), sH do drawLine(i) end
end
if n[5] == 208 then
camY = camY + 1
for i = (headlines + 1), sH do drawLine(i) end
end
end
end
end
if n[1] == "k.timer" then
local now = os.uptime()
local nowIT = neo.totalIdleTime()
local tD = now - lastIdleTimeTime
local iD = nowIT - lastIdleTime
cpuPercent = math.ceil(((tD - iD) / tD) * 100)
lastIdleTimeTime = now
lastIdleTime = nowIT
local newRecord = {}
cpuCause = "(none)"
local causeUsage = 0
local pt = neo.listProcs()
local localPercent = {}
for _, v in ipairs(pt) do
-- pkg, pid, cpuTime
local baseline = 0
if lastCPUTimeRecord[v[1]] then
baseline = lastCPUTimeRecord[v[1]]
end
local val = v[3] - baseline
localPercent[v[1]] = math.ceil(100 * (val / tD))
if causeUsage < val then
cpuCause = v[2] .. "/" .. v[1]
causeUsage = val
end
newRecord[v[1]] = v[3]
end
lastCPUTimeRecord = newRecord
updateConsistentProcList(pt, localPercent)
for i = 1, sH do drawLine(i) end
p = p + 1
neo.scheduleTimer(p)
end
end