zdebug.go 657 Bytes
Newer Older
Vladimir Barsukov's avatar
zdebug  
Vladimir Barsukov committed
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
package zdebug

import (
	"fmt"
	"git.barsukov.pro/barsukov/zgo/zjson"
	"runtime"
	"time"
)

type Monitor struct {
	Alloc,
	TotalAlloc,
	Sys,
	LiveObjects uint64

	NumGC        uint32
	NumGoroutine int
}

func NewMonitor(secs int) {
	go func() {
		var m Monitor
		var rtm runtime.MemStats
		var interval = time.Duration(secs) * time.Second
		for {
			<-time.After(interval)

			runtime.ReadMemStats(&rtm)

			m.NumGoroutine = runtime.NumGoroutine()

			m.Alloc = rtm.Alloc / 1e6
			m.TotalAlloc = rtm.TotalAlloc / 1e6
			m.Sys = rtm.Sys / 1e6
			m.LiveObjects = rtm.Mallocs - rtm.Frees
			m.NumGC = rtm.NumGC

			fmt.Println(zjson.MustString(m))
		}
	}()
}