-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Limit the memory size of falcon-agent and the number of metric pushed #912
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
Open
apriltommy0525
wants to merge
3
commits into
open-falcon:master
Choose a base branch
from
apriltommy0525:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package funcs | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/open-falcon/falcon-plus/g" | ||
"github.com/toolkits/file" | ||
) | ||
|
||
var ( | ||
memLimit int = 200 // agent最大可以使用的内存,单位MB | ||
cgroupRoot string = "/sys/fs/cgroup/memory/falcon-agent" | ||
) | ||
|
||
const ( | ||
procsFile = "cgroup.procs" | ||
memStat = "memory.stat" | ||
mb = 1024 * 1024 | ||
) | ||
|
||
// InitCgroup init falcon-agent | ||
func InitCgroup() { | ||
pid := g.Pid("agent") | ||
_ = os.RemoveAll(cgroupRoot) | ||
// create falcon-agent cgroup dir | ||
err := os.Mkdir(cgroupRoot, 751) | ||
if err != nil { | ||
fmt.Println("falcon-agent cgroup init failed", err) | ||
return | ||
} | ||
// set memory limit | ||
pPath := filepath.Join(cgroupRoot, procsFile) | ||
err = ioutil.WriteFile(pPath, []byte(fmt.Sprintf("%s", pid)), 644) | ||
if err != nil { | ||
fmt.Println("falcon-agent cgroup write cgroup.procs failed", err) | ||
return | ||
} | ||
} | ||
|
||
// GetAgentMem get agent memory info | ||
func GetAgentMem() (int64, error) { | ||
filePath := filepath.Join(cgroupRoot, memStat) | ||
contents, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
fmt.Printf("error: %v", err) | ||
return 0, err | ||
} | ||
|
||
reader := bufio.NewReader(bytes.NewBuffer(contents)) | ||
var agentRSS int64 | ||
for { | ||
info, err := file.ReadLine(reader) | ||
if err != nil { | ||
return agentRSS, err | ||
} | ||
fields := strings.Fields(string(info)) | ||
if len(fields) < 2 || fields[0] != "rss" { | ||
continue | ||
} | ||
val, numErr := strconv.ParseInt(fields[1], 10, 64) | ||
if numErr != nil { | ||
continue | ||
} | ||
agentRSS = val / mb | ||
break | ||
} | ||
return agentRSS, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package funcs | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestGetAgentMem(t *testing.T) { | ||
_ = os.RemoveAll(cgroupRoot) | ||
// create falcon-agent cgroup dir | ||
err := os.Mkdir(cgroupRoot, 751) | ||
if err != nil { | ||
t.Error(err) | ||
_ = os.RemoveAll(cgroupRoot) | ||
return | ||
} | ||
_, err = GetAgentMem() | ||
if err != nil { | ||
t.Error(err) | ||
_ = os.RemoveAll(cgroupRoot) | ||
return | ||
} | ||
_ = os.RemoveAll(cgroupRoot) | ||
return | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.