/
psi_stats.go
288 lines (260 loc) · 7.8 KB
/
psi_stats.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright 2021 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package memory
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"path"
"strings"
"time"
"chromiumos/tast/common/perf"
"chromiumos/tast/common/testexec"
"chromiumos/tast/errors"
"chromiumos/tast/local/arc"
)
const (
psiFilename = "/proc/pressure/memory"
psiLineFormat = " avg10=%f avg60=%f avg300=%f total=%d"
psiNItems = 4
psiSomeTag = "some"
psiFullTag = "full"
)
// PSIDetail holds one line of statistics from memory pressure dumps.
type PSIDetail struct {
Avg10, Avg60, Avg300 float64
Total uint64
}
// PSIOneSystemStats holds statistics from memory pressure dumps for one system.
type PSIOneSystemStats struct {
Some PSIDetail
Full PSIDetail
}
// PSIStats holds statistics from memory pressure dumps.
type PSIStats struct {
Host *PSIOneSystemStats
Arc *PSIOneSystemStats
Timestamp time.Time
}
// Clone creates a deep copy of the provided pointer.
func (t *PSIOneSystemStats) Clone() *PSIOneSystemStats {
if t == nil {
return nil
}
deepcopy := *t
return &deepcopy
}
// Clone creates a deep copy of the provided pointer.
func (t *PSIStats) Clone() *PSIStats {
if t == nil {
return nil
}
deepcopy := *t
deepcopy.Host = t.Host.Clone()
deepcopy.Arc = t.Arc.Clone()
return &deepcopy
}
// newPSISystemStats parses PSI text output into a struct for one system.
func newPSISystemStats(statBlob []byte) (*PSIOneSystemStats, error) {
stats := &PSIOneSystemStats{}
blocks := []struct {
tag string
data *PSIDetail
}{
{psiSomeTag, &(stats.Some)},
{psiFullTag, &(stats.Full)},
}
statString := string(statBlob)
lines := strings.SplitN(statString, "\n", len(blocks))
if len(lines) != len(blocks) {
return nil, errors.Errorf("PSI metrics file[%s] should have %d lines, found %d", statString, len(blocks), len(lines))
}
for i, line := range lines {
tag := blocks[i].tag
d := blocks[i].data
if nitems, err := fmt.Sscanf(line, tag+psiLineFormat, &(d.Avg10), &(d.Avg60), &(d.Avg300), &(d.Total)); nitems != psiNItems {
return nil, errors.Wrapf(err, "found %d PSI fields, expected %d, file[%s]", nitems, psiNItems, statString)
}
}
return stats, nil
}
// NewPSIStats parses /proc/pressure/memory to create a PSIStats.
func NewPSIStats(ctx context.Context, a *arc.ARC) (*PSIStats, error) {
stats := &PSIStats{Timestamp: time.Now()}
// Gather PSI from the host first.
statBlob, err := ioutil.ReadFile(psiFilename)
if err == nil {
stats.Host, err = newPSISystemStats(statBlob)
if err != nil {
return nil, errors.Wrap(err, "error parsing Host PSI")
}
} // Otherwise, it is a host that does not support PSI.
if a != nil && ctx != nil {
out, err := a.Command(ctx, "cat", psiFilename).Output(testexec.DumpLogOnError)
if err == nil {
stats.Arc, err = newPSISystemStats(out)
if err != nil {
return nil, errors.Wrap(err, "error parsing Arc PSI")
}
} // Otherwise, this ARC does not allow access to PSI yet.
}
return stats, nil
}
func psiDetailMetrics(tag, sysname, suffix string, detail *PSIDetail, p *perf.Values, includeTotal bool) {
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_%s_avg10%s", sysname, tag, suffix),
Unit: "Percentage",
Direction: perf.SmallerIsBetter,
},
detail.Avg10,
)
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_%s_avg60%s", sysname, tag, suffix),
Unit: "Percentage",
Direction: perf.SmallerIsBetter,
},
detail.Avg60,
)
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_%s_avg300%s", sysname, tag, suffix),
Unit: "Percentage",
Direction: perf.SmallerIsBetter,
},
detail.Avg300,
)
if includeTotal {
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_%s_total%s", sysname, tag, suffix),
Unit: "Microseconds",
Direction: perf.SmallerIsBetter,
},
float64(detail.Total),
)
}
}
// psiDeltaMetrics dumps PSI metrics for one system (host or guest).
func psiDeltaMetrics(base, stat *PSIOneSystemStats, elapsedMicroseconds int64, p *perf.Values, sysname, suffix string) {
// Ignore inverted timings, which would generate noise.
// Inversion is the result of incorrect calling or
// (rare but normal) total counter wrap-arounds.
if elapsedMicroseconds <= 0 {
return
}
if stat.Some.Total >= base.Some.Total {
diff := float64(stat.Some.Total - base.Some.Total)
rate := diff / float64(elapsedMicroseconds)
rate *= 100.0
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_some_custom%s", sysname, suffix),
Unit: "Percentage",
Direction: perf.SmallerIsBetter,
},
rate,
)
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_some_delta%s", sysname, suffix),
Unit: "Microseconds",
Direction: perf.SmallerIsBetter,
},
diff,
)
}
if stat.Full.Total >= base.Full.Total {
diff := float64(stat.Full.Total - base.Full.Total)
rate := diff / float64(elapsedMicroseconds)
rate *= 100.0
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_full_custom%s", sysname, suffix),
Unit: "Percentage",
Direction: perf.SmallerIsBetter,
},
rate,
)
p.Set(
perf.Metric{
Name: fmt.Sprintf("%spsi_full_delta%s", sysname, suffix),
Unit: "Microseconds",
Direction: perf.SmallerIsBetter,
},
diff,
)
}
}
// PSIMetrics writes a JSON file containing statistics from PSI metrics.
// Parameter base is optional:
// * if base is set, it defines the starting point for metrics, and its contents
// are overwritten with the latest snapshot of PSI metrics.
// * if base is nil, metrics are averaged since boot.
// If outdir is "", then no logs are written.
func PSIMetrics(ctx context.Context, a *arc.ARC, base *PSIStats, p *perf.Values, outdir, suffix string) error {
stat, err := NewPSIStats(ctx, a)
if err != nil {
return err
}
if stat == nil {
return nil
}
if len(outdir) > 0 {
statJSON, err := json.MarshalIndent(stat, "", " ")
if err != nil {
return errors.Wrap(err, "failed to serialize psi metrics to JSON")
}
filename := fmt.Sprintf("psi%s.json", suffix)
if err := ioutil.WriteFile(path.Join(outdir, filename), statJSON, 0644); err != nil {
return errors.Wrapf(err, "failed to write psi stats to %s", filename)
}
}
if p == nil {
// No perf.Values, so exit without computing metrics.
return nil
}
includeTotalSinceBoot := true
if base != nil {
elapsedMicroseconds := stat.Timestamp.Sub(base.Timestamp).Microseconds()
// We will log blocked times during a custom interval, so no need for these,
// which are less useful.
includeTotalSinceBoot = false
if base.Host != nil && stat.Host != nil {
psiDeltaMetrics(base.Host, stat.Host, elapsedMicroseconds, p, "", suffix)
}
if base.Arc != nil && stat.Arc != nil {
psiDeltaMetrics(base.Arc, stat.Arc, elapsedMicroseconds, p, "arc_", suffix)
}
*base = *stat
}
if stat.Host != nil {
psiDetailMetrics(psiSomeTag, "", suffix, &(stat.Host.Some), p, includeTotalSinceBoot)
psiDetailMetrics(psiFullTag, "", suffix, &(stat.Host.Full), p, includeTotalSinceBoot)
}
if stat.Arc != nil {
psiDetailMetrics(psiSomeTag, "arc_", suffix, &(stat.Arc.Some), p, includeTotalSinceBoot)
psiDetailMetrics(psiFullTag, "arc_", suffix, &(stat.Arc.Full), p, includeTotalSinceBoot)
}
return nil
}
// LogPSIMetricsSlice logs delta metrics between two existing snapshots
// (no new snapshots are taken).
func LogPSIMetricsSlice(begin, end *PSIStats, p *perf.Values, suffix string) {
if begin == nil {
return
}
if end == nil {
return
}
elapsedMicroseconds := end.Timestamp.Sub(begin.Timestamp).Microseconds()
if begin.Host != nil && end.Host != nil {
psiDeltaMetrics(begin.Host, end.Host, elapsedMicroseconds, p, "", suffix)
}
if begin.Arc != nil && end.Arc != nil {
psiDeltaMetrics(begin.Arc, end.Arc, elapsedMicroseconds, p, "arc_", suffix)
}
}