File: misc.go

package info (click to toggle)
golang-github-opencontainers-cgroups 0.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: makefile: 2
file content (52 lines) | stat: -rw-r--r-- 900 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package fs2

import (
	"bufio"
	"os"
	"strings"

	"github.com/opencontainers/cgroups"
	"github.com/opencontainers/cgroups/fscommon"
)

func statMisc(dirPath string, stats *cgroups.Stats) error {
	for _, file := range []string{"current", "events"} {
		fd, err := cgroups.OpenFile(dirPath, "misc."+file, os.O_RDONLY)
		if err != nil {
			return err
		}

		s := bufio.NewScanner(fd)
		for s.Scan() {
			key, value, err := fscommon.ParseKeyValue(s.Text())
			if err != nil {
				fd.Close()
				return err
			}

			key = strings.TrimSuffix(key, ".max")

			if _, ok := stats.MiscStats[key]; !ok {
				stats.MiscStats[key] = cgroups.MiscStats{}
			}

			tmp := stats.MiscStats[key]

			switch file {
			case "current":
				tmp.Usage = value
			case "events":
				tmp.Events = value
			}

			stats.MiscStats[key] = tmp
		}
		fd.Close()

		if err := s.Err(); err != nil {
			return err
		}
	}

	return nil
}