...

Source file src/github.com/containerd/cgroups/pids.go

Documentation: github.com/containerd/cgroups

     1  /*
     2     Copyright The containerd Authors.
     3  
     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  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package cgroups
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"strconv"
    23  	"strings"
    24  
    25  	v1 "github.com/containerd/cgroups/stats/v1"
    26  	specs "github.com/opencontainers/runtime-spec/specs-go"
    27  )
    28  
    29  func NewPids(root string) *pidsController {
    30  	return &pidsController{
    31  		root: filepath.Join(root, string(Pids)),
    32  	}
    33  }
    34  
    35  type pidsController struct {
    36  	root string
    37  }
    38  
    39  func (p *pidsController) Name() Name {
    40  	return Pids
    41  }
    42  
    43  func (p *pidsController) Path(path string) string {
    44  	return filepath.Join(p.root, path)
    45  }
    46  
    47  func (p *pidsController) Create(path string, resources *specs.LinuxResources) error {
    48  	if err := os.MkdirAll(p.Path(path), defaultDirPerm); err != nil {
    49  		return err
    50  	}
    51  	if resources.Pids != nil && resources.Pids.Limit > 0 {
    52  		return retryingWriteFile(
    53  			filepath.Join(p.Path(path), "pids.max"),
    54  			[]byte(strconv.FormatInt(resources.Pids.Limit, 10)),
    55  			defaultFilePerm,
    56  		)
    57  	}
    58  	return nil
    59  }
    60  
    61  func (p *pidsController) Update(path string, resources *specs.LinuxResources) error {
    62  	return p.Create(path, resources)
    63  }
    64  
    65  func (p *pidsController) Stat(path string, stats *v1.Metrics) error {
    66  	current, err := readUint(filepath.Join(p.Path(path), "pids.current"))
    67  	if err != nil {
    68  		return err
    69  	}
    70  	var max uint64
    71  	maxData, err := os.ReadFile(filepath.Join(p.Path(path), "pids.max"))
    72  	if err != nil {
    73  		return err
    74  	}
    75  	if maxS := strings.TrimSpace(string(maxData)); maxS != "max" {
    76  		if max, err = parseUint(maxS, 10, 64); err != nil {
    77  			return err
    78  		}
    79  	}
    80  	stats.Pids = &v1.PidsStat{
    81  		Current: current,
    82  		Limit:   max,
    83  	}
    84  	return nil
    85  }
    86  

View as plain text