-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_state_machine.go
725 lines (611 loc) · 18.7 KB
/
input_state_machine.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
package libphonelabgo
import (
"fmt"
phonelab "github.com/shaseley/phonelab-go"
)
var ismDebug = false
var ignoreFrameTimes = true
// InputStateMachine parameters. These control what we consider local vs.
// global response, jankiness, timeouts, etc.
type InputStateMachineParams struct {
JankThresholdMs int64
JankFilterValue float64
LocalResponsePercent float64
LocalResponseRegions int
GlobalResponsePercent float64
GlobalResponseRegions int
UITimeoutMs int64
Connectivity PixelConnectivity
UsePendingTimestamp bool
SkipUndefinedResponse bool
JankOnFrameUpdate bool
}
// Create a new InputStateMachineParams with the default settings.
func DefaultInputStateMachineParams() *InputStateMachineParams {
return &InputStateMachineParams{
JankThresholdMs: 70,
JankFilterValue: 0.1,
LocalResponsePercent: 60.0,
LocalResponseRegions: 0,
GlobalResponsePercent: 20.0,
GlobalResponseRegions: 10,
UITimeoutMs: 3000,
Connectivity: FourConnected,
UsePendingTimestamp: false,
SkipUndefinedResponse: true,
}
}
func NewInputStateMachineParams(kwargs map[string]interface{}) *InputStateMachineParams {
params := DefaultInputStateMachineParams()
if v, ok := kwargs["jank_threshold_ms"]; ok {
params.JankThresholdMs = int64(v.(int))
}
if v, ok := kwargs["jank_filter_value"]; ok {
switch t := v.(type) {
case int:
params.JankFilterValue = float64(t)
case float64:
params.JankFilterValue = t
}
}
if v, ok := kwargs["ui_timeout_ms"]; ok {
params.UITimeoutMs = int64(v.(int))
}
if v, ok := kwargs["connectivity"]; ok {
switch v.(string) {
case "one":
params.Connectivity = OneConnected
case "four":
params.Connectivity = FourConnected
case "eight":
params.Connectivity = EightConnected
}
}
if v, ok := kwargs["local_resp_pct"]; ok {
switch t := v.(type) {
case int:
params.LocalResponsePercent = float64(t)
case float64:
params.LocalResponsePercent = t
}
}
if v, ok := kwargs["global_resp_pct"]; ok {
switch t := v.(type) {
case int:
params.GlobalResponsePercent = float64(t)
case float64:
params.GlobalResponsePercent = t
}
}
if v, ok := kwargs["global_regions"]; ok {
params.GlobalResponseRegions = v.(int)
}
if v, ok := kwargs["local_regions"]; ok {
params.LocalResponseRegions = v.(int)
}
if v, ok := kwargs["use_pending_ts"]; ok {
params.UsePendingTimestamp = v.(bool)
}
if v, ok := kwargs["skip_undefined_resp"]; ok {
params.SkipUndefinedResponse = v.(bool)
}
if v, ok := kwargs["jank_on_frame_update"]; ok {
params.JankOnFrameUpdate = v.(bool)
}
fmt.Println("ISM Parameters:", *params)
return params
}
// InputStateMachine states
const (
InputStateWaitInput = iota
InputStateWaitResponse
InputStateMeasureLocal
InputStateMeasureGlobal
InputStateMeasureScroll
)
// InputStateMachine is the state machine we use to measure performance metrics
// of an input event. For now, we only support taps, but this can (and should)
// be extended to handle scrolls as well. Eventually, we'd like to have a state
// machine that models all basic interactions.
type InputStateMachine struct {
// Parameters
Params *InputStateMachineParams
// State
curState int
curResult *InputEventResult
curEvent *TouchScreenEvent
pendingResponseStartNs int64
scrollKeepaliveNs int64
}
// Create a new InputStateMachine with the default parameters.
func NewInputStateMachine() *InputStateMachine {
return &InputStateMachine{
Params: DefaultInputStateMachineParams(),
pendingResponseStartNs: InvalidResponseTime,
}
}
// JankEvent defines one instance in a measured response where consecutive
// inter-frame time delta is above some threshold. In practice, there are
// different types of jank, but they are all measured in the same way.
type JankEvent struct {
TimestampNs int64 `json:"timestamp_ns"`
JankAmount int64 `json:"jank_amount"`
}
const (
InvalidResponseTime = -1
InvalidResponseDuration = 999999999999
)
const (
TapEventFinishTimeout = iota
TapEventFinishShortCircuit
)
// InputEventResult encapsulates the response detail and performance metrics of a
// single tap event.
type InputEventResult struct {
TimestampNs int64 `json:"timestamp_ns"`
FinishNs int64 `json:"finish_ns"`
FinishType int `json:"finish_type"`
LocalResponse *ResponseDetail `json:"local_response"`
GlobalResponse *ResponseDetail `json:"global_response"`
Jank []*JankEvent `json:"jank_events"`
EventType int `json:"event_type"`
ScrollStopNs int64 `json:"scroll_stop_ns"`
prevFrameTimeNs int64
}
func NewInputEventResult(event *TouchScreenEvent) *InputEventResult {
return &InputEventResult{
EventType: event.What,
TimestampNs: event.Timestamp,
LocalResponse: NewResponseDetail(),
GlobalResponse: NewResponseDetail(),
Jank: make([]*JankEvent, 0),
}
}
func (t *InputEventResult) HasLocalResponse() bool {
return t.LocalResponse.HasResponse()
}
func (t *InputEventResult) HasGlobalResponse() bool {
return t.GlobalResponse.HasResponse()
}
func (t *InputEventResult) HasResponse() bool {
return t.HasLocalResponse() || t.HasLocalResponse()
}
func (t *InputEventResult) TouchResponseMs() int64 {
if t.HasLocalResponse() {
return (t.LocalResponse.StartNs - t.TimestampNs) / nsPerMs
} else {
return t.GlobalResponseMs()
}
}
func (t *InputEventResult) GlobalResponseMs() int64 {
if t.HasGlobalResponse() {
return (t.GlobalResponse.StartNs - t.TimestampNs) / nsPerMs
} else {
return InvalidResponseDuration
}
}
func (t *InputEventResult) LocalResponseDurationMs() int64 {
return t.LocalResponse.DurationMs()
}
func (t *InputEventResult) GlobalResponseDurationMs() int64 {
return t.GlobalResponse.DurationMs()
}
func (t *InputEventResult) ContentDelayMs() int64 {
if t.HasGlobalResponse() {
return (t.GlobalResponse.StartNs - t.TimestampNs) / nsPerMs
} else {
return InvalidResponseDuration
}
}
// ResponseDetail contains the information needed to capture the performance
// metrics for either a local or global response to an input event.
type ResponseDetail struct {
StartNs int64 `json:"start_ns"`
EndNs int64 `json:"end_ns"`
params *InputStateMachineParams `json:"-"`
}
func NewResponseDetail() *ResponseDetail {
return &ResponseDetail{
StartNs: InvalidResponseTime,
EndNs: InvalidResponseTime,
}
}
func (response *ResponseDetail) HasResponse() bool {
return response.StartNs != InvalidResponseTime
}
func (response *ResponseDetail) DurationMs() int64 {
if response.StartNs == InvalidResponseTime || response.EndNs == InvalidResponseTime {
return InvalidResponseDuration
} else {
return (response.EndNs - response.StartNs) / nsPerMs
}
}
func (response *ResponseDetail) DurationNs() int64 {
if response.StartNs == InvalidResponseTime || response.EndNs == InvalidResponseTime {
return InvalidResponseDuration
} else {
return response.EndNs - response.StartNs
}
}
// Update the response with a new diff sample.
func (response *ResponseDetail) onFrameDiff(diff *FrameDiffSample) {
if diff.PctDiff == 0.0 {
// Don't advance the state at all.
return
}
// Advance the end time for this response
response.EndNs = diff.TimestampNs()
}
// Short-circuit the current result/analysis. This gets called when we're
// analyzing the post-tap stream and another input event comes along.
func (ism *InputStateMachine) shortCircuit(ts int64) *InputEventResult {
if ism.curResult == nil {
return nil
}
// Finish detail
ism.curResult.FinishType = TapEventFinishShortCircuit
ism.curResult.FinishNs = ts
ism.curResult.prevFrameTimeNs = 0
// TODO: Do we need to detect timeouts here?
// If the diffs interlace zeros, then probably not since we'll have
// frequent timestamps.
return ism.curResult
}
// State change InputStateWaitResponse --> InputStateWaitInput
func (ism *InputStateMachine) handleTimeout(ts int64) *InputEventResult {
res := ism.curResult
res.FinishType = TapEventFinishTimeout
res.FinishNs = ts
res.prevFrameTimeNs = 0
// --> InputStateWaitInput
ism.reset()
return res
}
// Reset to the start state (--> InputStateWaitInput).
func (ism *InputStateMachine) reset() {
if ismDebug {
fmt.Println("State = wait input")
}
ism.curState = InputStateWaitInput
ism.curEvent = nil
ism.curResult = nil
}
type responseType int
const (
responseTypeLocal responseType = iota
responseTypeGlobal
responseTypeNone
responseTypeNeither
)
// Determine the responseType of the frame diff
func (ism *InputStateMachine) getResponseType(diff *FrameDiffSample) responseType {
if diff.PctDiff == 0.0 {
return responseTypeNone
}
localPctDiff, localPctNormalized, err := diff.LocalDiff(ism.Params.Connectivity, ism.curEvent.X, ism.curEvent.Y)
if err != nil {
// Not good.
panic(err)
}
// FIXME: Is this approach reasonable?
// If the local diff is non-zero and a substantial percentage of the
// total diff, we consider it a local response.
if localPctDiff > 0 {
ratio := 100.0 * (localPctNormalized / diff.PctDiff)
if ismDebug {
fmt.Println("Local ratio:", ratio)
}
if ratio >= ism.Params.LocalResponsePercent &&
(ism.Params.LocalResponseRegions == 0 || len(diff.GridEntries) <= ism.Params.LocalResponseRegions) {
return responseTypeLocal
}
} else if ismDebug {
fmt.Println("No local diff, pct=", diff.PctDiff)
}
// Not a local diff
if diff.PctDiff >= ism.Params.GlobalResponsePercent || len(diff.GridEntries) >= ism.Params.GlobalResponseRegions {
return responseTypeGlobal
}
return responseTypeNeither
}
// Update state and possibly return an event result
func (ism *InputStateMachine) OnTouchEvent(event *TouchScreenEvent) *InputEventResult {
// For all other touch events, this short-circuits the current state if
// we're not at the start/wait state.
var cur *InputEventResult = nil
// Skip key events, except power
if event.What == TouchScreenEventKey {
if event.Code == KEYCODE_POWER {
// Hard key, we need this one.
// TODO: Should we just look at the screen on/off logs?
if ism.curState != InputStateWaitInput {
cur = ism.shortCircuit(event.Timestamp)
}
ism.reset()
return cur
}
return nil
}
// New event staring
if event.What == TouchScreenEventTap || event.What == TouchScreenEventScrollStart {
if ism.curState != InputStateWaitInput {
cur = ism.shortCircuit(event.Timestamp)
}
// Clear state
ism.reset()
// If it's a tap or scroll event, transition --> InputStateWaitResponse
ism.startWaitingForResponse(event)
} else if event.What == TouchScreenEventScrollEnd && ism.curEvent != nil {
// No state transition though, we want to keep evaluating the output.
// TODO: Eventually, this might want to transition to a new scrolling
// non-active state.
ism.curResult.ScrollStopNs = event.Timestamp
}
return cur
}
// State change from InputStateWaitInput --> InputStateWaitResponse
func (ism *InputStateMachine) startWaitingForResponse(event *TouchScreenEvent) {
if ismDebug {
fmt.Println("State = wait response")
}
ism.curState = InputStateWaitResponse
ism.curEvent = event
ism.curResult = NewInputEventResult(event)
//ism.curResult.prevFrameTime = event.Timestamp / 1000000
}
// State change from InputStateWaitResponse --> InputStateMeasureLocal
func (ism *InputStateMachine) startMeasuringLocalRepsonse(diff *FrameDiffSample) {
if ismDebug {
fmt.Println("State = measure local")
}
ism.curState = InputStateMeasureLocal
ism.startMeasuringRepsonse(ism.curResult.LocalResponse, diff)
}
// State change from InputStateWaitResponse or InputStateMeasureLocal --> InputStateMeasureGlobal
func (ism *InputStateMachine) startMeasuringGlobalRepsonse(diff *FrameDiffSample) {
if ismDebug {
fmt.Println("State = measure global")
}
ism.curState = InputStateMeasureGlobal
ism.startMeasuringRepsonse(ism.curResult.GlobalResponse, diff)
}
func (ism *InputStateMachine) startMeasuringRepsonse(response *ResponseDetail, diff *FrameDiffSample) {
if ism.pendingResponseStartNs != InvalidResponseTime {
// Inch closer to the actual start by marking the start at the first
// refresh event we saw while waiting
response.StartNs = ism.pendingResponseStartNs
ism.pendingResponseStartNs = InvalidResponseTime
} else {
response.StartNs = diff.TimestampNs()
}
response.EndNs = diff.TimestampNs()
}
func (ism *InputStateMachine) checkJank(timestampNs int64) {
if ism.curResult != nil {
if ism.curResult.prevFrameTimeNs > 0 {
delta := (timestampNs - ism.curResult.prevFrameTimeNs) / 1000000
if delta >= ism.Params.JankThresholdMs {
ism.curResult.Jank = append(ism.curResult.Jank, &JankEvent{
TimestampNs: timestampNs,
JankAmount: delta,
})
}
}
ism.curResult.prevFrameTimeNs = timestampNs
}
}
// Update state and possibly return an event result.
func (ism *InputStateMachine) OnFrameDiff(diff *FrameDiffSample) *InputEventResult {
// Short circuit: we don't do anything with diffs if we're waiting for
// input.
if ism.curState == InputStateWaitInput {
return nil
}
// We have a response (which could be 0.0%), but what type is it?
rt := ism.getResponseType(diff)
// Reset the pending response start if the diff is actully zero
if ism.Params.UsePendingTimestamp &&
rt == responseTypeNone && ism.curState == InputStateWaitResponse {
ism.pendingResponseStartNs = InvalidResponseTime
}
// Jank check - applies to taps and scrolls, all states unless waiting for input.
if ism.curResult != nil && diff.PctDiff > ism.Params.JankFilterValue &&
!ism.Params.JankOnFrameUpdate {
ism.checkJank(diff.TimestampNs())
}
// Handle timeouts in one shot
if rt == responseTypeNone {
ns := int64(0)
switch ism.curState {
case InputStateWaitResponse:
ns = ism.curResult.TimestampNs
case InputStateMeasureLocal:
ns = ism.curResult.LocalResponse.EndNs
case InputStateMeasureGlobal:
ns = ism.curResult.GlobalResponse.EndNs
case InputStateMeasureScroll:
ns = ism.scrollKeepaliveNs
}
if ns > 0 && (diff.TimestampNs()-ns)/nsPerMs >= ism.Params.UITimeoutMs {
// We timed out
return ism.handleTimeout(diff.TimestampNs())
} else {
// Otherwise, no change
return nil
}
}
if ismDebug {
fmt.Println("Diff TS =", diff.Timestamp)
}
// OK, we have a resonse of some sort and we're not in the start state.
// We'll either update the current response, or transition to a different
// start.
switch ism.curState {
default:
{
panic(fmt.Sprint("Unexpected state: %v", ism.curState))
}
case InputStateWaitResponse:
{
if ism.curEvent.What == TouchScreenEventScrollStart {
// Transition to the
return nil
} else if ism.Params.SkipUndefinedResponse && rt == responseTypeNeither {
// No change
if ismDebug {
fmt.Println("Skipping non-local/non-global response while waiting")
}
return nil
} else if rt == responseTypeLocal {
if ismDebug {
fmt.Println("Local response (wait response)")
}
// State transition to wait start measuring local response
ism.startMeasuringLocalRepsonse(diff)
} else {
if ismDebug {
fmt.Println("Global response (wait response)")
}
// TODO: This is a simplified -- we're considering any
// non-local response as a global response. In practice, we
// could have an animated transition which we shouldn't count.
// For now, the spinner detection module handles that and we
// evaluate it separately.
// State transition to wait start measuring global response
ism.startMeasuringGlobalRepsonse(diff)
}
}
case InputStateMeasureLocal:
{
if rt == responseTypeLocal {
if ismDebug {
fmt.Println("Local response (measure local)")
}
// TODO: This is a simplified -- we're considering any
// Keep measuring local
ism.curResult.LocalResponse.onFrameDiff(diff)
} else {
if ismDebug {
fmt.Println("Global response (measure local)")
}
// State transition to start measuring the global response
ism.startMeasuringGlobalRepsonse(diff)
}
}
case InputStateMeasureGlobal:
{
if ismDebug {
fmt.Println("? response (measure global)")
}
// Even if the diff is only local, we had a global response and we
// don't flip-flop states.
ism.curResult.GlobalResponse.onFrameDiff(diff)
}
case InputStateMeasureScroll:
{
// All we need to do is update the keepalive for timeouts. The jank
// detection is already handled.
ism.scrollKeepaliveNs = diff.TimestampNs()
}
}
return nil
}
func (ism *InputStateMachine) OnFrameRefresh(event *FrameRefreshEvent) *InputEventResult {
// At this point, we'll only use this info to update the jankiness, so we'll always
// return nil.
// It's an option where to do this.
if !ism.Params.JankOnFrameUpdate {
return nil
}
switch ism.curState {
default:
{
return nil
}
case InputStateWaitResponse:
{
if ism.Params.UsePendingTimestamp {
if ism.pendingResponseStartNs == InvalidResponseTime {
ism.pendingResponseStartNs = event.SysTimeNs
}
}
}
fallthrough
case InputStateMeasureScroll:
fallthrough
case InputStateMeasureLocal:
fallthrough
case InputStateMeasureGlobal:
{
// Jank check - applies to taps and scrolls, all states unless waiting for input.
if ism.curResult != nil {
ism.checkJank(event.SysTimeNs)
}
}
}
return nil
}
// Called when the input log stream is finished
func (ism *InputStateMachine) Finish(ts int64) *InputEventResult {
if ism.curState != InputStateWaitInput {
// Just short-circuit the current test in the same way as if we would
// received a new touch event.
return ism.shortCircuit(ts)
}
return nil
}
type InputStateMachineProcessor struct {
Source phonelab.Processor
Args map[string]interface{}
}
func (proc *InputStateMachineProcessor) Process() <-chan interface{} {
outChan := make(chan interface{})
inChan := proc.Source.Process()
go func() {
ism := NewInputStateMachine()
if len(proc.Args) > 0 {
ism.Params = NewInputStateMachineParams(proc.Args)
}
var lastTs int64
for iLog := range inChan {
// We're only expecting frame diffs and input logs
switch t := iLog.(type) {
case *TouchScreenEvent:
{
if res := ism.OnTouchEvent(t); res != nil {
outChan <- res
}
}
case *FrameDiffSample:
{
// TODO: This should be done elsewhere
(&t.SFFrameDiff).initScreenGrid(allScreenGrids[0])
if res := ism.OnFrameDiff(t); res != nil {
outChan <- res
}
}
case *FrameRefreshEvent:
{
lastTs = t.SysTimeNs
if res := ism.OnFrameRefresh(t); res != nil {
outChan <- res
}
}
}
}
if res := ism.Finish(lastTs); res != nil {
outChan <- res
}
// Done.
close(outChan)
}()
return outChan
}
////////////////////////////////////////////////////////////////////////////////
func GenerateISMProcessor(source *phonelab.PipelineSourceInstance,
kwargs map[string]interface{}) phonelab.Processor {
return &InputStateMachineProcessor{
Source: source.Processor,
Args: kwargs,
}
}