-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_surfaceflinger.go
343 lines (273 loc) · 9.23 KB
/
parse_surfaceflinger.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
package libphonelabgo
import (
"encoding/json"
"fmt"
phonelab "github.com/shaseley/phonelab-go"
"strings"
)
const (
nexus6ScreenW = 1440
nexus6ScreenH = 2560
)
// Example:
// 3a45bd43-82d2-4650-8571-039f48c0fdca 2016-12-02 03:03:28.15999782 453831 [184538.859034] 291 291 I SurfaceFlinger: {"fps":37.8, "tot_frames":84729, "prev_frames":84691, "cur_time": 184538788137586, "prev_time": 184537784018732}
type SFFpsLog struct {
FPS float64 `json:"fps"`
TotalFrames int `json:"tot_frames"`
PrevFrames int `json:"prev_frames"`
SysTimestamp int64 `json:"cur_time"`
PrevTimestamp int64 `json:"prev_time"`
}
type SFFpsParserProps struct{}
func (p *SFFpsParserProps) New() interface{} {
return &SFFpsLog{}
}
func NewSFFpsParer() phonelab.Parser {
return phonelab.NewJSONParser(&SFFpsParserProps{})
}
// Example:
// 2017-04-25 12:27:30.713.713999999 11237 11237 I SurfaceFlinger: {"ft_token":12, "times":[32149519089268,32149536072757,32149552896768,32149586269372,32149602984164,32149653607080,32149671089632,32149687328382,32149721374789,32149737946091,32149755442289,32149772077393,32149788590726,32149805492028,32149822233590,32149840559632,32149856163018,32149873109840,32149940872601,32149957166559,32150007928903,32150058306715,32150108952340,32150159534320,32150226974945,32150244002861,32150278004267,32150361778069,32150446512913,32150513607861]}
type SFFrameTimesLog struct {
Token int64 `json:"ft_token"`
Times []int64 `json:"times"`
}
type SFFrameTimesParserProps struct{}
func (p *SFFrameTimesParserProps) New() interface{} {
return &SFFrameTimesLog{}
}
func NewSFFrameTimesParser() phonelab.Parser {
return phonelab.NewJSONParser(&SFFrameTimesParserProps{})
}
// Example -- old logs -- malformed json :-(
//3a45bd43-82d2-4650-8571-039f48c0fdca 2016-12-02 03:03:27.885999782 453830 [184538.724191] 291 291 I SurfaceFlinger: {"token":1699, "diffs":[[184533085, 0.000000],[184533135, 0.000000],[184533880, 0.000000],[184533941, 0.493028],[184533994, 69.184029],[184534044, 100.000000],[184534095, 100.000000],[184534146, 99.305557],[184534196, 97.222221],[184534246, 93.315971],[184534297, 71.571182],[184534347, 0.000000],[184538215, 100.000000],[184538279, 0.010851],[184538333, 0.008138],[184538384, 0.007968],[184538429, 0.007629],[184538497, 0.008138],[184538563, 0.008816],[184538632, 0.008816]]}
type SFFrameDiffLog struct {
Token int64 `json:"token"`
Diffs []*SFFrameDiff `json:"diffs"`
}
type SFFrameDiff struct {
Timestamp int64 `json:"ts"`
PctDiff float64 `json:"diff"`
Mode int `json:"mode"`
HasColor int `json:"color"`
GridWH int `json:"wh"`
GridEntries []*GridEntry `json:"grid"`
Grid *ScreenGrid `json:"-"`
}
func (diff *SFFrameDiff) TimestampNs() int64 {
return diff.Timestamp * nsPerMs
}
type GridEntry struct {
Position int `json:"p"`
Value float64 `json:"v"`
}
type ScreenGrid struct {
grid [][]float64
props *screenGridProps
}
type screenGridProps struct {
rows int
cols int
gridWH int
screenW int
screenH int
edgeMultRow float64
edgeMultCol float64
pixelsPerWH float64
}
var allScreenGrids []*screenGridProps
func init() {
allScreenGrids = []*screenGridProps{
&screenGridProps{
screenW: 1440,
screenH: 2560,
gridWH: 8,
rows: 8,
cols: 5,
edgeMultRow: 1.0,
edgeMultCol: 2.0,
pixelsPerWH: 2560 / 8,
},
}
}
// Returns -1 if out of expected bounds
func (props *screenGridProps) entryPosToGridPos(pos int) (row, col int) {
row = pos / props.gridWH
col = pos - (row * props.gridWH)
if row >= props.rows || col >= props.cols {
row, col = -1, -1
return
}
// SF grid starts in lower left, but input coordinates start in upper left,
// so we'll mirror the grid height pos.
row = (props.gridWH - 1) - row
return row, col
}
func (props *screenGridProps) gridPosFromXY(x, y float64) (row, col int) {
row = int(y / props.pixelsPerWH)
col = int(x / props.pixelsPerWH)
if col < 0 || col >= props.cols ||
row < 0 || row >= props.rows {
row, col = -1, -1
}
return
}
func (diff *SFFrameDiff) initScreenGrid(props *screenGridProps) {
diff.Grid = &ScreenGrid{
props: props,
}
grid := make([][]float64, props.rows, props.rows)
for i := 0; i < props.rows; i += 1 {
grid[i] = make([]float64, props.cols, props.cols)
}
// Old format logs didn't have gridded diffs
if props.rows == 1 && props.cols == 1 {
grid[0][0] = diff.PctDiff
return
}
for _, entry := range diff.GridEntries {
if row, col := props.entryPosToGridPos(entry.Position); row < 0 || col < 0 {
panic("New grid position < 0!")
} else {
grid[row][col] = entry.Value
}
}
diff.Grid.grid = grid
}
type PixelConnectivity int
const (
OneConnected PixelConnectivity = 0
FourConnected = 4
EightConnected = 8
)
type position struct {
row int
col int
}
func (diff *SFFrameDiff) LocalDiff(connectivity PixelConnectivity, x, y float64) (float64, float64, error) {
if connectivity != FourConnected && connectivity != EightConnected && connectivity != OneConnected {
return 0.0, 0, fmt.Errorf("Invalid connectivity '%v', expected 4 or 8", connectivity)
}
props := diff.Grid.props
row, col := props.gridPosFromXY(x, y)
if row < 0 || col < 0 {
return 0.0, 0, fmt.Errorf("Invalid positions: x=%v, y=%v is out of bounds", x, y)
}
positions := make([]*position, 0, int(connectivity)+1)
positions = append(positions, &position{row, col})
if connectivity != OneConnected {
positions = append(positions, &position{row - 1, col})
positions = append(positions, &position{row + 1, col})
positions = append(positions, &position{row, col - 1})
positions = append(positions, &position{row, col + 1})
}
if connectivity == EightConnected {
positions = append(positions, &position{row - 1, col - 1})
positions = append(positions, &position{row - 1, col + 1})
positions = append(positions, &position{row + 1, col + 1})
positions = append(positions, &position{row + 1, col - 1})
}
sum := float64(0)
count := 0
globalSum := 0.0
for _, p := range positions {
if p.row >= 0 && p.col >= 0 && p.row < props.rows && p.col < props.cols {
pctDiff := diff.Grid.grid[p.row][p.col]
globalSum += pctDiff
if p.row == props.rows-1 {
pctDiff *= props.edgeMultRow
}
if p.col == props.cols-1 {
pctDiff *= props.edgeMultCol
}
sum += pctDiff
count += 1
}
}
// FIXME: Remove hard-coded hack
return sum / float64(count), globalSum / (8.0 * 4.5), nil
}
type SFFrameDiffsJsonParserProps struct{}
func (p *SFFrameDiffsJsonParserProps) New() interface{} {
return &SFFrameDiffLog{}
}
func NewSFFrameDiffsJsonParser() phonelab.Parser {
return phonelab.NewJSONParser(&SFFrameDiffsJsonParserProps{})
}
////////////////////////////////////////////////////////////////////////////////
// SurfaceFlingerParser parses logs with the SurfaceFlinger tag.
// Currently, it handles FPS and frame diff logs.
type SurfaceFlingerParser struct {
fpsJsonParser phonelab.Parser
diffJsonParser phonelab.Parser
timesJsonParser phonelab.Parser
}
// Create a new SurfaceFlingerParser
func NewSurfaceFlingerParser() phonelab.Parser {
return &SurfaceFlingerParser{
fpsJsonParser: NewSFFpsParer(),
diffJsonParser: NewSFFrameDiffsJsonParser(),
timesJsonParser: NewSFFrameTimesParser(),
}
}
// Parse payloads of logs with the SurfaceFlinger tag
func (parser *SurfaceFlingerParser) Parse(payload string) (interface{}, error) {
if strings.Contains(payload, `{"fps":`) {
// FPS log
return parser.fpsJsonParser.Parse(payload)
} else if strings.Contains(payload, `[{"ts":`) {
// New, valid JSON log
return parser.diffJsonParser.Parse(payload)
} else if strings.Contains(payload, `"diffs":[[`) {
// Old JSON with heterogeneous arrays
return parseAndConvertOldDiffLog(payload)
} else if strings.Contains(payload, `"ft_token":`) {
return parser.timesJsonParser.Parse(payload)
} else {
// We can't parse it
return nil, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// Hacky, broken JSON frame diff log parsing
type oldSFDiffLog struct {
Token int64 `json:"token"`
Diffs []*oldSFFrameDiff `json:"diffs"`
}
type oldSFFrameDiff struct {
Timestamp int64
PctDiff float64
}
func (l *oldSFFrameDiff) UnmarshalJSON(buf []byte) error {
// We used to parse this manually, but there is a nice hack to get this into
// a []interface{}: http://eagain.net/articles/go-json-array-to-struct/
tmp := []interface{}{&l.Timestamp, &l.PctDiff}
wantLen := len(tmp)
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
if actual := len(tmp); actual != wantLen {
return fmt.Errorf("wrong number of fields in Notification: %d != %d", actual, wantLen)
}
return nil
}
func parseAndConvertOldDiffLog(payload string) (interface{}, error) {
var log *oldSFDiffLog
if err := json.Unmarshal([]byte(payload), &log); err != nil {
return nil, err
}
// Now, convert to the new format
newLog := &SFFrameDiffLog{
Token: log.Token,
Diffs: make([]*SFFrameDiff, 0, len(log.Diffs)),
}
for _, diff := range log.Diffs {
newLog.Diffs = append(newLog.Diffs,
&SFFrameDiff{
Timestamp: diff.Timestamp,
PctDiff: diff.PctDiff,
Mode: 0,
HasColor: 0,
})
}
return newLog, nil
}