Skip to content

Commit

Permalink
Allow ApplyOomScoreAdj to specify what PID to adjust for.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarmol committed Feb 20, 2015
1 parent 5eb71a1 commit 2d1a8d0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/kubelet/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
18 changes: 14 additions & 4 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

var pidStr string
if pid == 0 {
pidStr = "self"
} else {
pidStr = strconv.Itoa(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)
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
Expand Down

0 comments on commit 2d1a8d0

Please sign in to comment.