-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathburnmem.go
312 lines (281 loc) · 9.18 KB
/
burnmem.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"path"
"strings"
"time"
"github.com/chaosblade-io/chaosblade-spec-go/channel"
"github.com/chaosblade-io/chaosblade-spec-go/spec"
"github.com/chaosblade-io/chaosblade-spec-go/util"
"github.com/containerd/cgroups"
v1 "github.com/containerd/cgroups/stats/v1"
"github.com/shirou/gopsutil/mem"
"github.com/sirupsen/logrus"
"github.com/chaosblade-io/chaosblade-exec-os/exec"
"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
)
const PageCounterMax uint64 = 9223372036854770000
const (
//processOOMScoreAdj = "/proc/%s/oom_score_adj"
//oomMinScore = "-1000"
processOOMAdj = "/proc/%s/oom_adj"
oomMinAdj = "-17"
)
// 128K
type Block [32 * 1024]int32
var (
burnMemStart, burnMemStop, burnMemNohup, includeBufferCache, avoidBeingKilled bool
memPercent, memReserve, memRate int
burnMemMode string
)
func main() {
flag.BoolVar(&burnMemStart, "start", false, "start burn memory")
flag.BoolVar(&burnMemStop, "stop", false, "stop burn memory")
flag.BoolVar(&burnMemNohup, "nohup", false, "nohup to run burn memory")
flag.BoolVar(&includeBufferCache, "include-buffer-cache", false, "ram model mem-percent is exclude buffer/cache")
flag.BoolVar(&avoidBeingKilled, "avoid-being-killed", false, "prevent mem-burn process from being killed by oom-killer")
flag.IntVar(&memPercent, "mem-percent", 0, "percent of burn memory")
flag.IntVar(&memReserve, "reserve", 0, "reserve to burn memory, unit is M")
flag.IntVar(&memRate, "rate", 100, "burn memory rate, unit is M/S, only support for ram mode")
flag.StringVar(&burnMemMode, "mode", "cache", "burn memory mode, cache or ram")
bin.ParseFlagAndInitLog()
if burnMemStart {
startBurnMem()
} else if burnMemStop {
if success, errs := stopBurnMem(); !success {
bin.PrintErrAndExit(errs)
}
} else if burnMemNohup {
if burnMemMode == "cache" {
burnMemWithCache()
} else if burnMemMode == "ram" {
burnMemWithRam()
}
} else {
bin.PrintAndExitWithErrPrefix("less --start or --stop flag")
}
}
var dirName = "burnmem_tmpfs"
var fileName = "file"
var fileCount = 1
func burnMemWithRam() {
tick := time.Tick(time.Second)
var cache = make(map[int][]Block, 1)
var count = 1
cache[count] = make([]Block, 0)
if memRate <= 0 {
memRate = 100
}
for range tick {
_, expectMem, err := calculateMemSize(memPercent, memReserve)
if err != nil {
stopBurnMemFunc()
bin.PrintErrAndExit(err.Error())
}
fillMem := expectMem
if expectMem > 0 {
if expectMem > int64(memRate) {
fillMem = int64(memRate)
} else {
fillMem = expectMem / 10
if fillMem == 0 {
continue
}
}
fillSize := int(8 * fillMem)
buf := cache[count]
if cap(buf)-len(buf) < fillSize &&
int(math.Floor(float64(cap(buf))*1.25)) >= int(8*expectMem) {
count += 1
cache[count] = make([]Block, 0)
buf = cache[count]
}
logrus.Debugf("count: %d, len(buf): %d, cap(buf): %d, expect mem: %d, fill size: %d",
count, len(buf), cap(buf), expectMem, fillSize)
cache[count] = append(buf, make([]Block, fillSize)...)
}
}
}
func burnMemWithCache() {
filePath := path.Join(path.Join(util.GetProgramPath(), dirName), fileName)
tick := time.Tick(time.Second)
for range tick {
_, expectMem, err := calculateMemSize(memPercent, memReserve)
if err != nil {
stopBurnMemFunc()
bin.PrintErrAndExit(err.Error())
}
fillMem := expectMem
if expectMem > 0 {
if expectMem > int64(memRate) {
fillMem = int64(memRate)
}
nFilePath := fmt.Sprintf("%s%d", filePath, fileCount)
response := cl.Run(context.Background(), "dd", fmt.Sprintf("if=/dev/zero of=%s bs=1M count=%d", nFilePath, fillMem))
if !response.Success {
stopBurnMemFunc()
bin.PrintErrAndExit(response.Error())
}
fileCount++
}
}
}
var burnMemBin = exec.BurnMemBin
var cl = channel.NewLocalChannel()
var stopBurnMemFunc = stopBurnMem
var runBurnMemFunc = runBurnMem
func startBurnMem() {
ctx := context.Background()
if burnMemMode == "cache" {
if !cl.IsCommandAvailable("mount") {
bin.PrintErrAndExit(spec.CommandMountNotFound.Msg)
}
flPath := path.Join(util.GetProgramPath(), dirName)
if _, err := os.Stat(flPath); err != nil {
err = os.Mkdir(flPath, os.ModePerm)
if err != nil {
bin.PrintErrAndExit(err.Error())
}
}
response := cl.Run(ctx, "mount", fmt.Sprintf("-t tmpfs tmpfs %s -o size=", flPath)+"100%")
if !response.Success {
bin.PrintErrAndExit(response.Error())
}
}
runBurnMemFunc(ctx, memPercent, memReserve, memRate, burnMemMode, includeBufferCache)
}
func runBurnMem(ctx context.Context, memPercent, memReserve, memRate int, burnMemMode string, includeBufferCache bool) {
args := fmt.Sprintf(`%s --nohup --mem-percent %d --reserve %d --rate %d --mode %s --include-buffer-cache=%t`,
path.Join(util.GetProgramPath(), burnMemBin), memPercent, memReserve, memRate, burnMemMode, includeBufferCache)
args = fmt.Sprintf(`%s > /dev/null 2>&1 &`, args)
response := cl.Run(ctx, "nohup", args)
if !response.Success {
stopBurnMemFunc()
bin.PrintErrAndExit(response.Err)
}
// check pid
newCtx := context.WithValue(context.Background(), channel.ProcessKey, "--nohup")
pids, err := cl.GetPidsByProcessName(burnMemBin, newCtx)
if err != nil {
stopBurnMemFunc()
bin.PrintErrAndExit(fmt.Sprintf("run burn memory by %s mode failed, cannot get the burning program pid, %v",
burnMemMode, err))
}
if len(pids) == 0 {
stopBurnMemFunc()
bin.PrintErrAndExit(fmt.Sprintf("run burn memory by %s mode failed, cannot find the burning program pid",
burnMemMode))
}
// adjust process oom_score_adj to avoid being killed
if avoidBeingKilled {
for _, pid := range pids {
scoreAdjFile := fmt.Sprintf(processOOMAdj, pid)
if _, err := os.Stat(scoreAdjFile); os.IsNotExist(err) {
continue
}
if err := ioutil.WriteFile(scoreAdjFile, []byte(oomMinAdj), 0644); err != nil {
stopBurnMemFunc()
bin.PrintErrAndExit(fmt.Sprintf("run burn memory by %s mode failed, cannot edit the process oom_score_adj",
burnMemMode))
}
}
}
}
func stopBurnMem() (success bool, errs string) {
ctx := context.WithValue(context.Background(), channel.ProcessKey, "nohup")
ctx = context.WithValue(ctx, channel.ExcludeProcessKey, "stop")
pids, _ := cl.GetPidsByProcessName(burnMemBin, ctx)
var response *spec.Response
if pids != nil && len(pids) != 0 {
response = cl.Run(ctx, "kill", fmt.Sprintf(`-9 %s`, strings.Join(pids, " ")))
if !response.Success {
return false, response.Err
}
}
if burnMemMode == "cache" {
dirPath := path.Join(util.GetProgramPath(), dirName)
if _, err := os.Stat(dirPath); err == nil {
if !cl.IsCommandAvailable("umount") {
bin.PrintErrAndExit(spec.CommandUmountNotFound.Msg)
}
response = cl.Run(ctx, "umount", dirPath)
if !response.Success {
if !strings.Contains(response.Err, "not mounted") {
bin.PrintErrAndExit(response.Error())
}
}
err = os.RemoveAll(dirPath)
if err != nil {
bin.PrintErrAndExit(err.Error())
}
}
}
return true, errs
}
func calculateMemSize(percent, reserve int) (int64, int64, error) {
total := int64(0)
available := int64(0)
memoryStat, err := getMemoryStatsByCGroup()
if err != nil {
logrus.Infof("get memory stats by cgroup failed, used proc memory, %v", err)
}
if memoryStat == nil || memoryStat.Usage.Limit >= PageCounterMax {
//no limit
virtualMemory, err := mem.VirtualMemory()
if err != nil {
return 0, 0, err
}
total = int64(virtualMemory.Total)
available = int64(virtualMemory.Free)
if burnMemMode == "ram" && !includeBufferCache {
available = available + int64(virtualMemory.Buffers+virtualMemory.Cached)
}
} else {
total = int64(memoryStat.Usage.Limit)
available = total - int64(memoryStat.Usage.Usage)
if burnMemMode == "ram" && !includeBufferCache {
available = available + int64(memoryStat.Cache)
}
}
reserved := int64(0)
if percent != 0 {
reserved = (total * int64(100-percent) / 100) / 1024 / 1024
} else {
reserved = int64(reserve)
}
expectSize := available/1024/1024 - reserved
logrus.Debugf("available: %d, percent: %d, reserved: %d, expectSize: %d",
available/1024/1024, percent, reserved, expectSize)
return total / 1024 / 1024, expectSize, nil
}
func getMemoryStatsByCGroup() (*v1.MemoryStat, error) {
cgroup, err := cgroups.Load(cgroups.V1, cgroups.StaticPath("/"))
if err != nil {
return nil, fmt.Errorf("load cgroup error, %v", err)
}
stats, err := cgroup.Stat(cgroups.IgnoreNotExist)
if err != nil {
return nil, fmt.Errorf("load cgroup stat error, %v", err)
}
return stats.Memory, nil
}