2020-05-05 15:42:54 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RuntimeInfo represents memory and cpu usage statistics.
|
|
|
|
type RuntimeInfo struct {
|
|
|
|
Cores int `json:"cores"`
|
|
|
|
Routines int `json:"routines"`
|
|
|
|
Memory struct {
|
2020-05-05 18:26:44 +02:00
|
|
|
Used uint64 `json:"used"`
|
|
|
|
Reserved uint64 `json:"reserved"`
|
|
|
|
Info string `json:"info"`
|
2020-05-05 15:42:54 +02:00
|
|
|
} `json:"memory"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRuntimeInfo returns a new RuntimeInfo instance.
|
|
|
|
func NewRuntimeInfo() (r RuntimeInfo) {
|
|
|
|
r = RuntimeInfo{}
|
|
|
|
r.Refresh()
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-12-18 20:42:12 +01:00
|
|
|
// Refresh updates runtime info options like number of goroutines and memory usage.
|
2020-05-05 15:42:54 +02:00
|
|
|
func (r *RuntimeInfo) Refresh() {
|
|
|
|
r.Cores = runtime.NumCPU()
|
|
|
|
r.Routines = runtime.NumGoroutine()
|
|
|
|
var mem runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&mem)
|
|
|
|
r.Memory.Used = mem.Alloc
|
|
|
|
r.Memory.Reserved = mem.Sys
|
|
|
|
r.Memory.Info = fmt.Sprintf("Used %s / Reserved %s", humanize.Bytes(r.Memory.Used), humanize.Bytes(r.Memory.Reserved))
|
|
|
|
}
|