Log oom_score_adj value on error
Updates #1873 PiperOrigin-RevId: 297695241
This commit is contained in:
parent
aa9f8abaef
commit
88f7369922
|
@ -17,6 +17,7 @@ package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
@ -1066,18 +1067,10 @@ func runInCgroup(cg *cgroup.Cgroup, fn func() error) error {
|
||||||
|
|
||||||
// adjustGoferOOMScoreAdj sets the oom_store_adj for the container's gofer.
|
// adjustGoferOOMScoreAdj sets the oom_store_adj for the container's gofer.
|
||||||
func (c *Container) adjustGoferOOMScoreAdj() error {
|
func (c *Container) adjustGoferOOMScoreAdj() error {
|
||||||
if c.GoferPid != 0 && c.Spec.Process.OOMScoreAdj != nil {
|
if c.GoferPid == 0 || c.Spec.Process.OOMScoreAdj == nil {
|
||||||
if err := setOOMScoreAdj(c.GoferPid, *c.Spec.Process.OOMScoreAdj); err != nil {
|
return nil
|
||||||
// Ignore NotExist error because it can be returned when the sandbox
|
|
||||||
// exited while OOM score was being adjusted.
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return fmt.Errorf("setting gofer oom_score_adj for container %q: %v", c.ID, err)
|
|
||||||
}
|
|
||||||
log.Warningf("Gofer process (%d) not found setting oom_score_adj", c.GoferPid)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return setOOMScoreAdj(c.GoferPid, *c.Spec.Process.OOMScoreAdj)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjustSandboxOOMScoreAdj sets the oom_score_adj for the sandbox.
|
// adjustSandboxOOMScoreAdj sets the oom_score_adj for the sandbox.
|
||||||
|
@ -1154,29 +1147,29 @@ func adjustSandboxOOMScoreAdj(s *sandbox.Sandbox, rootDir string, destroy bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the lowest of all containers oom_score_adj to the sandbox.
|
// Set the lowest of all containers oom_score_adj to the sandbox.
|
||||||
if err := setOOMScoreAdj(s.Pid, lowScore); err != nil {
|
return setOOMScoreAdj(s.Pid, lowScore)
|
||||||
// Ignore NotExist error because it can be returned when the sandbox
|
|
||||||
// exited while OOM score was being adjusted.
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return fmt.Errorf("setting oom_score_adj for sandbox %q: %v", s.ID, err)
|
|
||||||
}
|
|
||||||
log.Warningf("Sandbox process (%d) not found setting oom_score_adj", s.Pid)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setOOMScoreAdj sets oom_score_adj to the given value for the given PID.
|
// setOOMScoreAdj sets oom_score_adj to the given value for the given PID.
|
||||||
// /proc must be available and mounted read-write. scoreAdj should be between
|
// /proc must be available and mounted read-write. scoreAdj should be between
|
||||||
// -1000 and 1000.
|
// -1000 and 1000. It's a noop if the process has already exited.
|
||||||
func setOOMScoreAdj(pid int, scoreAdj int) error {
|
func setOOMScoreAdj(pid int, scoreAdj int) error {
|
||||||
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid), os.O_WRONLY, 0644)
|
f, err := os.OpenFile(fmt.Sprintf("/proc/%d/oom_score_adj", pid), os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Ignore NotExist errors because it can race with process exit.
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
log.Warningf("Process (%d) not found setting oom_score_adj", pid)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
if _, err := f.WriteString(strconv.Itoa(scoreAdj)); err != nil {
|
if _, err := f.WriteString(strconv.Itoa(scoreAdj)); err != nil {
|
||||||
return err
|
if errors.Is(err, syscall.ESRCH) {
|
||||||
|
log.Warningf("Process (%d) exited while setting oom_score_adj", pid)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("setting oom_score_adj to %q: %v", scoreAdj, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue