/
FlameGraph.js
404 lines (368 loc) · 12.5 KB
/
FlameGraph.js
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
/* 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/. */
// @flow
import * as React from 'react';
import explicitConnect from '../../utils/connect';
import { FlameGraphCanvas } from './Canvas';
import {
getCategories,
getCommittedRange,
getPreviewSelection,
getScrollToSelectionGeneration,
getProfileInterval,
getPageList,
} 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 { getCallNodePathFromIndex } from 'firefox-profiler/profile-logic/profile-data';
import {
changeSelectedCallNode,
changeRightClickedCallNode,
handleCallNodeTransformShortcut,
} from 'firefox-profiler/actions/profile-view';
import type {
Thread,
CategoryList,
PageList,
Milliseconds,
StartEndRange,
WeightType,
SamplesLikeTable,
PreviewSelection,
CallTreeSummaryStrategy,
CallNodeInfo,
IndexIntoCallNodeTable,
TracedTiming,
ThreadsKey,
} from 'firefox-profiler/types';
import type { FlameGraphTiming } from 'firefox-profiler/profile-logic/flame-graph';
import type { CallTree } 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 = {|
+thread: Thread,
+weightType: WeightType,
+pages: PageList | null,
+unfilteredThread: Thread,
+sampleIndexOffset: number,
+maxStackDepth: number,
+timeRange: StartEndRange,
+previewSelection: PreviewSelection,
+flameGraphTiming: FlameGraphTiming,
+callTree: CallTree,
+callNodeInfo: CallNodeInfo,
+threadsKey: ThreadsKey,
+selectedCallNodeIndex: IndexIntoCallNodeTable | null,
+rightClickedCallNodeIndex: IndexIntoCallNodeTable | null,
+scrollToSelectionGeneration: number,
+categories: CategoryList,
+interval: Milliseconds,
+isInverted: boolean,
+callTreeSummaryStrategy: CallTreeSummaryStrategy,
+samples: SamplesLikeTable,
+unfilteredSamples: SamplesLikeTable,
+tracedTiming: TracedTiming | null,
|};
type DispatchProps = {|
+changeSelectedCallNode: typeof changeSelectedCallNode,
+changeRightClickedCallNode: typeof changeRightClickedCallNode,
+handleCallNodeTransformShortcut: typeof handleCallNodeTransformShortcut,
|};
type Props = ConnectedProps<{||}, StateProps, DispatchProps>;
class FlameGraphImpl extends React.PureComponent<Props> {
_viewport: HTMLDivElement | null = null;
_onSelectedCallNodeChange = (
callNodeIndex: IndexIntoCallNodeTable | null
) => {
const { callNodeInfo, threadsKey, changeSelectedCallNode } = this.props;
changeSelectedCallNode(
threadsKey,
getCallNodePathFromIndex(callNodeIndex, callNodeInfo.callNodeTable)
);
};
_onRightClickedCallNodeChange = (
callNodeIndex: IndexIntoCallNodeTable | null
) => {
const { callNodeInfo, threadsKey, changeRightClickedCallNode } = this.props;
changeRightClickedCallNode(
threadsKey,
getCallNodePathFromIndex(callNodeIndex, callNodeInfo.callNodeTable)
);
};
_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: { callNodeTable },
} = this.props;
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: { callNodeTable },
} = this.props;
let callNodeIndex = startingCallNodeIndex;
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: SyntheticKeyboardEvent<HTMLElement>) => {
const {
threadsKey,
callTree,
callNodeInfo: { callNodeTable },
selectedCallNodeIndex,
rightClickedCallNodeIndex,
changeSelectedCallNode,
handleCallNodeTransformShortcut,
} = this.props;
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,
getCallNodePathFromIndex(0, callNodeTable)
);
return;
}
switch (event.key) {
case 'ArrowDown': {
const prefix = callNodeTable.prefix[selectedCallNodeIndex];
if (prefix !== -1) {
changeSelectedCallNode(
threadsKey,
getCallNodePathFromIndex(prefix, callNodeTable)
);
}
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,
getCallNodePathFromIndex(callNodeIndex, callNodeTable)
);
}
break;
}
case 'ArrowLeft':
case 'ArrowRight': {
const callNodeIndex = this._nextSelectableInRow(
selectedCallNodeIndex,
event.key === 'ArrowLeft' ? -1 : 1
);
if (callNodeIndex !== undefined) {
changeSelectedCallNode(
threadsKey,
getCallNodePathFromIndex(callNodeIndex, callNodeTable)
);
}
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) {
handleCallNodeTransformShortcut(event, threadsKey, nodeIndex);
}
};
render() {
const {
thread,
unfilteredThread,
sampleIndexOffset,
threadsKey,
maxStackDepth,
flameGraphTiming,
callTree,
callNodeInfo,
timeRange,
previewSelection,
selectedCallNodeIndex,
scrollToSelectionGeneration,
callTreeSummaryStrategy,
categories,
interval,
isInverted,
pages,
weightType,
samples,
unfilteredSamples,
tracedTiming,
} = this.props;
const maxViewportHeight = maxStackDepth * 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,
pages,
weightType,
unfilteredThread,
sampleIndexOffset,
maxStackDepth,
flameGraphTiming,
callTree,
callNodeInfo,
categories,
selectedCallNodeIndex,
scrollToSelectionGeneration,
callTreeSummaryStrategy,
stackFrameHeight: STACK_FRAME_HEIGHT,
onSelectionChange: this._onSelectedCallNodeChange,
onRightClick: this._onRightClickedCallNodeChange,
shouldDisplayTooltips: this._shouldDisplayTooltips,
interval,
isInverted,
samples,
unfilteredSamples,
tracedTiming,
}}
/>
</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 = explicitConnect<{||}, StateProps, DispatchProps>({
mapStateToProps: (state) => ({
thread: selectedThreadSelectors.getFilteredThread(state),
unfilteredThread: selectedThreadSelectors.getThread(state),
weightType: selectedThreadSelectors.getWeightTypeForCallTree(state),
sampleIndexOffset:
selectedThreadSelectors.getSampleIndexOffsetFromCommittedRange(state),
// Use the filtered call node max depth, rather than the preview filtered one, so
// that the viewport height is stable across preview selections.
maxStackDepth: selectedThreadSelectors.getFilteredCallNodeMaxDepth(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),
rightClickedCallNodeIndex:
selectedThreadSelectors.getRightClickedCallNodeIndex(state),
scrollToSelectionGeneration: getScrollToSelectionGeneration(state),
interval: getProfileInterval(state),
isInverted: getInvertCallstack(state),
callTreeSummaryStrategy:
selectedThreadSelectors.getCallTreeSummaryStrategy(state),
pages: getPageList(state),
samples:
selectedThreadSelectors.getPreviewFilteredSamplesForCallTree(state),
unfilteredSamples:
selectedThreadSelectors.getUnfilteredSamplesForCallTree(state),
tracedTiming: selectedThreadSelectors.getTracedTiming(state),
}),
mapDispatchToProps: {
changeSelectedCallNode,
changeRightClickedCallNode,
handleCallNodeTransformShortcut,
},
options: { forwardRef: true },
component: FlameGraphImpl,
});