Change the openvpn and connmand OOM adjust to only happen if existing value is 0

This commit is contained in:
Praneeth Bodduluri 2016-01-05 20:56:27 +05:30
parent 1bc53ebc8d
commit b28e6c7648
2 changed files with 29 additions and 21 deletions

View File

@ -38,28 +38,21 @@ func startApi(listenAddress string, router *mux.Router) {
}
}
func startServiceOOMProtection(hostproc string) {
err := psutils.AdjustOOMPriorityByName(hostproc, "openvpn", -1000)
if err != nil {
log.Printf(err.Error())
}
err = psutils.AdjustOOMPriorityByName(hostproc, "connmand", -1000)
if err != nil {
log.Printf(err.Error())
}
}
func startOOMProtection(hostproc string, dockerSocket string, ticker *time.Ticker) {
log.Println("Changing OOMScore Adjust Value for this container to -800")
err := psutils.AdjustDockerOOMPriority(hostproc, "unix://"+dockerSocket, "resin-supervisor", -800)
err := psutils.AdjustDockerOOMPriority(hostproc, "unix://"+dockerSocket, "resin-supervisor", -800, false)
if err != nil {
log.Printf(err.Error())
}
log.Println("Changing OOMScore Adjust Value for openvpn and connmand to -1000 every 5 minutes")
startServiceOOMProtection(hostproc)
// Code below this could be eventually deprecated after all the devices are > 5 Jan 2016 deployment.
log.Println("Changing OOMScore Adjust Value for openvpn and connmand to -1000 if 0, every 5 minutes")
// Errors are not being caught here as users could have openvpn and connmand disabled.
psutils.AdjustOOMPriorityByName(hostproc, "openvpn", -1000, true)
psutils.AdjustOOMPriorityByName(hostproc, "connmand", -1000, true)
go func() {
for _ = range ticker.C {
startServiceOOMProtection(hostproc)
psutils.AdjustOOMPriorityByName(hostproc, "openvpn", -1000, true)
psutils.AdjustOOMPriorityByName(hostproc, "connmand", -1000, true)
}
}()
}

View File

@ -1,6 +1,7 @@
package psutils
import (
"bufio"
"fmt"
"os"
"path"
@ -12,7 +13,7 @@ import (
)
//AdjustOOMPriorityByName Adjust the OOM adj value for the process' with the given name regexp
func AdjustOOMPriorityByName(procPath string, processName string, value int64) error {
func AdjustOOMPriorityByName(procPath string, processName string, value int64, ignoreIfNonZero bool) error {
runningProcess, err := process.Pids()
if err != nil {
return err
@ -25,7 +26,7 @@ func AdjustOOMPriorityByName(procPath string, processName string, value int64) e
pidName, _ := pidProcess.Name()
if processMatch.MatchString(pidName) {
processFound = true
err := AdjustOOMPriority(procPath, int64(pid), value)
err := AdjustOOMPriority(procPath, int64(pid), value, ignoreIfNonZero)
if err != nil {
return err
}
@ -37,14 +38,28 @@ func AdjustOOMPriorityByName(procPath string, processName string, value int64) e
return fmt.Errorf("No process matches: %s\n", processName)
}
//AdjustOOMPriority Adjust the OOM adj value for the process with the given pid
func AdjustOOMPriority(procPath string, pid int64, value int64) error {
//AdjustOOMPriority Adjust the OOM adj value for the process with the given pid.
func AdjustOOMPriority(procPath string, pid int64, value int64, ignoreIfNonZero bool) error {
// Open the oom_score_adj file for the pid
oomAdjFile, err := os.OpenFile(path.Clean(procPath)+"/"+strconv.FormatInt(pid, 10)+"/oom_score_adj", os.O_RDWR, os.ModeType)
if err != nil {
return fmt.Errorf("Unable to open OOM adjust proc file for pid: %d\n", pid)
}
defer oomAdjFile.Close()
// Read the oom_score_adj value currently set
scanner := bufio.NewScanner(oomAdjFile)
scanner.Split(bufio.ScanLines)
var currentOOMString string
for scanner.Scan() {
currentOOMString = scanner.Text() // Read the OOMString
}
currentOOMValue, err := strconv.ParseInt(currentOOMString, 10, 64)
if err != nil {
return fmt.Errorf("Unable to read OOM adjust for pid: %d\n", pid)
}
if ignoreIfNonZero && currentOOMValue != 0 {
return nil
}
// Write to the procfile to adjust the OOM adj value.
_, err = oomAdjFile.WriteString(strconv.FormatInt(value, 10))
if err != nil {
@ -54,7 +69,7 @@ func AdjustOOMPriority(procPath string, pid int64, value int64) error {
}
//AdjustDockerOOMPriority Adjusts the OOM Adj value for the entire docker container specified by the name. This should point to root proc filesystem
func AdjustDockerOOMPriority(procPath string, connection string, containerName string, value int64) error {
func AdjustDockerOOMPriority(procPath string, connection string, containerName string, value int64, ignoreIfNonZero bool) error {
// Connect to the docker host with the connection string
docker, err := dockerclient.NewDockerClient(connection, nil)
if err != nil {
@ -70,7 +85,7 @@ func AdjustDockerOOMPriority(procPath string, connection string, containerName s
if err != nil {
return err
}
err = AdjustOOMPriority(procPath, int64(containerInfo.State.Pid), value)
err = AdjustOOMPriority(procPath, int64(containerInfo.State.Pid), value, ignoreIfNonZero)
if err != nil {
return err
}