oom_linux.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // +build cgo,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. "os"
  18. "path"
  19. "strconv"
  20. "github.com/golang/glog"
  21. "github.com/opencontainers/runc/libcontainer/cgroups/fs"
  22. "github.com/opencontainers/runc/libcontainer/configs"
  23. )
  24. func NewOOMAdjuster() *OOMAdjuster {
  25. oomAdjuster := &OOMAdjuster{
  26. pidLister: getPids,
  27. ApplyOOMScoreAdj: applyOOMScoreAdj,
  28. }
  29. oomAdjuster.ApplyOOMScoreAdjContainer = oomAdjuster.applyOOMScoreAdjContainer
  30. return oomAdjuster
  31. }
  32. func getPids(cgroupName string) ([]int, error) {
  33. fsManager := fs.Manager{
  34. Cgroups: &configs.Cgroup{
  35. Parent: "/",
  36. Name: cgroupName,
  37. },
  38. }
  39. return fsManager.GetPids()
  40. }
  41. // Writes 'value' to /proc/<pid>/oom_score_adj. PID = 0 means self
  42. // Returns os.ErrNotExist if the `pid` does not exist.
  43. func applyOOMScoreAdj(pid int, oomScoreAdj int) error {
  44. if pid < 0 {
  45. return fmt.Errorf("invalid PID %d specified for oom_score_adj", pid)
  46. }
  47. var pidStr string
  48. if pid == 0 {
  49. pidStr = "self"
  50. } else {
  51. pidStr = strconv.Itoa(pid)
  52. }
  53. maxTries := 2
  54. oomScoreAdjPath := path.Join("/proc", pidStr, "oom_score_adj")
  55. value := strconv.Itoa(oomScoreAdj)
  56. var err error
  57. for i := 0; i < maxTries; i++ {
  58. f, err := os.Open(oomScoreAdjPath)
  59. if err != nil {
  60. if os.IsNotExist(err) {
  61. return os.ErrNotExist
  62. }
  63. err = fmt.Errorf("failed to apply oom-score-adj to pid %d (%v)", pid, err)
  64. continue
  65. }
  66. if _, err := f.Write([]byte(value)); err != nil {
  67. // we can ignore the return value of f.Close() here.
  68. f.Close()
  69. err = fmt.Errorf("failed to apply oom-score-adj to pid %d (%v)", pid, err)
  70. continue
  71. }
  72. if err = f.Close(); err != nil {
  73. err = fmt.Errorf("failed to apply oom-score-adj to pid %d (%v)", pid, err)
  74. continue
  75. }
  76. return nil
  77. }
  78. return err
  79. }
  80. // Writes 'value' to /proc/<pid>/oom_score_adj for all processes in cgroup cgroupName.
  81. // Keeps trying to write until the process list of the cgroup stabilizes, or until maxTries tries.
  82. func (oomAdjuster *OOMAdjuster) applyOOMScoreAdjContainer(cgroupName string, oomScoreAdj, maxTries int) error {
  83. adjustedProcessSet := make(map[int]bool)
  84. for i := 0; i < maxTries; i++ {
  85. continueAdjusting := false
  86. pidList, err := oomAdjuster.pidLister(cgroupName)
  87. if err != nil {
  88. if os.IsNotExist(err) {
  89. // Nothing to do since the container doesn't exist anymore.
  90. return os.ErrNotExist
  91. }
  92. continueAdjusting = true
  93. glog.V(10).Infof("Error getting process list for cgroup %s: %+v", cgroupName, err)
  94. } else if len(pidList) == 0 {
  95. glog.V(10).Infof("Pid list is empty")
  96. continueAdjusting = true
  97. } else {
  98. for _, pid := range pidList {
  99. if !adjustedProcessSet[pid] {
  100. glog.V(10).Infof("pid %d needs to be set", pid)
  101. if err = oomAdjuster.ApplyOOMScoreAdj(pid, oomScoreAdj); err == nil {
  102. adjustedProcessSet[pid] = true
  103. } else if err == os.ErrNotExist {
  104. continue
  105. } else {
  106. glog.V(10).Infof("cannot adjust oom score for pid %d - %v", pid, err)
  107. continueAdjusting = true
  108. }
  109. // Processes can come and go while we try to apply oom score adjust value. So ignore errors here.
  110. }
  111. }
  112. }
  113. if !continueAdjusting {
  114. return nil
  115. }
  116. // There's a slight race. A process might have forked just before we write its OOM score adjust.
  117. // The fork might copy the parent process's old OOM score, then this function might execute and
  118. // update the parent's OOM score, but the forked process id might not be reflected in cgroup.procs
  119. // for a short amount of time. So this function might return without changing the forked process's
  120. // OOM score. Very unlikely race, so ignoring this for now.
  121. }
  122. return fmt.Errorf("exceeded maxTries, some processes might not have desired OOM score")
  123. }