-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathFlameGraph.tsx
More file actions
489 lines (447 loc) · 15.8 KB
/
Copy pathFlameGraph.tsx
File metadata and controls
489 lines (447 loc) · 15.8 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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import * as React from 'react';
import { explicitConnectWithForwardRef } from '../../utils/connect';
import { FlameGraphCanvas } from './Canvas';
import {
getCategories,
getCommittedRange,
getPreviewSelection,
getScrollToSelectionGeneration,
getProfileInterval,
getInnerWindowIDToPageMap,
getProfileUsesMultipleStackTypes,
} from 'firefox-profiler/selectors/profile';
import { selectedThreadSelectors } from 'firefox-profiler/selectors/per-thread';
import {
getSelectedThreadsKey,
getInvertCallstack,
} from '../../selectors/url-state';
import { ContextMenuTrigger } from 'firefox-profiler/components/shared/ContextMenuTrigger';
import {
changeSelectedCallNode,
changeZoomedInCallNode,
changeRightClickedCallNode,
handleCallNodeTransformShortcut,
updateBottomBoxContentsAndMaybeOpen,
} from 'firefox-profiler/actions/profile-view';
import { extractNonInvertedCallTreeTimings } from 'firefox-profiler/profile-logic/call-tree';
import { ensureExists } from 'firefox-profiler/utils/types';
import type {
Thread,
CategoryList,
Milliseconds,
StartEndRange,
WeightType,
SamplesLikeTable,
PreviewSelection,
CallTreeSummaryStrategy,
IndexIntoCallNodeTable,
ThreadsKey,
InnerWindowID,
Page,
SampleCategoriesAndSubcategories,
} from 'firefox-profiler/types';
import type { FlameGraphTiming } from 'firefox-profiler/profile-logic/flame-graph';
import type { CallNodeInfo } from 'firefox-profiler/profile-logic/call-node-info';
import type {
CallTree,
CallTreeTimings,
} from 'firefox-profiler/profile-logic/call-tree';
import type { ConnectedProps } from 'firefox-profiler/utils/connect';
import './FlameGraph.css';
const STACK_FRAME_HEIGHT = 16;
/**
* How "wide" a call node box needs to be for it to be able to be
* selected with keyboard navigation. This is a fraction between 0 and
* 1, where 1 means the box spans the whole viewport.
*/
const SELECTABLE_THRESHOLD = 0.001;
type StateProps = {
readonly thread: Thread;
readonly weightType: WeightType;
readonly innerWindowIDToPageMap: Map<InnerWindowID, Page> | null;
readonly maxStackDepthPlusOne: number;
readonly timeRange: StartEndRange;
readonly previewSelection: PreviewSelection | null;
readonly flameGraphTiming: FlameGraphTiming;
readonly callTree: CallTree;
readonly callNodeInfo: CallNodeInfo;
readonly threadsKey: ThreadsKey;
readonly selectedCallNodeIndex: IndexIntoCallNodeTable | null;
readonly zoomedInCallNodeIndex: IndexIntoCallNodeTable | null;
readonly rightClickedCallNodeIndex: IndexIntoCallNodeTable | null;
readonly scrollToSelectionGeneration: number;
readonly categories: CategoryList;
readonly interval: Milliseconds;
readonly isInverted: boolean;
readonly callTreeSummaryStrategy: CallTreeSummaryStrategy;
readonly ctssSamples: SamplesLikeTable;
readonly ctssSampleCategoriesAndSubcategories: SampleCategoriesAndSubcategories;
readonly tracedTiming: CallTreeTimings | null;
readonly displayStackType: boolean;
};
type DispatchProps = {
readonly changeSelectedCallNode: typeof changeSelectedCallNode;
readonly changeZoomedInCallNode: typeof changeZoomedInCallNode;
readonly changeRightClickedCallNode: typeof changeRightClickedCallNode;
readonly handleCallNodeTransformShortcut: typeof handleCallNodeTransformShortcut;
readonly updateBottomBoxContentsAndMaybeOpen: typeof updateBottomBoxContentsAndMaybeOpen;
};
type Props = ConnectedProps<{}, StateProps, DispatchProps>;
export interface FlameGraphHandle {
focus(): void;
}
class FlameGraphImpl
extends React.PureComponent<Props>
implements FlameGraphHandle
{
_viewport: HTMLDivElement | null = null;
override componentDidMount() {
document.addEventListener('copy', this._onCopy, false);
}
override componentWillUnmount() {
document.removeEventListener('copy', this._onCopy, false);
}
_onSelectedCallNodeChange = (
callNodeIndex: IndexIntoCallNodeTable | null
) => {
const {
callNodeInfo,
threadsKey,
changeSelectedCallNode,
changeZoomedInCallNode,
} = this.props;
changeSelectedCallNode(
threadsKey,
callNodeInfo.getCallNodePathFromIndex(callNodeIndex)
);
changeZoomedInCallNode(
callNodeInfo.getCallNodePathFromIndex(callNodeIndex)
);
};
_onRightClickedCallNodeChange = (
callNodeIndex: IndexIntoCallNodeTable | null
) => {
const { callNodeInfo, threadsKey, changeRightClickedCallNode } = this.props;
changeRightClickedCallNode(
threadsKey,
callNodeInfo.getCallNodePathFromIndex(callNodeIndex)
);
};
_onCallNodeEnterOrDoubleClick = (
callNodeIndex: IndexIntoCallNodeTable | null
) => {
if (callNodeIndex === null) {
return;
}
const { callTree, updateBottomBoxContentsAndMaybeOpen } = this.props;
const bottomBoxInfo = callTree.getBottomBoxInfoForCallNode(callNodeIndex);
updateBottomBoxContentsAndMaybeOpen('flame-graph', bottomBoxInfo);
};
_shouldDisplayTooltips = () => this.props.rightClickedCallNodeIndex === null;
_takeViewportRef = (viewport: HTMLDivElement | null) => {
this._viewport = viewport;
};
/* This method is called from MaybeFlameGraph. */
/* eslint-disable-next-line react/no-unused-class-component-methods */
focus = () => {
if (this._viewport) {
this._viewport.focus();
}
};
/**
* Is the box for this call node wide enough to be selected?
*/
_wideEnough = (callNodeIndex: IndexIntoCallNodeTable): boolean => {
const { flameGraphTiming, callNodeInfo } = this.props;
const callNodeTable = callNodeInfo.getCallNodeTable();
const depth = callNodeTable.depth[callNodeIndex];
const row = flameGraphTiming[depth];
const columnIndex = row.callNode.indexOf(callNodeIndex);
return row.end[columnIndex] - row.start[columnIndex] > SELECTABLE_THRESHOLD;
};
/**
* Return next keyboard selectable callNodeIndex along one
* horizontal direction.
*
* `direction` should be either -1 (left) or 1 (right).
*
* Returns undefined if no selectable callNodeIndex can be found.
* This means we're already at the end, or the boxes of all
* candidate call nodes are too narrow to be selected.
*/
_nextSelectableInRow = (
startingCallNodeIndex: IndexIntoCallNodeTable,
direction: 1 | -1
): IndexIntoCallNodeTable | void => {
const { flameGraphTiming, callNodeInfo } = this.props;
let callNodeIndex = startingCallNodeIndex;
const callNodeTable = callNodeInfo.getCallNodeTable();
const depth = callNodeTable.depth[callNodeIndex];
const row = flameGraphTiming[depth];
let columnIndex = row.callNode.indexOf(callNodeIndex);
do {
columnIndex += direction;
callNodeIndex = row.callNode[columnIndex];
if (
row.end[columnIndex] - row.start[columnIndex] >
SELECTABLE_THRESHOLD
) {
// The box for this callNodeIndex is wide enough. We've found
// a candidate.
break;
}
} while (callNodeIndex !== undefined);
return callNodeIndex;
};
_handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
const {
threadsKey,
callTree,
callNodeInfo,
selectedCallNodeIndex,
rightClickedCallNodeIndex,
changeSelectedCallNode,
handleCallNodeTransformShortcut,
} = this.props;
const callNodeTable = callNodeInfo.getCallNodeTable();
if (
// Please do not forget to update the switch/case below if changing the array to allow more keys.
['ArrowDown', 'ArrowUp', 'ArrowLeft', 'ArrowRight'].includes(event.key)
) {
if (selectedCallNodeIndex === null) {
// Just select the "root" node if we've got no prior selection.
changeSelectedCallNode(
threadsKey,
callNodeInfo.getCallNodePathFromIndex(0)
);
return;
}
switch (event.key) {
case 'ArrowDown': {
const prefix = callNodeTable.prefix[selectedCallNodeIndex];
if (prefix !== -1) {
changeSelectedCallNode(
threadsKey,
callNodeInfo.getCallNodePathFromIndex(prefix)
);
}
break;
}
case 'ArrowUp': {
const [callNodeIndex] = callTree.getChildren(selectedCallNodeIndex);
// The call nodes returned from getChildren are sorted by
// total time in descending order. The first one in the
// array, which is the one we pick, has the longest time and
// thus the widest box.
if (callNodeIndex !== undefined && this._wideEnough(callNodeIndex)) {
changeSelectedCallNode(
threadsKey,
callNodeInfo.getCallNodePathFromIndex(callNodeIndex)
);
}
break;
}
case 'ArrowLeft':
case 'ArrowRight': {
const callNodeIndex = this._nextSelectableInRow(
selectedCallNodeIndex,
event.key === 'ArrowLeft' ? -1 : 1
);
if (callNodeIndex !== undefined) {
changeSelectedCallNode(
threadsKey,
callNodeInfo.getCallNodePathFromIndex(callNodeIndex)
);
}
break;
}
default:
// We shouldn't arrive here, thanks to the if block at the top.
console.error(
`An unknown key "${event.key}" was pressed, this shouldn't happen.`
);
}
return;
}
// Otherwise, handle shortcuts for the call node transforms.
const nodeIndex =
rightClickedCallNodeIndex !== null
? rightClickedCallNodeIndex
: selectedCallNodeIndex;
if (nodeIndex === null) {
return;
}
if (event.key === 'Enter') {
this._onCallNodeEnterOrDoubleClick(nodeIndex);
return;
}
handleCallNodeTransformShortcut(event, threadsKey, nodeIndex);
};
_onCopy = (event: ClipboardEvent) => {
if (document.activeElement === this._viewport) {
event.preventDefault();
const { callNodeInfo, selectedCallNodeIndex, thread } = this.props;
const callNodeTable = callNodeInfo.getCallNodeTable();
if (selectedCallNodeIndex !== null) {
const funcIndex = callNodeTable.func[selectedCallNodeIndex];
const funcName = thread.stringTable.getString(
thread.funcTable.name[funcIndex]
);
event.clipboardData!.setData('text/plain', funcName);
}
}
};
override render() {
const {
thread,
threadsKey,
maxStackDepthPlusOne,
flameGraphTiming,
callTree,
callNodeInfo,
timeRange,
previewSelection,
rightClickedCallNodeIndex,
selectedCallNodeIndex,
zoomedInCallNodeIndex,
scrollToSelectionGeneration,
callTreeSummaryStrategy,
categories,
interval,
isInverted,
innerWindowIDToPageMap,
weightType,
ctssSamples,
ctssSampleCategoriesAndSubcategories,
tracedTiming,
displayStackType,
} = this.props;
// Get the CallTreeTimingsNonInverted out of tracedTiming. We pass this
// along rather than the more generic CallTreeTimings type so that the
// FlameGraphCanvas component can operate on the more specialized type.
// (CallTreeTimingsNonInverted and CallTreeTimingsInverted are very
// different, and the flame graph is only used with non-inverted timings.)
const tracedTimingNonInverted =
tracedTiming !== null
? ensureExists(
extractNonInvertedCallTreeTimings(tracedTiming),
'The flame graph should only ever see non-inverted timings, see UrlState.getInvertCallstack'
)
: null;
const maxViewportHeight = maxStackDepthPlusOne * STACK_FRAME_HEIGHT;
return (
<div className="flameGraphContent" onKeyDown={this._handleKeyDown}>
<ContextMenuTrigger
id="CallNodeContextMenu"
attributes={{
className: 'treeViewContextMenu',
}}
>
<FlameGraphCanvas
key={threadsKey}
// ChartViewport props
viewportProps={{
timeRange,
maxViewportHeight,
maximumZoom: 1,
previewSelection,
startsAtBottom: true,
disableHorizontalMovement: true,
viewportNeedsUpdate,
marginLeft: 0,
marginRight: 0,
containerRef: this._takeViewportRef,
}}
// FlameGraphCanvas props
chartProps={{
thread,
innerWindowIDToPageMap,
weightType,
maxStackDepthPlusOne,
flameGraphTiming,
callTree,
callNodeInfo,
categories,
selectedCallNodeIndex,
zoomedInCallNodeIndex,
rightClickedCallNodeIndex,
scrollToSelectionGeneration,
callTreeSummaryStrategy,
stackFrameHeight: STACK_FRAME_HEIGHT,
onSelectionChange: this._onSelectedCallNodeChange,
onRightClick: this._onRightClickedCallNodeChange,
onDoubleClick: this._onCallNodeEnterOrDoubleClick,
shouldDisplayTooltips: this._shouldDisplayTooltips,
interval,
isInverted,
ctssSamples,
ctssSampleCategoriesAndSubcategories,
tracedTiming: tracedTimingNonInverted,
displayStackType,
}}
/>
</ContextMenuTrigger>
</div>
);
}
}
function viewportNeedsUpdate() {
// By always returning false we prevent the viewport from being
// reset and scrolled all the way to the bottom when doing
// operations like changing the time selection or applying a
// transform.
return false;
}
export const FlameGraph = explicitConnectWithForwardRef<
{},
StateProps,
DispatchProps,
FlameGraphHandle
>({
mapStateToProps: (state) => ({
thread: selectedThreadSelectors.getFilteredThread(state),
weightType: selectedThreadSelectors.getWeightTypeForCallTree(state),
// Use the filtered call node max depth, rather than the preview filtered one, so
// that the viewport height is stable across preview selections.
maxStackDepthPlusOne:
selectedThreadSelectors.getFilteredCallNodeMaxDepthPlusOne(state),
flameGraphTiming: selectedThreadSelectors.getFlameGraphTiming(state),
callTree: selectedThreadSelectors.getCallTree(state),
timeRange: getCommittedRange(state),
previewSelection: getPreviewSelection(state),
callNodeInfo: selectedThreadSelectors.getCallNodeInfo(state),
categories: getCategories(state),
threadsKey: getSelectedThreadsKey(state),
selectedCallNodeIndex:
selectedThreadSelectors.getSelectedCallNodeIndex(state),
zoomedInCallNodeIndex:
selectedThreadSelectors.getZoomedInCallNodeIndex(state),
rightClickedCallNodeIndex:
selectedThreadSelectors.getRightClickedCallNodeIndex(state),
scrollToSelectionGeneration: getScrollToSelectionGeneration(state),
interval: getProfileInterval(state),
isInverted: getInvertCallstack(state),
callTreeSummaryStrategy:
selectedThreadSelectors.getCallTreeSummaryStrategy(state),
innerWindowIDToPageMap: getInnerWindowIDToPageMap(state),
ctssSamples: selectedThreadSelectors.getPreviewFilteredCtssSamples(state),
ctssSampleCategoriesAndSubcategories:
selectedThreadSelectors.getPreviewFilteredCtssSampleCategoriesAndSubcategories(
state
),
tracedTiming: selectedThreadSelectors.getTracedTiming(state),
displayStackType: getProfileUsesMultipleStackTypes(state),
}),
mapDispatchToProps: {
changeSelectedCallNode,
changeZoomedInCallNode,
changeRightClickedCallNode,
handleCallNodeTransformShortcut,
updateBottomBoxContentsAndMaybeOpen,
},
options: { forwardRef: true },
component: FlameGraphImpl,
});