-
Notifications
You must be signed in to change notification settings - Fork 17
/
system_stats.go
88 lines (72 loc) · 2.2 KB
/
system_stats.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package supervisor
//#cgo LDFLAGS: -lsigar
//#include <sigar.h>
//#include <sigar_control_group.h>
import "C"
import (
"fmt"
)
const (
SIGAR_CGROUP_SUPPORTED = 1
BYTES_TO_MB = float64(1024 * 1024)
)
type systemConfig struct {
handle *C.sigar_t
pid C.sigar_pid_t
}
func NewSystemConfig() (*systemConfig, error) {
var handle *C.sigar_t
if err := C.sigar_open(&handle); err != C.SIGAR_OK {
return nil, fmt.Errorf(fmt.Sprintf("Fail to open sigar. Error code = %v", err))
}
s := &systemConfig{}
s.handle = handle
s.pid = C.sigar_pid_get(handle)
return s, nil
}
func (s *systemConfig) Close() {
C.sigar_close(s.handle)
}
// Returns cgroup memory limit in MB
// Returns -1 if cgroups are not supported OR max memory configured is <= 0
// mem limit will be atleast 1MB if cgroup is defined
func (stats *systemConfig) getCgroupMemLimit() (float64, bool) {
cgroupInfo := stats.GetControlGroupInfo()
if cgroupInfo.Supported == SIGAR_CGROUP_SUPPORTED {
cGroupTotal := cgroupInfo.MemoryMax
if cGroupTotal <= 0 {
// cgroup is memory max not defined even if cgroup is supported
return float64(cGroupTotal), true
}
memLimitInMB := float64(cGroupTotal) / BYTES_TO_MB
if memLimitInMB < 1 {
memLimitInMB = 1
}
return memLimitInMB, true
}
return -1, false
}
// SystemTotalMem gets the total memory in bytes available to this Go runtime
// on the bare node's total memory
func (s *systemConfig) SystemTotalMem() (float64, error) {
var mem C.sigar_mem_t
if err := C.sigar_mem_get(s.handle, &mem); err != C.SIGAR_OK {
return 0, fmt.Errorf("Fail to get total memory. Err=%v", C.sigar_strerror(s.handle, err))
}
return float64(mem.total) / BYTES_TO_MB, nil
}
type sigarControlGroupInfo struct {
Supported uint8 // "1" if cgroup info is supprted, "0" otherwise
Version uint8 // "1" for cgroup v1, "2" for cgroup v2
// Maximum memory available in the group. Derived from memory.max
MemoryMax uint64
}
func (h *systemConfig) GetControlGroupInfo() *sigarControlGroupInfo {
var info C.sigar_control_group_info_t
C.sigar_get_control_group_info(&info)
return &sigarControlGroupInfo{
Supported: uint8(info.supported),
Version: uint8(info.version),
MemoryMax: uint64(info.memory_max),
}
}