forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-tracer.js
1035 lines (946 loc) · 35.7 KB
/
js-tracer.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
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/. */
// @flow
import {
getEmptyFrameTable,
getEmptyStackTable,
getEmptySamplesTableWithEventDelay,
getEmptyRawMarkerTable,
} from './data-structures';
import { ensureExists } from '../utils/flow';
import type {
JsTracerTable,
IndexIntoStringTable,
IndexIntoJsTracerEvents,
IndexIntoFuncTable,
Thread,
IndexIntoStackTable,
SamplesTable,
CategoryList,
JsTracerTiming,
Microseconds,
} from 'firefox-profiler/types';
import type { UniqueStringArray } from '../utils/unique-string-array';
import type { JsImplementation } from '../profile-logic/profile-data';
// See the function below for more information.
type ScriptLocationToFuncIndex = Map<string, IndexIntoFuncTable | null>;
/**
* Create a map that keys off of the string `${fileName}:${line}:${column}`. This maps
* the JS tracer script locations to functions in the profiling data structure.
* This operation can fail, as there is no guarantee that every location in the JS
* tracer information was sampled.
*/
function getScriptLocationToFuncIndex(
thread: Thread
): ScriptLocationToFuncIndex {
const { funcTable, stringTable } = thread;
const scriptLocationToFuncIndex = new Map();
for (let funcIndex = 0; funcIndex < funcTable.length; funcIndex++) {
if (!funcTable.isJS[funcIndex]) {
continue;
}
const line = funcTable.lineNumber[funcIndex];
const column = funcTable.columnNumber[funcIndex];
const fileNameIndex = funcTable.fileName[funcIndex];
if (column !== null && line !== null && fileNameIndex !== null) {
const fileName = stringTable.getString(fileNameIndex);
const key = `${fileName}:${line}:${column}`;
if (scriptLocationToFuncIndex.has(key)) {
// Multiple functions map to this script location.
scriptLocationToFuncIndex.set(key, null);
} else {
scriptLocationToFuncIndex.set(key, funcIndex);
}
}
}
return scriptLocationToFuncIndex;
}
/**
* This function is very similar in implementation as getStackTimingByDepth.
* It creates a list of JsTracerTiming entries that represent the underlying
* tree structure of the tracing data. This is then used by the JS Tracer panel
* to render the chart to the screen.
*
* The data looks visually something like this:
*
* [A------------------------]
* [B------][F----] [H--]
* [C---] [G]
* [D][E]
*
* Where a single row, like B F H, would be one JsTracerTiming.
*/
export function getJsTracerTiming(
jsTracer: JsTracerTable,
thread: Thread
): JsTracerTiming[] {
const jsTracerTiming: JsTracerTiming[] = [];
const { stringTable, funcTable } = thread;
// This has already been computed by the conversion of the JS tracer structure to
// a thread, but it's probably not worth the complexity of caching this object.
// Just recompute it.
const scriptLocationToFuncIndex = getScriptLocationToFuncIndex(thread);
// Go through all of the events.
for (
let tracerEventIndex = 0;
tracerEventIndex < jsTracer.length;
tracerEventIndex++
) {
const stringIndex = jsTracer.events[tracerEventIndex];
const column = jsTracer.column[tracerEventIndex];
const line = jsTracer.line[tracerEventIndex];
// By default we use the display name from JS tracer, but we may update it if
// we can figure out more information about it.
let displayName = stringTable.getString(stringIndex);
// We may have deduced the funcIndex in the scriptLocationToFuncIndex Map.
let funcIndex: null | IndexIntoFuncTable = null;
if (column !== null && line !== null) {
// There is both a column and line number for this script. Look up to see if this
// script location has a function in the sampled data. This is a simple way
// to tie together the JS tracer information with the Gecko profiler's stack
// walking.
const scriptLocation = `${displayName}:${line}:${column}`;
const funcIndexInMap = scriptLocationToFuncIndex.get(scriptLocation);
if (funcIndexInMap !== undefined) {
if (funcIndexInMap === null) {
// This is probably a failure case in our source information.
displayName = `(multiple matching functions) ${displayName}`;
} else {
// Update the information with the function that was found.
funcIndex = funcIndexInMap;
displayName = `ƒ ${stringTable.getString(
funcTable.name[funcIndex]
)} ${displayName}`;
}
}
}
// Place the event in the closest row that is empty.
for (let i = 0; i <= jsTracerTiming.length; i++) {
// Get or create a row.
let timingRow = jsTracerTiming[i];
if (!timingRow) {
timingRow = {
start: [],
end: [],
index: [],
label: [],
func: [],
name: 'Tracing Information',
length: 0,
};
jsTracerTiming.push(timingRow);
}
// The timing is converted here from Microseconds to Milliseconds.
const start = jsTracer.timestamps[tracerEventIndex] / 1000;
const durationRaw = jsTracer.durations[tracerEventIndex];
const duration = durationRaw === null ? 0 : durationRaw / 1000;
// Since the events are sorted, look at the last added event in this row. If
// the new event fits, go ahead and insert it.
const otherEnd = timingRow.end[timingRow.length - 1];
if (otherEnd === undefined || otherEnd <= start) {
timingRow.start.push(start);
timingRow.end.push(start + duration);
timingRow.label.push(displayName);
timingRow.index.push(tracerEventIndex);
timingRow.func.push(funcIndex);
timingRow.length++;
break;
}
}
}
return jsTracerTiming;
}
/**
* Determine the self time for JS Tracer events. This generates a row of timing
* information for each type event. This function walks the stack structure of JS Tracer
* events, and determines what's actually executing at a given time.
*
* The data visually would look something like this:
*
* A: [----] [-----]
* B: [-----]
* C: [-] [--]
* D: [----]
*
* Where "A" would be the name of the event, and the boxes in that row would be the self
* time. Each row is stored as a JsTracerTiming.
*/
export function getJsTracerLeafTiming(
jsTracer: JsTracerTable,
stringTable: UniqueStringArray
): JsTracerTiming[] {
// Each event type will have it's own timing information, later collapse these into
// a single array.
const jsTracerTimingMap: Map<string, JsTracerTiming> = new Map();
const isUrlCache = [];
const isUrlRegex = /:\/\//;
function isUrl(index: IndexIntoStringTable): boolean {
const cachedIsUrl = isUrlCache[index];
if (cachedIsUrl !== undefined) {
return cachedIsUrl;
}
const booleanValue = isUrlRegex.test(stringTable.getString(index));
isUrlCache[index] = booleanValue;
return booleanValue;
}
// This function reports self time in the correct row of JsTracerTiming in
// jsTracerTimingMap, based on the event that is passed to it.
function reportSelfTime(
tracerEventIndex: number,
start: Microseconds,
end: Microseconds
): void {
if (start === end) {
// If this self time is 0, do not report it.
return;
}
const stringIndex = jsTracer.events[tracerEventIndex];
const displayName = stringTable.getString(stringIndex);
// Event names are either some specific event in the JS engine, or it is the URL
// of the script that is executing. Put all of the URL events into a single row
// labeled 'Script'.
const rowName = isUrl(stringIndex) ? 'Script' : displayName;
let timingRow = jsTracerTimingMap.get(rowName);
if (timingRow === undefined) {
timingRow = {
start: [],
end: [],
index: [],
label: [],
func: [],
name: rowName,
length: 0,
};
jsTracerTimingMap.set(rowName, timingRow);
}
{
// Perform sanity checks on the data that is being added.
const currStart = timingRow.start[timingRow.length - 1];
const currEnd = timingRow.end[timingRow.length - 1];
const prevEnd = timingRow.end[timingRow.length - 2];
if (end < start) {
throw new Error('end is less than the start');
}
if (currEnd < currStart) {
throw new Error(
`currEnd < currStart "${displayName} - ${currEnd} < ${currStart}"`
);
}
if (currStart < prevEnd) {
throw new Error(
`currStart < prevEnd "${displayName} - ${currStart} < ${prevEnd}"`
);
}
}
// Convert the timing to milliseconds.
timingRow.start.push(start / 1000);
timingRow.end.push(end / 1000);
timingRow.label.push(displayName);
timingRow.index.push(tracerEventIndex);
timingRow.length++;
}
// Determine the self time of the various events.
// These arrays implement the stack of "prefix" events. This is implemented as 3
// arrays instead of an array of objects to reduce the GC pressure in this tight
// loop, but conceptually this is just one stack, not 3 stacks. At any given time,
// the stack represents the prefixes, aka the parents, of the current event.
const prefixesStarts: Microseconds[] = [];
const prefixesEnds: Microseconds[] = [];
const prefixesEventIndexes: IndexIntoJsTracerEvents[] = [];
// If there is no prefix, set it to -1. This simplifies some of the real-time
// type checks that would be required by Flow for this mutable variable.
let prefixesTip = -1;
// Go through all of the events. Each `if` branch is documented with a small diagram
// that includes a little bit of ascii art to help explain the step.
//
// Legend:
// xxxxxxxx - Already reported information, not part of the prefix stack.
// [======] - Some event on the stack that has not been reported.
// [prefix] - The prefix event to consider, the top of the prefix stack. This
// could also be only part of an event, that has been split into multiple
// pieces.
// [current] - The current event to add.
for (
let currentEventIndex = 0;
currentEventIndex < jsTracer.length;
currentEventIndex++
) {
const currentStart = jsTracer.timestamps[currentEventIndex];
const durationRaw = jsTracer.durations[currentEventIndex];
const duration = durationRaw === null ? 0 : durationRaw;
// The end needs to be adjustable in case there are precision errors.
let currentEnd = currentStart + duration;
if (prefixesTip === -1) {
// Nothing has been added yet, add this "current" event to the stack of prefixes.
prefixesTip = 0;
prefixesStarts[prefixesTip] = currentStart;
prefixesEnds[prefixesTip] = currentEnd;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
continue;
}
while (true) {
const prefixStart = prefixesStarts[prefixesTip];
const prefixEnd = prefixesEnds[prefixesTip];
const prefixEventIndex = prefixesEventIndexes[prefixesTip];
if (
prefixStart <= currentStart &&
prefixEnd > currentStart &&
prefixEnd < currentEnd
) {
// This check handles precision errors that creates timing like so:
// [prefix=======]
// [current========]
//
// It reformats the current as:
//
// [prefix=======]
// [current===]
currentEnd = prefixEnd;
}
// The following if/else blocks go through every potential case of timing for the
// the data, and finally throw if those cases weren't handled. Each block should
// have a diagram explaining the various states the timing can be in. Inside the
// if checks, the loop is either continued or broken.
if (prefixEnd <= currentStart) {
// In this case, the "current" event has passed the other "prefix" event.
//
// xxxxxxxxxxxxxxxx[================]
// xxxxxxxx[======] [current]
// [prefix]
//
// This next step would match too:
//
// xxxxxxxxxxxxxxxx[================]
// xxxxxxxx[prefix] [current]
// xxxxxxxx
// Commit the previous "prefix" event, then continue on with this loop.
reportSelfTime(prefixEventIndex, prefixStart, prefixEnd);
// Move the tip towards the prefix.
prefixesTip--;
if (prefixesTip === -1) {
// This next step would match too:
//
// [prefix] [current]
// There are no more "prefix" events to commit. Add the "current" event on, and
// break out of this loop.
prefixesTip = 0;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
prefixesStarts[prefixesTip] = currentStart;
prefixesEnds[prefixesTip] = currentEnd;
break;
}
// Move up the prefix stack to report more self times. This is the only branch
// that has a `continue`, and not a `break`.
continue;
} else if (prefixEnd > currentEnd) {
// The current event occludes the prefix event.
//
// [prefix=================]
// [current]
//
// Split the reported self time of the prefix.
//
// [prefix]xxxxxxxxx[prefix]
// [current]
//
// ^leave the second prefix piece on the "prefixes" stack
// ^report the first prefix piece
//
// After reporting we are left with:
//
// xxxxxxxxxxxxxxxxx[======]
// [current]
// Report the first part of the prefix's self time.
reportSelfTime(prefixEventIndex, prefixStart, currentStart);
// Shorten the prefix's start time.
prefixesStarts[prefixesTip] = currentEnd;
// Now add on the "current" event to the stack of prefixes.
prefixesTip++;
prefixesStarts[prefixesTip] = currentStart;
prefixesEnds[prefixesTip] = currentEnd;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
break;
} else if (prefixEnd === currentEnd) {
if (prefixStart !== currentStart) {
// The current event splits the event above it, so split the reported self
// time of the prefix.
//
// [prefix]xxxxxxxxx
// [current]
// Report the prefix's self time.
reportSelfTime(prefixEventIndex, prefixStart, currentStart);
// Update both the index and the start time.
prefixesStarts[prefixesTip] = currentStart;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
} else {
// The prefix and current events completely match, so don't report any
// self time from the prefix. Replace the prefix's index.
prefixesEventIndexes[prefixesTip] = currentEventIndex;
}
break;
} else {
// There is some error in the data structure or this functions logic. Throw
// and error, and report some nice information to the console.
const prefixName = stringTable.getString(
jsTracer.events[prefixEventIndex]
);
const currentName = stringTable.getString(
jsTracer.events[currentEventIndex]
);
console.error('Current JS Tracer information:', {
jsTracer,
stringTable,
prefixEventIndex,
currentEventIndex,
});
console.error(
`Prefix (parent) times for "${prefixName}": ${prefixStart}ms to ${prefixEnd}ms`
);
console.error(
`Current (child) times for "${currentName}": ${currentStart}ms to ${currentEnd}ms`
);
throw new Error(
'The JS Tracer algorithm encountered some data it could not handle.'
);
}
}
}
// Drain off the remaining "prefixes" from the stack, and report the self time.
for (; prefixesTip >= 0; prefixesTip--) {
reportSelfTime(
prefixesEventIndexes[prefixesTip],
prefixesStarts[prefixesTip],
prefixesEnds[prefixesTip]
);
}
// Return the list of events, sorted alphabetically.
return [...jsTracerTimingMap.values()].sort((a, b) => {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
return 0;
});
}
/**
* This function builds a Thread structure without any samples. It derives the functions
* and stacks from the JS Tracer data.
* Given JS tracer data:
*
* [A------------------------------]
* [B-------][D--------------]
* [C--] [E-------]
*
* Build this StackTable:
*
* A -> B -> C
* \
* D -> E
*
* This function is also in charge of matching up JS tracer events that have script
* locations to functions that we sampled in the Gecko Profiler sampling mechanism.
* If an event shares the same script location, row, and column numbers as a JS
* function, then the function information is used.
*/
export function convertJsTracerToThreadWithoutSamples(
fromThread: Thread,
jsTracer: JsTracerFixed,
categories: CategoryList
): {
thread: Thread,
stackMap: Map<IndexIntoJsTracerEvents, IndexIntoStackTable>,
} {
// Create a new thread, with empty information, but preserve some of the existing
// thread information.
const frameTable = getEmptyFrameTable();
const stackTable = getEmptyStackTable();
const samples: SamplesTable = {
...getEmptySamplesTableWithEventDelay(),
weight: [],
weightType: 'tracing-ms',
};
const markers = getEmptyRawMarkerTable();
const funcTable = { ...fromThread.funcTable };
const { stringTable } = fromThread;
const thread: Thread = {
...fromThread,
markers,
funcTable,
stackTable,
frameTable,
samples,
};
// Keep a stack of js tracer events, and end timings, that will be used to find
// the stack prefixes. Once a JS tracer event starts past another event end, the
// stack will be "popped" by decrementing the unmatchedIndex.
let unmatchedIndex = 0;
const unmatchedEventIndexes = [null];
const unmatchedEventEnds = [0];
// Build up maps between index values.
const funcMap: Map<IndexIntoStringTable, IndexIntoFuncTable> = new Map();
const stackMap: Map<IndexIntoJsTracerEvents, IndexIntoStackTable> = new Map();
// The implementationMap maps to index in the string table, that refers to a frame's
// implementation. e.g. the number 132 which maps to the string "IonMonkey".
const implementationMap: Map<
IndexIntoJsTracerEvents,
IndexIntoStringTable | null
> = new Map();
// Get some computed values before entering the loop.
const blankStringIndex = stringTable.indexForString('');
const otherCategory = categories.findIndex((c) => c.name === 'Other');
if (otherCategory === -1) {
throw new Error("Expected to find an 'Other' category.");
}
const scriptLocationToFuncIndex = getScriptLocationToFuncIndex(thread);
const eventNameToImplementationStringIndex = {
Interpreter: stringTable.indexForString(('interpreter': JsImplementation)),
Baseline: stringTable.indexForString(('baseline': JsImplementation)),
IonMonkey: stringTable.indexForString(('ion': JsImplementation)),
};
const frameEventTypes = new Set(
Object.keys(eventNameToImplementationStringIndex)
);
// Go through all of the JS tracer events, and build up the func, stack, and
// frame tables.
for (
let tracerEventIndex = 0;
tracerEventIndex < jsTracer.length;
tracerEventIndex++
) {
// Look up various values for this event.
const eventStringIndex = jsTracer.events[tracerEventIndex];
const eventName = stringTable.getString(eventStringIndex);
const end = jsTracer.end[tracerEventIndex];
const column = jsTracer.column[tracerEventIndex];
const line = jsTracer.line[tracerEventIndex];
let funcIndex: null | IndexIntoFuncTable = null;
// First try to look up the func index by script location.
if (column !== null && line !== null) {
// Look up to see if this script location has a function in the sampled data.
const key = `${eventName}:${line}:${column}`;
const maybeFuncIndex = scriptLocationToFuncIndex.get(key);
if (maybeFuncIndex !== undefined && maybeFuncIndex !== null) {
funcIndex = maybeFuncIndex;
funcMap.set(eventStringIndex, funcIndex);
}
}
if (funcIndex === null) {
const generatedFuncIndex = funcMap.get(eventStringIndex);
if (generatedFuncIndex === undefined) {
// Create a new function only if the event string is different.
funcIndex = funcTable.length++;
funcTable.name.push(eventStringIndex);
funcTable.isJS.push(false);
funcTable.resource.push(-1);
funcTable.relevantForJS.push(true);
funcTable.fileName.push(null);
funcTable.lineNumber.push(null);
funcTable.columnNumber.push(null);
funcMap.set(eventStringIndex, funcIndex);
} else {
funcIndex = generatedFuncIndex;
}
}
// Try to find the prefix stack for this event.
let prefixIndex = unmatchedEventIndexes[unmatchedIndex];
while (prefixIndex !== null) {
const prefixEnd = unmatchedEventEnds[unmatchedIndex];
if (end <= prefixEnd) {
// This prefix is the correct one.
break;
}
// Keep on searching for the next prefix.
prefixIndex = unmatchedEventIndexes[unmatchedIndex];
unmatchedIndex--;
}
// Figure out the frame implementation for this event.
let implementation: null | IndexIntoStringTable = null;
if (frameEventTypes.has(eventName)) {
// The current event matches a known frame type, switch to that frame type.
implementation = eventNameToImplementationStringIndex[eventName];
} else if (prefixIndex !== null) {
// Look up the implementation of the prefix.
const prefixImplementation = implementationMap.get(prefixIndex);
if (prefixImplementation === undefined) {
throw new Error(
`Expected to find an implementation from a js tracer prefix index prefixIndex: ${prefixIndex}`
);
}
implementation = prefixImplementation;
}
implementationMap.set(tracerEventIndex, implementation);
// Every event gets a unique frame entry.
const frameIndex = frameTable.length++;
frameTable.address.push(blankStringIndex);
frameTable.inlineDepth.push(0);
frameTable.category.push(otherCategory);
frameTable.func.push(funcIndex);
frameTable.nativeSymbol.push(null);
frameTable.innerWindowID.push(0);
frameTable.implementation.push(implementation);
frameTable.line.push(line);
frameTable.column.push(column);
frameTable.optimizations.push(null);
// Each event gets a stack table entry.
const stackIndex = stackTable.length++;
stackTable.frame.push(frameIndex);
stackTable.category.push(otherCategory);
stackTable.prefix.push(prefixIndex);
stackMap.set(tracerEventIndex, stackIndex);
// Add this stack to the unmatched stacks of events.
unmatchedIndex++;
if (unmatchedIndex === 0) {
// Reached a new root, reset the index to 1.
unmatchedIndex = 1;
}
unmatchedEventIndexes[unmatchedIndex] = tracerEventIndex;
unmatchedEventEnds[unmatchedIndex] = end;
}
return { thread, stackMap };
}
type JsTracerFixed = {|
events: Array<IndexIntoStringTable>,
start: Array<Microseconds>,
end: Array<Microseconds>,
line: Array<number | null>, // Line number.
column: Array<number | null>, // Column number.
length: number,
|};
/**
* JS Tracer information has a start and duration, but due to precision issues, this
* can lead to errors with computing parent/child relationships. This function converts
* it into a start and end time, and corrects issues where the events incorrectly
* overlap.
*/
export function getJsTracerFixed(jsTracer: JsTracerTable): JsTracerFixed {
if (jsTracer.length === 0) {
// Create an empty "fixed" table, we can't use getEmptyJsTracerTable here
// because the "fixed" one is slightly different.
return {
events: [],
start: [],
end: [],
line: [],
column: [],
length: 0,
};
}
let prevStart = jsTracer.timestamps[0];
let prevEnd = prevStart + jsTracer.durations[0];
const start = [prevStart];
const end = [prevEnd];
const jsTracerFixed = {
events: jsTracer.events,
start,
end,
line: jsTracer.line,
column: jsTracer.column,
length: jsTracer.length,
};
for (let eventIndex = 1; eventIndex < jsTracer.length; eventIndex++) {
const duration = jsTracer.durations[eventIndex];
let currStart = jsTracer.timestamps[eventIndex];
let currEnd = currStart + (duration === null ? 0 : duration);
// The following if branches were produced to enumerate through all possible
// overlapping cases.
if (currStart < prevStart) {
if (prevStart < currEnd) {
// | Adjust to this:
// prev: [-----] | [----]
// curr: [---------] | [---]
currStart = prevStart;
} else {
// | Adjust to this:
// prev: [-----] | [----]
// curr: [---] | [---]
const duration = currEnd - currStart;
currStart = prevStart;
currEnd = Math.min(prevEnd, currStart + duration);
}
} else {
if (currEnd < prevEnd) {
// No adjustment needed
// prev: [-----]
// curr: [--]
} else {
if (prevEnd <= currStart) {
// There is no need to adjust the timing.
// prev: [----]
// curr: [----]
} else {
if (prevEnd - currStart < currEnd - prevEnd) {
// Fix the timing | Adjust to this:
// prev: [---] | [---][----]
// curr: [-----] |
currStart = prevEnd;
} else {
// Fix the timing | Adjust to this:
// prev: [----] | [----]
// curr: [---] | [--]
currEnd = prevEnd;
}
}
}
}
start.push(currStart);
end.push(currEnd);
prevStart = currStart;
prevEnd = currEnd;
}
return jsTracerFixed;
}
/**
* This function controls all of the finer-grained steps to convert JS Tracer information
* into Thread data structure. See each of those functions for more documentation on
* what is going on.
*/
export function convertJsTracerToThread(
fromThread: Thread,
jsTracer: JsTracerTable,
categories: CategoryList
): Thread {
const jsTracerFixed = getJsTracerFixed(jsTracer);
const { thread, stackMap } = convertJsTracerToThreadWithoutSamples(
fromThread,
jsTracerFixed,
categories
);
thread.samples = getSelfTimeSamplesFromJsTracer(
thread,
jsTracerFixed,
stackMap
);
return thread;
}
/**
* This function walks along the end of the JS tracer events, pushing and popping events
* on to a stack. It then determines the self time for the events, and translates the
* self time into individual weighted samples in the SamplesTable.
*
* Given JS tracer data:
*
* [A------------------------------]
* [B-------][D--------------]
* [C--] [E-------]
*
* This StackTable is generated in convertJsTracerToThreadWithoutSamples:
*
* A -> B -> C
* \
* D -> E
*
* This data would then have the following self time:
*
* 0 10 20 30
* Time: |123456789|123456789|123456789|12
* Node: AAABBBCCCCBBBDDDDEEEEEEEEEEDDDAAA
* Sample: ⎣A⎦⎣B⎦⎣C-⎦⎣B⎦⎣D-⎦⎣E-------⎦⎣D⎦⎣A⎦
*
* Insert a sample for each discrete bit of self time.
*
* SampleTable = {
* stack: [A, B, C, B, D, E, D, A ],
* time: [0, 3, 6, 10, 14, 17, 27, 30],
* weight: [3, 3, 4, 3, 4, 10, 3, 3 ]
* }
*/
export function getSelfTimeSamplesFromJsTracer(
thread: Thread,
jsTracer: JsTracerFixed,
stackMap: Map<IndexIntoJsTracerEvents, IndexIntoStackTable>
): SamplesTable {
// Give more leeway for floating number precision issues.
const epsilon = 1e-5;
const isNearlyEqual = (a, b) => Math.abs(a - b) < epsilon;
// Each event type will have it's own timing information, later collapse these into
// a single array.
const { stringTable } = thread;
const samples = getEmptySamplesTableWithEventDelay();
const sampleWeights = [];
samples.weight = sampleWeights;
function addSelfTimeAsASample(
eventIndex: IndexIntoJsTracerEvents,
start: Microseconds,
end: Microseconds
): void {
// Use a check against the epsilon, for float precision issues.
if (isNearlyEqual(start, end)) {
// If this self time is 0, do not report it.
return;
}
const stackIndex = ensureExists(
stackMap.get(eventIndex),
'The JS tracer event did not exist in the stack map.'
);
samples.stack.push(stackIndex);
samples.time.push(
// Convert from microseconds.
start / 1000
);
sampleWeights.push(
// The weight of the sample is in microseconds.
(end - start) / 1000
);
samples.length++;
}
// Determine the self time of the various events.
// These arrays implement the stack of "prefix" events. This is implemented as 3
// arrays instead of an array of objects to reduce the GC pressure in this tight
// loop, but conceptually this is just one stack, not 3 stacks. At any given time,
// the stack represents the prefixes, aka the parents, of the current event.
const prefixesStarts: Microseconds[] = [];
const prefixesEnds: Microseconds[] = [];
const prefixesEventIndexes: IndexIntoJsTracerEvents[] = [];
// If there is no prefix, set it to -1. This simplifies some of the real-time
// type checks that would be required by Flow for this mutable variable.
let prefixesTip = -1;
// Go through all of the events. Each `if` branch is documented with a small diagram
// that includes a little bit of ascii art to help explain the step.
//
// Legend:
// xxxxxxxx - Already reported information, not part of the prefix stack.
// [======] - Some event on the stack that has not been reported.
// [prefix] - The prefix event to consider, the top of the prefix stack. This
// could also be only part of an event, that has been split into multiple
// pieces.
// [current] - The current event to add.
for (
let currentEventIndex = 0;
currentEventIndex < jsTracer.length;
currentEventIndex++
) {
const currentStart = jsTracer.start[currentEventIndex];
const currentEnd = jsTracer.end[currentEventIndex];
if (prefixesTip === -1) {
// Nothing has been added yet, add this "current" event to the stack of prefixes.
prefixesTip = 0;
prefixesStarts[prefixesTip] = currentStart;
prefixesEnds[prefixesTip] = currentEnd;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
continue;
}
while (true) {
const prefixStart = prefixesStarts[prefixesTip];
const prefixEnd = prefixesEnds[prefixesTip];
const prefixEventIndex = prefixesEventIndexes[prefixesTip];
// The following if/else blocks go through every potential case of timing for the
// the data, and finally throw if those cases weren't handled. Each block should
// have a diagram explaining the various states the timing can be in. Inside the
// if checks, the loop is either continued or broken.
if (prefixEnd <= currentStart + epsilon) {
// In this case, the "current" event has passed the other "prefix" event.
//
// xxxxxxxxxxxxxxxx[================]
// xxxxxxxx[======] [current]
// [prefix]
//
// This next step would match too:
//
// xxxxxxxxxxxxxxxx[================]
// xxxxxxxx[prefix] [current]
// xxxxxxxx
// Commit the previous "prefix" event, then continue on with this loop.
addSelfTimeAsASample(prefixEventIndex, prefixStart, prefixEnd);
// Move the tip towards the prefix.
prefixesTip--;
if (prefixesTip === -1) {
// This next step would match too:
//
// [prefix] [current]
// There are no more "prefix" events to commit. Add the "current" event on, and
// break out of this loop.
prefixesTip = 0;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
prefixesStarts[prefixesTip] = currentStart;
prefixesEnds[prefixesTip] = currentEnd;
break;
}
// Move up the prefix stack to report more self times. This is the only branch
// that has a `continue`, and not a `break`.
continue;
} else if (currentEnd < prefixEnd + epsilon) {
// The current event occludes the prefix event.
//
// [prefix=================]
// [current]
//
// Split the reported self time of the prefix.
//
// [prefix]xxxxxxxxx[prefix]
// [current]
//
// ^leave the second prefix piece on the "prefixes" stack
// ^report the first prefix piece
//
// After reporting we are left with:
//
// xxxxxxxxxxxxxxxxx[======]
// [current]
// Report the first part of the prefix's self time.
addSelfTimeAsASample(prefixEventIndex, prefixStart, currentStart);
// Shorten the prefix's start time.
prefixesStarts[prefixesTip] = currentEnd;
// Now add on the "current" event to the stack of prefixes.
prefixesTip++;
prefixesStarts[prefixesTip] = currentStart;
prefixesEnds[prefixesTip] = currentEnd;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
break;
} else if (isNearlyEqual(prefixEnd, currentEnd)) {
if (!isNearlyEqual(prefixStart, currentStart)) {
// The current event splits the event above it, so split the reported self
// time of the prefix.
//
// [prefix]xxxxxxxxx
// [current]
// Report the prefix's self time.
addSelfTimeAsASample(prefixEventIndex, prefixStart, currentStart);
// Update both the index and the start time.
prefixesStarts[prefixesTip] = currentStart;
prefixesEventIndexes[prefixesTip] = currentEventIndex;
} else {
// The prefix and current events completely match, so don't report any
// self time from the prefix. Replace the prefix's index.
prefixesEventIndexes[prefixesTip] = currentEventIndex;
}
break;
} else {
// There is some error in the data structure or this functions logic. Throw
// an error, and report some nice information to the console.
const prefixName = stringTable.getString(