aboutsummaryrefslogtreecommitdiff
path: root/ui/src/frontend/chrome_slice_panel.ts
blob: 6c982cfe93d180cda981ecbce0707a54b34ff051 (plain)
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
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use size 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 m from 'mithril';

import {sqliteString} from '../base/string_utils';
import {Actions} from '../common/actions';
import {Arg, ArgsTree, isArgTreeArray, isArgTreeMap} from '../common/arg_types';
import {timeToCode} from '../common/time';

import {FlowPoint, globals, SliceDetails} from './globals';
import {PanelSize} from './panel';
import {PopupMenuButton, PopupMenuItem} from './popup_menu';
import {runQueryInNewTab} from './query_result_tab';
import {verticalScrollToTrack} from './scroll_helper';
import {SlicePanel} from './slice_panel';

interface ContextMenuItem {
  name: string;
  shouldDisplay(slice: SliceDetails): boolean;
  getAction(slice: SliceDetails): void;
}

const ITEMS: ContextMenuItem[] = [
  {
    name: 'Average duration',
    shouldDisplay: (slice: SliceDetails) => slice.name !== undefined,
    getAction: (slice: SliceDetails) => runQueryInNewTab(
        `SELECT AVG(dur) / 1e9 FROM slice WHERE name = '${slice.name!}'`,
        `${slice.name} average dur`,
        ),
  },
  {
    name: 'Binder by TXN',
    shouldDisplay: () => true,
    getAction: () => runQueryInNewTab(
        `SELECT IMPORT('android.binder');

         SELECT *
         FROM android_sync_binder_metrics_by_txn
         ORDER BY client_dur DESC`,
        'Binder by TXN',
        ),
  },
  {
    name: 'Lock graph',
    shouldDisplay: (slice: SliceDetails) => slice.id !== undefined,
    getAction: (slice: SliceDetails) => runQueryInNewTab(
        `SELECT IMPORT('android.monitor_contention');
         DROP TABLE IF EXISTS FAST;
         CREATE TABLE FAST
         AS
         WITH slice_process AS (
         SELECT process.name, process.upid FROM slice
         JOIN thread_track ON thread_track.id = slice.track_id
         JOIN thread USING(utid)
         JOIN process USING(upid)
         WHERE slice.id = ${slice.id!}
         )
         SELECT *,
         IIF(blocked_thread_name LIKE 'binder:%', 'binder', blocked_thread_name)
          AS blocked_thread_name_norm,
         IIF(blocking_thread_name LIKE 'binder:%', 'binder', blocking_thread_name)
          AS blocking_thread_name_norm
         FROM android_monitor_contention_chain, slice_process
         WHERE android_monitor_contention_chain.upid = slice_process.upid;

         WITH
         R AS (
         SELECT
           id,
           dur,
           CAT_STACKS(blocked_thread_name_norm || ':' || short_blocked_method,
             blocking_thread_name_norm || ':' || short_blocking_method) AS stack
         FROM FAST
         WHERE parent_id IS NULL
         UNION ALL
         SELECT
         c.id,
         c.dur AS dur,
         CAT_STACKS(stack, blocking_thread_name_norm || ':' || short_blocking_method) AS stack
         FROM FAST c, R AS p
         WHERE p.id = c.parent_id
         )
         SELECT TITLE.process_name, EXPERIMENTAL_PROFILE(stack, 'duration', 'ns', dur) AS pprof
         FROM R, (SELECT process_name FROM FAST LIMIT 1) TITLE;`,
        'Lock graph',
        ),
  },
];

function getSliceContextMenuItems(slice: SliceDetails): PopupMenuItem[] {
  return ITEMS.filter((item) => item.shouldDisplay(slice)).map((item) => {
    return {
      itemType: 'regular',
      text: item.name,
      callback: () => item.getAction(slice),
    };
  });
}

// Table row contents is one of two things:
// 1. Key-value pair
interface TableRow {
  kind: 'TableRow';
  key: string;
  value: Arg;

  // Whether it's an argument (from the `args` table) or whether it's a property
  // of the slice (i.e. `dur`, coming from `slice` table). Args have additional
  // actions associated with them.
  isArg: boolean;

