-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psi.go
106 lines (91 loc) · 2.72 KB
/
psi.go
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
package psi
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"strings"
)
func AllPSIStats() (PSIStatsResource, error) {
cpu, err := PSIStatsForResource(CPU)
if err != nil {
return PSIStatsResource{}, err
}
memory, err := PSIStatsForResource(Memory)
if err != nil {
return PSIStatsResource{}, err
}
io, err := PSIStatsForResource(IO)
if err != nil {
return PSIStatsResource{}, err
}
return PSIStatsResource{
CPU: &cpu,
Memory: &memory,
IO: &io,
}, nil
}
// PSIStatsForResource reads pressure stall information for the specified
// resource from /proc/pressure/<resource>. At time of writing this can be
// either "cpu", "memory" or "io".
func PSIStatsForResource(resource Resource) (PSIStats, error) {
data, err := ReadFileNoStat(ResourceToPath(resource))
if err != nil {
return PSIStats{}, fmt.Errorf("psi_stats: unavailable for %q: %w", resource, err)
}
return parsePSIStats(resource, bytes.NewReader(data))
}
// parsePSIStats parses the specified file for pressure stall information.
func parsePSIStats(resource Resource, r io.Reader) (PSIStats, error) {
psiStats := PSIStats{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
l := scanner.Text()
prefix := strings.Split(l, " ")[0]
switch prefix {
case "some":
psi := PSILine{}
_, err := fmt.Sscanf(l, fmt.Sprintf("some %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
if err != nil {
return PSIStats{}, err
}
psiStats.Some = &psi
case "full":
psi := PSILine{}
_, err := fmt.Sscanf(l, fmt.Sprintf("full %s", lineFormat), &psi.Avg10, &psi.Avg60, &psi.Avg300, &psi.Total)
if err != nil {
return PSIStats{}, err
}
psiStats.Full = &psi
default:
// If we encounter a line with an unknown prefix, ignore it and move on
// Should new measurement types be added in the future we'll simply ignore them instead
// of erroring on retrieval
continue
}
}
return psiStats, nil
}
// ReadFileNoStat uses io.ReadAll to read contents of entire file.
// This is similar to os.ReadFile but without the call to os.Stat, because
// many files in /proc and /sys report incorrect file sizes (either 0 or 4096).
// Reads a max file size of 1024kB. For files larger than this, a scanner
// should be used.
func ReadFileNoStat(filename string) ([]byte, error) {
const maxBufferSize = 1024 * 1024
if !strings.HasPrefix(filename, "/proc/") {
return nil, fmt.Errorf("file %q is not in /proc", filename)
}
// bearer:disable go_gosec_filesystem_filereadtaint
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
reader := io.LimitReader(f, maxBufferSize)
return io.ReadAll(reader)
}
func CompareThreshold(threshold int, current int) bool {
return threshold >= 0 && current > threshold
}