Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mem load cache mode bug #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}