-
Notifications
You must be signed in to change notification settings - Fork 729
Expand file tree
/
Copy pathindex.ts
More file actions
286 lines (263 loc) · 8.47 KB
/
index.ts
File metadata and controls
286 lines (263 loc) · 8.47 KB
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
// Copyright (C) 2024 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {createQueryCounterTrack} from '../../components/tracks/query_counter_track';
import {getTimeSpanOfSelectionOrVisibleWindow} from '../../public/utils';
import {uuidv4} from '../../base/uuid';
import {LONG, LONG_NULL, STR} from '../../trace_processor/query_result';
import {PerfettoPlugin} from '../../public/plugin';
import {Trace} from '../../public/trace';
import {SliceTrack} from '../../components/tracks/slice_track';
import {SourceDataset} from '../../trace_processor/dataset';
import {TrackNode} from '../../public/workspace';
import StandardGroupsPlugin from '../dev.perfetto.StandardGroups';
import {TimeSpan} from '../../base/time';
export default class AndroidInputEvents implements PerfettoPlugin {
static readonly id = 'com.android.InputEvents';
static readonly dependencies = [StandardGroupsPlugin];
async onTraceLoad(ctx: Trace): Promise<void> {
await ctx.engine.query(`
INCLUDE PERFETTO MODULE android.input;
INCLUDE PERFETTO MODULE intervals.overlap;
`);
ctx.commands.registerCommand({
id: 'com.android.InputEvents.visualizeOverlaps',
name: 'Input Events: Visualize event overlaps (over selection)',
callback: () => this.visualizeOverlaps(ctx),
});
const cnt = await ctx.engine.query(`
SELECT
COUNT(*) AS cnt
FROM slice
WHERE name GLOB 'UnwantedInteractionBlocker::notifyMotion*'
`);
if (cnt.firstRow({cnt: LONG}).cnt == 0n) {
return;
}
const uri = 'com.android.InputEvents#InputEventsTrack';
const track = await SliceTrack.createMaterialized({
trace: ctx,
uri,
dataset: new SourceDataset({
src: `
SELECT
read_time AS ts,
end_to_end_latency_dur AS dur,
CONCAT(event_type, ' ', event_action, ': ', process_name, ' (', input_event_id, ')') as name
FROM android_input_events
WHERE end_to_end_latency_dur > 0
`,
schema: {
ts: LONG,
dur: LONG_NULL,
name: STR,
},
}),
});
ctx.tracks.registerTrack({
uri,
renderer: track,
});
const node = new TrackNode({uri, name: 'Input Events'});
const group = ctx.plugins
.getPlugin(StandardGroupsPlugin)
.getOrCreateStandardGroup(ctx.defaultWorkspace, 'USER_INTERACTION');
group.addChildInOrder(node);
}
async visualizeOverlaps(ctx: Trace): Promise<void> {
const window = await getTimeSpanOfSelectionOrVisibleWindow(ctx);
const rootNode = await this.createRootTrack(ctx, window);
ctx.defaultWorkspace.addChildLast(rootNode);
rootNode.pin();
const processes = await this.getProcesses(ctx, window);
const processTrackPromises: Promise<TrackNode>[] = [];
for (
const it = processes.iter({upid: LONG, process_name: STR});
it.valid();
it.next()
) {
const upid = Number(it.upid);
const processName = it.process_name;
processTrackPromises.push(
this.createProcessTrack(ctx, window, upid, processName),
);
}
const processTracks = await Promise.all(processTrackPromises);
for (const processTrack of processTracks) {
rootNode.addChildLast(processTrack);
}
}
private async createRootTrack(
ctx: Trace,
window: TimeSpan,
): Promise<TrackNode> {
const uri = `com.android.InputEvents.event_overlaps_parent.${uuidv4()}`;
const sqlSource = this.getOverlapSqlSource(window);
return this.createTrack(
ctx,
uri,
sqlSource,
'Input Events',
'Number of concurrent input events (from input dispatch to input ACK received).',
);
}
private async getProcesses(ctx: Trace, window: TimeSpan) {
return ctx.engine.query(`
WITH
process_peaks AS (
SELECT
group_name AS upid,
MAX(value) AS peak
FROM intervals_overlap_count_by_group!((${this.getEventsSubquery(window)}), dispatch_ts, total_latency_dur, upid)
GROUP BY upid
HAVING MAX(value) > 0
)
SELECT
pp.upid,
p.name AS process_name
FROM process_peaks pp
JOIN process p USING (upid)
ORDER BY pp.peak DESC
`);
}
private async createProcessTrack(
ctx: Trace,
window: TimeSpan,
upid: number,
processName: string,
): Promise<TrackNode> {
const uri = `com.android.InputEvents.event_overlaps.proc_${upid}.${uuidv4()}`;
const channels = await this.getChannels(ctx, window, upid);
const numberOfChannels = channels.numRows();
const plural = numberOfChannels === 1 ? '' : 's';
const name = `${processName} ${upid} (${numberOfChannels} channel${plural})`;
const sqlSource = this.getOverlapSqlSource(window, [`upid = ${upid}`]);
const processNode = await this.createTrack(
ctx,
uri,
sqlSource,
name,
`Number of concurrent input events received by process ${processName} ${upid} (from input dispatch to input ACK received).`,
);
const channelTrackPromises: Promise<TrackNode>[] = [];
for (
const it = channels.iter({event_channel: STR});
it.valid();
it.next()
) {
const channel = it.event_channel;
channelTrackPromises.push(
this.createChannelTrack(ctx, window, channel, upid),
);
}
const channelTracks = await Promise.all(channelTrackPromises);
for (const channelTrack of channelTracks) {
processNode.addChildLast(channelTrack);
}
return processNode;
}
private async getChannels(ctx: Trace, window: TimeSpan, upid: number) {
return ctx.engine.query(`
SELECT
group_name AS event_channel
FROM intervals_overlap_count_by_group!((${this.getEventsSubquery(window, [`upid = ${upid}`])}), dispatch_ts, total_latency_dur, event_channel)
GROUP BY event_channel
HAVING MAX(value) > 0
ORDER BY MAX(value) DESC
`);
}
private async createChannelTrack(
ctx: Trace,
window: TimeSpan,
channel: string,
upid: number,
): Promise<TrackNode> {
const uri = `com.android.InputEvents.event_overlaps.proc_${upid}.${channel}.${uuidv4()}`;
const sqlSource = this.getOverlapSqlSource(window, [
`upid = ${upid}`,
`event_channel = '${channel}'`,
]);
return this.createTrack(
ctx,
uri,
sqlSource,
`Channel: ${channel}`,
`Number of concurrent input events on the ${channel} channel (from input dispatch to input ACK received).`,
);
}
private async createTrack(
ctx: Trace,
uri: string,
sqlSource: string,
name: string,
description: string,
removable = true,
): Promise<TrackNode> {
const track = await createQueryCounterTrack({
trace: ctx,
uri,
materialize: false,
data: {
sqlSource,
},
columns: {
ts: 'ts',
value: 'value',
},
});
ctx.tracks.registerTrack({
uri,
renderer: track,
description,
});
return new TrackNode({
uri,
name,
removable,
});
}
private getOverlapSqlSource(
window: TimeSpan,
whereClauses: string[] = [],
): string {
const subquery = this.getEventsSubquery(window, whereClauses);
return `
SELECT *
FROM intervals_overlap_count!(
(${subquery}),
dispatch_ts,
total_latency_dur
)
`;
}
private getEventsSubquery(
window: TimeSpan,
whereClauses: string[] = [],
): string {
const whereClause =
whereClauses.length > 0 ? `AND ${whereClauses.join(' AND ')}` : '';
return `
SELECT
upid,
process_name,
event_channel,
MAX(dispatch_ts, ${window.start}) AS dispatch_ts,
MIN(dispatch_ts + total_latency_dur, ${window.end}) - MAX(dispatch_ts, ${window.start}) AS total_latency_dur
FROM android_input_events
WHERE
total_latency_dur IS NOT NULL AND
dispatch_ts < ${window.end} AND dispatch_ts + total_latency_dur > ${window.start}
${whereClause}
`;
}
}