/
gamelbc.go
39 lines (32 loc) · 922 Bytes
/
gamelbc.go
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
package gamelbc
import (
"os"
"context"
"time"
"github.com/shirou/gopsutil/process"
"github.com/xiaonanln/goworld/engine/dispatchercluster"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/gwutils"
"github.com/xiaonanln/goworld/engine/proto"
)
func Initialize(ctx context.Context, collectInterval time.Duration) {
pid := os.Getpid()
p, err := process.NewProcess(int32(pid))
if err != nil {
gwlog.Fatalf("can not find game process: pid = %v", pid)
}
gwlog.Infof("gamelbc: found game process: %s", p)
go gwutils.RepeatUntilPanicless(func() {
for {
time.Sleep(collectInterval)
pcnt, err := p.CPUPercentWithContext(ctx)
if err != nil {
gwlog.Panicf("gamelbc: get process cpu percent failed: %s", err)
}
gwlog.Debugf("gamelbc: cpu percent is %.3f%%", pcnt)
dispatchercluster.SendGameLBCInfo(proto.GameLBCInfo{
CPUPercent: pcnt,
})
}
})
}