-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspinners.go
398 lines (327 loc) · 9.41 KB
/
spinners.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
393
394
395
396
397
398
package libphonelabgo
import (
"encoding/json"
phonelab "github.com/shaseley/phonelab-go"
)
type Spinner struct {
StartTimeMs int64 `json:"start_time_ms"`
EndTimeMs int64 `json:"end_time_ms"`
DurationMs int64 `json:"duration_ms"`
TraceTimeStart float64 `json:"trace_time_start"`
TraceTimeEnd float64 `json:"trace_time_end"`
}
func (s *Spinner) MonotonicTimestamp() float64 {
return s.TraceTimeStart
}
func (s *Spinner) Append(other *Spinner) {
s.EndTimeMs = other.EndTimeMs
s.TraceTimeEnd = other.TraceTimeEnd
s.DurationMs = s.EndTimeMs - s.StartTimeMs
}
type SpinnerAlgoGenerator struct{}
// All spinner algorithm parameters. Not all algorithms use the same parameters,
// but we put them all in one struct to make things simpler.
type SpinnerAlgoConf struct {
Name string `json:"name" yaml:"name"`
Group string `json:"group" yaml:"group"`
Min float64 `json:"min" yaml:"min"`
Max float64 `json:"max" yaml:"max"`
IgnoreZeros bool `json:"ignore_zeros" yaml:"ignore_zeros"`
NumVotesIn int `json:"num_votes_in" yaml:"num_votes_in"`
NumVotesOut int `json:"num_votes_out" yaml:"num_votes_out"`
}
func NewSpinnerAlgoConf(kwargs map[string]interface{}) *SpinnerAlgoConf {
p := &SpinnerAlgoConf{}
if v, ok := kwargs["min"]; ok {
p.Min, ok = v.(float64)
if !ok {
// Maybe it was an int?
if tmp, ok := v.(int); ok {
p.Min = float64(tmp)
}
}
}
if v, ok := kwargs["max"]; ok {
p.Max, ok = v.(float64)
if !ok {
// Maybe it was an int?
if tmp, ok := v.(int); ok {
p.Max = float64(tmp)
}
}
}
if v, ok := kwargs["ignoreZeros"]; ok {
p.IgnoreZeros, _ = v.(bool)
}
if v, ok := kwargs["algo"]; ok {
p.Name, _ = v.(string)
}
if v, ok := kwargs["group"]; ok {
p.Group, _ = v.(string)
}
if v, ok := kwargs["votesIn"]; ok {
p.NumVotesIn, _ = v.(int)
}
if v, ok := kwargs["votesOut"]; ok {
p.NumVotesOut, _ = v.(int)
}
return p
}
func (g *SpinnerAlgoGenerator) GenerateProcessor(source *phonelab.PipelineSourceInstance,
kwargs map[string]interface{}) phonelab.Processor {
conf := NewSpinnerAlgoConf(kwargs)
switch conf.Name {
case "naive":
return phonelab.NewSimpleProcessor(source.Processor,
NewNaiveSpinnerAlgo(conf))
case "voting":
return phonelab.NewSimpleProcessor(source.Processor,
NewVotingSpinnerAlgo(conf))
default:
// TODO: this should be able to return an error
panic("Cannot find algo '" + conf.Name + "'!")
}
}
// TODO:
// Build processors to run the spinner algorithms
// -> Output spinners, one at a time
// -> Parameterize them so we can specify from yaml
//
// Try to extract out common logic
////////////////////////////////////////////////////////////////////////////////
// Common spinner state
type spinnerState struct {
isSpinner bool
startMs int64
startTrace float64
}
func (state *spinnerState) setState(isSpinner bool) {
state.isSpinner = isSpinner
}
func (state *spinnerState) markStartTime(sample *FrameDiffSample) {
state.startMs = sample.Timestamp
state.startTrace = sample.MonotonicTimestamp()
}
func (state *spinnerState) endSpinner(sample *FrameDiffSample) *Spinner {
return &Spinner{
StartTimeMs: state.startMs,
EndTimeMs: sample.Timestamp,
DurationMs: sample.Timestamp - state.startMs,
TraceTimeStart: state.startTrace,
TraceTimeEnd: sample.MonotonicTimestamp(),
}
}
////////////////////////////////////////////////////////////////////////////////
// NaiveSpinnerAlgo is just that, very naive. It classifies on a per-frame basis
// without caring about previous or future frames.
type NaiveSpinnerAlgo struct {
Conf SpinnerAlgoConf
state *spinnerState
}
func NewNaiveSpinnerAlgo(conf *SpinnerAlgoConf) *NaiveSpinnerAlgo {
return &NaiveSpinnerAlgo{
Conf: *conf,
state: &spinnerState{},
}
}
func (algo *NaiveSpinnerAlgo) Handle(log interface{}) interface{} {
// We're expecting only frame diff samples
sample, ok := log.(*FrameDiffSample)
if !ok {
return nil
}
diff := sample.PctDiff
// Only if we have a state change do we need to do anything,
// and if 0.0 and ignore zeros, we don't change states.
if diff > float64(0.0) || !algo.Conf.IgnoreZeros {
isSpinner := (diff > algo.Conf.Min && diff < algo.Conf.Max)
if isSpinner && !algo.state.isSpinner {
algo.state.setState(true)
algo.state.markStartTime(sample)
} else if !isSpinner && algo.state.isSpinner {
algo.state.setState(false)
return algo.state.endSpinner(sample)
}
}
return nil
}
func (algo *NaiveSpinnerAlgo) Finish() {}
////////////////////////////////////////////////////////////////////////////////
// Simple voting classifier
type VotingSpinnerAlgo struct {
Conf SpinnerAlgoConf
// State
state *spinnerState
votesNeeded int
}
func NewVotingSpinnerAlgo(conf *SpinnerAlgoConf) *VotingSpinnerAlgo {
c := &VotingSpinnerAlgo{
Conf: *conf,
state: &spinnerState{},
votesNeeded: conf.NumVotesIn,
}
return c
}
func (algo *VotingSpinnerAlgo) voteFor(sample *FrameDiffSample) {
if algo.state.isSpinner {
// positive reinforcement, reset votes
algo.votesNeeded = algo.Conf.NumVotesOut
} else {
// A step in the right direction. But, how many steps?
if algo.votesNeeded == algo.Conf.NumVotesIn {
// No samples yet, mark the start time
algo.state.markStartTime(sample)
}
algo.votesNeeded -= 1
if algo.votesNeeded <= 0 {
// State change, we've identified a spinner
algo.state.setState(true)
algo.votesNeeded = algo.Conf.NumVotesOut
}
}
}
func (algo *VotingSpinnerAlgo) voteAgainst(sample *FrameDiffSample) *Spinner {
if !algo.state.isSpinner {
// negative reinforcement, reset votes
algo.votesNeeded = algo.Conf.NumVotesIn
} else {
algo.votesNeeded -= 1
if algo.votesNeeded <= 0 {
// State change, the spinner is done
algo.state.setState(false)
algo.votesNeeded = algo.Conf.NumVotesIn
// Return the previous spinner
return algo.state.endSpinner(sample)
}
}
return nil
}
func (algo *VotingSpinnerAlgo) Handle(log interface{}) interface{} {
// Simple state machine:
// Require N consecutive samples with the bounds to classify as yes,
// and require M consecutive samples outside of the bounds to classify
// as no.
// We're expecting only frame diff samples
sample, ok := log.(*FrameDiffSample)
if !ok {
return nil
}
diff := sample.PctDiff
cur := (diff > algo.Conf.Min && diff < algo.Conf.Max)
if cur {
algo.voteFor(sample)
return nil
} else {
// Only votes against can return a spinner.
if res := algo.voteAgainst(sample); res != nil {
return res
}
}
return nil
}
func (algo *VotingSpinnerAlgo) Finish() {}
////////////////////////////////////////////////////////////////////////////////
// Spinner Collector
type SpinnerCollectorProcessor struct {
FileName string
SpinnerConf *SpinnerAlgoConf
Source phonelab.Processor
}
func NewSpinnerCollectorProcessor(inst *phonelab.PipelineSourceInstance, args map[string]interface{}) *SpinnerCollectorProcessor {
res := &SpinnerCollectorProcessor{
SpinnerConf: NewSpinnerAlgoConf(args),
Source: inst.Processor,
}
res.FileName = inst.Info.Context()
return res
}
type SpinnerCollectorOutput struct {
File string `json:"file"`
Conf *SpinnerAlgoConf `json:"conf"`
Spinners []*Spinner `json:"spinners"`
}
func (o *SpinnerCollectorOutput) Json() string {
outputBytes, err := json.MarshalIndent(o, "", " ")
if err != nil {
return ""
}
return string(outputBytes)
}
func (p *SpinnerCollectorProcessor) Process() <-chan interface{} {
outChan := make(chan interface{})
go func() {
inChan := p.Source.Process()
spinners := make([]*Spinner, 0)
for log := range inChan {
if spinner, ok := log.(*Spinner); ok && spinner != nil {
spinners = append(spinners, spinner)
}
}
outChan <- &SpinnerCollectorOutput{
File: p.FileName,
Conf: p.SpinnerConf,
Spinners: spinners,
}
close(outChan)
}()
return outChan
}
type SpinnerCollectorGenerator struct{}
func (g *SpinnerCollectorGenerator) GenerateProcessor(source *phonelab.PipelineSourceInstance,
kwargs map[string]interface{}) phonelab.Processor {
return NewSpinnerCollectorProcessor(source, kwargs)
}
type SpinnerStitcher struct {
StitchInterval int64
Source phonelab.Processor
}
func (stitcher *SpinnerStitcher) Process() <-chan interface{} {
outChan := make(chan interface{})
inChan := stitcher.Source.Process()
go func() {
var curSpinner *Spinner = nil
for log := range inChan {
// We're expecting only spinners
s, ok := log.(*Spinner)
if ok {
if stitcher.StitchInterval <= 0 {
outChan <- s
} else if s.DurationMs <= 1000 {
if curSpinner != nil {
outChan <- curSpinner
curSpinner = nil
}
outChan <- s
} else if curSpinner == nil {
curSpinner = s
} else if s.StartTimeMs-curSpinner.EndTimeMs < stitcher.StitchInterval {
// Combine the spinners
curSpinner.Append(s)
} else {
// Send old, save off new
outChan <- curSpinner
curSpinner = s
}
}
}
// Send the final spinner, if there is one
if curSpinner != nil {
outChan <- curSpinner
}
// Done.
close(outChan)
}()
return outChan
}
type SpinnerStitcherGen struct{}
func (g *SpinnerStitcherGen) GenerateProcessor(source *phonelab.PipelineSourceInstance,
kwargs map[string]interface{}) phonelab.Processor {
var interval int
if v, ok := kwargs["interval"]; ok {
interval, _ = v.(int)
}
return &SpinnerStitcher{
StitchInterval: int64(interval),
Source: source.Processor,
}
}