github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/cgroups/fs2/misc.go (about)

     1  package fs2
     2  
     3  import (
     4  	"bufio"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/opencontainers/runc/libcontainer/cgroups"
     9  	"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
    10  )
    11  
    12  func statMisc(dirPath string, stats *cgroups.Stats) error {
    13  	for _, file := range []string{"current", "events"} {
    14  		fd, err := cgroups.OpenFile(dirPath, "misc."+file, os.O_RDONLY)
    15  		if err != nil {
    16  			return err
    17  		}
    18  
    19  		s := bufio.NewScanner(fd)
    20  		for s.Scan() {
    21  			key, value, err := fscommon.ParseKeyValue(s.Text())
    22  			if err != nil {
    23  				fd.Close()
    24  				return err
    25  			}
    26  
    27  			key = strings.TrimSuffix(key, ".max")
    28  
    29  			if _, ok := stats.MiscStats[key]; !ok {
    30  				stats.MiscStats[key] = cgroups.MiscStats{}
    31  			}
    32  
    33  			tmp := stats.MiscStats[key]
    34  
    35  			switch file {
    36  			case "current":
    37  				tmp.Usage = value
    38  			case "events":
    39  				tmp.Events = value
    40  			}
    41  
    42  			stats.MiscStats[key] = tmp
    43  		}
    44  		fd.Close()
    45  
    46  		if err := s.Err(); err != nil {
    47  			return err
    48  		}
    49  	}
    50  
    51  	return nil
    52  }