Skip to content

Commit

Permalink
app/fiber: wait till a full event loop iteration ends
Browse files Browse the repository at this point in the history
fiber.top() fills in statistics every event loop iteration,
so if it was just enabled, fiber.top() may contain 'inf's and
'nan's in fiber cpu usage averages because total time consumed by
the main thread was not yet accounted for.
Same stands for viewing top() results for a freshly created fiber:
its metrics will be zero since it hasn't lived a full ev loop iteration
yet.
Fix this by delaying the test till top() results are meaningful and add
minor refactoring.

Follow-up #2694
  • Loading branch information
sergepetrenko committed Nov 14, 2019
1 parent d66f924 commit e7d381c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
32 changes: 23 additions & 9 deletions test/app/fiber.result
Expand Up @@ -1469,6 +1469,16 @@ sum = 0
fiber.top_enable()
---
...
function isnan(num)\
return num ~= num\
end
---
...
-- Wait till a full event loop iteration passes, so that
-- top() contains meaningful results.
while isnan(fiber.top().cpu["1/sched"].instant) do fiber.yield() end
---
...
a = fiber.top()
---
...
Expand Down Expand Up @@ -1504,9 +1514,9 @@ for k, v in pairs(a) do\
end
---
...
sum_inst
sum_inst > 99 and sum_inst < 101 or sum_inst
---
- 100
- true
...
-- not exact due to accumulated integer division errors
sum_avg > 99 and sum_avg < 101 or sum_avg
Expand All @@ -1517,24 +1527,28 @@ tbl = nil
---
...
f = fiber.new(function()\
for i = 1,1000 do end\
fiber.yield()\
tbl = fiber.top().cpu[fiber.self().id()..'/'..fiber.self().name()]\
local fiber_key = fiber.self().id()..'/'..fiber.self().name()\
tbl = fiber.top().cpu[fiber_key]\
while tbl.time == 0 do\
for i = 1,1000 do end\
fiber.yield()\
tbl = fiber.top().cpu[fiber_key]\
end\
end)
---
...
while f:status() ~= 'dead' do fiber.sleep(0.01) end
while f:status() ~= 'dead' do fiber.yield() end
---
...
tbl["average"] > 0
tbl.average > 0
---
- true
...
tbl["instant"] > 0
tbl.instant > 0
---
- true
...
tbl["time"] > 0
tbl.time > 0
---
- true
...
Expand Down
28 changes: 20 additions & 8 deletions test/app/fiber.test.lua
Expand Up @@ -634,6 +634,14 @@ sum = 0
-- gh-2694 fiber.top()
fiber.top_enable()

function isnan(num)\
return num ~= num\
end

-- Wait till a full event loop iteration passes, so that
-- top() contains meaningful results.
while isnan(fiber.top().cpu["1/sched"].instant) do fiber.yield() end

a = fiber.top()
type(a)
-- scheduler is present in fiber.top()
Expand All @@ -652,19 +660,23 @@ for k, v in pairs(a) do\
sum_avg = sum_avg + v["average"]\
end

sum_inst
sum_inst > 99 and sum_inst < 101 or sum_inst
-- not exact due to accumulated integer division errors
sum_avg > 99 and sum_avg < 101 or sum_avg
tbl = nil
f = fiber.new(function()\
for i = 1,1000 do end\
fiber.yield()\
tbl = fiber.top().cpu[fiber.self().id()..'/'..fiber.self().name()]\
local fiber_key = fiber.self().id()..'/'..fiber.self().name()\
tbl = fiber.top().cpu[fiber_key]\
while tbl.time == 0 do\
for i = 1,1000 do end\
fiber.yield()\
tbl = fiber.top().cpu[fiber_key]\
end\
end)
while f:status() ~= 'dead' do fiber.sleep(0.01) end
tbl["average"] > 0
tbl["instant"] > 0
tbl["time"] > 0
while f:status() ~= 'dead' do fiber.yield() end
tbl.average > 0
tbl.instant > 0
tbl.time > 0

fiber.top_disable()
fiber.top()
Expand Down

0 comments on commit e7d381c

Please sign in to comment.