-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathcall-tree.ts
More file actions
1322 lines (1204 loc) · 41.8 KB
/
Copy pathcall-tree.ts
File metadata and controls
1322 lines (1204 loc) · 41.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
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 { oneLine } from 'common-tags';
import { timeCode } from '../utils/time-code';
import {
getOriginAnnotationForFunc,
getCategoryPairLabel,
} from './profile-data';
import { getFunctionName } from './function-info';
import type {
CategoryList,
Thread,
IndexIntoFuncTable,
SamplesLikeTable,
WeightType,
CallNodeTable,
CallNodeTableBitSet,
CallNodePath,
IndexIntoCallNodeTable,
CallNodeData,
CallNodeDisplayData,
Milliseconds,
ExtraBadgeInfo,
BottomBoxInfo,
CallNodeSelfAndSummary,
SelfAndTotal,
BalancedNativeAllocationsTable,
SampleCategoriesAndSubcategories,
IndexIntoCategoryList,
} from 'firefox-profiler/types';
import { ResourceType } from 'firefox-profiler/types';
import ExtensionIcon from '../../res/img/svg/extension.svg';
import { formatCallNodeNumber, formatPercent } from '../utils/format-numbers';
import { assertExhaustiveCheck, ensureExists } from '../utils/types';
import { checkBit } from '../utils/bitset';
import * as ProfileData from './profile-data';
import type { CallTreeSummaryStrategy } from '../types/actions';
import type { CallNodeInfo, CallNodeInfoInverted } from './call-node-info';
import type { SortableColumn } from '../components/shared/TreeView';
import { getBottomBoxInfoForCallNode } from './bottom-box';
type CallNodeChildren = IndexIntoCallNodeTable[];
export type CallTreeTimingsNonInverted = {
callNodeHasChildren: Uint8Array;
self: Float64Array;
total: Float64Array;
rootTotalSummary: number; // sum of absolute values, this is used for computing percentages
};
type TotalAndHasChildren = { total: number; hasChildren: boolean };
export type InvertedCallTreeRoot = {
totalAndHasChildren: TotalAndHasChildren;
func: IndexIntoFuncTable;
};
export type CallTreeTimingsInverted = {
callNodeSelf: Float64Array;
rootTotalSummary: number;
sortedRoots: IndexIntoFuncTable[];
totalPerRootFunc: Float64Array;
hasChildrenPerRootFunc: Uint8Array;
};
export type CallTreeTimingsFunctionList = {
funcSelf: Float64Array;
funcTotal: Float64Array;
sortedFuncs: IndexIntoFuncTable[];
rootTotalSummary: number;
};
export type CallTreeTimings =
| { type: 'NON_INVERTED'; timings: CallTreeTimingsNonInverted }
| { type: 'FUNCTION_LIST'; timings: CallTreeTimingsFunctionList }
| { type: 'INVERTED'; timings: CallTreeTimingsInverted };
/**
* Gets the CallTreeTimingsNonInverted out of a CallTreeTimings object.
*/
export function extractNonInvertedCallTreeTimings(
callTreeTimings: CallTreeTimings
): CallTreeTimingsNonInverted | null {
if (callTreeTimings.type === 'NON_INVERTED') {
return callTreeTimings.timings;
}
return null;
}
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) {
return null;
}
}
interface CallTreeInternal {
hasChildren(callNodeIndex: IndexIntoCallNodeTable): boolean;
createChildren(nodeIndex: IndexIntoCallNodeTable): CallNodeChildren;
createRoots(): CallNodeChildren;
getSelf(nodeIndex: IndexIntoCallNodeTable): number;
getTotal(nodeIndex: IndexIntoCallNodeTable): number;
findHeaviestPathInSubtree(
callNodeIndex: IndexIntoCallNodeTable
): CallNodePath;
}
export class CallTreeInternalNonInverted implements CallTreeInternal {
_callNodeInfo: CallNodeInfo;
_callNodeTable: CallNodeTable;
_callTreeTimings: CallTreeTimingsNonInverted;
_callNodeHasChildren: Uint8Array; // A table column matching the callNodeTable
constructor(
callNodeInfo: CallNodeInfo,
callTreeTimings: CallTreeTimingsNonInverted
) {
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;
}
getSelf(callNodeIndex: IndexIntoCallNodeTable): number {
return this._callTreeTimings.self[callNodeIndex];
}
getTotal(callNodeIndex: IndexIntoCallNodeTable): number {
return this._callTreeTimings.total[callNodeIndex];
}
findHeaviestPathInSubtree(
callNodeIndex: IndexIntoCallNodeTable
): CallNodePath {
const rangeEnd = this._callNodeTable.subtreeRangeEnd[callNodeIndex];
// Find the call node with the highest self time.
let maxNode = -1;
let maxAbs = 0;
for (let nodeIndex = callNodeIndex; nodeIndex < rangeEnd; nodeIndex++) {
const nodeSelf = Math.abs(this._callTreeTimings.self[nodeIndex]);
if (maxNode === -1 || nodeSelf > maxAbs) {
maxNode = nodeIndex;
maxAbs = nodeSelf;
}
}
return this._callNodeInfo.getCallNodePathFromIndex(maxNode);
}
}
export class CallTreeInternalFunctionList implements CallTreeInternal {
_timings: CallTreeTimingsFunctionList;
constructor(timings: CallTreeTimingsFunctionList) {
this._timings = timings;
}
hasChildren(_callNodeIndex: IndexIntoCallNodeTable): boolean {
return false;
}
createChildren(_nodeIndex: IndexIntoCallNodeTable): CallNodeChildren {
return [];
}
createRoots(): CallNodeChildren {
return this._timings.sortedFuncs;
}
getSelf(nodeIndex: IndexIntoCallNodeTable): number {
return this._timings.funcSelf[nodeIndex];
}
getTotal(nodeIndex: IndexIntoCallNodeTable): number {
return this._timings.funcTotal[nodeIndex];
}
findHeaviestPathInSubtree(
_callNodeIndex: IndexIntoCallNodeTable
): CallNodePath {
throw new Error(
'unexpected call to findHeaviestPathInSubtree in CallTreeInternalFunctionList'
);
}
}
class CallTreeInternalInverted implements CallTreeInternal {
_callNodeInfo: CallNodeInfoInverted;
_callNodeTable: CallNodeTable;
_callNodeSelf: Float64Array;
_rootNodes: IndexIntoCallNodeTable[];
_totalPerRootFunc: Float64Array;
_hasChildrenPerRootFunc: Uint8Array;
_totalAndHasChildrenPerNonRootNode: Map<
IndexIntoCallNodeTable,
TotalAndHasChildren
> = new Map();
constructor(
callNodeInfo: CallNodeInfoInverted,
callTreeTimingsInverted: CallTreeTimingsInverted
) {
this._callNodeInfo = callNodeInfo;
this._callNodeTable = callNodeInfo.getCallNodeTable();
this._callNodeSelf = callTreeTimingsInverted.callNodeSelf;
const { sortedRoots, totalPerRootFunc, hasChildrenPerRootFunc } =
callTreeTimingsInverted;
this._totalPerRootFunc = totalPerRootFunc;
this._hasChildrenPerRootFunc = hasChildrenPerRootFunc;
this._rootNodes = sortedRoots;
}
createRoots(): IndexIntoCallNodeTable[] {
return this._rootNodes;
}
hasChildren(callNodeIndex: IndexIntoCallNodeTable): boolean {
if (this._callNodeInfo.isRoot(callNodeIndex)) {
return this._hasChildrenPerRootFunc[callNodeIndex] !== 0;
}
return this._getTotalAndHasChildren(callNodeIndex).hasChildren;
}
createChildren(nodeIndex: IndexIntoCallNodeTable): CallNodeChildren {
if (!this.hasChildren(nodeIndex)) {
return [];
}
const children = this._callNodeInfo
.getChildren(nodeIndex)
.filter((child) => {
const { total, hasChildren } = this._getTotalAndHasChildren(child);
return total !== 0 || hasChildren;
});
children.sort(
(a, b) =>
Math.abs(this._getTotalAndHasChildren(b).total) -
Math.abs(this._getTotalAndHasChildren(a).total)
);
return children;
}
getSelf(callNodeIndex: IndexIntoCallNodeTable): number {
if (this._callNodeInfo.isRoot(callNodeIndex)) {
return this._totalPerRootFunc[callNodeIndex];
}
return 0;
}
getTotal(callNodeIndex: IndexIntoCallNodeTable): number {
if (this._callNodeInfo.isRoot(callNodeIndex)) {
return this._totalPerRootFunc[callNodeIndex];
}
const { total } = this._getTotalAndHasChildren(callNodeIndex);
return total;
}
_getTotalAndHasChildren(
callNodeIndex: IndexIntoCallNodeTable
): TotalAndHasChildren {
if (this._callNodeInfo.isRoot(callNodeIndex)) {
throw new Error('This function should not be called for roots');
}
const cached = this._totalAndHasChildrenPerNonRootNode.get(callNodeIndex);
if (cached !== undefined) {
return cached;
}
const totalAndHasChildren = _getInvertedTreeNodeTotalAndHasChildren(
callNodeIndex,
this._callNodeInfo,
this._callNodeSelf
);
this._totalAndHasChildrenPerNonRootNode.set(
callNodeIndex,
totalAndHasChildren
);
return totalAndHasChildren;
}
findHeaviestPathInSubtree(
callNodeIndex: IndexIntoCallNodeTable
): CallNodePath {
const [rangeStart, rangeEnd] =
this._callNodeInfo.getSuffixOrderIndexRangeForCallNode(callNodeIndex);
const orderedCallNodes = this._callNodeInfo.getSuffixOrderedCallNodes();
// Find the non-inverted node with the highest self time.
let maxNode = -1;
let maxAbs = 0;
for (let i = rangeStart; i < rangeEnd; i++) {
const nodeIndex = orderedCallNodes[i];
const nodeSelf = Math.abs(this._callNodeSelf[nodeIndex]);
if (maxNode === -1 || nodeSelf > maxAbs) {
maxNode = nodeIndex;
maxAbs = nodeSelf;
}
}
const callPath = [];
for (
let currentNode = maxNode;
currentNode !== -1;
currentNode = this._callNodeTable.prefix[currentNode]
) {
callPath.push(this._callNodeTable.func[currentNode]);
}
return callPath;
}
}
export class CallTree {
_categories: CategoryList;
_internal: CallTreeInternal;
_callNodeInfo: CallNodeInfo;
_thread: Thread;
_previewFilteredCtssSamples: SamplesLikeTable;
_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,
previewFilteredCtssSamples: SamplesLikeTable,
internal: CallTreeInternal,
rootTotalSummary: number,
isHighPrecision: boolean,
weightType: WeightType
) {
this._categories = categories;
this._internal = internal;
this._callNodeInfo = callNodeInfo;
this._thread = thread;
this._previewFilteredCtssSamples = previewFilteredCtssSamples;
this._rootTotalSummary = rootTotalSummary;
this._displayDataByIndex = new Map();
this._children = [];
this._roots = internal.createRoots();
this._isHighPrecision = isHighPrecision;
this._weightType = weightType;
}
// The call tree's own order (by total sample count) is the intended order,
// so no columns are user-sortable.
getSortableColumns(): SortableColumn[] {
return [];
}
getTotal(): number {
return this._rootTotalSummary;
}
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<IndexIntoCallNodeTable>();
this._addDescendantsToSet(callNodeIndex, result);
return result;
}
getParent(
callNodeIndex: IndexIntoCallNodeTable
): IndexIntoCallNodeTable | -1 {
return this._callNodeInfo.prefixForNode(callNodeIndex);
}
getDepth(callNodeIndex: IndexIntoCallNodeTable): number {
return this._callNodeInfo.depthForNode(callNodeIndex);
}
getNodeData(callNodeIndex: IndexIntoCallNodeTable): CallNodeData {
const funcIndex = this._callNodeInfo.funcForNode(callNodeIndex);
const funcName = this._thread.stringTable.getString(
this._thread.funcTable.name[funcIndex]
);
const total = this._internal.getTotal(callNodeIndex);
const self = this._internal.getSelf(callNodeIndex);
const totalRelative = total / this._rootTotalSummary;
const selfRelative = self / this._rootTotalSummary;
return {
funcName,
total,
totalRelative,
self,
selfRelative,
};
}
_getInliningBadge(
callNodeIndex: IndexIntoCallNodeTable,
funcName: string
): ExtraBadgeInfo | undefined {
const calledFunction = getFunctionName(funcName);
const inlinedIntoNativeSymbol =
this._callNodeInfo.sourceFramesInlinedIntoSymbolForNode(callNodeIndex);
if (inlinedIntoNativeSymbol === -2) {
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._callNodeInfo.funcForNode(callNodeIndex);
const categoryIndex = this._callNodeInfo.categoryForNode(callNodeIndex);
const subcategoryIndex =
this._callNodeInfo.subcategoryForNode(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 === ResourceType.Webhost) {
icon = iconSrc = extractFaviconFromLibname(libName);
} else if (resourceType === ResourceType.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,
null,
this._thread.frameTable,
this._thread.funcTable,
this._thread.resourceTable,
this._thread.stringTable,
this._thread.sources,
this._thread.sourceLocationTable
);
}
getBottomBoxInfoForCallNode(
callNodeIndex: IndexIntoCallNodeTable
): BottomBoxInfo {
return getBottomBoxInfoForCallNode(
callNodeIndex,
this._callNodeInfo,
this._thread,
this._previewFilteredCtssSamples
);
}
/**
* Take a IndexIntoCallNodeTable, 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 self 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._callNodeInfo.depthForNode(callNodeIndex);
const partialPath = heaviestPath.slice(startingDepth);
return partialPath.reverse();
}
}
/**
* Compute the self time for each call node, and the sum of the absolute self
* values.
*/
export function computeCallNodeSelfAndSummary(
samples: SamplesLikeTable,
sampleIndexToCallNodeIndex: Array<null | IndexIntoCallNodeTable>,
callNodeCount: number
): CallNodeSelfAndSummary {
const callNodeSelf = new Float64Array(callNodeCount);
for (
let sampleIndex = 0;
sampleIndex < sampleIndexToCallNodeIndex.length;
sampleIndex++
) {
const callNodeIndex = sampleIndexToCallNodeIndex[sampleIndex];
if (callNodeIndex !== null) {
const weight = samples.weight ? samples.weight[sampleIndex] : 1;
callNodeSelf[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(callNodeSelf[callNodeIndex]);
}
return { callNodeSelf, rootTotalSummary };
}
export function getSelfAndTotalForCallNode(
callNodeIndex: IndexIntoCallNodeTable,
callNodeInfo: CallNodeInfo,
callTreeTimings: CallTreeTimings
): SelfAndTotal {
switch (callTreeTimings.type) {
case 'NON_INVERTED': {
const { timings } = callTreeTimings;
const self = timings.self[callNodeIndex];
const total = timings.total[callNodeIndex];
return { self, total };
}
case 'FUNCTION_LIST': {
const callNodeInfoInverted = ensureExists(callNodeInfo.asInverted());
const { timings } = callTreeTimings;
const { funcSelf, funcTotal } = timings;
if (!callNodeInfoInverted.isRoot(callNodeIndex)) {
throw new Error(
'When using function list timings, callNodeIndex must always refer to a function (an inverted root)'
);
}
return { self: funcSelf[callNodeIndex], total: funcTotal[callNodeIndex] };
}
case 'INVERTED': {
const callNodeInfoInverted = ensureExists(callNodeInfo.asInverted());
const { timings } = callTreeTimings;
const { callNodeSelf, totalPerRootFunc } = timings;
if (callNodeInfoInverted.isRoot(callNodeIndex)) {
const total = totalPerRootFunc[callNodeIndex];
return { self: total, total };
}
const { total } = _getInvertedTreeNodeTotalAndHasChildren(
callNodeIndex,
callNodeInfoInverted,
callNodeSelf
);
return { self: 0, total };
}
default:
throw assertExhaustiveCheck(
callTreeTimings as never,
'callTreeTimings.type'
);
}
}
function _getInvertedTreeNodeTotalAndHasChildren(
callNodeIndex: IndexIntoCallNodeTable,
callNodeInfo: CallNodeInfoInverted,
callNodeSelf: Float64Array
): TotalAndHasChildren {
const nodeDepth = callNodeInfo.depthForNode(callNodeIndex);
const [rangeStart, rangeEnd] =
callNodeInfo.getSuffixOrderIndexRangeForCallNode(callNodeIndex);
const suffixOrderedCallNodes = callNodeInfo.getSuffixOrderedCallNodes();
const callNodeTableDepthCol = callNodeInfo.getCallNodeTable().depth;
// Warning: This function can be quite confusing. That's because we are dealing
// with both inverted call nodes and non-inverted call nodes.
// `callNodeIndex` is a node in the *inverted* tree.
// The suffixOrderedCallNodes we iterate over below are nodes in the
// *non-inverted* tree.
// The total time of a node in the inverted tree is the sum of the self times
// of all the non-inverted nodes that contribute to the inverted node.
let total = 0;
let hasChildren = false;
for (let i = rangeStart; i < rangeEnd; i++) {
const selfNode = suffixOrderedCallNodes[i];
const self = callNodeSelf[selfNode];
total += self;
// The inverted call node has children if it has any inverted child nodes
// with non-zero total time. The total time of such an inverted child node
// is the sum of the self times of the non-inverted call nodes which
// contribute to it. Does `selfNode` contribute to one of our children?
// Maybe. To do so, it would need to describe a call path whose length is at
// least as long as the inverted call paths of our children - if not, it only
// contributes to `callNodeIndex` and not to our children.
// Rather than comparing the length of the call paths, we can just compare
// the depths.
//
// In other words:
// The inverted call node has children if any deeper call paths with non-zero
// self time contribute to it.
hasChildren =
hasChildren ||
(self !== 0 && callNodeTableDepthCol[selfNode] > nodeDepth);
}
return { total, hasChildren };
}
export function computeCallTreeTimingsInverted(
callNodeInfo: CallNodeInfoInverted,
{ callNodeSelf, rootTotalSummary }: CallNodeSelfAndSummary
): CallTreeTimingsInverted {
const funcCount = callNodeInfo.getFuncCount();
const callNodeTable = callNodeInfo.getCallNodeTable();
const callNodeTableFuncCol = callNodeTable.func;
const callNodeTableDepthCol = callNodeTable.depth;
const totalPerRootFunc = new Float64Array(funcCount);
const hasChildrenPerRootFunc = new Uint8Array(funcCount);
const seenPerRootFunc = new Uint8Array(funcCount);
const sortedRoots = [];
for (let i = 0; i < callNodeSelf.length; i++) {
const self = callNodeSelf[i];
if (self === 0) {
continue;
}
// Map the non-inverted call node to its corresponding root in the inverted
// call tree. This is done by finding the inverted root which corresponds to
// the self function of the non-inverted call node.
const func = callNodeTableFuncCol[i];
totalPerRootFunc[func] += self;
if (seenPerRootFunc[func] === 0) {
seenPerRootFunc[func] = 1;
sortedRoots.push(func);
}
if (callNodeTableDepthCol[i] !== 0) {
hasChildrenPerRootFunc[func] = 1;
}
}
sortedRoots.sort(
(a, b) => Math.abs(totalPerRootFunc[b]) - Math.abs(totalPerRootFunc[a])
);
return {
callNodeSelf,
rootTotalSummary,
sortedRoots,
totalPerRootFunc,
hasChildrenPerRootFunc,
};
}
export function computeCallTreeTimings(
callNodeInfo: CallNodeInfo,
callNodeSelfAndSummary: CallNodeSelfAndSummary
): CallTreeTimings {
const callNodeInfoInverted = callNodeInfo.asInverted();
if (callNodeInfoInverted !== null) {
return {
type: 'INVERTED',
timings: computeCallTreeTimingsInverted(
callNodeInfoInverted,
callNodeSelfAndSummary
),
};
}
return {
type: 'NON_INVERTED',
timings: computeCallTreeTimingsNonInverted(
callNodeInfo,
callNodeSelfAndSummary
),
};
}
/**
* This computes all of the count and timing information displayed in the
* regular (non-inverted) calltree.
*/
export function computeCallTreeTimingsNonInverted(
callNodeInfo: CallNodeInfo,
callNodeSelfAndSummary: CallNodeSelfAndSummary
): CallTreeTimingsNonInverted {
const callNodeTable = callNodeInfo.getCallNodeTable();
const { callNodeSelf, rootTotalSummary } = callNodeSelfAndSummary;
// Compute the following variables:
const callNodeTotal = new Float64Array(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--
) {
// callNodeTotal[callNodeIndex] is the sum of our children's totals.
// Compute this node's total by adding this node's self.
const total = callNodeTotal[callNodeIndex] + callNodeSelf[callNodeIndex];
const hasChildren = callNodeHasChildren[callNodeIndex] !== 0;
const hasTotalValue = total !== 0;
callNodeTotal[callNodeIndex] = total;
if (!hasChildren && !hasTotalValue) {
continue;
}
const prefixCallNode = callNodeTable.prefix[callNodeIndex];
if (prefixCallNode !== -1) {
callNodeTotal[prefixCallNode] += total;
callNodeHasChildren[prefixCallNode] = 1;
}
}
return {
self: callNodeSelf,
total: callNodeTotal,
callNodeHasChildren,
rootTotalSummary,
};
}
/**
* For each function in the func table, compute the sum of the sample weights
* of the samples which have that function somewhere in their stack.
*
* Example:
*
* 1 A -> B -> A
* 1 A -> B
* 1 B -> B
*
* funcTotal[A]: 2 (only the first two samples have A in them)
* funcTotal[B]: 3 (all three samples have B in them)
*
* We compute these totals by propagating totals upwards in the non-inverted
* call tree, and then on each node, also sum that node's total to the funcTotal
* for its function, but *only if this function is not present in the node's
* ancestors*.
*
* For the ancestor check we use the pre-computed `callNodeFuncIsDuplicate` bitset.
*
* - A (total: 2, self: 0, duplicate: no)
* - B (total: 2, self: 1, duplicate: no)
* - A (total: 1, self: 1, duplicate: yes)
* - B (total: 1, self: 0, duplicate: no)
* - B (total: 1, self: 1, duplicate: yes)
*/
function _computeFuncTotal(
callNodeTable: CallNodeTable,
callNodeFuncIsDuplicate: CallNodeTableBitSet,
callNodeSelf: Float64Array,
funcCount: number
): { funcTotal: Float64Array; sortedFuncs: IndexIntoFuncTable[] } {
const callNodeTableFuncCol = callNodeTable.func;
const callNodeTablePrefixCol = callNodeTable.prefix;
const callNodeCount = callNodeTable.length;
const callNodeChildrenTotal = new Float64Array(callNodeCount);
const funcTotal = new Float64Array(funcCount);
// The set of "functions with potentially non-zero totals", stored as an array.
const seenFuncs = [];
// seenPerFunc[func] stores whether seenFuncs.includes(func).
const seenPerFunc = new Uint8Array(funcCount);
// 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 = callNodeCount - 1;
callNodeIndex >= 0;
callNodeIndex--
) {
// callNodeChildrenTotal[callNodeIndex] is the sum of our children's totals.
// Compute this node's total by adding this node's self.
const total =
callNodeChildrenTotal[callNodeIndex] + callNodeSelf[callNodeIndex];
if (total === 0) {
continue;
}
const prefix = callNodeTablePrefixCol[callNodeIndex];
if (prefix !== -1) {
callNodeChildrenTotal[prefix] += total;
}
// Accumulate this node's total to the funcTotal for this node's func.
// But don't do it if this function is present in our ancestors! We don't
// want to count the sample twice if the function is present in the stack
// multiple times.
// We've already propagated this node's total to our parent node (via
// callNodeChildrenTotal[prefix]), so this node's total will keep propagating
// upwards. At some point it will hit the shallowest ancestor node
// with our function, where callNodeFuncIsDuplicate will be false, and we
// will accumulate our total into funcTotal there.
if (!checkBit(callNodeFuncIsDuplicate, callNodeIndex)) {
// We now know that func is not present in any of this node's ancestors.
const func = callNodeTableFuncCol[callNodeIndex];
funcTotal[func] += total;