-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
71 lines (58 loc) · 1.29 KB
/
runtime.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gotk
import (
"fmt"
"runtime"
"strconv"
"time"
)
type RuntimeInfo struct {
handler func(map[string]string)
seconds int64
ch chan struct{}
ticker *time.Ticker
}
func NewRuntimeInfo(handler func(map[string]string), seconds int64) *RuntimeInfo {
if seconds <= 0 {
panic("invalid seconds")
}
return &RuntimeInfo{
handler: handler,
seconds: seconds,
ch: make(chan struct{}),
ticker: time.NewTicker(time.Duration(seconds) * time.Second),
}
}
func (item *RuntimeInfo) collect() map[string]string {
var m runtime.MemStats
runtime.ReadMemStats(&m)
data := make(map[string]string)
uint64ToStr := func(size uint64) string {
return fmt.Sprintf("%.3fMiB", float64(size/1024.0/1024.0))
}
data["num_goroutine"] = strconv.Itoa(runtime.NumGoroutine())
data["heap_objects"] = strconv.FormatUint(m.HeapObjects, 10)
data["heap_alloc"] = uint64ToStr(m.HeapAlloc)
data["total_alloc"] = uint64ToStr(m.TotalAlloc)
data["stack_inuse"] = uint64ToStr(m.StackInuse)
return data
}
func (item *RuntimeInfo) Start() {
go func() {
ok := true
for {
select {
case <-item.ch:
ok = false
case _, ok = <-item.ticker.C:
}
if !ok {
return
}
item.handler(item.collect())
}
}()
}
func (item *RuntimeInfo) End() {
item.ticker.Stop()
item.ch <- struct{}{}
}