oom_linux.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // +build linux
  2. /*
  3. Copyright 2015 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package oom
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strconv"
  22. "time"
  23. cmutil "k8s.io/kubernetes/pkg/kubelet/cm/util"
  24. "k8s.io/klog"
  25. )
  26. func NewOOMAdjuster() *OOMAdjuster {
  27. oomAdjuster := &OOMAdjuster{
  28. pidLister: getPids,
  29. ApplyOOMScoreAdj: applyOOMScoreAdj,
  30. }
  31. oomAdjuster.ApplyOOMScoreAdjContainer = oomAdjuster.applyOOMScoreAdjContainer
  32. return oomAdjuster
  33. }
  34. func getPids(cgroupName string) ([]int, error) {
  35. return cmutil.GetPids(filepath.Join("/", cgroupName))
  36. }
  37. // Writes 'value' to /proc/<pid>/oom_score_adj. PID = 0 means self
  38. // Returns os.ErrNotExist if the `pid` does not exist.
  39. func applyOOMScoreAdj(pid int, oomScoreAdj int) error {
  40. if pid < 0 {
  41. return fmt.Errorf("invalid PID %d specified for oom_score_adj", pid)
  42. }
  43. var pidStr string
  44. if pid == 0 {
  45. pidStr = "self"
  46. } else {
  47. pidStr = strconv.Itoa(pid)
  48. }
  49. maxTries := 2
  50. oomScoreAdjPath := path.Join("/proc", pidStr, "oom_score_adj")
  51. value := strconv.Itoa(oomScoreAdj)
  52. klog.V(4).Infof("attempting to set %q to %q", oomScoreAdjPath, value)
  53. var err error
  54. for i := 0; i < maxTries; i++ {
  55. err = ioutil.WriteFile(oomScoreAdjPath, []byte(value), 0700)
  56. if err != nil {
  57. if os.IsNotExist(err) {
  58. klog.V(2).Infof("%q does not exist", oomScoreAdjPath)
  59. return os.ErrNotExist
  60. }
  61. klog.V(3).Info(err)
  62. time.Sleep(100 * time.Millisecond)
  63. continue
  64. }
  65. return nil
  66. }
  67. if err != nil {
  68. klog.V(2).Infof("failed to set %q to %q: %v", oomScoreAdjPath, value, err)
  69. }
  70. return err
  71. }
  72. // Writes 'value' to /proc/<pid>/oom_score_adj for all processes in cgroup cgroupName.
  73. // Keeps trying to write until the process list of the cgroup stabilizes, or until maxTries tries.
  74. func (oomAdjuster *OOMAdjuster) applyOOMScoreAdjContainer(cgroupName string, oomScoreAdj, maxTries int) error {
  75. adjustedProcessSet := make(map[int]bool)
  76. for i := 0; i < maxTries; i++ {
  77. continueAdjusting := false
  78. pidList, err := oomAdjuster.pidLister(cgroupName)
  79. if err != nil {
  80. if os.IsNotExist(err) {
  81. // Nothing to do since the container doesn't exist anymore.
  82. return os.ErrNotExist
  83. }
  84. continueAdjusting = true
  85. klog.V(10).Infof("Error getting process list for cgroup %s: %+v", cgroupName, err)
  86. } else if len(pidList) == 0 {
  87. klog.V(10).Infof("Pid list is empty")
  88. continueAdjusting = true
  89. } else {
  90. for _, pid := range pidList {
  91. if !adjustedProcessSet[pid] {
  92. klog.V(10).Infof("pid %d needs to be set", pid)
  93. if err = oomAdjuster.ApplyOOMScoreAdj(pid, oomScoreAdj); err == nil {
  94. adjustedProcessSet[pid] = true
  95. } else if err == os.ErrNotExist {
  96. continue
  97. } else {
  98. klog.V(10).Infof("cannot adjust oom score for pid %d - %v", pid, err)
  99. continueAdjusting = true
  100. }
  101. // Processes can come and go while we try to apply oom score adjust value. So ignore errors here.
  102. }
  103. }
  104. }
  105. if !continueAdjusting {
  106. return nil
  107. }
  108. // There's a slight race. A process might have forked just before we write its OOM score adjust.
  109. // The fork might copy the parent process's old OOM score, then this function might execute and
  110. // update the parent's OOM score, but the forked process id might not be reflected in cgroup.procs
  111. // for a short amount of time. So this function might return without changing the forked process's
  112. // OOM score. Very unlikely race, so ignoring this for now.
  113. }
  114. return fmt.Errorf("exceeded maxTries, some processes might not have desired OOM score")
  115. }