  // A full key for the arguments displayed in a tree.
  full_key?: string;
}

// 2. Common prefix for values in an array
interface TableHeader {
  kind: 'TableHeader';
  header: string;
}

type RowContents = TableRow|TableHeader;

function isTableHeader(contents: RowContents): contents is TableHeader {
  return contents.kind === 'TableHeader';
}

function appendPrefix(p1: string, p2: string): string {
  if (p1.length === 0) {
    return p2;
  }
  return `${p1}.${p2}`;
}

interface Row {
  // How many columns (empty or with an index) precede a key
  indentLevel: number;
  // Optional tooltip to be displayed on the key. Used to display the full key,
  // which has to be reconstructed from the information that might not even be
  // visible on the screen otherwise.
  tooltip?: string;
  contents: RowContents;
}

class TableBuilder {
  // Row data generated by builder
  rows: Row[] = [];
  indentLevel = 0;

  // Maximum indent level of a key, used to determine total number of columns
  maxIndent = 0;

  // Add a key-value pair into the table
  add(key: string, value: Arg) {
    this.rows.push({
      indentLevel: 0,
      contents: {kind: 'TableRow', key, value, isArg: false},
    });
  }

  // Add arguments tree into the table
  addTree(tree: ArgsTree) {
    this.addTreeInternal(tree, '', '');
  }

  private addTreeInternal(
      record: ArgsTree, prefix: string, completePrefix: string) {
    if (isArgTreeArray(record)) {
      if (record.length === 1) {
        this.addTreeInternal(record[0], `${prefix}[0]`, `${completePrefix}[0]`);
        return;
      }

      // Add the current prefix as a separate row
      if (prefix.length > 0) {
        this.rows.push({
          indentLevel: this.indentLevel,
          contents: {kind: 'TableHeader', header: prefix},
          tooltip: completePrefix,
        });
      }

      this.indentLevel++;
      for (let i = 0; i < record.length; i++) {
        // Prefix is empty for array elements because we don't want to repeat
        // the common prefix
        this.addTreeInternal(record[i], `[${i}]`, `${completePrefix}[${i}]`);
      }
      this.indentLevel--;
    } else if (isArgTreeMap(record)) {
      const entries = Object.entries(record);
      if (entries.length === 1) {
        // Don't want to create a level of indirection in case object contains
        // only one value; think of it like file browser in IDEs not showing
        // intermediate nodes for common hierarchy corresponding to Java package
        // prefix (e.g. "com/google/perfetto").
        //
        // In this case, add key as a prefix part.
        const [key, value] = entries[0];
        this.addTreeInternal(
            value,
            appendPrefix(prefix, key),
            appendPrefix(completePrefix, key));
      } else {
        if (prefix.length > 0) {
          const row = this.indentLevel;
          this.rows.push({
            indentLevel: row,
            contents: {kind: 'TableHeader', header: prefix},
            tooltip: completePrefix,
          });
          this.indentLevel++;
        }
        for (const [key, value] of entries) {
          this.addTreeInternal(value, key, appendPrefix(completePrefix, key));
        }
        if (prefix.length > 0) {
          this.indentLevel--;
        }
      }
    } else {
      // Leaf value in the tree: add to the table
      const row = this.indentLevel;
      this.rows.push({
        indentLevel: row,
        contents: {
          kind: 'TableRow',
          key: prefix,
          value: record,
          full_key: completePrefix,
          isArg: true,
        },
        tooltip: completePrefix,
      });
    }
  }
}

