Skip to content

Commit daff228

Browse files
asiekierkapayonel
authored andcommitted
replace hardcoded hookInterval with bogomips calculator
1 parent cca7f20 commit daff228

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/main/resources/assets/opencomputers/lua/machine.lua

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,44 @@
1-
local hookInterval = 100
1+
local hookInterval = 10000
2+
local function calcHookInterval()
3+
local bogomipsDivider = 0.05
4+
local bogomipsDeadline = computer.realTime() + bogomipsDivider
5+
local ipsCount = 0
6+
local bogomipsBusy = true
7+
local function calcBogoMips()
8+
ipsCount = ipsCount + hookInterval
9+
if computer.realTime() > bogomipsDeadline then
10+
bogomipsBusy = false
11+
end
12+
end
13+
-- The following is a bit of nonsensical-seeming code attempting
14+
-- to cover Lua's VM sufficiently for the IPS calculation.
15+
local bogomipsTmpA = {{["b"]=3, ["d"]=9}}
16+
local function c(k)
17+
if k <= 2 then
18+
bogomipsTmpA[1].d = k / 2.0
19+
end
20+
end
21+
debug.sethook(calcBogoMips, "", hookInterval)
22+
while bogomipsBusy do
23+
local st = ""
24+
for k=2,4 do
25+
st = st .. "a" .. k
26+
c(k)
27+
if k >= 3 then
28+
bogomipsTmpA[1].b = bogomipsTmpA[1].b * (k ^ k)
29+
end
30+
end
31+
end
32+
debug.sethook()
33+
return ipsCount / bogomipsDivider
34+
end
35+
36+
local ipsCount = calcHookInterval()
37+
-- Since our IPS might still be too generous (hookInterval needs to run at most
38+
-- every 0.05 seconds), we divide it further by 10 relative to that.
39+
hookInterval = (ipsCount * 0.005)
40+
if hookInterval < 1000 then hookInterval = 1000 end
41+
242
local deadline = math.huge
343
local hitDeadline = false
444
local function checkDeadline()

0 commit comments

Comments
 (0)