forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall-tree.js
725 lines (658 loc) · 23.5 KB
/
call-tree.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/* 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 { oneLine } from 'common-tags';
import { timeCode } from '../utils/time-code';
import {
getOriginAnnotationForFunc,
getCategoryPairLabel,
getBottomBoxInfoForCallNode,
} from './profile-data';
import { resourceTypes } from './data-structures';
import { getFunctionName } from './function-info';
import type {
CategoryList,
Thread,
IndexIntoFuncTable,
SamplesLikeTable,
WeightType,
CallNodeTable,
CallNodePath,
IndexIntoCallNodeTable,
CallNodeInfo,
CallNodeData,
CallNodeDisplayData,
Milliseconds,
ExtraBadgeInfo,
BottomBoxInfo,
CallNodeLeafAndSummary,
SelfAndTotal,
} from 'firefox-profiler/types';
import ExtensionIcon from '../../res/img/svg/extension.svg';
import { formatCallNodeNumber, formatPercent } from '../utils/format-numbers';
import { assertExhaustiveCheck, ensureExists } from '../utils/flow';
import * as ProfileData from './profile-data';
import type { CallTreeSummaryStrategy } from '../types/actions';
type CallNodeChildren = IndexIntoCallNodeTable[];
export type CallTreeTimings = {
callNodeHasChildren: Uint8Array,
self: Float32Array,
leaf: Float32Array,
total: Float32Array,
rootTotalSummary: number,
};
function extractFaviconFromLibname(libname: string): string | null {
try {
const url = new URL('/favicon.ico', libname);
if (url.protocol === 'http:') {
// Upgrade http requests.
url.protocol = 'https:';
}
return url.href;
} catch (e) {
console.error(
'Error while extracing the favicon from the libname',
libname
);
return null;
}
}
interface CallTreeInternal {
hasChildren(callNodeIndex: IndexIntoCallNodeTable): boolean;
createChildren(nodeIndex: IndexIntoCallNodeTable): CallNodeChildren;
createRoots(): CallNodeChildren;
getSelfAndTotal(nodeIndex: IndexIntoCallNodeTable): SelfAndTotal;
findHeaviestPathInSubtree(
callNodeIndex: IndexIntoCallNodeTable
): CallNodePath;
}
export class CallTreeInternalImpl implements CallTreeInternal {
_callNodeInfo: CallNodeInfo;
_callNodeTable: CallNodeTable;
_callTreeTimings: CallTreeTimings;
_callNodeHasChildren: Uint8Array; // A table column matching the callNodeTable
constructor(callNodeInfo: CallNodeInfo, callTreeTimings: CallTreeTimings) {
this._callNodeInfo = callNodeInfo;
this._callNodeTable = callNodeInfo.getCallNodeTable();
this._callTreeTimings = callTreeTimings;
this._callNodeHasChildren = callTreeTimings.callNodeHasChildren;
}
_getFirstChildIndex(
callNodeIndex: IndexIntoCallNodeTable | -1
): IndexIntoCallNodeTable | -1 {
if (callNodeIndex === -1) {
return this._callNodeTable.length !== 0 ? 0 : -1;
}
const subtreeRangeEnd = this._callNodeTable.subtreeRangeEnd[callNodeIndex];
if (subtreeRangeEnd !== callNodeIndex + 1) {
return callNodeIndex + 1;
}
return -1;
}
createRoots() {
return this.createChildren(-1);
}
createChildren(callNodeIndex: IndexIntoCallNodeTable): CallNodeChildren {
const firstChild = this._getFirstChildIndex(callNodeIndex);
const children = [];
for (
let childCallNodeIndex = firstChild;
childCallNodeIndex !== -1;
childCallNodeIndex = this._callNodeTable.nextSibling[childCallNodeIndex]
) {
const childTotalSummary = this._callTreeTimings.total[childCallNodeIndex];
const childHasChildren = this._callNodeHasChildren[childCallNodeIndex];
if (childTotalSummary !== 0 || childHasChildren !== 0) {
children.push(childCallNodeIndex);
}
}
children.sort(
(a, b) =>
Math.abs(this._callTreeTimings.total[b]) -
Math.abs(this._callTreeTimings.total[a])
);
return children;
}
hasChildren(callNodeIndex: IndexIntoCallNodeTable): boolean {
return this._callNodeHasChildren[callNodeIndex] !== 0;
}
getSelfAndTotal(callNodeIndex: IndexIntoCallNodeTable): SelfAndTotal {
const self = this._callTreeTimings.self[callNodeIndex];
const total = this._callTreeTimings.total[callNodeIndex];
return { self, total };
}
findHeaviestPathInSubtree(
callNodeIndex: IndexIntoCallNodeTable
): CallNodePath {
const rangeEnd = this._callNodeTable.subtreeRangeEnd[callNodeIndex];
// Find the call node with the highest leaf time.
let maxNode = -1;
let maxAbs = 0;
for (let nodeIndex = callNodeIndex; nodeIndex < rangeEnd; nodeIndex++) {
const nodeLeaf = Math.abs(this._callTreeTimings.leaf[nodeIndex]);
if (maxNode === -1 || nodeLeaf > maxAbs) {
maxNode = nodeIndex;
maxAbs = nodeLeaf;
}
}
return this._callNodeInfo.getCallNodePathFromIndex(maxNode);
}
}
export class CallTree {
_categories: CategoryList;
_internal: CallTreeInternal;
_callNodeInfo: CallNodeInfo;
_callNodeTable: CallNodeTable;
_thread: Thread;
_rootTotalSummary: number;
_displayDataByIndex: Map<IndexIntoCallNodeTable, CallNodeDisplayData>;
// _children is indexed by IndexIntoCallNodeTable. Since they are
// integers, using an array directly is faster than going through a Map.
_children: Array<CallNodeChildren>;
_roots: IndexIntoCallNodeTable[];
_isHighPrecision: boolean;
_weightType: WeightType;
constructor(
thread: Thread,
categories: CategoryList,
callNodeInfo: CallNodeInfo,
internal: CallTreeInternal,
rootTotalSummary: number,
isHighPrecision: boolean,
weightType: WeightType
) {
this._categories = categories;
this._internal = internal;
this._callNodeInfo = callNodeInfo;
this._callNodeTable = callNodeInfo.getCallNodeTable();
this._thread = thread;
this._rootTotalSummary = rootTotalSummary;
this._displayDataByIndex = new Map();
this._children = [];
this._roots = internal.createRoots();
this._isHighPrecision = isHighPrecision;
this._weightType = weightType;
}
getRoots() {
return this._roots;
}
getChildren(callNodeIndex: IndexIntoCallNodeTable): CallNodeChildren {
let children = this._children[callNodeIndex];
if (children === undefined) {
children = this._internal.createChildren(callNodeIndex);
this._children[callNodeIndex] = children;
}
return children;
}
hasChildren(callNodeIndex: IndexIntoCallNodeTable): boolean {
return this._internal.hasChildren(callNodeIndex);
}
_addDescendantsToSet(
callNodeIndex: IndexIntoCallNodeTable,
set: Set<IndexIntoCallNodeTable>
): void {
for (const child of this.getChildren(callNodeIndex)) {
set.add(child);
this._addDescendantsToSet(child, set);
}
}
getAllDescendants(
callNodeIndex: IndexIntoCallNodeTable
): Set<IndexIntoCallNodeTable> {
const result = new Set();
this._addDescendantsToSet(callNodeIndex, result);
return result;
}
getParent(
callNodeIndex: IndexIntoCallNodeTable
): IndexIntoCallNodeTable | -1 {
return this._callNodeTable.prefix[callNodeIndex];
}
getDepth(callNodeIndex: IndexIntoCallNodeTable): number {
return this._callNodeTable.depth[callNodeIndex];
}
getNodeData(callNodeIndex: IndexIntoCallNodeTable): CallNodeData {
const funcIndex = this._callNodeTable.func[callNodeIndex];
const funcName = this._thread.stringTable.getString(
this._thread.funcTable.name[funcIndex]
);
const { self, total } = this._internal.getSelfAndTotal(callNodeIndex);
const totalRelative = total / this._rootTotalSummary;
const selfRelative = self / this._rootTotalSummary;
return {
funcName,
total,
totalRelative,
self,
selfRelative,
};
}
_getInliningBadge(
callNodeIndex: IndexIntoCallNodeTable,
funcName: string
): ExtraBadgeInfo | void {
const calledFunction = getFunctionName(funcName);
const inlinedIntoNativeSymbol =
this._callNodeTable.sourceFramesInlinedIntoSymbol[callNodeIndex];
if (inlinedIntoNativeSymbol === null) {
return undefined;
}
if (inlinedIntoNativeSymbol === -1) {
return {
name: 'divergent-inlining',
vars: { calledFunction },
localizationId: 'CallTree--divergent-inlining-badge',
contentFallback: '',
titleFallback: `Some calls to ${calledFunction} were inlined by the compiler.`,
};
}
const outerFunction = getFunctionName(
this._thread.stringTable.getString(
this._thread.nativeSymbols.name[inlinedIntoNativeSymbol]
)
);
return {
name: 'inlined',
vars: { calledFunction, outerFunction },
localizationId: 'CallTree--inlining-badge',
contentFallback: '(inlined)',
titleFallback: `Calls to ${calledFunction} were inlined into ${outerFunction} by the compiler.`,
};
}
getDisplayData(callNodeIndex: IndexIntoCallNodeTable): CallNodeDisplayData {
let displayData: CallNodeDisplayData | void =
this._displayDataByIndex.get(callNodeIndex);
if (displayData === undefined) {
const { funcName, total, totalRelative, self } =
this.getNodeData(callNodeIndex);
const funcIndex = this._callNodeTable.func[callNodeIndex];
const categoryIndex = this._callNodeTable.category[callNodeIndex];
const subcategoryIndex = this._callNodeTable.subcategory[callNodeIndex];
const badge = this._getInliningBadge(callNodeIndex, funcName);
const resourceIndex = this._thread.funcTable.resource[funcIndex];
const resourceType = this._thread.resourceTable.type[resourceIndex];
const isFrameLabel = resourceIndex === -1;
const libName = this._getOriginAnnotation(funcIndex);
const weightType = this._weightType;
let iconSrc = null;
let icon = null;
if (resourceType === resourceTypes.webhost) {
icon = iconSrc = extractFaviconFromLibname(libName);
} else if (resourceType === resourceTypes.addon) {
iconSrc = ExtensionIcon;
const resourceNameIndex =
this._thread.resourceTable.name[resourceIndex];
const iconText = this._thread.stringTable.getString(resourceNameIndex);
icon = iconText;
}
const formattedTotal = formatCallNodeNumber(
weightType,
this._isHighPrecision,
total
);
const formattedSelf = formatCallNodeNumber(
weightType,
this._isHighPrecision,
self
);
const totalPercent = `${formatPercent(totalRelative)}`;
let ariaLabel;
let totalWithUnit;
let selfWithUnit;
switch (weightType) {
case 'tracing-ms': {
totalWithUnit = `${formattedTotal}ms`;
selfWithUnit = `${formattedSelf}ms`;
ariaLabel = oneLine`
${funcName},
running time is ${totalWithUnit} (${totalPercent}),
self time is ${selfWithUnit}
`;
break;
}
case 'samples': {
// TODO - L10N pluralization
totalWithUnit =
total === 1
? `${formattedTotal} sample`
: `${formattedTotal} samples`;
selfWithUnit =
self === 1 ? `${formattedSelf} sample` : `${formattedSelf} samples`;
ariaLabel = oneLine`
${funcName},
running count is ${totalWithUnit} (${totalPercent}),
self count is ${selfWithUnit}
`;
break;
}
case 'bytes': {
totalWithUnit = `${formattedTotal} bytes`;
selfWithUnit = `${formattedSelf} bytes`;
ariaLabel = oneLine`
${funcName},
total size is ${totalWithUnit} (${totalPercent}),
self size is ${selfWithUnit}
`;
break;
}
default:
throw assertExhaustiveCheck(weightType, 'Unhandled WeightType.');
}
displayData = {
total: total === 0 ? '—' : formattedTotal,
totalWithUnit: total === 0 ? '—' : totalWithUnit,
self: self === 0 ? '—' : formattedSelf,
selfWithUnit: self === 0 ? '—' : selfWithUnit,
totalPercent,
name: funcName,
lib: libName.slice(0, 1000),
// Dim platform pseudo-stacks.
isFrameLabel,
badge,
categoryName: getCategoryPairLabel(
this._categories,
categoryIndex,
subcategoryIndex
),
categoryColor: this._categories[categoryIndex].color,
iconSrc,
icon,
ariaLabel,
};
this._displayDataByIndex.set(callNodeIndex, displayData);
}
return displayData;
}
_getOriginAnnotation(funcIndex: IndexIntoFuncTable): string {
return getOriginAnnotationForFunc(
funcIndex,
this._thread.funcTable,
this._thread.resourceTable,
this._thread.stringTable
);
}
getBottomBoxInfoForCallNode(
callNodeIndex: IndexIntoCallNodeTable
): BottomBoxInfo {
return getBottomBoxInfoForCallNode(
callNodeIndex,
this._callNodeInfo,
this._thread
);
}
/**
* Take a CallNodeIndex, and compute an inverted path for it.
*
* e.g:
* (invertedPath, invertedCallTree) => path
* (path, callTree) => invertedPath
*
* Call trees are sorted with the CallNodes with the heaviest total time as the first
* entry. This function walks to the tip of the heaviest branches to find the leaf node,
* then construct an inverted CallNodePath with the result. This gives a pretty decent
* result, but it doesn't guarantee that it will select the heaviest CallNodePath for the
* INVERTED call tree. This would require doing a round trip through the reducers or
* some other mechanism in order to first calculate the next inverted call tree. This is
* probably not worth it, so go ahead and use the uninverted call tree, as it's probably
* good enough.
*/
findHeavyPathToSameFunctionAfterInversion(
callNodeIndex: IndexIntoCallNodeTable | null
): CallNodePath {
if (callNodeIndex === null) {
return [];
}
const heaviestPath =
this._internal.findHeaviestPathInSubtree(callNodeIndex);
const startingDepth = this._callNodeTable.depth[callNodeIndex];
const partialPath = heaviestPath.slice(startingDepth);
return partialPath.reverse();
}
}
// In an inverted profile, all the amount of self unit (time, bytes, count, etc.) is
// accounted to the root nodes. So `callNodeSelf` will be 0 for all non-root nodes.
function _getInvertedCallNodeSelf(
callNodeLeaf: Float32Array,
callNodeTable: CallNodeTable
): Float32Array {
// Compute an array that maps the callNodeIndex to its root.
const callNodeToRoot = new Int32Array(callNodeTable.length);
// Compute the self time during the same loop.
const callNodeSelf = new Float32Array(callNodeTable.length);
for (
let callNodeIndex = 0;
callNodeIndex < callNodeTable.length;
callNodeIndex++
) {
const prefixCallNode = callNodeTable.prefix[callNodeIndex];
if (prefixCallNode === -1) {
// callNodeIndex is a root node
callNodeToRoot[callNodeIndex] = callNodeIndex;
} else {
// The callNodeTable guarantees that a callNode's prefix always comes
// before the callNode; prefix references are always to lower callNode
// indexes and never to higher indexes.
// We are iterating the callNodeTable in forwards direction (starting at
// index 0) so we know that we have already visited the current call
// node's prefix call node and can reuse its stored root node, which
// recursively is the value we're looking for.
callNodeToRoot[callNodeIndex] = callNodeToRoot[prefixCallNode];
}
callNodeSelf[callNodeToRoot[callNodeIndex]] += callNodeLeaf[callNodeIndex];
}
return callNodeSelf;
}
/**
* Compute the leaf time for each call node, and the sum of the absolute leaf
* values.
*/
export function computeCallNodeLeafAndSummary(
samples: SamplesLikeTable,
sampleIndexToCallNodeIndex: Array<null | IndexIntoCallNodeTable>,
callNodeCount: number
): CallNodeLeafAndSummary {
const callNodeLeaf = new Float32Array(callNodeCount);
for (
let sampleIndex = 0;
sampleIndex < sampleIndexToCallNodeIndex.length;
sampleIndex++
) {
const callNodeIndex = sampleIndexToCallNodeIndex[sampleIndex];
if (callNodeIndex !== null) {
const weight = samples.weight ? samples.weight[sampleIndex] : 1;
callNodeLeaf[callNodeIndex] += weight;
}
}
// Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1858310
const abs = Math.abs;
let rootTotalSummary = 0;
for (let callNodeIndex = 0; callNodeIndex < callNodeCount; callNodeIndex++) {
rootTotalSummary += abs(callNodeLeaf[callNodeIndex]);
}
return { callNodeLeaf, rootTotalSummary };
}
/**
* This computes all of the count and timing information displayed in the calltree.
* It takes into account both the normal tree, and the inverted tree.
*/
export function computeCallTreeTimings(
callNodeInfo: CallNodeInfo,
callNodeLeafAndSummary: CallNodeLeafAndSummary
): CallTreeTimings {
const callNodeTable = callNodeInfo.getCallNodeTable();
const { callNodeLeaf, rootTotalSummary } = callNodeLeafAndSummary;
// The self values depend on whether the call tree is inverted: In an inverted
// tree, all the self time is in the roots.
const callNodeSelf = callNodeInfo.isInverted()
? _getInvertedCallNodeSelf(callNodeLeaf, callNodeTable)
: callNodeLeaf;
// Compute the following variables:
const callNodeTotalSummary = new Float32Array(callNodeTable.length);
const callNodeHasChildren = new Uint8Array(callNodeTable.length);
// We loop the call node table in reverse, so that we find the children
// before their parents, and the total is known at the time we reach a
// node.
for (
let callNodeIndex = callNodeTable.length - 1;
callNodeIndex >= 0;
callNodeIndex--
) {
callNodeTotalSummary[callNodeIndex] += callNodeLeaf[callNodeIndex];
const hasChildren = callNodeHasChildren[callNodeIndex] !== 0;
const hasTotalValue = callNodeTotalSummary[callNodeIndex] !== 0;
if (!hasChildren && !hasTotalValue) {
continue;
}
const prefixCallNode = callNodeTable.prefix[callNodeIndex];
if (prefixCallNode !== -1) {
callNodeTotalSummary[prefixCallNode] +=
callNodeTotalSummary[callNodeIndex];
callNodeHasChildren[prefixCallNode] = 1;
}
}
return {
self: callNodeSelf,
leaf: callNodeLeaf,
total: callNodeTotalSummary,
callNodeHasChildren,
rootTotalSummary,
};
}
/**
* An exported interface to get an instance of the CallTree class.
*/
export function getCallTree(
thread: Thread,
callNodeInfo: CallNodeInfo,
categories: CategoryList,
callTreeTimings: CallTreeTimings,
weightType: WeightType
): CallTree {
return timeCode('getCallTree', () => {
return new CallTree(
thread,
categories,
callNodeInfo,
new CallTreeInternalImpl(callNodeInfo, callTreeTimings),
callTreeTimings.rootTotalSummary,
Boolean(thread.isJsTracer),
weightType
);
});
}
/**
* This function takes the call tree summary strategy, and finds the appropriate data
* structure. This can then be used by the call tree and other UI to report on the data.
*/
export function extractSamplesLikeTable(
thread: Thread,
strategy: CallTreeSummaryStrategy
): SamplesLikeTable {
switch (strategy) {
case 'timing':
return thread.samples;
case 'js-allocations':
return ensureExists(
thread.jsAllocations,
'Expected the NativeAllocationTable to exist when using a "js-allocation" strategy'
);
case 'native-retained-allocations': {
const nativeAllocations = ensureExists(
thread.nativeAllocations,
'Expected the NativeAllocationTable to exist when using a "native-allocation" strategy'
);
/* istanbul ignore if */
if (!nativeAllocations.memoryAddress) {
throw new Error(
'Attempting to filter by retained allocations data that is missing the memory addresses.'
);
}
return ProfileData.filterToRetainedAllocations(nativeAllocations);
}
case 'native-allocations':
return ProfileData.filterToAllocations(
ensureExists(
thread.nativeAllocations,
'Expected the NativeAllocationTable to exist when using a "native-allocations" strategy'
)
);
case 'native-deallocations-sites':
return ProfileData.filterToDeallocationsSites(
ensureExists(
thread.nativeAllocations,
'Expected the NativeAllocationTable to exist when using a "native-deallocations-sites" strategy'
)
);
case 'native-deallocations-memory': {
const nativeAllocations = ensureExists(
thread.nativeAllocations,
'Expected the NativeAllocationTable to exist when using a "native-deallocations-memory" strategy'
);
/* istanbul ignore if */
if (!nativeAllocations.memoryAddress) {
throw new Error(
'Attempting to filter by retained allocations data that is missing the memory addresses.'
);
}
return ProfileData.filterToDeallocationsMemory(
ensureExists(
nativeAllocations,
'Expected the NativeAllocationTable to exist when using a "js-allocation" strategy'
)
);
}
/* istanbul ignore next */
default:
throw assertExhaustiveCheck(strategy);
}
}
/**
* This function is extremely similar to computeCallNodeLeafAndSummary,
* but is specialized for converting sample counts into traced timing. Samples
* don't have duration information associated with them, it's mostly how long they
* were observed to be running. This function computes the timing the exact same
* way that the stack chart will display the information, so that timing information
* will agree. In the past, timing was computed by samplingInterval * sampleCount.
* This caused confusion when switching to the trace-based views when the numbers
* did not agree. In order to remove confusion, we can show the sample counts,
* plus the traced timing, which is a compromise between correctness, and consistency.
*/
export function computeCallNodeTracedLeafAndSummary(
samples: SamplesLikeTable,
sampleIndexToCallNodeIndex: Array<IndexIntoCallNodeTable | null>,
callNodeCount: number,
interval: Milliseconds
): CallNodeLeafAndSummary | null {
if (samples.weightType !== 'samples' || samples.weight) {
// Only compute for the samples weight types that have no weights. If a samples
// table has weights then it's a diff profile. Currently, we aren't calculating
// diff profiles, but it could be possible to compute this information twice,
// once for positive weights, and once for negative weights, then sum them
// together. At this time it's not really worth it.
//
// See https://github.com/firefox-devtools/profiler/issues/2615
return null;
}
const callNodeLeaf = new Float32Array(callNodeCount);
let rootTotalSummary = 0;
for (let sampleIndex = 0; sampleIndex < samples.length - 1; sampleIndex++) {
const callNodeIndex = sampleIndexToCallNodeIndex[sampleIndex];
if (callNodeIndex !== null) {
const sampleTracedTime =
samples.time[sampleIndex + 1] - samples.time[sampleIndex];
callNodeLeaf[callNodeIndex] += sampleTracedTime;
rootTotalSummary += sampleTracedTime;
}
}
if (samples.length > 0) {
const callNodeIndex = sampleIndexToCallNodeIndex[samples.length - 1];
if (callNodeIndex !== null) {
// Use the sampling interval for the last sample.
const sampleTracedTime = interval;
callNodeLeaf[callNodeIndex] += sampleTracedTime;
rootTotalSummary += sampleTracedTime;
}
}
return { callNodeLeaf, rootTotalSummary };
}