export class ChromeSliceDetailsPanel extends SlicePanel {
  view() {
    const sliceInfo = globals.sliceDetails;
    if (sliceInfo.ts !== undefined && sliceInfo.dur !== undefined &&
        sliceInfo.name !== undefined) {
      const defaultBuilder = new TableBuilder();
      defaultBuilder.add('Name', sliceInfo.name);
      defaultBuilder.add(
          'Category',
          !sliceInfo.category || sliceInfo.category === '[NULL]' ?
              'N/A' :
              sliceInfo.category);
      defaultBuilder.add('Start time', timeToCode(sliceInfo.ts));
      if (sliceInfo.absTime !== undefined) {
        defaultBuilder.add('Absolute Time', sliceInfo.absTime);
      }
      defaultBuilder.add(
          'Duration', this.computeDuration(sliceInfo.ts, sliceInfo.dur));
      if (sliceInfo.threadTs !== undefined &&
          sliceInfo.threadDur !== undefined) {
        // If we have valid thread duration, also display a percentage of
        // |threadDur| compared to |dur|.
        const threadDurFractionSuffix = sliceInfo.threadDur === -1 ?
            '' :
            ` (${(sliceInfo.threadDur / sliceInfo.dur * 100).toFixed(2)}%)`;
        defaultBuilder.add(
            'Thread duration',
            this.computeDuration(sliceInfo.threadTs, sliceInfo.threadDur) +
                threadDurFractionSuffix);
      }

      for (const [key, value] of this.getProcessThreadDetails(sliceInfo)) {
        if (value !== undefined) {
          defaultBuilder.add(key, value);
        }
      }

      defaultBuilder.add(
          'Slice ID',
          (sliceInfo.id !== undefined) ? sliceInfo.id.toString() : 'Unknown');
      if (sliceInfo.description) {
        for (const [key, value] of sliceInfo.description) {
          defaultBuilder.add(key, value);
        }
      }
      return m(
          '.details-panel',
          m('.details-panel-heading', m('h2', `Slice Details`)),
          m('.details-table-multicolumn', [
            this.renderTable(defaultBuilder, '.half-width-panel'),
            this.renderRhs(sliceInfo),
          ]));
    } else {
      return m(
          '.details-panel',
          m('.details-panel-heading',
            m(
                'h2',
                `Slice Details`,
                )));
    }
  }

  private fillFlowPanel(
      name: string, flows: {flow: FlowPoint, dur: number}[],
      includeProcessName: boolean, result: Map<string, TableBuilder>) {
    if (flows.length === 0) return;

    const builder = new TableBuilder();
    for (const {flow, dur} of flows) {
      builder.add('Slice', {
        kind: 'SLICE',
        sliceId: flow.sliceId,
        trackId: globals.state.uiTrackIdByTraceTrackId[flow.trackId],
        description: flow.sliceChromeCustomName === undefined ?
            flow.sliceName :
            flow.sliceChromeCustomName,
      });
      builder.add('Delay', timeToCode(dur));
      builder.add(
          'Thread',
          includeProcessName ? `${flow.threadName} (${flow.processName})` :
                               flow.threadName);
    }
    result.set(name, builder);
  }

  renderCanvas(_ctx: CanvasRenderingContext2D, _size: PanelSize) {}

  fillArgs(slice: SliceDetails, builder: TableBuilder) {
    if (slice.argsTree && slice.args) {
      // Parsed arguments are available, need only to iterate over them to get
      // slice references
      for (const [key, value] of slice.args) {
        if (typeof value !== 'string') {
          builder.add(key, value);
        }
      }
      builder.addTree(slice.argsTree);
    } else if (slice.args) {
      // Parsing has failed, but arguments are available: display them in a flat
      // 2-column table
      for (const [key, value] of slice.args) {
        builder.add(key, value);
      }
    }
  }

  private getArgumentContextMenuItems(argument: TableRow): PopupMenuItem[] {
    if (argument.full_key === undefined) return [];
    if (typeof argument.value !== 'string') return [];
    const argValue: string = argument.value;

    const fullKey = argument.full_key;
    return [
      {
        itemType: 'regular',
        text: 'Copy full key',
        callback: () => {
          navigator.clipboard.writeText(fullKey);
        },
      },
      {
        itemType: 'regular',
        text: 'Find slices with the same arg value',
        callback: () => {
          runQueryInNewTab(
              `
              select slice.*
              from slice
              join args using (arg_set_id)
              where key=${sqliteString(fullKey)} and display_value=${
                  sqliteString(argValue)}
          `,
              `Arg: ${sqliteString(fullKey)}=${sqliteString(argValue)}`);
        },
      },
      {
        itemType: 'regular',
        text: 'Visualise argument values',
        callback: () => {
          globals.dispatch(Actions.addVisualisedArg({argName: fullKey}));
        },
      },
    ];
  }

