File: cpu.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (141 lines) | stat: -rw-r--r-- 3,097 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package resources

import (
	"bufio"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"

	resourcestypes "github.com/moby/buildkit/executor/resources/types"
	"github.com/pkg/errors"
)

const (
	cpuUsageUsec     = "usage_usec"
	cpuUserUsec      = "user_usec"
	cpuSystemUsec    = "system_usec"
	cpuNrPeriods     = "nr_periods"
	cpuNrThrottled   = "nr_throttled"
	cpuThrottledUsec = "throttled_usec"
)

func getCgroupCPUStat(cgroupPath string) (*resourcestypes.CPUStat, error) {
	cpuStat := &resourcestypes.CPUStat{}

	// Read cpu.stat file
	cpuStatFile, err := os.Open(filepath.Join(cgroupPath, "cpu.stat"))
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			return nil, nil
		}
		return nil, err
	}
	defer cpuStatFile.Close()

	scanner := bufio.NewScanner(cpuStatFile)
	for scanner.Scan() {
		line := scanner.Text()
		fields := strings.Fields(line)

		if len(fields) < 2 {
			continue
		}

		key := fields[0]
		value, err := strconv.ParseUint(fields[1], 10, 64)
		if err != nil {
			continue
		}

		switch key {
		case cpuUsageUsec:
			cpuStat.UsageNanos = uint64Ptr(value * 1000)
		case cpuUserUsec:
			cpuStat.UserNanos = uint64Ptr(value * 1000)
		case cpuSystemUsec:
			cpuStat.SystemNanos = uint64Ptr(value * 1000)
		case cpuNrPeriods:
			cpuStat.NrPeriods = new(uint32)
			*cpuStat.NrPeriods = uint32(value)
		case cpuNrThrottled:
			cpuStat.NrThrottled = new(uint32)
			*cpuStat.NrThrottled = uint32(value)
		case cpuThrottledUsec:
			cpuStat.ThrottledNanos = uint64Ptr(value * 1000)
		}
	}

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

	// Read cpu.pressure file
	pressure, err := parsePressureFile(filepath.Join(cgroupPath, "cpu.pressure"))
	if err == nil {
		cpuStat.Pressure = pressure
	}

	return cpuStat, nil
}
func parsePressureFile(filename string) (*resourcestypes.Pressure, error) {
	content, err := os.ReadFile(filename)
	if err != nil {
		if errors.Is(err, os.ErrNotExist) || errors.Is(err, syscall.ENOTSUP) { // pressure file requires CONFIG_PSI
			return nil, nil
		}
		return nil, err
	}

	lines := strings.Split(string(content), "\n")

	pressure := &resourcestypes.Pressure{}
	for _, line := range lines {
		// Skip empty lines
		if len(strings.TrimSpace(line)) == 0 {
			continue
		}

		fields := strings.Fields(line)
		prefix := fields[0]
		pressureValues := &resourcestypes.PressureValues{}

		for i := 1; i < len(fields); i++ {
			keyValue := strings.Split(fields[i], "=")
			key := keyValue[0]
			valueStr := keyValue[1]

			if key == "total" {
				totalValue, err := strconv.ParseUint(valueStr, 10, 64)
				if err != nil {
					return nil, err
				}
				pressureValues.Total = &totalValue
			} else {
				value, err := strconv.ParseFloat(valueStr, 64)
				if err != nil {
					return nil, err
				}

				switch key {
				case "avg10":
					pressureValues.Avg10 = &value
				case "avg60":
					pressureValues.Avg60 = &value
				case "avg300":
					pressureValues.Avg300 = &value
				}
			}
		}

		switch prefix {
		case "some":
			pressure.Some = pressureValues
		case "full":
			pressure.Full = pressureValues
		}
	}

	return pressure, nil
}