summaryrefslogtreecommitdiff
path: root/server-beta/src/service/service.lua
blob: 2c576a72c80d8cc9b0246bb8c9c4b4e2adf836a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
local pub        = require 'pub'
local thread     = require 'bee.thread'
local task       = require 'task'
local timer      = require 'timer'
local proto      = require 'proto'

local m = {}
m.type = 'service'

function m.reportMemory()
    local mems = {}
    local totalMem = 0
    mems[0] = collectgarbage 'count'
    totalMem = totalMem + collectgarbage 'count'
    for id, brave in ipairs(pub.braves) do
        mems[id] = brave.memory
        totalMem = totalMem + brave.memory
    end

    local lines = {}
    lines[#lines+1] = '    --------------- Memory ---------------'
    lines[#lines+1] = ('        Total: %.3f MB'):format(totalMem / 1000.0)
    for i = 0, #mems do
        lines[#lines+1] = ('        # %02d : %.3f MB'):format(i, mems[i] / 1000.0)
    end
    return table.concat(lines, '\n')
end

function m.report()
    local t = timer.loop(60.0, function ()
        local lines = {}
        lines[#lines+1] = ''
        lines[#lines+1] = '========= Medical Examination Report ========='
        lines[#lines+1] = m.reportMemory()
        lines[#lines+1] = '=============================================='

        log.debug(table.concat(lines, '\n'))
    end)
    t:onTimer()
end

function m.startTimer()
    local last = os.clock()
    while true do
        thread.sleep(0.001)
        local current = os.clock()
        local delta = current - last
        last = current
        timer.update(delta)
    end
end

function m.start()
    pub.recruitBraves(4)
    task.setErrorHandle(log.error)
    proto.listen()
    pub.listen()
    m.report()

    m.startTimer()
end

return m