github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/cgroups/fs/pids.go (about)

     1  // +build linux
     2  
     3  package fs
     4  
     5  import (
     6  	"fmt"
     7  	"path/filepath"
     8  	"strconv"
     9  
    10  	"github.com/opencontainers/runc/libcontainer/cgroups"
    11  	"github.com/opencontainers/runc/libcontainer/configs"
    12  )
    13  
    14  type PidsGroup struct {
    15  }
    16  
    17  func (s *PidsGroup) Name() string {
    18  	return "pids"
    19  }
    20  
    21  func (s *PidsGroup) Apply(d *cgroupData) error {
    22  	_, err := d.join("pids")
    23  	if err != nil && !cgroups.IsNotFound(err) {
    24  		return err
    25  	}
    26  	return nil
    27  }
    28  
    29  func (s *PidsGroup) Set(path string, cgroup *configs.Cgroup) error {
    30  	if cgroup.Resources.PidsLimit != 0 {
    31  		// "max" is the fallback value.
    32  		limit := "max"
    33  
    34  		if cgroup.Resources.PidsLimit > 0 {
    35  			limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10)
    36  		}
    37  
    38  		if err := writeFile(path, "pids.max", limit); err != nil {
    39  			return err
    40  		}
    41  	}
    42  
    43  	return nil
    44  }
    45  
    46  func (s *PidsGroup) Remove(d *cgroupData) error {
    47  	return removePath(d.path("pids"))
    48  }
    49  
    50  func (s *PidsGroup) GetStats(path string, stats *cgroups.Stats) error {
    51  	current, err := getCgroupParamUint(path, "pids.current")
    52  	if err != nil {
    53  		return fmt.Errorf("failed to parse pids.current - %s", err)
    54  	}
    55  
    56  	maxString, err := getCgroupParamString(path, "pids.max")
    57  	if err != nil {
    58  		return fmt.Errorf("failed to parse pids.max - %s", err)
    59  	}
    60  
    61  	// Default if pids.max == "max" is 0 -- which represents "no limit".
    62  	var max uint64
    63  	if maxString != "max" {
    64  		max, err = parseUint(maxString, 10, 64)
    65  		if err != nil {
    66  			return fmt.Errorf("failed to parse pids.max - unable to parse %q as a uint from Cgroup file %q", maxString, filepath.Join(path, "pids.max"))
    67  		}
    68  	}
    69  
    70  	stats.PidsStats.Current = current
    71  	stats.PidsStats.Limit = max
    72  	return nil
    73  }