|
| 1 | +package cgroup |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/coroot/coroot-node-agent/common" |
| 10 | + "k8s.io/klog/v2" |
| 11 | +) |
| 12 | + |
| 13 | +type PSIStats struct { |
| 14 | + CPUSecondsSome float64 |
| 15 | + CPUSecondsFull float64 |
| 16 | + MemorySecondsSome float64 |
| 17 | + MemorySecondsFull float64 |
| 18 | + IOSecondsSome float64 |
| 19 | + IOSecondsFull float64 |
| 20 | +} |
| 21 | + |
| 22 | +type PressureTotals struct { |
| 23 | + SomeSecondsTotal float64 |
| 24 | + FullSecondsTotal float64 |
| 25 | +} |
| 26 | + |
| 27 | +func (cg *Cgroup) PSI() *PSIStats { |
| 28 | + if cg.subsystems[""] == "" { |
| 29 | + return nil |
| 30 | + } |
| 31 | + stats := &PSIStats{} |
| 32 | + for _, controller := range []string{"cpu", "memory", "io"} { |
| 33 | + p, err := cg.readPressure(controller) |
| 34 | + if err != nil { |
| 35 | + if !common.IsNotExist(err) { |
| 36 | + klog.Warningln(err) |
| 37 | + } |
| 38 | + return nil |
| 39 | + } |
| 40 | + switch controller { |
| 41 | + case "cpu": |
| 42 | + stats.CPUSecondsSome = p.SomeSecondsTotal |
| 43 | + stats.CPUSecondsFull = p.FullSecondsTotal |
| 44 | + case "memory": |
| 45 | + stats.MemorySecondsSome = p.SomeSecondsTotal |
| 46 | + stats.MemorySecondsFull = p.FullSecondsTotal |
| 47 | + case "io": |
| 48 | + stats.IOSecondsSome = p.SomeSecondsTotal |
| 49 | + stats.IOSecondsFull = p.FullSecondsTotal |
| 50 | + } |
| 51 | + } |
| 52 | + return stats |
| 53 | +} |
| 54 | + |
| 55 | +func (cg *Cgroup) readPressure(controller string) (*PressureTotals, error) { |
| 56 | + data, err := os.ReadFile(path.Join(cg2Root, cg.subsystems[""], controller+".pressure")) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + pressure := &PressureTotals{} |
| 61 | + for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") { |
| 62 | + parts := strings.Fields(line) |
| 63 | + if len(parts) == 0 { |
| 64 | + continue |
| 65 | + } |
| 66 | + kind := parts[0] |
| 67 | + for _, p := range parts[1:] { |
| 68 | + if strings.HasPrefix(p, "total=") { |
| 69 | + vStr := strings.TrimPrefix(p, "total=") |
| 70 | + v, err := strconv.ParseUint(vStr, 10, 64) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + switch kind { |
| 75 | + case "some": |
| 76 | + pressure.SomeSecondsTotal = float64(v) / 1e6 // microseconds to seconds |
| 77 | + case "full": |
| 78 | + pressure.FullSecondsTotal = float64(v) / 1e6 |
| 79 | + } |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + return pressure, nil |
| 85 | +} |
0 commit comments