forked from dingoblog/dingo
/
monitor.go
35 lines (32 loc) · 893 Bytes
/
monitor.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
package utils
import (
"fmt"
"runtime"
"time"
)
type monitorStats struct {
NumGoroutine int
MemAllocated string
MemMalloc string
MemTotal string
MemSys string
MemHeap string
MemGc string
LastGcTime string
}
// ReadMemStats reads and returns various stats from the runtime. Used for
// displaying runtime info is the admin dashboard.
func ReadMemStats() *monitorStats {
m := new(runtime.MemStats)
runtime.ReadMemStats(m)
ms := new(monitorStats)
ms.NumGoroutine = runtime.NumGoroutine()
ms.MemAllocated = FileSize(int64(m.Alloc))
ms.MemTotal = FileSize(int64(m.TotalAlloc))
ms.MemSys = FileSize(int64(m.Sys))
ms.MemHeap = FileSize(int64(m.HeapAlloc))
ms.MemMalloc = FileSize(int64(m.Mallocs))
ms.LastGcTime = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
ms.MemGc = FileSize(int64(m.NextGC))
return ms
}