  renderRhs(sliceInfo: SliceDetails): m.Vnode {
    const builders = new Map<string, TableBuilder>();

    const immediatelyPrecedingByFlowSlices = [];
    const immediatelyFollowingByFlowSlices = [];
    for (const flow of globals.connectedFlows) {
      if (flow.begin.sliceId === sliceInfo.id) {
        immediatelyFollowingByFlowSlices.push({flow: flow.end, dur: flow.dur});
      }
      if (flow.end.sliceId === sliceInfo.id) {
        immediatelyPrecedingByFlowSlices.push(
            {flow: flow.begin, dur: flow.dur});
      }
    }

    // This is Chrome-specific bits:
    const isRunTask = sliceInfo.name === 'ThreadControllerImpl::RunTask' ||
        sliceInfo.name === 'ThreadPool_RunTask';
    const isPostTask = sliceInfo.name === 'ThreadPool_PostTask' ||
        sliceInfo.name === 'SequenceManager PostTask';

    // RunTask and PostTask are always same-process, so we can skip
    // emitting process name for them.
    this.fillFlowPanel(
        'Preceding flows',
        immediatelyPrecedingByFlowSlices,
        !isRunTask,
        builders);
    this.fillFlowPanel(
        'Following flows',
        immediatelyFollowingByFlowSlices,
        !isPostTask,
        builders);

    const argsBuilder = new TableBuilder();
    this.fillArgs(sliceInfo, argsBuilder);
    builders.set('Arguments', argsBuilder);

    const rows: m.Vnode<any, any>[] = [];
    for (const [name, builder] of builders) {
      rows.push(m('h3', name));
      rows.push(this.renderTable(builder));
    }

    const contextMenuItems = getSliceContextMenuItems(sliceInfo);
    if (contextMenuItems.length > 0) {
      rows.push(
          m(PopupMenuButton,
            {
              icon: 'arrow_drop_down',
              items: contextMenuItems,
            },
            'Contextual Options'));
    }

    return m('.half-width-panel', rows);
  }

  renderTable(builder: TableBuilder, additionalClasses: string = ''): m.Vnode {
    const rows: m.Vnode[] = [];
    for (const row of builder.rows) {
      const renderedRow: m.Vnode[] = [];
      const paddingLeft = `${row.indentLevel * 20}px`;
      if (isTableHeader(row.contents)) {
        renderedRow.push(
            m('th',
              {
                colspan: 2,
                title: row.tooltip,
                style: {'padding-left': paddingLeft},
              },
              row.contents.header));
      } else {
        const contents: any[] = [row.contents.key];
        if (row.contents.isArg) {
          contents.push(
              m('span.context-wrapper', m.trust('&nbsp;'), m(PopupMenuButton, {
                  icon: 'arrow_drop_down',
                  items: this.getArgumentContextMenuItems(row.contents),
                })));
        }

        renderedRow.push(
            m('th',
              {title: row.tooltip, style: {'padding-left': paddingLeft}},
              contents));
        const value = row.contents.value;
        if (typeof value === 'string') {
          renderedRow.push(m('td.value', this.mayLinkify(value)));
        } else {
          // Type of value being a record is not propagated into the callback
          // for some reason, extracting necessary parts as constants instead.
          const sliceId = value.sliceId;
          const trackId = value.trackId;
          renderedRow.push(
              m('td',
                m('i.material-icons.grey',
                  {
                    onclick: () => {
                      globals.makeSelection(Actions.selectChromeSlice(
                          {id: sliceId, trackId, table: 'slice'}));
                      // Ideally we want to have a callback to
                      // findCurrentSelection after this selection has been
                      // made. Here we do not have the info for horizontally
                      // scrolling to ts.
                      verticalScrollToTrack(trackId, true);
                    },
                    title: 'Go to destination slice',
                  },
                  'call_made'),
                value.description));
        }
      }

      rows.push(m('tr', renderedRow));
    }

    return m(`table.auto-layout${additionalClasses}`, rows);
  }

  private mayLinkify(what: string): string|m.Vnode {
    if (what.startsWith('http://') || what.startsWith('https://')) {
      return m('a', {href: what, target: '_blank'}, what);
    }
    return what;
  }
}