Allow ApplyOomScoreAdj to specify what PID to adjust for.

This commit is contained in:
Victor Marmol 2015-02-19 17:16:31 -08:00
parent 5eb71a1877
commit 2d1a8d0da0
3 changed files with 16 additions and 6 deletions

View File

@ -170,7 +170,7 @@ func (s *KubeletServer) Run(_ []string) error {
s.EtcdServerList = util.StringList{}
}
if err := util.ApplyOomScoreAdj(s.OOMScoreAdj); err != nil {
if err := util.ApplyOomScoreAdj(0, s.OOMScoreAdj); err != nil {
glog.Info(err)
}

View File

@ -89,7 +89,7 @@ func (s *ProxyServer) AddFlags(fs *pflag.FlagSet) {
// Run runs the specified ProxyServer. This should never exit.
func (s *ProxyServer) Run(_ []string) error {
if err := util.ApplyOomScoreAdj(s.OOMScoreAdj); err != nil {
if err := util.ApplyOomScoreAdj(0, s.OOMScoreAdj); err != nil {
glog.Info(err)
}

View File

@ -175,14 +175,24 @@ func CompileRegexps(regexpStrings []string) ([]*regexp.Regexp, error) {
return regexps, nil
}
// Writes 'value' to /proc/self/oom_score_adj.
func ApplyOomScoreAdj(value int) error {
// Writes 'value' to /proc/<pid>/oom_score_adj. PID = 0 means self
func ApplyOomScoreAdj(pid int, value int) error {
if value < -1000 || value > 1000 {
return fmt.Errorf("invalid value(%d) specified for oom_score_adj. Values must be within the range [-1000, 1000]", value)
}
if pid < 0 {
return fmt.Errorf("invalid PID %d specified for oom_score_adj", pid)
}
if err := ioutil.WriteFile("/proc/self/oom_score_adj", []byte(strconv.Itoa(value)), 0700); err != nil {
fmt.Errorf("failed to set oom_score_adj to %d - %q", value, err)
var pidStr string
if pid == 0 {
pidStr = "self"
} else {
pidStr = strconv.Itoa(pid)
}
if err := ioutil.WriteFile(path.Join("/proc", pidStr, "oom_score_adj"), []byte(strconv.Itoa(value)), 0700); err != nil {
fmt.Errorf("failed to set oom_score_adj to %d: %v", value, err)
}
return nil