xref: /aosp_15_r20/external/perfetto/ui/src/components/details/thread_slice_details_tab.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use size file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import m from 'mithril';
16import {Icons} from '../../base/semantic_icons';
17import {TimeSpan} from '../../base/time';
18import {exists} from '../../base/utils';
19import {Engine} from '../../trace_processor/engine';
20import {Button} from '../../widgets/button';
21import {DetailsShell} from '../../widgets/details_shell';
22import {GridLayout, GridLayoutColumn} from '../../widgets/grid_layout';
23import {MenuItem, PopupMenu2} from '../../widgets/menu';
24import {Section} from '../../widgets/section';
25import {Tree} from '../../widgets/tree';
26import {Flow, FlowPoint} from '../../core/flow_types';
27import {hasArgs, renderArguments} from './slice_args';
28import {renderDetails} from './slice_details';
29import {getSlice, SliceDetails} from '../sql_utils/slice';
30import {
31  BreakdownByThreadState,
32  breakDownIntervalByThreadState,
33} from './thread_state';
34import {asSliceSqlId} from '../sql_utils/core_types';
35import {DurationWidget} from '../widgets/duration';
36import {SliceRef} from '../widgets/slice';
37import {BasicTable} from '../../widgets/basic_table';
38import {getSqlTableDescription} from '../widgets/sql/table/sql_table_registry';
39import {assertExists} from '../../base/logging';
40import {Trace} from '../../public/trace';
41import {TrackEventDetailsPanel} from '../../public/details_panel';
42import {TrackEventSelection} from '../../public/selection';
43import {extensions} from '../extensions';
44import {TraceImpl} from '../../core/trace_impl';
45
46interface ContextMenuItem {
47  name: string;
48  shouldDisplay(slice: SliceDetails): boolean;
49  run(slice: SliceDetails, trace: Trace): void;
50}
51
52function getTidFromSlice(slice: SliceDetails): number | undefined {
53  return slice.thread?.tid;
54}
55
56function getPidFromSlice(slice: SliceDetails): number | undefined {
57  return slice.process?.pid;
58}
59
60function getProcessNameFromSlice(slice: SliceDetails): string | undefined {
61  return slice.process?.name;
62}
63
64function getThreadNameFromSlice(slice: SliceDetails): string | undefined {
65  return slice.thread?.name;
66}
67
68function hasName(slice: SliceDetails): boolean {
69  return slice.name !== undefined;
70}
71
72function hasTid(slice: SliceDetails): boolean {
73  return getTidFromSlice(slice) !== undefined;
74}
75
76function hasPid(slice: SliceDetails): boolean {
77  return getPidFromSlice(slice) !== undefined;
78}
79
80function hasProcessName(slice: SliceDetails): boolean {
81  return getProcessNameFromSlice(slice) !== undefined;
82}
83
84function hasThreadName(slice: SliceDetails): boolean {
85  return getThreadNameFromSlice(slice) !== undefined;
86}
87
88const ITEMS: ContextMenuItem[] = [
89  {
90    name: 'Ancestor slices',
91    shouldDisplay: (slice: SliceDetails) => slice.parentId !== undefined,
92    run: (slice: SliceDetails, trace: Trace) =>
93      extensions.addSqlTableTab(trace, {
94        table: assertExists(getSqlTableDescription('slice')),
95        filters: [
96          {
97            op: (cols) =>
98              `${cols[0]} IN (SELECT id FROM _slice_ancestor_and_self(${slice.id}))`,
99            columns: ['id'],
100          },
101        ],
102        imports: ['slices.hierarchy'],
103      }),
104  },
105  {
106    name: 'Descendant slices',
107    shouldDisplay: () => true,
108    run: (slice: SliceDetails, trace: Trace) =>
109      extensions.addSqlTableTab(trace, {
110        table: assertExists(getSqlTableDescription('slice')),
111        filters: [
112          {
113            op: (cols) =>
114              `${cols[0]} IN (SELECT id FROM _slice_descendant_and_self(${slice.id}))`,
115            columns: ['id'],
116          },
117        ],
118        imports: ['slices.hierarchy'],
119      }),
120  },
121  {
122    name: 'Average duration of slice name',
123    shouldDisplay: (slice: SliceDetails) => hasName(slice),
124    run: (slice: SliceDetails, trace: Trace) =>
125      extensions.addQueryResultsTab(trace, {
126        query: `SELECT AVG(dur) / 1e9 FROM slice WHERE name = '${slice.name!}'`,
127        title: `${slice.name} average dur`,
128      }),
129  },
130  {
131    name: 'Binder txn names + monitor contention on thread',
132    shouldDisplay: (slice) =>
133      hasProcessName(slice) &&
134      hasThreadName(slice) &&
135      hasTid(slice) &&
136      hasPid(slice),
137    run: (slice: SliceDetails, trace: Trace) => {
138      trace.engine
139        .query(
140          `INCLUDE PERFETTO MODULE android.binder;
141           INCLUDE PERFETTO MODULE android.monitor_contention;`,
142        )
143        .then(() =>
144          extensions.addDebugSliceTrack({
145            trace,
146            data: {
147              sqlSource: `
148                                WITH merged AS (
149                                  SELECT s.ts, s.dur, tx.aidl_name AS name, 0 AS depth
150                                  FROM android_binder_txns tx
151                                  JOIN slice s
152                                    ON tx.binder_txn_id = s.id
153                                  JOIN thread_track
154                                    ON s.track_id = thread_track.id
155                                  JOIN thread
156                                    USING (utid)
157                                  JOIN process
158                                    USING (upid)
159                                  WHERE pid = ${getPidFromSlice(slice)}
160                                        AND tid = ${getTidFromSlice(slice)}
161                                        AND aidl_name IS NOT NULL
162                                  UNION ALL
163                                  SELECT
164                                    s.ts,
165                                    s.dur,
166                                    short_blocked_method || ' -> ' || blocking_thread_name || ':' || short_blocking_method AS name,
167                                    1 AS depth
168                                  FROM android_binder_txns tx
169                                  JOIN android_monitor_contention m
170                                    ON m.binder_reply_tid = tx.server_tid AND m.binder_reply_ts = tx.server_ts
171                                  JOIN slice s
172                                    ON tx.binder_txn_id = s.id
173                                  JOIN thread_track
174                                    ON s.track_id = thread_track.id
175                                  JOIN thread ON thread.utid = thread_track.utid
176                                  JOIN process ON process.upid = thread.upid
177                                  WHERE process.pid = ${getPidFromSlice(slice)}
178                                        AND thread.tid = ${getTidFromSlice(
179                                          slice,
180                                        )}
181                                        AND short_blocked_method IS NOT NULL
182                                  ORDER BY depth
183                                ) SELECT ts, dur, name FROM merged`,
184            },
185            title: `Binder names (${getProcessNameFromSlice(
186              slice,
187            )}:${getThreadNameFromSlice(slice)})`,
188          }),
189        );
190    },
191  },
192];
193
194function getSliceContextMenuItems(slice: SliceDetails) {
195  return ITEMS.filter((item) => item.shouldDisplay(slice));
196}
197
198async function getSliceDetails(
199  engine: Engine,
200  id: number,
201): Promise<SliceDetails | undefined> {
202  return getSlice(engine, asSliceSqlId(id));
203}
204
205export class ThreadSliceDetailsPanel implements TrackEventDetailsPanel {
206  private sliceDetails?: SliceDetails;
207  private breakdownByThreadState?: BreakdownByThreadState;
208
209  constructor(private readonly trace: TraceImpl) {}
210
211  async load({eventId}: TrackEventSelection) {
212    const {trace} = this;
213    const details = await getSliceDetails(trace.engine, eventId);
214
215    if (
216      details !== undefined &&
217      details.thread !== undefined &&
218      details.dur > 0
219    ) {
220      this.breakdownByThreadState = await breakDownIntervalByThreadState(
221        trace.engine,
222        TimeSpan.fromTimeAndDuration(details.ts, details.dur),
223        details.thread.utid,
224      );
225    }
226
227    this.sliceDetails = details;
228  }
229
230  render() {
231    if (!exists(this.sliceDetails)) {
232      return m(DetailsShell, {title: 'Slice', description: 'Loading...'});
233    }
234    const slice = this.sliceDetails;
235    return m(
236      DetailsShell,
237      {
238        title: 'Slice',
239        description: slice.name,
240        buttons: this.renderContextButton(slice),
241      },
242      m(
243        GridLayout,
244        renderDetails(this.trace, slice, this.breakdownByThreadState),
245        this.renderRhs(this.trace, slice),
246      ),
247    );
248  }
249
250  private renderRhs(trace: Trace, slice: SliceDetails): m.Children {
251    const precFlows = this.renderPrecedingFlows(slice);
252    const followingFlows = this.renderFollowingFlows(slice);
253    const args =
254      hasArgs(slice.args) &&
255      m(
256        Section,
257        {title: 'Arguments'},
258        m(Tree, renderArguments(trace, slice.args)),
259      );
260    // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
261    if (precFlows ?? followingFlows ?? args) {
262      return m(GridLayoutColumn, precFlows, followingFlows, args);
263    } else {
264      return undefined;
265    }
266  }
267
268  private renderPrecedingFlows(slice: SliceDetails): m.Children {
269    const flows = this.trace.flows.connectedFlows;
270    const inFlows = flows.filter(({end}) => end.sliceId === slice.id);
271
272    if (inFlows.length > 0) {
273      const isRunTask =
274        slice.name === 'ThreadControllerImpl::RunTask' ||
275        slice.name === 'ThreadPool_RunTask';
276
277      return m(
278        Section,
279        {title: 'Preceding Flows'},
280        m(BasicTable<Flow>, {
281          columns: [
282            {
283              title: 'Slice',
284              render: (flow: Flow) =>
285                m(SliceRef, {
286                  id: asSliceSqlId(flow.begin.sliceId),
287                  name:
288                    flow.begin.sliceChromeCustomName ?? flow.begin.sliceName,
289                }),
290            },
291            {
292              title: 'Delay',
293              render: (flow: Flow) =>
294                m(DurationWidget, {
295                  dur: flow.end.sliceStartTs - flow.begin.sliceEndTs,
296                }),
297            },
298            {
299              title: 'Thread',
300              render: (flow: Flow) =>
301                this.getThreadNameForFlow(flow.begin, !isRunTask),
302            },
303          ],
304          data: inFlows,
305        }),
306      );
307    } else {
308      return null;
309    }
310  }
311
312  private renderFollowingFlows(slice: SliceDetails): m.Children {
313    const flows = this.trace.flows.connectedFlows;
314    const outFlows = flows.filter(({begin}) => begin.sliceId === slice.id);
315
316    if (outFlows.length > 0) {
317      const isPostTask =
318        slice.name === 'ThreadPool_PostTask' ||
319        slice.name === 'SequenceManager PostTask';
320
321      return m(
322        Section,
323        {title: 'Following Flows'},
324        m(BasicTable<Flow>, {
325          columns: [
326            {
327              title: 'Slice',
328              render: (flow: Flow) =>
329                m(SliceRef, {
330                  id: asSliceSqlId(flow.end.sliceId),
331                  name: flow.end.sliceChromeCustomName ?? flow.end.sliceName,
332                }),
333            },
334            {
335              title: 'Delay',
336              render: (flow: Flow) =>
337                m(DurationWidget, {
338                  dur: flow.end.sliceStartTs - flow.begin.sliceEndTs,
339                }),
340            },
341            {
342              title: 'Thread',
343              render: (flow: Flow) =>
344                this.getThreadNameForFlow(flow.end, !isPostTask),
345            },
346          ],
347          data: outFlows,
348        }),
349      );
350    } else {
351      return null;
352    }
353  }
354
355  private getThreadNameForFlow(
356    flow: FlowPoint,
357    includeProcessName: boolean,
358  ): string {
359    return includeProcessName
360      ? `${flow.threadName} (${flow.processName})`
361      : flow.threadName;
362  }
363
364  private renderContextButton(sliceInfo: SliceDetails): m.Children {
365    const contextMenuItems = getSliceContextMenuItems(sliceInfo);
366    if (contextMenuItems.length > 0) {
367      const trigger = m(Button, {
368        compact: true,
369        label: 'Contextual Options',
370        rightIcon: Icons.ContextMenu,
371      });
372      return m(
373        PopupMenu2,
374        {trigger},
375        contextMenuItems.map(({name, run}) =>
376          m(MenuItem, {label: name, onclick: () => run(sliceInfo, this.trace)}),
377        ),
378      );
379    } else {
380      return undefined;
381    }
382  }
383}
384