forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile-data.ts
More file actions
4432 lines (4091 loc) · 146 KB
/
profile-data.ts
File metadata and controls
4432 lines (4091 loc) · 146 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 memoize from 'memoize-immutable';
import MixedTupleMap from 'mixedtuplemap';
import { oneLine } from 'common-tags';
import {
getEmptyRawStackTable,
getEmptyCallNodeTable,
shallowCloneFrameTable,
shallowCloneFuncTable,
} from './data-structures';
import {
CallNodeInfoNonInverted,
CallNodeInfoInverted,
} from './call-node-info';
import { computeThreadCPURatio } from './cpu';
import {
INSTANT,
INTERVAL,
INTERVAL_START,
INTERVAL_END,
} from 'firefox-profiler/app-logic/constants';
import { timeCode } from 'firefox-profiler/utils/time-code';
import { bisectionRight, bisectionLeft } from 'firefox-profiler/utils/bisect';
import {
type BitSet,
checkBit,
combineTwoBitSetsWithAnd,
makeBitSet,
setBit,
} from 'firefox-profiler/utils/bitset';
import { parseFileNameFromSymbolication } from 'firefox-profiler/utils/special-paths';
import {
ensureExists,
getFirstItemFromSet,
} from 'firefox-profiler/utils/types';
import {
numberSeriesFromDeltas,
numberSeriesToDeltas,
} from 'firefox-profiler/utils/number-series';
import type { StringTable } from 'firefox-profiler/utils/string-table';
import type {
Profile,
RawProfileSharedData,
RawThread,
Thread,
RawSamplesTable,
SamplesTable,
RawStackTable,
SampleUnits,
StackTable,
FrameTable,
FuncTable,
NativeSymbolTable,
ResourceTable,
CategoryList,
IndexIntoCategoryList,
IndexIntoFuncTable,
IndexIntoSamplesTable,
IndexIntoStackTable,
IndexIntoResourceTable,
IndexIntoNativeSymbolTable,
CallNodeTableBitSet,
ThreadIndex,
Category,
RawCounter,
Counter,
RawCounterSamplesTable,
CounterSamplesTable,
NativeAllocationsTable,
InnerWindowID,
BalancedNativeAllocationsTable,
IndexIntoFrameTable,
PageList,
CallNodeTable,
CallNodePath,
IndexIntoCallNodeTable,
AccumulatedCounterSamples,
SamplesLikeTable,
ProfileFilterPageData,
Milliseconds,
StartEndRange,
ImplementationFilter,
CallTreeSummaryStrategy,
EventDelayInfo,
ThreadsKey,
MarkerPayload,
Address,
AddressProof,
TimelineType,
NativeSymbolInfo,
Bytes,
FuncTableWithReservedFunctions,
TabID,
SourceTable,
IndexIntoSourceTable,
TransformOutput,
SampleCategoriesAndSubcategories,
} from 'firefox-profiler/types';
import { SelectedState, ResourceType } from 'firefox-profiler/types';
import type { CallNodeInfo, SuffixOrderIndex } from './call-node-info';
/**
* Various helpers for dealing with the profile as a data structure.
* @module profile-data
*/
/**
* Generate the non-inverted CallNodeInfo for a thread.
*/
export function getCallNodeInfo(
stackTable: StackTable,
frameTable: FrameTable,
defaultCategory: IndexIntoCategoryList
): CallNodeInfo {
const { callNodeTable, stackIndexToCallNodeIndex } = computeCallNodeTable(
stackTable,
frameTable,
defaultCategory
);
return new CallNodeInfoNonInverted(callNodeTable, stackIndexToCallNodeIndex);
}
// Return a column which represents a Map<IndexIntoFrameTable, IndexIntoNativeSymbolTable | -2>,
// with -2 meaning "not inlined".
// The reason we use -2 is that this matches what's used in the CallNodeTable,
// which also uses -2 to mean "not inlined" because it uses -1 to mean "divergent
// inlining", i.e. "this call node represents multiple stack nodes which differ
// in whether they were inlined or in which symbol they were inlined into".
function _computeFrameTableInlinedIntoColumn(
frameTable: FrameTable
): Int32Array {
const frameCount = frameTable.length;
const frameTableInlineDepthCol = frameTable.inlineDepth;
const frameTableNativeSymbolCol = frameTable.nativeSymbol;
const inlinedIntoCol = new Int32Array(frameCount);
for (let i = 0; i < frameCount; i++) {
let inlinedInto = -2;
if (frameTableInlineDepthCol[i] > 0) {
const nativeSymbol = frameTableNativeSymbolCol[i];
if (nativeSymbol !== null) {
inlinedInto = nativeSymbol;
}
}
inlinedIntoCol[i] = inlinedInto;
}
return inlinedIntoCol;
}
type CallNodeTableAndStackMap = {
callNodeTable: CallNodeTable;
// IndexIntoStackTable -> IndexIntoCallNodeTable
stackIndexToCallNodeIndex: Int32Array;
};
/**
* Generate the CallNodeTable, and a map to convert an IndexIntoStackTable to a
* IndexIntoCallNodeTable. This function runs through a stackTable, and
* de-duplicates stacks that have frames that point to the same function.
*
* See `src/types/profile-derived.js` for the type definitions.
* See `docs-developer/call-trees.md` for a detailed explanation of CallNodes.
*/
export function computeCallNodeTable(
stackTable: StackTable,
frameTable: FrameTable,
defaultCategory: IndexIntoCategoryList
): CallNodeTableAndStackMap {
if (stackTable.length === 0) {
return {
callNodeTable: getEmptyCallNodeTable(),
stackIndexToCallNodeIndex: new Int32Array(0),
};
}
const hierarchy = _computeCallNodeTableHierarchy(stackTable, frameTable);
const dfsOrder = _computeCallNodeTableDFSOrder(hierarchy);
const { stackIndexToCallNodeIndex } = dfsOrder;
const frameInlinedIntoCol = _computeFrameTableInlinedIntoColumn(frameTable);
const extraColumns = _computeCallNodeTableExtraColumns(
stackTable,
frameTable,
stackIndexToCallNodeIndex,
frameInlinedIntoCol,
hierarchy.length,
defaultCategory
);
const callNodeTable = {
prefix: dfsOrder.prefixSorted,
nextSibling: dfsOrder.nextSiblingSorted,
subtreeRangeEnd: dfsOrder.subtreeRangeEndSorted,
func: extraColumns.funcCol,
category: extraColumns.categoryCol,
subcategory: extraColumns.subcategoryCol,
innerWindowID: extraColumns.innerWindowIDCol,
sourceFramesInlinedIntoSymbol: extraColumns.inlinedIntoCol,
depth: dfsOrder.depthSorted,
maxDepth: dfsOrder.maxDepth,
length: hierarchy.length,
};
return {
callNodeTable,
stackIndexToCallNodeIndex,
};
}
/**
* The return type of _computeCallNodeTableHierarchy.
*
* This is an intermediate representation of the call node table, before we are
* fully done constructing it.
* At this point we are done with grouping stacks into call nodes.
* But we haven't put the call nodes in the final order yet.
*/
type CallNodeTableHierarchy = {
prefix: Array<IndexIntoCallNodeTable>;
firstChild: Array<IndexIntoFuncTable>;
nextSibling: Array<IndexIntoFuncTable>;
length: number;
stackIndexToCallNodeIndex: Int32Array;
};
/**
* The return type of _computeCallNodeTableDFSOrder.
*
* The values in these columns are in the final order in which they'll be in the
* actual call node table. DFS here means "depth-first search".
*/
type CallNodeTableDFSOrder = {
length: number;
stackIndexToCallNodeIndex: Int32Array;
nextSiblingSorted: Int32Array;
subtreeRangeEndSorted: Uint32Array;
prefixSorted: Int32Array;
depthSorted: Int32Array;
maxDepth: number;
};
/**
* The return type of _computeCallNodeTableExtraColumns.
*
* We compute these columns once we know the final size and order of the call
* node table.
*/
type CallNodeTableExtraColumns = {
funcCol: Int32Array; // IndexIntoCallNodeTable -> IndexIntoFuncTable
categoryCol: Int32Array; // IndexIntoCallNodeTable -> IndexIntoCategoryList
subcategoryCol: Int32Array; // IndexIntoCallNodeTable -> IndexIntoSubcategoryListForCategory
innerWindowIDCol: Float64Array; // IndexIntoCallNodeTable -> InnerWindowID
inlinedIntoCol: Int32Array; // IndexIntoCallNodeTable -> IndexIntoNativeSymbolTable | -1 | -2
};
/**
* Used as part of creating the call node table.
*
* This function groups stacks into call nodes, by mapping sibling stack nodes
* to the same call node if they have the same func.
*
* This function also builds up three columns which describe the tree structure
* of the call node table: prefix, firstChild, and nextSibling. The tree
* structure represented by those columns only has a very basic property, which
* is "a prefix always comes before its children".
*
* This function does not compute the other columns yet, because at this point
* we don't know the final order of the call nodes. And we want to store those
* other values in typed arrays, for which we need to know the size upfront, and
* this function only knows the number of call nodes once it's finished.
*/
function _computeCallNodeTableHierarchy(
stackTable: StackTable,
frameTable: FrameTable
): CallNodeTableHierarchy {
const stackIndexToCallNodeIndex = new Int32Array(stackTable.length);
// The callNodeTable components.
const prefix: Array<IndexIntoCallNodeTable> = [];
const firstChild: Array<IndexIntoCallNodeTable> = [];
const nextSibling: Array<IndexIntoCallNodeTable> = [];
const func: Array<IndexIntoFuncTable> = [];
let length = 0;
// An extra column that only gets used while the table is built up: For each
// node A, currentLastChild[A] tracks the last currently-known child node of A.
// It is updated whenever a new node is created; e.g. creating node B updates
// currentLastChild[prefix[B]].
// currentLastChild[A] is -1 while A has no children.
const currentLastChild: Array<IndexIntoCallNodeTable> = [];
// The last currently-known root node, i.e. the last known "child of -1".
let currentLastRoot = -1;
// Go through each stack, and create a new callNode table, which is based off of
// functions rather than frames.
for (let stackIndex = 0; stackIndex < stackTable.length; stackIndex++) {
const prefixStack = stackTable.prefix[stackIndex];
// We know that at this point the following condition holds:
// assert(prefixStack === null || prefixStack < stackIndex);
const prefixCallNode =
prefixStack === null ? -1 : stackIndexToCallNodeIndex[prefixStack];
const frameIndex = stackTable.frame[stackIndex];
const funcIndex = frameTable.func[frameIndex];
// Check if the call node for this stack already exists.
let callNodeIndex = -1;
if (stackIndex !== 0) {
const currentFirstSibling =
prefixCallNode === -1 ? 0 : firstChild[prefixCallNode];
for (
let currentSibling = currentFirstSibling;
currentSibling !== -1;
currentSibling = nextSibling[currentSibling]
) {
if (func[currentSibling] === funcIndex) {
callNodeIndex = currentSibling;
break;
}
}
}
if (callNodeIndex !== -1) {
stackIndexToCallNodeIndex[stackIndex] = callNodeIndex;
continue;
}
// New call node.
callNodeIndex = length++;
stackIndexToCallNodeIndex[stackIndex] = callNodeIndex;
prefix[callNodeIndex] = prefixCallNode;
func[callNodeIndex] = funcIndex;
// Initialize these firstChild and nextSibling to -1. They will be updated
// once this node's first child or next sibling gets created.
firstChild[callNodeIndex] = -1;
nextSibling[callNodeIndex] = -1;
currentLastChild[callNodeIndex] = -1;
// Update the next sibling of our previous sibling, and the first child of
// our prefix (if we're the first child).
// Also set this node's depth.
if (prefixCallNode === -1) {
// This node is a root. Just update the previous root's nextSibling. Because
// this node has no parent, there's also no firstChild information to update.
if (currentLastRoot !== -1) {
nextSibling[currentLastRoot] = callNodeIndex;
}
currentLastRoot = callNodeIndex;
} else {
// This node is not a root: update both firstChild and nextSibling information
// when appropriate.
const prevSiblingIndex = currentLastChild[prefixCallNode];
if (prevSiblingIndex === -1) {
// This is the first child for this prefix.
firstChild[prefixCallNode] = callNodeIndex;
} else {
nextSibling[prevSiblingIndex] = callNodeIndex;
}
currentLastChild[prefixCallNode] = callNodeIndex;
}
}
return {
prefix,
firstChild,
nextSibling,
length,
stackIndexToCallNodeIndex,
};
}
/**
* Used as part of creating the call node table. This function computes the
* final order of call nodes, and returns columns which describe the tree
* structure with that final order, i.e. in DFS order. DFS here means
* "depth-first search":
*
* - If a node A has children, its first child B directly follows A.
* - Otherwise, the node following A is A's next sibling (if it has one), or
* the next sibling of the closest ancestor which has a next sibling.
*
* This means that for any node, the node and all its descendants are laid out
* contiguously. This contiguous chunk is described by the `subtreeRangeEnd`
* column and allows other parts of the codebase to perform cheap "is descendant"
* checks.
*
* We do not order siblings by func. The order of siblings is meaningless, and
* is based on the somewhat arbitrary order in which we encounter the original
* stack nodes in the stack table.
*/
function _computeCallNodeTableDFSOrder(
hierarchy: CallNodeTableHierarchy
): CallNodeTableDFSOrder {
const { prefix, firstChild, nextSibling, length, stackIndexToCallNodeIndex } =
hierarchy;
const prefixSorted = new Int32Array(length);
const nextSiblingSorted = new Int32Array(length);
const subtreeRangeEndSorted = new Uint32Array(length);
const depthSorted = new Int32Array(length);
let maxDepth = 0;
if (length === 0) {
return {
prefixSorted,
subtreeRangeEndSorted,
nextSiblingSorted,
depthSorted,
maxDepth,
length,
stackIndexToCallNodeIndex,
};
}
// Traverse the entire tree, as follows:
// 1. nextOldIndex is the next node in DFS order. Copy over all values from
// the unsorted columns into the sorted columns.
// 2. Find the next node in DFS order, set nextOldIndex to it, and continue
// to the next loop iteration.
const oldIndexToNewIndex = new Uint32Array(length);
let nextOldIndex = 0;
let nextNewIndex = 0;
let currentDepth = 0;
let currentOldPrefix = -1;
let currentNewPrefix = -1;
while (nextOldIndex !== -1) {
const oldIndex = nextOldIndex;
const newIndex = nextNewIndex;
oldIndexToNewIndex[oldIndex] = newIndex;
nextNewIndex++;
prefixSorted[newIndex] = currentNewPrefix;
depthSorted[newIndex] = currentDepth;
// The remaining two columns, nextSiblingSorted and subtreeRangeEndSorted,
// will be filled in when we get to the end of the current subtree.
// Find the next index in DFS order: If we have children, then our first child
// is next. Otherwise, we need to advance to our next sibling, if we have one,
// otherwise to the next sibling of the first ancestor which has one.
const oldFirstChild = firstChild[oldIndex];
if (oldFirstChild !== -1) {
// We have children. Our first child is the next node in DFS order.
currentOldPrefix = oldIndex;
currentNewPrefix = newIndex;
nextOldIndex = oldFirstChild;
currentDepth++;
if (currentDepth > maxDepth) {
maxDepth = currentDepth;
}
continue;
}
// We have no children. The next node is the next sibling of this node or
// of an ancestor node. Now is also a good time to fill in the values for
// subtreeRangeEnd and nextSibling.
subtreeRangeEndSorted[newIndex] = nextNewIndex;
nextOldIndex = nextSibling[oldIndex];
nextSiblingSorted[newIndex] = nextOldIndex === -1 ? -1 : nextNewIndex;
while (nextOldIndex === -1 && currentOldPrefix !== -1) {
subtreeRangeEndSorted[currentNewPrefix] = nextNewIndex;
const oldPrefixNextSibling = nextSibling[currentOldPrefix];
nextSiblingSorted[currentNewPrefix] =
oldPrefixNextSibling === -1 ? -1 : nextNewIndex;
nextOldIndex = oldPrefixNextSibling;
currentOldPrefix = prefix[currentOldPrefix];
currentNewPrefix = prefixSorted[currentNewPrefix];
currentDepth--;
}
}
for (let i = 0; i < stackIndexToCallNodeIndex.length; i++) {
stackIndexToCallNodeIndex[i] =
oldIndexToNewIndex[stackIndexToCallNodeIndex[i]];
}
return {
prefixSorted,
subtreeRangeEndSorted,
nextSiblingSorted,
depthSorted,
maxDepth,
length,
stackIndexToCallNodeIndex,
};
}
/**
* Used as part of creating the call node table.
*
* This function computes the remaining columns that haven't been computed by
* any other parts of call node table creation.
*
* We only compute these columns once we know the final size and order of the
* call node table, so that we can immediately put values in the right spot in
* the fixed-size typed array columns.
*/
function _computeCallNodeTableExtraColumns(
stackTable: StackTable,
frameTable: FrameTable,
stackIndexToCallNodeIndex: Int32Array,
frameTableInlinedIntoCol: Int32Array,
callNodeCount: number,
defaultCategory: IndexIntoCategoryList
): CallNodeTableExtraColumns {
const stackCount = stackTable.length;
const stackTableCategoryCol = stackTable.category;
const stackTableFrameCol = stackTable.frame;
const stackTableSubcategoryCol = stackTable.subcategory;
const frameTableInnerWindowIDCol = frameTable.innerWindowID;
const frameTableFuncCol = frameTable.func;
const funcCol = new Int32Array(callNodeCount);
const categoryCol = new Int32Array(callNodeCount);
const subcategoryCol = new Int32Array(callNodeCount);
const innerWindowIDCol = new Float64Array(callNodeCount);
const inlinedIntoCol = new Int32Array(callNodeCount);
const haveFilled = new Uint8Array(callNodeCount);
for (let stackIndex = 0; stackIndex < stackCount; stackIndex++) {
const category = stackTableCategoryCol[stackIndex];
const subcategory = stackTableSubcategoryCol[stackIndex];
const frameIndex = stackTableFrameCol[stackIndex];
const inlinedIntoSymbol = frameTableInlinedIntoCol[frameIndex];
const callNodeIndex = stackIndexToCallNodeIndex[stackIndex];
if (haveFilled[callNodeIndex] === 0) {
funcCol[callNodeIndex] = frameTableFuncCol[frameIndex];
categoryCol[callNodeIndex] = category;
subcategoryCol[callNodeIndex] = subcategory;
inlinedIntoCol[callNodeIndex] = inlinedIntoSymbol;
const innerWindowID = frameTableInnerWindowIDCol[frameIndex];
if (innerWindowID !== null && innerWindowID !== 0) {
// Set innerWindowID when it's not zero. Otherwise the value is already
// zero because typed arrays are initialized to zero.
innerWindowIDCol[callNodeIndex] = innerWindowID;
}
haveFilled[callNodeIndex] = 1;
} else {
// Resolve category conflicts, by resetting a conflicting subcategory or
// category to the default category.
if (categoryCol[callNodeIndex] !== category) {
// Conflicting origin stack categories -> default category + subcategory.
categoryCol[callNodeIndex] = defaultCategory;
subcategoryCol[callNodeIndex] = 0;
} else if (subcategoryCol[callNodeIndex] !== subcategory) {
// Conflicting origin stack subcategories -> "Other" subcategory.
subcategoryCol[callNodeIndex] = 0;
}
// Resolve "inlined into" conflicts. This can happen if you have two
// function calls A -> B where only one of the B calls is inlined, or
// if you use call tree transforms in such a way that a function B which
// was inlined into two different callers (A -> B, C -> B) gets collapsed
// into one call node.
if (inlinedIntoCol[callNodeIndex] !== inlinedIntoSymbol) {
// Conflicting inlining: -1.
inlinedIntoCol[callNodeIndex] = -1;
}
}
}
return {
funcCol,
categoryCol,
subcategoryCol,
innerWindowIDCol,
inlinedIntoCol,
};
}
/**
* Generate the inverted CallNodeInfo for a thread.
*/
export function getInvertedCallNodeInfo(
callNodeInfo: CallNodeInfo,
defaultCategory: IndexIntoCategoryList,
funcCount: number
): CallNodeInfoInverted {
return new CallNodeInfoInverted(
callNodeInfo.getCallNodeTable(),
callNodeInfo.getStackIndexToNonInvertedCallNodeIndex(),
defaultCategory,
funcCount
);
}
// Compare two non-inverted call nodes in "suffix order".
// The suffix order is defined as the lexicographical order of the inverted call
// path, or, in other words, the "backwards" lexicographical order of the
// non-inverted call paths.
//
// Example of some suffix ordered non-inverted call paths:
// [0]
// [0, 0]
// [2, 0]
// [4, 5, 1]
// [4, 5]
function _compareNonInvertedCallNodesInSuffixOrder(
callNodeA: IndexIntoCallNodeTable,
callNodeB: IndexIntoCallNodeTable,
callNodeTable: CallNodeTable
): number {
// Walk up both and stop at the first non-matching function.
// Walking up the non-inverted tree is equivalent to walking down the
// inverted tree.
while (true) {
const funcA = callNodeTable.func[callNodeA];
const funcB = callNodeTable.func[callNodeB];
if (funcA !== funcB) {
return funcA - funcB;
}
callNodeA = callNodeTable.prefix[callNodeA];
callNodeB = callNodeTable.prefix[callNodeB];
if (callNodeA === callNodeB) {
break;
}
if (callNodeA === -1) {
return -1;
}
if (callNodeB === -1) {
return 1;
}
}
return 0;
}
// Given a stack index `needleStack` and a call node in the inverted tree
// `invertedCallTreeNode`, find an ancestor stack of `needleStack` which
// corresponds to the given call node in the inverted call tree. Returns null if
// there is no such ancestor stack.
//
// Also returns null for any stacks which aren't used as self stacks.
//
// Note: This function doesn't actually have a parameter named `invertedCallTreeNode`.
// Instead, it has two parameters for the node's suffix order index range. This
// range is obtained by the caller and is enough to check whether a stack's call
// path ends with the path suffix represented by the inverted call node. The caller
// gets the suffix order index range as follows:
//
// ```
// const [rangeStart, rangeEnd] =
// callNodeInfo.getSuffixOrderIndexRangeForCallNode(callNodeIndex);
// ```
//
// Example:
//
// Stack table (`<func>:<line>`): Inverted call tree:
//
// - A:10 - A
// - B:20 - B
// - C:30 - A
// - C:31 - C
// - B:21 - B
// - A
//
// In this example, given the inverted tree call node C <- B and the needle
// stack A:10 -> B:20 -> C:30, the function will return the stack A:10 -> B:20.
//
// For example, if you double click the call node C <- B in the inverted tree,
// and if all samples spend their time in the stack A:10 -> B:20 -> C:30, then
// the source view should be scrolled to line 20.
//
// Background: needleStack has some self time. This self time shows up in a
// root node of the inverted tree. If you go to needleStack's prefix stack, i.e.
// if you go "up" a level in the non-inverted stack table, you go "down" a level
// in the inverted call tree. We want to go up/down enough so that we hit our
// call node. This gives us a stack node whose frame's func is the same as the
// func of `invertedCallTreeNode`. Then our caller can get some information from
// that frame, for example the frame's address or line.
export function getMatchingAncestorStackForInvertedCallNode(
needleStack: IndexIntoStackTable,
suffixOrderIndexRangeStart: SuffixOrderIndex,
suffixOrderIndexRangeEnd: SuffixOrderIndex,
suffixOrderIndexes: Uint32Array,
invertedTreeCallNodeDepth: number,
stackIndexToCallNodeIndex: Int32Array,
stackTablePrefixCol: Array<IndexIntoStackTable | null>
): IndexIntoStackTable | null {
// Get the non-inverted call tree node for the (non-inverted) stack.
// For example, if the stack has the call path A -> B -> C,
// this will give us the node A -> B -> C in the non-inverted tree.
const needleCallNode = stackIndexToCallNodeIndex[needleStack];
const needleSuffixOrderIndex = suffixOrderIndexes[needleCallNode];
// Check if needleCallNode's call path ends with the call path suffix represented
// by the inverted call node.
if (
needleSuffixOrderIndex >= suffixOrderIndexRangeStart &&
needleSuffixOrderIndex < suffixOrderIndexRangeEnd
) {
// Yes, needleCallNode's call path ends with the call path suffix represented
// by the inverted call node.
// For example, if our node is C <- B in the inverted tree, and needleStack has the
// non-inverted call path A -> B -> C, then we now know that A -> B -> C ends
// with B -> C.
// Now we strip off this suffix. In the example, invertedTreeCallNodeDepth is 1
// so we strip off "-> C" at the end and return a stack for A -> B.
return getNthPrefixStack(
needleStack,
invertedTreeCallNodeDepth,
stackTablePrefixCol
);
}
// The stack's call path doesn't end with the suffix we were looking for; return null.
return null;
}
/**
* Returns the n'th prefix of a stack, or null if it doesn't exist.
* (n = 0: the node itself, n = 1: the immediate parent node,
* n = 2: the grandparent, etc)
*/
export function getNthPrefixStack(
stackIndex: IndexIntoStackTable | null,
n: number,
stackTablePrefixCol: Array<IndexIntoStackTable | null>
): IndexIntoStackTable | null {
let s = stackIndex;
for (let i = 0; i < n && s !== null; i++) {
s = stackTablePrefixCol[s];
}
return s;
}
/**
* Given a call node `callNodeIndex`, answer, for each stack S:
* - Does a sample with stack S contribute to `callNodeIndex`'s total time?
* - If so, which of `callNodeIndex`'s frames does such a sample contribute its
* total time to?
*
* If the answer to the first question is "no", we put frame index -1 into the
* returned array for that stack index.
*/
export function getCallNodeFramePerStack(
callNodeIndex: IndexIntoCallNodeTable,
callNodeInfo: CallNodeInfo,
stackTable: StackTable
): Int32Array {
const callNodeInfoInverted = callNodeInfo.asInverted();
return callNodeInfoInverted !== null
? getCallNodeFramePerStackInverted(
callNodeIndex,
callNodeInfoInverted,
stackTable
)
: getCallNodeFramePerStackNonInverted(
callNodeIndex,
callNodeInfo,
stackTable
);
}
/**
* This function handles the non-inverted case of getCallNodeFramePerStack.
*
* Gathers the frames which are hit in a given call node by each stack,
* or -1 if the stack isn't in the call node's subtree.
*
* This is best explained with an example.
* Let the call node be the node for the call path [A, B, C].
* Let this be the stack tree:
*
* - stack 0, func A, frame 100
* - stack 1, func B, frame 110
* - stack 2, func C, frame 120
* - stack 3, func C, frame 130
* - stack 4, func B, frame 140
* - stack 5, func C, frame 150
* - stack 6, func C, frame 160
* - stack 7, func D, frame 170
* - stack 8, func E, frame 180
* - stack 9, func F, frame 190
*
* This maps to the following call tree:
*
* - call node 0, func A
* - call node 1, func B
* - call node 2, func C
* - call node 3, func D
* - call node 4, func E
* - call node 5, func F
*
* The call path [A, B, C] uniquely identifies call node 2.
* The following stacks all "collapse into" ("map to") call node 2:
* stack 2, 3, 5 and 6.
* Stack 7 maps to call node 3, which is a child of call node 2.
* Stacks 0, 1, 4, 8 and 9 are outside the call path [A, B, C].
*
* Stacks 2, 3, 4 and 5 all make a "total time" contribution to call
* node 2, to the frames 120, 130, 150, and 160, respectively.
* Stack 7 also contributes total time to call node 2, to frame 160.
* Stacks 0, 1, 4, 8 and 9 don't contribute to call node 2's total time.
*
* So this function returns the following array in the example:
* new Int32Array([-1, -1, 120, 130, -1, 150, 160, 160, -1, -1])
* // for stacks 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
*/
export function getCallNodeFramePerStackNonInverted(
callNodeIndex: IndexIntoCallNodeTable,
callNodeInfo: CallNodeInfo,
stackTable: StackTable
): Int32Array {
const stackIndexToCallNodeIndex =
callNodeInfo.getStackIndexToNonInvertedCallNodeIndex();
const { frame: frameCol, prefix: prefixCol, length: stackCount } = stackTable;
const callNodeFramePerStack = new Int32Array(stackCount);
// This loop takes advantage of the stack table's ordering:
// Prefix stacks are always visited before their descendants.
for (let stackIndex = 0; stackIndex < stackCount; stackIndex++) {
let frame = -1;
const callNodeForThisStack = stackIndexToCallNodeIndex[stackIndex];
if (callNodeForThisStack === callNodeIndex) {
frame = frameCol[stackIndex];
} else {
// We're either already in the call node's subtree, or we are
// outside the subtree. Either way, we can just inherit the frame
// that our prefix stack hits in this call node.
const prefix = prefixCol[stackIndex];
if (prefix !== null) {
frame = callNodeFramePerStack[prefix];
}
}
callNodeFramePerStack[stackIndex] = frame;
}
return callNodeFramePerStack;
}
/**
* This handles the inverted case of getCallNodeFramePerStack.
*/
export function getCallNodeFramePerStackInverted(
callNodeIndex: IndexIntoCallNodeTable,
callNodeInfo: CallNodeInfoInverted,
stackTable: StackTable
): Int32Array {
const depth = callNodeInfo.depthForNode(callNodeIndex);
const [rangeStart, rangeEnd] =
callNodeInfo.getSuffixOrderIndexRangeForCallNode(callNodeIndex);
const stackIndexToCallNodeIndex =
callNodeInfo.getStackIndexToNonInvertedCallNodeIndex();
const stackTablePrefixCol = stackTable.prefix;
const suffixOrderIndexes = callNodeInfo.getSuffixOrderIndexes();
const callNodeFramePerStack = new Int32Array(stackTable.length);
for (let stackIndex = 0; stackIndex < stackTable.length; stackIndex++) {
let callNodeFrame = -1;
// Get the non-inverted call tree node for the (non-inverted) stack.
// For example, if the stack has the call path A -> B -> C,
// this will give us the node A -> B -> C in the non-inverted tree.
const thisStackCallNode = stackIndexToCallNodeIndex[stackIndex];
const thisStackSuffixOrderIndex = suffixOrderIndexes[thisStackCallNode];
if (
thisStackSuffixOrderIndex >= rangeStart &&
thisStackSuffixOrderIndex < rangeEnd
) {
const stackForCallNode = getNthPrefixStack(
stackIndex,
depth,
stackTablePrefixCol
);
if (stackForCallNode !== null) {
callNodeFrame = stackTable.frame[stackForCallNode];
}
}
callNodeFramePerStack[stackIndex] = callNodeFrame;
}
return callNodeFramePerStack;
}
/**
* Take a samples table, and return an array that contain indexes that point to the
* leaf most call node, or null.
*/
export function getSampleIndexToCallNodeIndex(
stacks: Array<IndexIntoStackTable | null>,
stackIndexToCallNodeIndex: {
[key: IndexIntoStackTable]: IndexIntoCallNodeTable;
}
): Array<IndexIntoCallNodeTable | null> {
return stacks.map((stack) => {
return stack === null ? null : stackIndexToCallNodeIndex[stack];
});
}
/**
* This is an implementation of getSampleSelectedStates for just the case where
* no call node is selected.
*/
function _getSampleSelectedStatesForNoSelection(
sampleCallNodes: Array<IndexIntoCallNodeTable | null>
): Uint8Array {
const result = new Uint8Array(sampleCallNodes.length);
for (
let sampleIndex = 0;
sampleIndex < sampleCallNodes.length;
sampleIndex++
) {
// When there's no selected call node, we don't want to shadow everything
// because everything is unselected. So let's pretend that
// everything is selected so that anything not filtered out will be nicely
// visible.
let sampleSelectedState = SelectedState.Selected;
// But we still want to display filtered-out samples differently.
const callNodeIndex = sampleCallNodes[sampleIndex];
if (callNodeIndex === null) {
sampleSelectedState = SelectedState.FilteredOutByTransform;
}
result[sampleIndex] = sampleSelectedState;
}
return result;
}
/**
* Given the call node for each sample and the selected call node,
* compute each sample's selected state.
*
* For samples that are not filtered out, the sample's selected state is based
* on the relation of the sample's call node to the selected call node: Any call
* nodes in the selected node's subtree are "selected"; all other nodes are
* either "before" or "after" the selected subtree.
*
* Call node tables are ordered in depth-first traversal order, so we can
* determine whether a node is before, inside or after a subtree simply by
* comparing the call node index to the "selected index range". Example:
*
* ```
* before, 0
* before, 1
* before, 2
* before, 3
* before, 4
* before, 5
* before, 6
* before, 7
* before, 8
* before, 9
* before, 10
* before, 11
* before, 12
* selected, 13 <-- selected node
* selected, 14
* selected, 15
* selected, 16
* selected, 17
* selected, 18
* selected, 19
* selected, 20
* after, 21
* after, 22
* after, 23
* after, 24
* after, 25
* after, 26
* after, 27
* ```
*
* In this example, the selected node has index 13 and the "selected index range"
* is the range from 13 to 21 (not including 21).
*/
function _getSampleSelectedStatesNonInverted(
sampleCallNodes: Array<IndexIntoCallNodeTable | null>,
selectedCallNodeIndex: IndexIntoCallNodeTable,
callNodeInfo: CallNodeInfo
): Uint8Array {
const callNodeTable = callNodeInfo.getCallNodeTable();
const selectedCallNodeDescendantsEndIndex =
callNodeTable.subtreeRangeEnd[selectedCallNodeIndex];
const sampleCount = sampleCallNodes.length;
const sampleSelectedStates = new Uint8Array(sampleCount);
for (let sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++) {
let sampleSelectedState: SelectedState = SelectedState.Selected;
const callNodeIndex = sampleCallNodes[sampleIndex];
if (callNodeIndex !== null) {
if (callNodeIndex < selectedCallNodeIndex) {
sampleSelectedState = SelectedState.UnselectedOrderedBeforeSelected;
} else if (callNodeIndex < selectedCallNodeDescendantsEndIndex) {
sampleSelectedState = SelectedState.Selected;
} else {
sampleSelectedState = SelectedState.UnselectedOrderedAfterSelected;