Skip to content

Commit

Permalink
ui: Remove some gratuitous use of private member variables in plugins
Browse files Browse the repository at this point in the history
Change-Id: If81373ab011b2c47d771464d854e06d35df1e59d
  • Loading branch information
stevegolton committed Oct 28, 2024
1 parent 6c9f16b commit 057280e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 59 deletions.
20 changes: 10 additions & 10 deletions ui/src/plugins/com.android.InputEvents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ import {TrackNode} from '../../public/workspace';
import {getOrCreateUserInteractionGroup} from '../../public/standard_groups';

class InputEvents implements PerfettoPlugin {
private readonly SQL_SOURCE = `
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 IS NOT NULL
`;

async onTraceLoad(ctx: Trace): Promise<void> {
const cnt = await ctx.engine.query(`
SELECT
Expand All @@ -43,9 +34,18 @@ class InputEvents implements PerfettoPlugin {
return;
}

const SQL_SOURCE = `
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 IS NOT NULL
`;

const config: SimpleSliceTrackConfig = {
data: {
sqlSource: this.SQL_SOURCE,
sqlSource: SQL_SOURCE,
columns: ['ts', 'dur', 'name'],
},
columns: {ts: 'ts', dur: 'dur', name: 'name'},
Expand Down
18 changes: 7 additions & 11 deletions ui/src/plugins/com.google.android.GoogleCamera/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ import {PerfettoPlugin, PluginDescriptor} from '../../public/plugin';
import * as cameraConstants from './googleCameraConstants';

class GoogleCamera implements PerfettoPlugin {
private ctx!: Trace;

async onTraceLoad(ctx: Trace): Promise<void> {
this.ctx = ctx;

ctx.commands.registerCommand({
id: 'com.google.android.GoogleCamera#LoadGoogleCameraStartupView',
name: 'Load google camera startup view',
callback: () => {
this.loadGCAStartupView();
this.loadGCAStartupView(ctx);
},
});

Expand All @@ -43,18 +39,18 @@ class GoogleCamera implements PerfettoPlugin {
) {
return item.trim();
});
this.pinTracks(trackNameList);
this.pinTracks(ctx, trackNameList);
},
});
}

private loadGCAStartupView() {
this.pinTracks(cameraConstants.MAIN_THREAD_TRACK);
this.pinTracks(cameraConstants.STARTUP_RELATED_TRACKS);
private loadGCAStartupView(ctx: Trace) {
this.pinTracks(ctx, cameraConstants.MAIN_THREAD_TRACK);
this.pinTracks(ctx, cameraConstants.STARTUP_RELATED_TRACKS);
}

private pinTracks(trackNames: ReadonlyArray<string>) {
this.ctx.workspace.flatTracks.forEach((track) => {
private pinTracks(ctx: Trace, trackNames: ReadonlyArray<string>) {
ctx.workspace.flatTracks.forEach((track) => {
trackNames.forEach((trackName) => {
if (track.title.match(trackName)) {
track.pin();
Expand Down
33 changes: 20 additions & 13 deletions ui/src/plugins/dev.perfetto.AsyncSlices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ import {getSliceTable} from './table';
import {extensions} from '../../public/lib/extensions';

class AsyncSlicePlugin implements PerfettoPlugin {
private readonly trackIdsToUris = new Map<number, string>();

async onTraceLoad(ctx: Trace): Promise<void> {
this.trackIdsToUris.clear();
const trackIdsToUris = new Map<number, string>();

await this.addGlobalAsyncTracks(ctx);
await this.addProcessAsyncSliceTracks(ctx);
await this.addThreadAsyncSliceTracks(ctx);
await this.addGlobalAsyncTracks(ctx, trackIdsToUris);
await this.addProcessAsyncSliceTracks(ctx, trackIdsToUris);
await this.addThreadAsyncSliceTracks(ctx, trackIdsToUris);

ctx.selection.registerSqlSelectionResolver({
sqlTableName: 'slice',
Expand All @@ -61,7 +59,7 @@ class AsyncSlicePlugin implements PerfettoPlugin {
trackId: NUM,
});

const trackUri = this.trackIdsToUris.get(trackId);
const trackUri = trackIdsToUris.get(trackId);
if (!trackUri) {
return undefined;
}
Expand Down Expand Up @@ -90,7 +88,10 @@ class AsyncSlicePlugin implements PerfettoPlugin {
});
}

async addGlobalAsyncTracks(ctx: Trace): Promise<void> {
async addGlobalAsyncTracks(
ctx: Trace,
trackIdsToUris: Map<number, string>,
): Promise<void> {
const {engine} = ctx;
// TODO(stevegolton): The track exclusion logic is currently a hack. This will be replaced
// by a mechanism for more specific plugins to override tracks from more generic plugins.
Expand Down Expand Up @@ -204,7 +205,7 @@ class AsyncSlicePlugin implements PerfettoPlugin {
});
trackIds.forEach((id) => {
trackMap.set(id, {parentId: it.parentId, trackNode});
this.trackIdsToUris.set(id, uri);
trackIdsToUris.set(id, uri);
});
}
}
Expand All @@ -220,7 +221,10 @@ class AsyncSlicePlugin implements PerfettoPlugin {
});
}

async addProcessAsyncSliceTracks(ctx: Trace): Promise<void> {
async addProcessAsyncSliceTracks(
ctx: Trace,
trackIdsToUris: Map<number, string>,
): Promise<void> {
const result = await ctx.engine.query(`
select
upid,
Expand Down Expand Up @@ -283,7 +287,7 @@ class AsyncSlicePlugin implements PerfettoPlugin {
const track = new TrackNode({uri, title, sortOrder: 30});
trackIds.forEach((id) => {
trackMap.set(id, {trackNode: track, parentId: it.parentId, upid});
this.trackIdsToUris.set(id, uri);
trackIdsToUris.set(id, uri);
});
}

Expand All @@ -299,7 +303,10 @@ class AsyncSlicePlugin implements PerfettoPlugin {
});
}

async addThreadAsyncSliceTracks(ctx: Trace): Promise<void> {
async addThreadAsyncSliceTracks(
ctx: Trace,
trackIdsToUris: Map<number, string>,
): Promise<void> {
const result = await ctx.engine.query(`
include perfetto module viz.summary.slices;
include perfetto module viz.summary.threads;
Expand Down Expand Up @@ -381,7 +388,7 @@ class AsyncSlicePlugin implements PerfettoPlugin {
const track = new TrackNode({uri, title, sortOrder: 20});
trackIds.forEach((id) => {
trackMap.set(id, {trackNode: track, parentId, utid});
this.trackIdsToUris.set(id, uri);
trackIdsToUris.set(id, uri);
});
}

Expand Down
54 changes: 29 additions & 25 deletions ui/src/plugins/dev.perfetto.ProcessThreadGroups/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,24 @@ function getThreadDisplayName(threadName: string | undefined, tid: number) {
// This plugin is responsible for organizing all process and thread groups
// including the kernel groups, sorting, and adding summary tracks.
class ProcessThreadGroupsPlugin implements PerfettoPlugin {
private readonly processGroups = new Map<number, TrackNode>();
private readonly threadGroups = new Map<number, TrackNode>();

async onTraceLoad(ctx: Trace): Promise<void> {
await this.addProcessAndThreadGroups(ctx);
}

private async addProcessAndThreadGroups(ctx: Trace): Promise<void> {
this.processGroups.clear();
this.threadGroups.clear();
const processGroups = new Map<number, TrackNode>();
const threadGroups = new Map<number, TrackNode>();

// Pre-group all kernel "threads" (actually processes) if this is a linux
// system trace. Below, addProcessTrackGroups will skip them due to an
// existing group uuid, and addThreadStateTracks will fill in the
// per-thread tracks. Quirk: since all threads will appear to be
// TrackKindPriority.MAIN_THREAD, any process-level tracks will end up
// pushed to the bottom of the group in the UI.
await this.addKernelThreadGrouping(ctx);
await this.addKernelThreadGrouping(ctx, threadGroups);

// Create the per-process track groups. Note that this won't necessarily
// create a track per process. If a process has been completely idle and has
// no sched events, no track group will be emitted.
// Will populate this.addTrackGroupActions
await this.addProcessGroups(ctx);
await this.addThreadGroups(ctx);
await this.addProcessGroups(ctx, processGroups, threadGroups);
await this.addThreadGroups(ctx, processGroups, threadGroups);

ctx.addEventListener('traceready', () => {
// If, by the time the trace has finished loading, some of the process or
Expand All @@ -74,12 +67,15 @@ class ProcessThreadGroupsPlugin implements PerfettoPlugin {
g.remove();
}
};
this.processGroups.forEach(removeIfEmpty);
this.threadGroups.forEach(removeIfEmpty);
processGroups.forEach(removeIfEmpty);
threadGroups.forEach(removeIfEmpty);
});
}

private async addKernelThreadGrouping(ctx: Trace): Promise<void> {
private async addKernelThreadGrouping(
ctx: Trace,
threadGroups: Map<number, TrackNode>,
): Promise<void> {
// Identify kernel threads if this is a linux system trace, and sufficient
// process information is available. Kernel threads are identified by being
// children of kthreadd (always pid 2).
Expand Down Expand Up @@ -136,13 +132,17 @@ class ProcessThreadGroupsPlugin implements PerfettoPlugin {
threadGroup.headless = true;
kernelThreadsGroup.addChildInOrder(threadGroup);

this.threadGroups.set(utid, threadGroup);
threadGroups.set(utid, threadGroup);
}
}

// Adds top level groups for processes and thread that don't belong to a
// process.
private async addProcessGroups(ctx: Trace): Promise<void> {
private async addProcessGroups(
ctx: Trace,
processGroups: Map<number, TrackNode>,
threadGroups: Map<number, TrackNode>,
): Promise<void> {
const result = await ctx.engine.query(`
with processGroups as (
select
Expand Down Expand Up @@ -230,7 +230,7 @@ class ProcessThreadGroupsPlugin implements PerfettoPlugin {

if (kind === 'process') {
// Ignore kernel process groups
if (this.processGroups.has(uid)) {
if (processGroups.has(uid)) {
continue;
}

Expand All @@ -253,10 +253,10 @@ class ProcessThreadGroupsPlugin implements PerfettoPlugin {

// Re-insert the child node to sort it
ctx.workspace.addChildInOrder(group);
this.processGroups.set(uid, group);
processGroups.set(uid, group);
} else {
// Ignore kernel process groups
if (this.threadGroups.has(uid)) {
if (threadGroups.has(uid)) {
continue;
}

Expand All @@ -268,14 +268,18 @@ class ProcessThreadGroupsPlugin implements PerfettoPlugin {

// Re-insert the child node to sort it
ctx.workspace.addChildInOrder(group);
this.threadGroups.set(uid, group);
threadGroups.set(uid, group);
}
}
}

// Create all the nested & headless thread groups that live inside existing
// process groups.
private async addThreadGroups(ctx: Trace): Promise<void> {
private async addThreadGroups(
ctx: Trace,
processGroups: Map<number, TrackNode>,
threadGroups: Map<number, TrackNode>,
): Promise<void> {
const result = await ctx.engine.query(`
with threadGroups as (
select
Expand Down Expand Up @@ -324,15 +328,15 @@ class ProcessThreadGroupsPlugin implements PerfettoPlugin {
const {utid, tid, upid, threadName} = it;

// Ignore kernel thread groups
if (this.threadGroups.has(utid)) {
if (threadGroups.has(utid)) {
continue;
}

const group = getOrCreateGroupForThread(ctx.workspace, utid);
group.title = getThreadDisplayName(threadName ?? undefined, tid);
this.threadGroups.set(utid, group);
threadGroups.set(utid, group);
group.headless = true;
this.processGroups.get(upid)?.addChildInOrder(group);
processGroups.get(upid)?.addChildInOrder(group);
}
}
}
Expand Down

0 comments on commit 057280e

Please sign in to comment.