forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
health.go
59 lines (49 loc) · 1.58 KB
/
health.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
package main
import (
"math"
"runtime"
"time"
)
var start = time.Now()
const MB float64 = 1.0 * 1024 * 1024
type HealthStats struct {
Uptime int64 `json:"uptime"`
AllocatedMemory float64 `json:"allocatedMemory"`
TotalAllocatedMemory float64 `json:"totalAllocatedMemory"`
Goroutines int `json:"goroutines"`
GCCycles uint32 `json:"completedGCCycles"`
NumberOfCPUs int `json:"cpus"`
HeapSys float64 `json:"maxHeapUsage"`
HeapAllocated float64 `json:"heapInUse"`
ObjectsInUse uint64 `json:"objectsInUse"`
OSMemoryObtained float64 `json:"OSMemoryObtained"`
}
func GetHealthStats() *HealthStats {
mem := &runtime.MemStats{}
runtime.ReadMemStats(mem)
return &HealthStats{
Uptime: GetUptime(),
AllocatedMemory: toMegaBytes(mem.Alloc),
TotalAllocatedMemory: toMegaBytes(mem.TotalAlloc),
Goroutines: runtime.NumGoroutine(),
NumberOfCPUs: runtime.NumCPU(),
GCCycles: mem.NumGC,
HeapSys: toMegaBytes(mem.HeapSys),
HeapAllocated: toMegaBytes(mem.HeapAlloc),
ObjectsInUse: mem.Mallocs - mem.Frees,
OSMemoryObtained: toMegaBytes(mem.Sys),
}
}
func GetUptime() int64 {
return time.Now().Unix() - start.Unix()
}
func toMegaBytes(bytes uint64) float64 {
return toFixed(float64(bytes)/MB, 2)
}
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}