-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
memory_monitor_linux.go
142 lines (118 loc) · 4.16 KB
/
memory_monitor_linux.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
// +build linux
package utils
import (
"fmt"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"sort"
"strconv"
"strings"
"github.com/alecthomas/units"
"github.com/DataDog/datadog-agent/pkg/util/cgroups"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
// MemoryMonitor monitors cgroups' memory usage
type MemoryMonitor = cgroups.MemoryController
const maxProfileCount = 10
func getActionCallback(action string) (func(), string, error) {
switch action {
case "gc":
return runtime.GC, "garbage collector", nil
case "log":
return func() {}, "nothing", nil
case "profile":
return func() {
tmpDir := os.TempDir()
tmpFiles, err := os.ReadDir(tmpDir)
if err != nil {
log.Errorf("Failed to list old memory profiles: %s", err)
} else {
var oldProfiles []os.FileInfo
for _, tmpFile := range tmpFiles {
if strings.HasPrefix(tmpFile.Name(), "memcg-pprof-heap") {
tmpFileInfo, err := tmpFile.Info()
if err != nil {
log.Errorf("Failed to get file info for: %s", tmpFile.Name())
continue
}
oldProfiles = append(oldProfiles, tmpFileInfo)
}
}
sort.Slice(oldProfiles, func(i, j int) bool {
return oldProfiles[i].ModTime().After(oldProfiles[j].ModTime())
})
for i := len(oldProfiles) - 1; i >= 0 && i >= maxProfileCount-1; i-- {
os.Remove(filepath.Join(tmpDir, oldProfiles[i].Name()))
oldProfiles = oldProfiles[:i]
}
}
memProfile, err := os.CreateTemp(tmpDir, "memcg-pprof-heap")
if err != nil {
log.Errorf("Failed to generate memory profile: %s", err)
return
}
defer func() {
if err := memProfile.Close(); err != nil {
log.Errorf("Failed to generate memory profile: %s", err)
}
}()
if err := pprof.WriteHeapProfile(memProfile); err != nil {
log.Errorf("Failed to generate memory profile: %s", err)
return
}
log.Infof("Wrote memory profile to %s", memProfile.Name())
}, "heap profile", nil
default:
return nil, "", fmt.Errorf("unknown memory controller action '%s'", action)
}
}
// NewMemoryMonitor instantiates a new memory monitor
func NewMemoryMonitor(kind string, containerized bool, pressureLevels map[string]string, thresholds map[string]string) (*MemoryMonitor, error) {
memoryMonitors := make([]cgroups.MemoryMonitor, 0, len(pressureLevels)+len(thresholds))
for pressureLevel, action := range pressureLevels {
actionCallback, name, err := getActionCallback(action)
if err != nil {
return nil, err
}
log.Infof("New memory pressure monitor on level %s with action %s", pressureLevel, name)
memoryMonitors = append(memoryMonitors, cgroups.MemoryPressureMonitor(func() {
log.Infof("Memory pressure reached level '%s', triggering %s", pressureLevel, name)
actionCallback()
}, pressureLevel))
}
for threshold, action := range thresholds {
actionCallback, name, err := getActionCallback(action)
if err != nil {
return nil, err
}
monitorCallback := func() {
log.Infof("Memory pressure above %s threshold, triggering %s", threshold, name)
actionCallback()
}
var memoryMonitor cgroups.MemoryMonitor
threshold = strings.TrimSpace(threshold)
if strings.HasSuffix(threshold, "%") {
percentage, err := strconv.Atoi(strings.TrimSuffix(threshold, "%"))
if err != nil {
return nil, fmt.Errorf("invalid memory threshold '%s': %w", threshold, err)
}
memoryMonitor = cgroups.MemoryPercentageThresholdMonitor(monitorCallback, uint64(percentage), false)
} else {
size, err := units.ParseBase2Bytes(strings.ToUpper(threshold))
if err != nil {
return nil, fmt.Errorf("invalid memory threshold '%s': %w", threshold, err)
}
memoryMonitor = cgroups.MemoryThresholdMonitor(monitorCallback, uint64(size), false)
}
log.Infof("New memory threshold monitor on level %s with action %s", threshold, name)
memoryMonitors = append(memoryMonitors, memoryMonitor)
}
return cgroups.NewMemoryController(kind, containerized, memoryMonitors...)
}