-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstats.go
392 lines (342 loc) · 10.9 KB
/
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package test161
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"path"
"reflect"
"regexp"
"strconv"
"strings"
)
// sys161 2.0.5: nsec kinsns uinsns udud idle irqs exns disk con emu net
var validHead = regexp.MustCompile(`^HEAD\s+nsec\s+kinsns\s+uinsns\s+udud\s+idle\s+irqs\s+exns\s+disk\s+con\s+emu\s+net`)
var validStat = regexp.MustCompile(`^DATA\s+(?P<Nsec>\d+)\s+(?P<Kinsns>\d+)\s+(?P<Uinsns>\d+)\s+(?P<Udud>\d+)\s+(?P<Idle>\d+)\s+(?P<IRQs>\d+)\s+(?P<Exns>\d+)\s+(?P<Disk>\d+)\s+(?P<Con>\d+)\s+(?P<Emu>\d+)\s+(?P<Net>\d+)`)
// monitorError is a custom error type to differentiate monitor errors from
// other stat errors, like I/O errors. This is useful for the runner loop that
// might need to know why we shut down.
type monitorError struct {
msg string
}
func (err *monitorError) Error() string {
return err.msg
}
type Stat struct {
initialized bool
Start TimeFixedPoint `json:"start"`
End TimeFixedPoint `json:"end"`
Length TimeFixedPoint `json:"length"`
Count uint `json:"count"`
WallStart TimeFixedPoint `json:"wallstart"`
WallEnd TimeFixedPoint `json:"wallend"`
WallLength TimeFixedPoint `json:"walllength"`
// Read from stat line
Nsec uint64 `json:"-" bson:"-"`
Kinsns uint32 `json:"kinsns"`
Uinsns uint32 `json:"uinsns"`
Udud uint32 `json:"udud"`
Idle uint32 `json:"idle"`
IRQs uint32 `json:"irqs"`
Exns uint32 `json:"exns"`
Disk uint32 `json:"disk"`
Con uint32 `json:"con"`
Emu uint32 `json:"emu"`
Net uint32 `json:"net"`
// Derived
Insns uint32 `json:"insns"`
}
// Add adds two stat objects.
func (i *Stat) Add(j Stat) {
i.Nsec += j.Nsec
i.Kinsns += j.Kinsns
i.Uinsns += j.Uinsns
i.Udud += j.Udud
i.Idle += j.Idle
i.IRQs += j.IRQs
i.Exns += j.Exns
i.Disk += j.Disk
i.Con += j.Con
i.Emu += j.Emu
i.Net += j.Net
i.Insns += j.Insns
}
// Sub subtracts two stat objects.
func (i *Stat) Sub(j Stat) {
i.Nsec -= j.Nsec
i.Kinsns -= j.Kinsns
i.Uinsns -= j.Uinsns
i.Udud -= j.Udud
i.Idle -= j.Idle
i.IRQs -= j.IRQs
i.Exns -= j.Exns
i.Disk -= j.Disk
i.Con -= j.Con
i.Emu -= j.Emu
i.Net -= j.Net
i.Insns -= j.Insns
}
// Append appends the stat object to an existing stat object.
func (i *Stat) Append(j Stat) {
if i.initialized == false {
i.initialized = true
i.Count = 0
i.Start = j.Start
i.WallStart = j.WallStart
} else {
if i.End-j.Start > 0.00001 {
panic("test161: merge stats should be adjacent")
}
}
i.Count += 1
i.End = j.End
i.Length = TimeFixedPoint(float64(i.End) - float64(i.Start))
i.WallEnd = j.WallEnd
i.WallLength = TimeFixedPoint(float64(i.WallEnd) - float64(i.WallStart))
i.Add(j)
}
// Shift shifts the stat object from an existing stat object.
func (i *Stat) Shift(j Stat) {
if i.initialized == false {
panic("test161: can't unshift from an empty stat object")
}
if i.Start-j.Start > 0.00001 {
panic("test161: unshift stats should be adjacent")
}
i.Count -= 1
i.Start = j.End
i.Length = TimeFixedPoint(float64(i.End) - float64(i.Start))
i.WallStart = j.WallEnd
i.WallLength = TimeFixedPoint(float64(i.WallEnd) - float64(i.WallStart))
i.Sub(j)
}
// stopStats disables stats collection.
func (t *Test) stopStats(status string, message string, statErr error) {
if status != "" {
t.addStatus(status, message)
}
t.statCond.L.Lock()
t.statErr = statErr
t.statActive = false
t.statCond.Signal()
t.statCond.L.Unlock()
// If we're shutting down because the stat socket closed (EOF), then
// this is probably just part of the normal shutdown chain of events.
// In this case, don't invoke stop161, which closes the underlying pty file.
// Doing so can cause races in Test.Run which manifests itself as expect errors.
if statErr != nil || message != "" {
t.stop161()
}
}
// getStats is the main stats collection and monitor goroutine.
func (t *Test) getStats() {
defer close(t.statChan)
// Mark started
t.statCond.L.Lock()
t.statActive = true
t.statCond.L.Unlock()
// Connect to the sys161 stats socket.
var statConn net.Conn
statConn, err := net.Dial("unix", path.Join(t.tempDir, ".sockets/meter"))
if err != nil {
t.stopStats("stats", "couldn't connect", err)
return
}
// Configure stat interval.
_, err =
statConn.Write([]byte(fmt.Sprintf("INTERVAL %v\n", uint32(t.Stat.Resolution*1000*1000*1000))))
if err != nil {
t.stopStats("stats", "couldn't set interval", err)
return
}
// Set up previous stat values and timestamps for diffs.
wallStart := t.getWallTime()
simStart := TimeFixedPoint(float64(0.0))
lastStat := Stat{}
// Stats cache for the monitor. Not needed when monitoring is disabled.
monitorWindow := &Stat{}
var monitorCache []Stat
if t.Monitor.Enabled == "true" {
monitorCache = make([]Stat, 0, t.Monitor.Window)
}
// Record when to flush stat cache
lastCounter := -1
statReader := bufio.NewReader(statConn)
for {
var line string
// Grab a stat message.
line, err := statReader.ReadString('\n')
if err == io.EOF {
t.stopStats("", "", nil)
return
} else if err != nil {
t.stopStats("stats", "problem reading stats", err)
return
}
// Set the timestamp
wallEnd := t.getWallTime()
// Check HEAD messages
if strings.HasPrefix(line, "HEAD ") && validHead.FindString(line) == "" {
t.stopStats("stats", fmt.Sprintf("incorrect stat format: %v", line), errors.New("incorrect stat format"))
return
}
// Ignore non-data messages
if !strings.HasPrefix(line, "DATA ") {
continue
}
// Make sure it's a data message and blow up if we can't parse it.
statMatch := validStat.FindStringSubmatch(line)
if statMatch == nil {
t.stopStats("stats", "couldn't parse stat message", errors.New("couldn't parse stat message"))
return
}
// Pulse the CV to free the main loop if needed and update our recording
// and monitoring flags
t.statCond.L.Lock()
statRecord := t.statRecord
t.statCond.Signal()
t.statCond.L.Unlock()
// Create the new stat object and update timestamps
stats := Stat{
WallStart: wallStart,
WallEnd: wallEnd,
WallLength: TimeFixedPoint(float64(wallEnd) - float64(wallStart)),
}
wallStart = wallEnd
// A bit of reflection to move data from the regexp match to the
// stat object...
s := reflect.ValueOf(&stats).Elem()
for i, name := range validStat.SubexpNames() {
f := s.FieldByName(name)
x, err := strconv.ParseUint(statMatch[i], 10, 32)
if err != nil {
continue
}
f.SetUint(x)
}
// ... which doesn't work for all fields
stats.Nsec, _ = strconv.ParseUint(statMatch[1], 10, 64)
// sys161 instructions are single-cycle, so we can combine idle (cycles)
// with instructions
stats.Insns = stats.Kinsns + stats.Uinsns + stats.Idle
// Parse the simulation timestamps and update our boundaries
stats.Start = simStart
stats.End = TimeFixedPoint(float64(stats.Nsec) / 1000000000.0)
stats.Length = TimeFixedPoint(float64(stats.End) - float64(stats.Start))
simStart = stats.End
// sys161 stat objects are cumulative, but we want incremental.
temp := stats
stats.Sub(lastStat)
lastStat = temp
// Non-blocking send of new stats
select {
case t.statChan <- stats:
default:
}
// Update shared state, caching some values to use after dropping the lock
t.L.Lock()
t.SimTime = stats.End
if statRecord {
if (len(t.currentCommand.AllStats) == 0) ||
(t.currentCommand.AllStats[len(t.currentCommand.AllStats)-1].Count == t.Stat.Window) {
t.currentCommand.AllStats = append(t.currentCommand.AllStats, Stat{})
}
t.currentCommand.AllStats[len(t.currentCommand.AllStats)-1].Append(stats)
t.currentCommand.SummaryStats.Append(stats)
}
// Cached for use by the monitoring code below
progressTime := float64(t.SimTime) - t.progressTime
commandTime := float64(t.SimTime - t.currentCommand.StartTime)
currentCounter := t.commandCounter
currentType := t.currentCommand.Type
t.L.Unlock()
// If we've moved forward one command clear the stat cache.
if statRecord && int(currentCounter) != lastCounter {
monitorCache = nil
monitorWindow = &Stat{}
lastCounter = int(currentCounter)
}
// Monitoring code starts here. Only run the monitor if it's enabled
// globally and at this time (statRecord).
if t.Monitor.Enabled != "true" || !statRecord {
continue
}
// Update the statCache and moving window.
if uint(len(monitorCache)) == t.Monitor.Window {
var head Stat
head, monitorCache = monitorCache[0], monitorCache[1:]
monitorWindow.Shift(head)
}
monitorCache = append(monitorCache, stats)
monitorWindow.Append(stats)
// Begin checks for various error conditions. No real rhyme or reason to
// the order here. We could return multiple errors but that would be a bit
// of a pain.
monitorErrorMsg := ""
if progressTime > float64(t.Monitor.ProgressTimeout) {
monitorErrorMsg =
fmt.Sprintf("no progress for %v s in %v mode", t.Monitor.ProgressTimeout, currentType)
} else if t.currentCommand.Timeout > 0 && commandTime > float64(t.currentCommand.Timeout) {
t.currentCommand.TimedOut = true
monitorErrorMsg =
fmt.Sprintf("command timed out after %v seconds", commandTime)
} else {
// Only run these checks if we have enough state
if uint(len(monitorCache)) >= t.Monitor.Window {
if currentType == "kernel" && monitorWindow.Uinsns > 0 {
monitorErrorMsg = "non-zero user instructions during kernel operation"
} else if t.Monitor.Kernel.EnableMin == "true" &&
float64(monitorWindow.Kinsns)/float64(monitorWindow.Insns) < t.Monitor.Kernel.Min {
monitorErrorMsg = "insufficient kernel instructions (potential deadlock)"
} else if float64(monitorWindow.Kinsns)/float64(monitorWindow.Insns) > t.Monitor.Kernel.Max {
monitorErrorMsg = "too many kernel instructions (potential livelock)"
} else if currentType == "user" && t.Monitor.User.EnableMin == "true" &&
(float64(monitorWindow.Uinsns)/float64(monitorWindow.Insns) < t.Monitor.User.Min) {
monitorErrorMsg = "insufficient user instructions"
} else if currentType == "user" &&
(float64(monitorWindow.Uinsns)/float64(monitorWindow.Insns) > t.Monitor.User.Max) {
monitorErrorMsg = "too many user instructions"
}
}
}
// Before we blow up check to make sure that we haven't moved on to a
// different command or disabled recording or monitoring while we've been
// calculating.
if monitorErrorMsg != "" {
t.statCond.L.Lock()
blowup := t.statRecord
t.statCond.L.Unlock()
if blowup {
t.L.Lock()
blowup = blowup && (currentCounter == t.commandCounter)
t.L.Unlock()
if blowup {
t.stopStats("monitor", monitorErrorMsg, &monitorError{monitorErrorMsg})
return
}
}
}
}
}
// enableStats enables stats collection
func (t *Test) enableStats() (bool, error) {
t.statCond.L.Lock()
defer t.statCond.L.Unlock()
if !t.statActive {
return false, t.statErr
}
t.statRecord = true
t.statCond.Wait()
return true, nil
}
// disableStats disables stats collection
func (t *Test) disableStats() (bool, error) {
t.statCond.L.Lock()
defer t.statCond.L.Unlock()
if !t.statActive {
return false, t.statErr
}
t.statRecord = false
return true, nil
}