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

     1  package cgroups
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	spec "github.com/opencontainers/runtime-spec/specs-go"
     8  )
     9  
    10  type memHandler struct {
    11  }
    12  
    13  func getMemoryHandler() *memHandler {
    14  	return &memHandler{}
    15  }
    16  
    17  // Apply set the specified constraints
    18  func (c *memHandler) Apply(ctr *CgroupControl, res *spec.LinuxResources) error {
    19  	if res.Memory == nil {
    20  		return nil
    21  	}
    22  	return fmt.Errorf("memory apply not implemented yet")
    23  }
    24  
    25  // Create the cgroup
    26  func (c *memHandler) Create(ctr *CgroupControl) (bool, error) {
    27  	if ctr.cgroup2 {
    28  		return false, nil
    29  	}
    30  	return ctr.createCgroupDirectory(Memory)
    31  }
    32  
    33  // Destroy the cgroup
    34  func (c *memHandler) Destroy(ctr *CgroupControl) error {
    35  	return rmDirRecursively(ctr.getCgroupv1Path(Memory))
    36  }
    37  
    38  // Stat fills a metrics structure with usage stats for the controller
    39  func (c *memHandler) Stat(ctr *CgroupControl, m *Metrics) error {
    40  	var err error
    41  	usage := MemoryUsage{}
    42  
    43  	var memoryRoot string
    44  	filenames := map[string]string{}
    45  
    46  	if ctr.cgroup2 {
    47  		memoryRoot = filepath.Join(cgroupRoot, ctr.path)
    48  		filenames["usage"] = "memory.current"
    49  		filenames["limit"] = "memory.max"
    50  	} else {
    51  		memoryRoot = ctr.getCgroupv1Path(Memory)
    52  		filenames["usage"] = "memory.usage_in_bytes"
    53  		filenames["limit"] = "memory.limit_in_bytes"
    54  	}
    55  	usage.Usage, err = readFileAsUint64(filepath.Join(memoryRoot, filenames["usage"]))
    56  	if err != nil {
    57  		return err
    58  	}
    59  	usage.Limit, err = readFileAsUint64(filepath.Join(memoryRoot, filenames["limit"]))
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	m.Memory = MemoryMetrics{Usage: usage}
    65  	return nil
    66  }