github.com/hms58/moby@v1.13.1/libcontainerd/oom_linux.go (about)

     1  package libcontainerd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/Sirupsen/logrus"
     9  	"github.com/opencontainers/runc/libcontainer/system"
    10  )
    11  
    12  func setOOMScore(pid, score int) error {
    13  	oomScoreAdjPath := fmt.Sprintf("/proc/%d/oom_score_adj", pid)
    14  	f, err := os.OpenFile(oomScoreAdjPath, os.O_WRONLY, 0)
    15  	if err != nil {
    16  		return err
    17  	}
    18  	stringScore := strconv.Itoa(score)
    19  	_, err = f.WriteString(stringScore)
    20  	f.Close()
    21  	if os.IsPermission(err) {
    22  		// Setting oom_score_adj does not work in an
    23  		// unprivileged container. Ignore the error, but log
    24  		// it if we appear not to be in that situation.
    25  		if !system.RunningInUserNS() {
    26  			logrus.Debugf("Permission denied writing %q to %s", stringScore, oomScoreAdjPath)
    27  		}
    28  		return nil
    29  	}
    30  	return err
    31  }