Skip to content

Commit

Permalink
fix mem load cache mode bug
Browse files Browse the repository at this point in the history
Signed-off-by: hengyoush <hengyoush1@163.com>
  • Loading branch information
hengyoush committed Jul 21, 2023
1 parent 8644cf9 commit 6266c22
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions exec/mem/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ 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"
"path"
"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"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}

0 comments on commit 6266c22

Please sign in to comment.