github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/cgroups/pids.go (about)

     1  package cgroups
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  
     8  	spec "github.com/opencontainers/runtime-spec/specs-go"
     9  )
    10  
    11  type pidHandler struct {
    12  }
    13  
    14  func getPidsHandler() *pidHandler {
    15  	return &pidHandler{}
    16  }
    17  
    18  // Apply set the specified constraints
    19  func (c *pidHandler) Apply(ctr *CgroupControl, res *spec.LinuxResources) error {
    20  	if res.Pids == nil {
    21  		return nil
    22  	}
    23  	var PIDRoot string
    24  
    25  	if ctr.cgroup2 {
    26  		PIDRoot = filepath.Join(cgroupRoot, ctr.path)
    27  	} else {
    28  		PIDRoot = ctr.getCgroupv1Path(Pids)
    29  	}
    30  
    31  	p := filepath.Join(PIDRoot, "pids.max")
    32  	return ioutil.WriteFile(p, []byte(fmt.Sprintf("%d\n", res.Pids.Limit)), 0644)
    33  }
    34  
    35  // Create the cgroup
    36  func (c *pidHandler) Create(ctr *CgroupControl) (bool, error) {
    37  	return ctr.createCgroupDirectory(Pids)
    38  }
    39  
    40  // Destroy the cgroup
    41  func (c *pidHandler) Destroy(ctr *CgroupControl) error {
    42  	return rmDirRecursively(ctr.getCgroupv1Path(Pids))
    43  }
    44  
    45  // Stat fills a metrics structure with usage stats for the controller
    46  func (c *pidHandler) Stat(ctr *CgroupControl, m *Metrics) error {
    47  	if ctr.path == "" {
    48  		// nothing we can do to retrieve the pids.current path
    49  		return nil
    50  	}
    51  
    52  	var PIDRoot string
    53  	if ctr.cgroup2 {
    54  		PIDRoot = filepath.Join(cgroupRoot, ctr.path)
    55  	} else {
    56  		PIDRoot = ctr.getCgroupv1Path(Pids)
    57  	}
    58  
    59  	current, err := readFileAsUint64(filepath.Join(PIDRoot, "pids.current"))
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	m.Pids = PidsMetrics{Current: current}
    65  	return nil
    66  }