-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathSampleGraph.tsx
More file actions
393 lines (351 loc) · 11.8 KB
/
Copy pathSampleGraph.tsx
File metadata and controls
393 lines (351 loc) · 11.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
/* 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 React, { PureComponent } from 'react';
import classNames from 'classnames';
import { InView } from 'react-intersection-observer';
import { timeCode } from 'firefox-profiler/utils/time-code';
import { getSampleIndexClosestToCenteredTime } from 'firefox-profiler/profile-logic/profile-data';
import { bisectionRight } from 'firefox-profiler/utils/bisect';
import { withSize } from 'firefox-profiler/components/shared/WithSize';
import { BLUE_70, BLUE_40 } from 'photon-colors';
import {
Tooltip,
MOUSE_OFFSET,
} from 'firefox-profiler/components/tooltip/Tooltip';
import { SampleTooltipContents } from 'firefox-profiler/components/shared/SampleTooltipContents';
import './SampleGraph.css';
import type {
Thread,
CategoryList,
IndexIntoSamplesTable,
Milliseconds,
SelectedState,
CssPixels,
TimelineType,
ImplementationFilter,
} from 'firefox-profiler/types';
import type { SizeProps } from 'firefox-profiler/components/shared/WithSize';
import type { CpuRatioInTimeRange } from './ActivityGraphFills';
export type HoveredPixelState = {
readonly sample: IndexIntoSamplesTable | null;
readonly cpuRatioInTimeRange: CpuRatioInTimeRange | null;
};
type Props = {
readonly className: string;
readonly thread: Thread;
readonly samplesSelectedStates: null | SelectedState[];
readonly interval: Milliseconds;
readonly rangeStart: Milliseconds;
readonly rangeEnd: Milliseconds;
readonly categories: CategoryList;
readonly onSampleClick: (
event: React.MouseEvent<HTMLElement>,
sampleIndex: IndexIntoSamplesTable | null
) => void;
readonly trackName: string;
readonly timelineType: TimelineType;
readonly implementationFilter: ImplementationFilter;
readonly zeroAt: Milliseconds;
readonly profileTimelineUnit: string;
} & SizeProps;
type State = {
hoveredPixelState: null | HoveredPixelState;
mouseX: CssPixels;
mouseY: CssPixels;
};
type CanvasProps = {
readonly className: string;
readonly thread: Thread;
readonly samplesSelectedStates: null | SelectedState[];
readonly interval: Milliseconds;
readonly rangeStart: Milliseconds;
readonly rangeEnd: Milliseconds;
readonly categories: CategoryList;
readonly trackName: string;
} & SizeProps;
/**
* This component controls the rendering of the canvas. Every render call through
* React triggers a new canvas render. Because of this, it's important to only pass
* in the props that are needed for the canvas draw call.
*/
class ThreadSampleGraphCanvas extends React.PureComponent<CanvasProps> {
_canvas: null | HTMLCanvasElement = null;
_takeCanvasRef = (canvas: HTMLCanvasElement | null) =>
(this._canvas = canvas);
_canvasState: { renderScheduled: boolean; inView: boolean } = {
renderScheduled: false,
inView: false,
};
_renderCanvas() {
if (!this._canvasState.inView) {
// Canvas is not in the view. Schedule the render for a later intersection
// observer callback.
this._canvasState.renderScheduled = true;
return;
}
// Canvas is in the view. Render the canvas and reset the schedule state.
this._canvasState.renderScheduled = false;
const canvas = this._canvas;
if (canvas !== null) {
timeCode('ThreadSampleGraph render', () => {
this.drawCanvas(canvas);
});
}
}
_observerCallback = (inView: boolean, _entry: IntersectionObserverEntry) => {
this._canvasState.inView = inView;
if (!this._canvasState.renderScheduled) {
// Skip if render is not scheduled.
return;
}
this._renderCanvas();
};
override componentDidMount() {
this._renderCanvas();
}
override componentDidUpdate() {
this._renderCanvas();
}
drawCanvas(canvas: HTMLCanvasElement) {
const {
thread,
interval,
rangeStart,
rangeEnd,
samplesSelectedStates,
categories,
width,
height,
} = this.props;
const devicePixelRatio = canvas.ownerDocument
? canvas.ownerDocument.defaultView?.devicePixelRatio || 1
: 1;
canvas.width = Math.round(width * devicePixelRatio);
canvas.height = Math.round(height * devicePixelRatio);
const ctx = canvas.getContext('2d')!;
const rangeLength = rangeEnd - rangeStart;
const xPixelsPerMs = canvas.width / rangeLength;
const trueIntervalPixelWidth = interval * xPixelsPerMs;
const multiplier = trueIntervalPixelWidth < 2.0 ? 1.2 : 1.0;
const drawnIntervalWidth = Math.max(
0.8,
trueIntervalPixelWidth * multiplier
);
const drawnSampleWidth = Math.min(drawnIntervalWidth, 10);
const firstDrawnSampleTime = rangeStart - drawnIntervalWidth / xPixelsPerMs;
const lastDrawnSampleTime = rangeEnd;
const firstDrawnSampleIndex = bisectionRight(
thread.samples.time,
firstDrawnSampleTime
);
const afterLastDrawnSampleIndex = bisectionRight(
thread.samples.time,
lastDrawnSampleTime,
firstDrawnSampleIndex
);
// Do one pass over the samples array to gather the samples we want to draw.
const regularSamples: number[] = [];
const idleSamples: number[] = [];
const highlightedSamples: number[] = [];
// Enforce a minimum distance so that we don't draw more than 4 samples per
// pixel.
const minGapMs = 0.25 / xPixelsPerMs;
let nextMinTime = -Infinity;
for (let i = firstDrawnSampleIndex; i < afterLastDrawnSampleIndex; i++) {
const sampleTime = thread.samples.time[i];
if (sampleTime < nextMinTime) {
continue;
}
const stackIndex = thread.samples.stack[i];
if (stackIndex === null) {
continue;
}
const xPos =
(sampleTime - rangeStart) * xPixelsPerMs - drawnSampleWidth / 2;
let samplesBucket;
if (
samplesSelectedStates !== null &&
samplesSelectedStates[i] === 'SELECTED'
) {
samplesBucket = highlightedSamples;
} else {
const categoryIndex = thread.stackTable.category[stackIndex];
const category = categories[categoryIndex];
if (category.name === 'Idle') {
samplesBucket = idleSamples;
} else {
samplesBucket = regularSamples;
}
}
samplesBucket.push(xPos);
nextMinTime = sampleTime + minGapMs;
}
function drawSamples(samplePositions: number[], color: string) {
if (samplePositions.length === 0) {
return;
}
ctx.fillStyle = color;
for (let i = 0; i < samplePositions.length; i++) {
const startY = 0;
const xPos = samplePositions[i];
ctx.fillRect(xPos, startY, drawnSampleWidth, canvas.height);
}
}
// Draw the samples in multiple passes, separated by color. This reduces the calls
// to ctx.fillStyle, which saves on time that's spent parsing color strings.
const lighterBlue = '#c5e1fe';
drawSamples(regularSamples, BLUE_40);
drawSamples(highlightedSamples, BLUE_70);
drawSamples(idleSamples, lighterBlue);
}
override render() {
const { trackName } = this.props;
return (
<InView onChange={this._observerCallback}>
<canvas
className={classNames(
`${this.props.className}Canvas`,
'threadSampleGraphCanvas'
)}
ref={this._takeCanvasRef}
>
<h2>Stack Graph for {trackName}</h2>
<p>This graph charts the stack height of each sample.</p>
</canvas>
</InView>
);
}
}
export class ThreadSampleGraphImpl extends PureComponent<Props, State> {
override state: State = {
hoveredPixelState: null,
mouseX: 0,
mouseY: 0,
};
_onClick = (event: React.MouseEvent<HTMLElement>) => {
const hoveredSample = this._getSampleAtMouseEvent(event);
this.props.onSampleClick(event, hoveredSample?.sample ?? null);
};
_onMouseLeave = () => {
this.setState({ hoveredPixelState: null });
};
_onMouseMove = (event: React.MouseEvent<HTMLElement>) => {
const canvas = event.currentTarget;
if (!canvas) {
return;
}
const rect = canvas.getBoundingClientRect();
this.setState({
hoveredPixelState: this._getSampleAtMouseEvent(event),
mouseX: event.pageX,
// Have the tooltip align to the bottom of the track.
mouseY: rect.bottom - MOUSE_OFFSET,
});
};
_getSampleAtMouseEvent(
event: React.MouseEvent<HTMLElement>
): null | HoveredPixelState {
const canvas = event.currentTarget as HTMLCanvasElement;
if (!canvas) {
return null;
}
const { rangeStart, rangeEnd, thread, interval } = this.props;
const r = canvas.getBoundingClientRect();
const x = event.nativeEvent.offsetX;
const time = rangeStart + (x / r.width) * (rangeEnd - rangeStart);
// These values are copied from the `drawCanvas` method to compute the
// `drawnSampleWidth` instead of extracting into a new function. Extracting
// into a new function is not really idea for performance reasons since we
// need these values for other values in `drawCanvas`.
const rangeLength = rangeEnd - rangeStart;
const xPixelsPerMs = r.width / rangeLength;
const trueIntervalPixelWidth = interval * xPixelsPerMs;
const multiplier = trueIntervalPixelWidth < 2.0 ? 1.2 : 1.0;
const drawnIntervalWidth = Math.max(
0.8,
trueIntervalPixelWidth * multiplier
);
const drawnSampleWidth = Math.min(drawnIntervalWidth, 10) / 2;
const maxTimeDistance = (drawnSampleWidth / 2 / r.width) * rangeLength;
const sampleIndex = getSampleIndexClosestToCenteredTime(
thread.samples,
time,
maxTimeDistance
);
if (sampleIndex === null) {
// No sample that is close enough found. Mouse doesn't hover any of the
// sample boxes in the sample graph.
return null;
}
if (thread.samples.stack[sampleIndex] === null) {
// If the sample index refers to a null sample, that sample
// has been filtered out and means that there was no stack bar
// drawn at the place where the user clicked. Do nothing here.
return null;
}
return {
sample: sampleIndex,
cpuRatioInTimeRange: null,
};
}
override render() {
const {
className,
trackName,
timelineType,
categories,
implementationFilter,
thread,
interval,
rangeStart,
rangeEnd,
samplesSelectedStates,
width,
height,
zeroAt,
profileTimelineUnit,
} = this.props;
const { hoveredPixelState, mouseX, mouseY } = this.state;
return (
<div
className={className}
onMouseMove={this._onMouseMove}
onMouseLeave={this._onMouseLeave}
onClick={this._onClick}
>
<ThreadSampleGraphCanvas
className={className}
trackName={trackName}
interval={interval}
thread={thread}
rangeStart={rangeStart}
rangeEnd={rangeEnd}
samplesSelectedStates={samplesSelectedStates}
categories={categories}
width={width}
height={height}
/>
{hoveredPixelState === null ? null : (
<Tooltip mouseX={mouseX} mouseY={mouseY}>
<SampleTooltipContents
sampleIndex={(hoveredPixelState as HoveredPixelState).sample}
cpuRatioInTimeRange={
timelineType === 'cpu-category'
? (hoveredPixelState as HoveredPixelState).cpuRatioInTimeRange
: null
}
rangeFilteredThread={thread}
categories={categories}
implementationFilter={implementationFilter}
zeroAt={zeroAt}
profileTimelineUnit={profileTimelineUnit}
interval={interval}
/>
</Tooltip>
)}
</div>
);
}
}
export const ThreadSampleGraph = withSize(ThreadSampleGraphImpl);