From 6266c2231c2ca939af2f9b196013fd3df22081d8 Mon Sep 17 00:00:00 2001 From: hengyoush Date: Fri, 21 Jul 2023 22:55:01 +0800 Subject: [PATCH] fix mem load cache mode bug Signed-off-by: hengyoush --- exec/mem/mem.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/exec/mem/mem.go b/exec/mem/mem.go index f18b738a..855de063 100644 --- a/exec/mem/mem.go +++ b/exec/mem/mem.go @@ -19,8 +19,6 @@ package mem import ( "context" "fmt" - "github.com/chaosblade-io/chaosblade-exec-os/exec" - "github.com/chaosblade-io/chaosblade-spec-go/log" "io/ioutil" "math" "os" @@ -28,6 +26,9 @@ import ( "strconv" "time" + "github.com/chaosblade-io/chaosblade-exec-os/exec" + "github.com/chaosblade-io/chaosblade-spec-go/log" + "github.com/chaosblade-io/chaosblade-spec-go/spec" "github.com/chaosblade-io/chaosblade-spec-go/util" @@ -209,7 +210,7 @@ func (ce *memExecutor) Exec(uid string, ctx context.Context, model *spec.ExpMode var err error memPercent, err = strconv.Atoi(memPercentStr) if err != nil { - log.Errorf(ctx,"`%s`: mem-percent must be a positive integer", memPercentStr) + log.Errorf(ctx, "`%s`: mem-percent must be a positive integer", memPercentStr) return spec.ResponseFailWithFlags(spec.ParameterIllegal, "mem-percent", memPercentStr, "it must be a positive integer") } if memPercent > 100 || memPercent < 0 { @@ -264,12 +265,22 @@ func calculateMemSize(ctx context.Context, burnMemMode string, percent, reserve var dirName = "burnmem_tmpfs" +var tmpfsName = "chaos_burn" + var fileName = "file" var fileCount = 1 func burnMemWithCache(ctx context.Context, memPercent, memReserve, memRate int, burnMemMode string, includeBufferCache bool, cl spec.Channel) { - filePath := path.Join(path.Join(util.GetProgramPath(), dirName), fileName) + tmpfsPath := path.Join(util.GetProgramPath(), dirName) + filePath := path.Join(tmpfsPath, fileName) + // prepare tmpfs + cl.Run(ctx, "mkdir", fmt.Sprintf("-p %s", tmpfsPath)) + cl.Run(ctx, "mount", fmt.Sprintf("-t tmpfs -o size=100%% %s %s", tmpfsName, tmpfsPath)) + + if memRate <= 0 { + memRate = 100 + } tick := time.Tick(time.Second) for range tick { _, expectMem, err := calculateMemSize(ctx, burnMemMode, memPercent, memReserve, includeBufferCache) @@ -281,6 +292,7 @@ func burnMemWithCache(ctx context.Context, memPercent, memReserve, memRate int, if expectMem > int64(memRate) { fillMem = int64(memRate) } + log.Debugf(ctx, "burn mem with cache fill memory: %d", fillMem) nFilePath := fmt.Sprintf("%s%d", filePath, fileCount) response := cl.Run(ctx, "dd", fmt.Sprintf("if=/dev/zero of=%s bs=1M count=%d", nFilePath, fillMem)) if !response.Success { @@ -296,7 +308,7 @@ func (ce *memExecutor) start(ctx context.Context, memPercent, memReserve, memRat // adjust process oom_score_adj to avoid being killed if avoidBeingKilled { scoreAdjFile := fmt.Sprintf(processOOMAdj, os.Getpid()) - if _, err := os.Stat(scoreAdjFile); err == nil || os.IsExist(err) { + if _, err := os.Stat(scoreAdjFile); err == nil || os.IsExist(err) { if err := ioutil.WriteFile(scoreAdjFile, []byte(oomMinAdj), 0644); err != nil { log.Errorf(ctx, "run burn memory by %s mode failed, cannot edit the process oom_score_adj, %v", burnMemMode, err) } @@ -348,6 +360,11 @@ func (ce *memExecutor) start(ctx context.Context, memPercent, memReserve, memRat // stop burn mem func (ce *memExecutor) stop(ctx context.Context, burnMemMode string) *spec.Response { - ctx = context.WithValue(ctx,"bin", BurnMemBin) - return exec.Destroy(ctx, ce.channel, "mem load") + ctx = context.WithValue(ctx, "bin", BurnMemBin) + response := exec.Destroy(ctx, ce.channel, "mem load") + // umount tmpfs + ce.channel.Run(ctx, "umount", tmpfsName) + tmpfsPath := path.Join(util.GetProgramPath(), dirName) + ce.channel.Run(ctx, "rm", fmt.Sprintf("-rf %s", tmpfsPath)) + return response }