forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarker-data.js
1431 lines (1315 loc) · 46.1 KB
/
marker-data.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 { getEmptyRawMarkerTable } from './data-structures';
import { getFriendlyThreadName } from './profile-data';
import { removeFilePath, removeURLs } from '../utils/string';
import { ensureExists, assertExhaustiveCheck } from '../utils/flow';
import {
INSTANT,
INTERVAL,
INTERVAL_START,
INTERVAL_END,
} from 'firefox-profiler/app-logic/constants';
import {
getMarkerSchemaName,
getSchemaFromMarker,
markerPayloadMatchesSearch,
} from './marker-schema';
import type {
Thread,
SamplesTable,
RawMarkerTable,
IndexIntoStringTable,
IndexIntoRawMarkerTable,
IndexIntoCategoryList,
CategoryList,
InnerWindowID,
Marker,
MarkerIndex,
MarkerPayload,
IPCSharedData,
IPCMarkerPayload,
NetworkPayload,
PrefMarkerPayload,
TextMarkerPayload,
StartEndRange,
IndexedArray,
DerivedMarkerInfo,
MarkerSchema,
MarkerSchemaByName,
MarkerDisplayLocation,
Tid,
} from 'firefox-profiler/types';
import type { UniqueStringArray } from '../utils/unique-string-array';
/**
* Jank instances are created from responsiveness values. Responsiveness is a profiler
* feature that can be turned on and off. When on, every sample includes a responsiveness
* value.
*
* This timing is captured by instrumenting the event queue. A runnable is added to the
* browser's event queue, then the profiler times how long it takes to come back.
* Generally, if this takes longer than some threshold, then this can be jank for the
* browser.
*
* This function converts those measurings of milliseconds into individual markers.
*
* For instance, take an array of responsiveness values:
*
* [5, 25, 33, 3, 23, 42, 65, 71, 3, 10, 22, 31, 42, 3, 20, 40]
* |___| |___| |___|
* Runnable is reset Jank of 71ms. The Runnable reset under threshold.
* but under 50ms, responsiveness was
* no jank. reset from 71 to 3.
*/
export function deriveJankMarkers(
samples: SamplesTable,
thresholdInMs: number,
otherCategoryIndex: IndexIntoCategoryList
): Marker[] {
const addMarker = () =>
jankInstances.push({
start: lastTimestamp - lastResponsiveness,
end: lastTimestamp,
name: 'Jank',
category: otherCategoryIndex,
threadId: null,
data: { type: 'Jank' },
});
let lastResponsiveness: number = 0;
let lastTimestamp: number = 0;
const jankInstances: Marker[] = [];
for (let i = 0; i < samples.length; i++) {
let currentResponsiveness;
if (samples.eventDelay) {
currentResponsiveness = samples.eventDelay[i];
} else if (samples.responsiveness) {
currentResponsiveness = samples.responsiveness[i];
}
if (currentResponsiveness === null || currentResponsiveness === undefined) {
// Ignore anything that's not numeric. This can happen if there is no responsiveness
// information, or if the sampler failed to collect a responsiveness value. This
// can happen intermittently.
//
// See Bug 1506226.
continue;
}
if (currentResponsiveness < lastResponsiveness) {
if (lastResponsiveness >= thresholdInMs) {
addMarker();
}
}
lastResponsiveness = currentResponsiveness;
lastTimestamp = samples.time[i];
}
if (lastResponsiveness >= thresholdInMs) {
addMarker();
}
return jankInstances;
}
export function getSearchFilteredMarkerIndexes(
getMarker: (MarkerIndex) => Marker,
markerIndexes: MarkerIndex[],
markerSchemaByName: MarkerSchemaByName,
searchRegExp: RegExp | null,
categoryList: CategoryList
): MarkerIndex[] {
if (!searchRegExp) {
return markerIndexes;
}
// Need to assign it to a constant variable so Flow doesn't complain about
// passing it inside a function below.
const regExp = searchRegExp;
function test(value, key) {
// Reset regexp for each iteration. Otherwise state from previous
// iterations can cause matches to fail if the search is global or
// sticky.
regExp.lastIndex = 0;
return (
regExp.test(value) ||
(!regExp.test(key + ':') && // avoid matching the key
// (eg. searching for 'me' should not match all markers,
// even though they all have a 'name:' field).
regExp.test(key + ':' + value))
);
}
const newMarkers: MarkerIndex[] = [];
for (const markerIndex of markerIndexes) {
const marker = getMarker(markerIndex);
const { data, name, category } = marker;
if (categoryList[category] !== undefined) {
const markerCategory = categoryList[category].name;
if (test(markerCategory, 'cat')) {
newMarkers.push(markerIndex);
continue;
}
}
if (test(name, 'name')) {
newMarkers.push(markerIndex);
continue;
}
if (data && typeof data === 'object') {
if (test(data.type, 'type')) {
// Check the type of the marker payload first.
newMarkers.push(markerIndex);
continue;
}
// Now check the schema for the marker payload for searchable
const markerSchema = getSchemaFromMarker(
markerSchemaByName,
marker.name,
marker.data
);
if (
markerSchema &&
markerPayloadMatchesSearch(markerSchema, marker, test)
) {
newMarkers.push(markerIndex);
continue;
}
}
}
return newMarkers;
}
/**
* Gets the markers and the web pages that are relevant to the current active tab
* and filters out the markers that don't belong to those revelant pages.
* If we don't have any item in relevantPages, return the whole marker list.
*/
export function getTabFilteredMarkerIndexes(
getMarker: (MarkerIndex) => Marker,
markerIndexes: MarkerIndex[],
relevantPages: Set<InnerWindowID>,
includeGlobalMarkers: boolean = true
): MarkerIndex[] {
if (relevantPages.size === 0) {
return markerIndexes;
}
const newMarkers: MarkerIndex[] = [];
for (const markerIndex of markerIndexes) {
const { name, data } = getMarker(markerIndex);
// We want to retain some markers even though they do not belong to a specific tab.
// We are checking those before and pushing those markers to the new array.
// As of now, those markers are:
// - Jank markers
if (includeGlobalMarkers) {
if (name === 'Jank') {
newMarkers.push(markerIndex);
continue;
}
} else {
if (data && data.type === 'Network') {
// Now network markers have innerWindowIDs inside their payloads but those markers
// can be inside the main thread and not be related to that specific thread.
continue;
}
}
if (data && data.innerWindowID && relevantPages.has(data.innerWindowID)) {
newMarkers.push(markerIndex);
}
}
return newMarkers;
}
/**
* This class is a specialized Map for IPC marker data, using the thread ID and
* marker index as the key, and storing IPCSharedData (which holds the start/end
* times for the IPC message.
*/
export class IPCMarkerCorrelations {
_correlations: Map<Tid, Map<number, IPCSharedData>>;
constructor() {
this._correlations = new Map();
}
set(tid: Tid, index: number, data: IPCSharedData): void {
let threadData = this._correlations.get(tid);
if (!threadData) {
threadData = new Map();
this._correlations.set(tid, threadData);
}
threadData.set(index, data);
}
get(tid: Tid, index: number): ?IPCSharedData {
const threadData = this._correlations.get(tid);
if (!threadData) {
return undefined;
}
return threadData.get(index);
}
}
/**
* This function correlates the sender and recipient sides of IPC markers so
* that we can share data between the two during profile processing.
*
* A single IPC message consists of 5 markers:
*
* endpoint (sender or background thread)
* | (or main thread in sender process if they are not profiled)
* |
* v
* transferStart (IPC I/O thread in sender process)
* | (or main thread in sender process if it's not profiled)
* |
* v
* transferEnd (IPC I/O thread in sender process)
* | (or main thread in sender process if it's not profiled)
* |
* v
* transferEnd (IPC I/O thread in receiver process)
* | (or main thread in receiver process if it's not profiled)
* |
* v
* endpoint (receiver or background thread)
* (or main thread in receiver process if they are not profiled)
*/
export function correlateIPCMarkers(threads: Thread[]): IPCMarkerCorrelations {
// Create a unique ID constructed from the source PID, destination PID,
// message seqno, and message type. Since the seqno is only unique for each
// message channel pair, we use the PIDs and message type as a way of
// identifying which channel pair generated this message.
function makeIPCMessageID(thread, data): string {
let pids;
if (data.direction === 'sending') {
pids = `${thread.pid},${data.otherPid}`;
} else {
pids = `${data.otherPid},${thread.pid}`;
}
return pids + `,${data.messageSeqno},${data.messageType}`;
}
function formatThreadName(tid: ?number): string | void {
if (tid !== null && tid !== undefined) {
const name = threadNames.get(tid);
if (name !== undefined) {
return `${name} (Thread ID: ${tid})`;
}
}
return undefined;
}
// Each IPC message has several (up to 5) markers associated with it. The
// order of these can be determined by the `direction` and `phase` attributes.
// To store all the markers for a given message, we convert the direction and
// phase to an index into an array.
function phaseToIndex(data: IPCMarkerPayload): number {
const { phase } = data;
if (data.direction === 'sending') {
switch (phase) {
case 'endpoint':
// We don't have a 'phase' field in the older profiles, in that case
// their phase is 'endpoint'. (fallthrough)
case undefined:
return 0;
case 'transferStart':
return 1;
case 'transferEnd':
return 2;
default:
throw assertExhaustiveCheck(phase, `Unhandled IPC phase type.`);
}
} else {
switch (phase) {
case 'transferEnd':
return 3;
case 'endpoint':
// We don't have a 'phase' field in the older profiles, in that case
// their phase is 'endpoint'. (fallthrough)
case undefined:
return 4;
case 'transferStart':
throw new Error(
'Unexpected "transferStart" phase found on receiving side.'
);
default:
throw assertExhaustiveCheck(phase, `Unhandled IPC phase type.`);
}
}
}
// First, construct a mapping of marker IDs to an array of markers with that
// ID for faster lookup. We also collect the friendly thread names while we
// have access to all the threads. It's considerably more difficult to do
// this processing later.
const markersByKey: Map<
string,
Array<{ tid: number, index: number, data: IPCMarkerPayload } | void>,
> = new Map();
const threadNames: Map<number, string> = new Map();
for (const thread of threads) {
// Don't bother checking for IPC markers if this thread's string table
// doesn't have the string "IPC". This lets us avoid looping over all the
// markers when we don't have to.
if (!thread.stringTable.hasString('IPC')) {
continue;
}
if (typeof thread.tid === 'number') {
const tid: number = thread.tid;
threadNames.set(tid, getFriendlyThreadName(threads, thread));
for (let index = 0; index < thread.markers.length; index++) {
const data = thread.markers.data[index];
if (!data || data.type !== 'IPC') {
continue;
}
const key = makeIPCMessageID(thread, data);
if (!markersByKey.has(key)) {
markersByKey.set(key, new Array(5).fill(undefined));
}
const currMarkers = ensureExists(markersByKey.get(key));
const phaseIdx = phaseToIndex(data);
if (currMarkers[phaseIdx] === undefined) {
currMarkers[phaseIdx] = { tid, index, data };
} else {
console.warn('Duplicate IPC marker found for key', key);
}
}
}
}
const correlations = new IPCMarkerCorrelations();
for (const markers of markersByKey.values()) {
const startEndpointMarker = markers[0];
const endEndpointMarker = markers[4];
const sendTid = startEndpointMarker ? startEndpointMarker.tid : undefined;
const recvTid = endEndpointMarker ? endEndpointMarker.tid : undefined;
const sharedData: IPCSharedData = {
startTime: startEndpointMarker
? startEndpointMarker.data.startTime
: undefined,
sendStartTime: markers[1] ? markers[1].data.startTime : undefined,
sendEndTime: markers[2] ? markers[2].data.startTime : undefined,
recvEndTime: markers[3] ? markers[3].data.startTime : undefined,
endTime: endEndpointMarker ? endEndpointMarker.data.startTime : undefined,
sendTid,
recvTid,
sendThreadName: formatThreadName(sendTid),
recvThreadName: formatThreadName(recvTid),
};
const addedThreadIds = new Set();
if (startEndpointMarker) {
addedThreadIds.add(startEndpointMarker.tid);
correlations.set(
startEndpointMarker.tid,
startEndpointMarker.index,
sharedData
);
}
if (endEndpointMarker) {
addedThreadIds.add(endEndpointMarker.tid);
correlations.set(
endEndpointMarker.tid,
endEndpointMarker.index,
sharedData
);
}
// We added both endpoints to the correlations now (if they are present).
// If both of them are present at the same time, we don't need to add the
// middle three markers because these are the I/O related markers that
// happen in another thread (IPC I/O if profiled, main thread if not profiled).
if (!startEndpointMarker || !endEndpointMarker) {
// If both markers from sender and receiver threads are present, we can
// only add the markers of them to the correlations. That way, the middle
// markers from IPC I/O threads will not be visible in the main thread and
// make the main thread crowded. It's important to add all the threads to
// the marker.
for (const m of markers.slice(1, 4)) {
if (m !== undefined && !addedThreadIds.has(m.tid)) {
// Add the marker to a thread only if it's not already added.
correlations.set(m.tid, m.index, sharedData);
addedThreadIds.add(m.tid);
}
}
}
}
return correlations;
}
/**
* This function is the canonical place to turn the RawMarkerTable into our fully
* processed Marker type. It handles the phases of markers that can be emitted
* by the Gecko profiler. These are defined by the MarkerPhase type.
*
* Instant - Represents a single point in time.
* Interval - A complete marker that represents an interval of time.
* IntervalStart, IntervalEnd - These two types represent incomplete markers that
* must be reconstructed into a single marker. If a start and end are found, they
* are matched together. Start and end markers should be correctly nested. If
* a start marker is found without an end, its end time is set to the end of
* the thread range. For the reverse situation, it's set to the start.
*
* There is also some special handling of different markers.
* - CompositorScreenshot - They are turned from Instant markers to Interval markers
* - IPC - They are matched up.
* - Network - They have different network phases.
*
* Finally, the Marker format is what we care about in the front-end, but sometimes
* we need to modify the RawMarkerTable, e.g. for comparisons and for sanitization.
* In order to get back to the RawMarkerTable, this function provides a
* markerIndexToRawMarkerIndexes array.
*/
export function deriveMarkersFromRawMarkerTable(
rawMarkers: RawMarkerTable,
stringTable: UniqueStringArray,
threadId: Tid,
threadRange: StartEndRange,
ipcCorrelations: IPCMarkerCorrelations
): DerivedMarkerInfo {
const markers: Marker[] = [];
const markerIndexToRawMarkerIndexes: IndexedArray<
MarkerIndex,
IndexIntoRawMarkerTable[],
> = [];
// These maps contain the start markers we find while looping the marker
// table.
// The first map contains the start markers for tracing markers. They can be
// nested and that's why we use an array structure as value.
const openIntervalMarkers: Map<IndexIntoStringTable, MarkerIndex[]> =
new Map();
// The second map contains the start markers for network markers.
// Note that we don't have more than 2 network markers with the same name as
// the name contains an incremented index. Therefore we don't need to use an
// array as value like for tracing markers.
const openNetworkMarkers: Map<number, MarkerIndex> = new Map();
function addMarker(indexes: IndexIntoRawMarkerTable[], marker: Marker) {
markerIndexToRawMarkerIndexes.push(indexes);
markers.push(marker);
}
// In the case of separate markers for the start and end of an interval,
// merge the payloads together, with the end data overriding the start.
function mergeIntervalData(
startData: MarkerPayload | null,
endData: MarkerPayload | null
): MarkerPayload | null {
if (!startData && !endData) {
return null;
}
return ({
...startData,
...endData,
}: any);
}
// We don't add a screenshot marker as we find it, because to know its
// duration we need to wait until the next one or the end of the profile. So
// we keep it here.
const previousScreenshotMarkers: Map<string, MarkerIndex> = new Map();
for (
let rawMarkerIndex = 0;
rawMarkerIndex < rawMarkers.length;
rawMarkerIndex++
) {
const name = rawMarkers.name[rawMarkerIndex];
const maybeStartTime = rawMarkers.startTime[rawMarkerIndex];
const maybeEndTime = rawMarkers.endTime[rawMarkerIndex];
const phase = rawMarkers.phase[rawMarkerIndex];
const data = rawMarkers.data[rawMarkerIndex];
const category = rawMarkers.category[rawMarkerIndex];
const markerThreadId = rawMarkers.threadId
? rawMarkers.threadId[rawMarkerIndex]
: null;
// Normally, we would look at the marker phase, but some types require special
// handling. See if these need to be handled first.
if (data) {
switch (data.type) {
case 'Network': {
// Network markers are similar to tracing markers in that they also
// normally exist in pairs of start/stop markers. But unlike tracing
// markers they have a duration and "startTime/endTime" properties like
// more generic markers. Lastly they're always adjacent: the start
// markers ends when the stop markers starts.
//
// The timestamps on the start and end markers describe two
// non-overlapping parts of the same load. The start marker has a
// duration from channel-creation until Start (i.e. AsyncOpen()). The
// end marker has a duration from AsyncOpen time until OnStopRequest.
// In the merged marker, we want to represent the entire duration, from
// channel-creation until OnStopRequest.
//
// |--- start marker ---|--- stop marker with timings ---|
//
// Usually the start marker is very small. It's emitted mostly to know
// about the start of the request. But most of the interesting bits are
// in the stop marker.
const ensureMessage =
'Network markers are assumed to have a start and end time.';
if (data.status === 'STATUS_START') {
openNetworkMarkers.set(data.id, rawMarkerIndex);
} else {
// End status can be any status other than 'STATUS_START'. They are
// either 'STATUS_STOP', 'STATUS_REDIRECT' or 'STATUS_CANCEL'.
const endData = data;
const startIndex = openNetworkMarkers.get(data.id);
if (startIndex !== undefined) {
// A start marker matches this end marker.
openNetworkMarkers.delete(data.id);
// We know this startIndex points to a Network marker.
const startData: NetworkPayload = (rawMarkers.data[
startIndex
]: any);
const startStartTime = ensureExists(
rawMarkers.startTime[startIndex],
ensureMessage
);
const endStartTime = ensureExists(maybeStartTime, ensureMessage);
const endEndTime = ensureExists(maybeEndTime, ensureMessage);
addMarker([startIndex, rawMarkerIndex], {
start: startStartTime,
end: endEndTime,
name: stringTable.getString(name),
category,
threadId: markerThreadId,
data: {
...endData,
startTime: startStartTime,
fetchStart: endStartTime,
cause: startData.cause || endData.cause,
},
});
} else {
// There's no start marker matching this end marker. This means an
// abstract marker exists before the start of the profile.
const start = Math.min(
threadRange.start,
ensureExists(
maybeStartTime,
'Network markers are assumed to have a start time.'
)
);
const end = ensureExists(maybeEndTime, ensureMessage);
addMarker([rawMarkerIndex], {
start,
end,
name: stringTable.getString(name),
category,
threadId: markerThreadId,
data: {
...endData,
startTime: start,
fetchStart: endData.startTime,
cause: endData.cause,
},
incomplete: true,
});
}
}
continue;
}
case 'CompositorScreenshot': {
// Screenshot markers are already ordered. In the raw marker table,
// they're Instant markers, but since they're valid until the following
// raw marker of the same type and the same window, we convert them to
// Interval markers with a a start and end time.
const { windowID } = data;
const previousScreenshotMarker =
previousScreenshotMarkers.get(windowID);
if (previousScreenshotMarker !== undefined) {
previousScreenshotMarkers.delete(windowID);
const previousStartTime = ensureExists(
rawMarkers.startTime[previousScreenshotMarker],
'Expected to find a start time for a screenshot marker.'
);
const thisStartTime = ensureExists(
maybeStartTime,
'The CompositorScreenshot is assumed to have a start time.'
);
const data = rawMarkers.data[previousScreenshotMarker];
const markerThreadId = rawMarkers.threadId
? rawMarkers.threadId[previousScreenshotMarker]
: null;
addMarker([previousScreenshotMarker], {
start: previousStartTime,
end: thisStartTime,
name: 'CompositorScreenshot',
category,
threadId: markerThreadId,
data,
});
}
if (
stringTable.getString(name) ===
'CompositorScreenshotWindowDestroyed'
) {
// This marker is added when a window is destroyed. In this case we
// don't want to store it as the start of the next compositor
// marker. But we do want to keep it, so we break out of the
// switch/case so that the standard processing happens.
break;
} else {
previousScreenshotMarkers.set(windowID, rawMarkerIndex);
}
continue;
}
case 'IPC': {
const sharedData = ipcCorrelations.get(
// Older profiles don't have a tid, but they also don't have the IPC markers.
threadId || 0,
rawMarkerIndex
);
if (!sharedData) {
// Sometimes a thread can include multiple IPC markers from the same
// IPC message. We only show a single marker from that thread. That
// way, there won't be any duplicate markers for the same IPC
// message in the thread.
continue;
}
if (
data.direction === 'sending' &&
data.phase === 'transferEnd' &&
sharedData.sendStartTime !== undefined
) {
// This marker corresponds to the end of the data transfer on the
// sender's IO thread, but we also have a marker for the *start* of
// the transfer. Since we don't need to show two markers for the same
// IPC message on the same thread, skip this one.
continue;
}
let name = data.direction === 'sending' ? 'IPCOut' : 'IPCIn';
if (data.sync) {
name = 'Sync' + name;
}
let start = ensureExists(
data.startTime,
'Expected IPC startTime to exist in the payload.'
);
let end = start;
let incomplete = true;
if (
sharedData.startTime !== undefined &&
sharedData.endTime !== undefined
) {
start = sharedData.startTime;
end = sharedData.endTime;
incomplete = false;
}
const allData = {
...data,
...sharedData,
niceDirection:
data.direction === 'sending'
? `sent to ${sharedData.recvThreadName || data.otherPid}`
: `received from ${sharedData.sendThreadName || data.otherPid}`,
};
// TODO - How do I get the other rawMarkerIndexes
addMarker([rawMarkerIndex], {
start,
end,
name,
category,
threadId: markerThreadId,
data: allData,
incomplete,
});
continue;
}
default:
// Do nothing;
}
}
switch (phase) {
case INSTANT:
addMarker([rawMarkerIndex], {
start: ensureExists(
maybeStartTime,
'An Instant marker did not have a startTime.'
),
end: null,
name: stringTable.getString(name),
category,
threadId: markerThreadId,
data,
});
break;
case INTERVAL:
{
const startTime = ensureExists(
maybeStartTime,
'An Interval marker did not have a startTime.'
);
const endTime = ensureExists(
maybeEndTime,
'An Interval marker did not have a startTime.'
);
// Add a marker with a zero duration
addMarker([rawMarkerIndex], {
start: startTime,
end: endTime,
name: stringTable.getString(name),
category,
threadId: markerThreadId,
data,
});
}
break;
case INTERVAL_START:
{
let openMarkersForName = openIntervalMarkers.get(name);
if (!openMarkersForName) {
openMarkersForName = [];
openIntervalMarkers.set(name, openMarkersForName);
}
openMarkersForName.push(rawMarkerIndex);
}
break;
case INTERVAL_END:
{
const openMarkersForName = openIntervalMarkers.get(name);
let startIndex;
if (openMarkersForName) {
startIndex = openMarkersForName.pop();
}
const endTime = ensureExists(
maybeEndTime,
'An IntervalEnd marker did not have an endTime'
);
if (startIndex !== undefined) {
// A start marker matches this end marker.
const start = ensureExists(
rawMarkers.startTime[startIndex],
'An IntervalStart marker did not have a startTime'
);
addMarker([startIndex, rawMarkerIndex], {
start,
name: stringTable.getString(name),
end: endTime,
category,
threadId: markerThreadId,
data: mergeIntervalData(rawMarkers.data[startIndex], data),
});
} else {
// No matching "start" marker has been encountered before this "end".
// This means it was issued before the capture started. Here we create
// an "incomplete" marker which will be truncated at the starting end
// since we don't know exactly when it started.
// Note we won't have additional data (eg the cause stack) for this
// marker because that data is contained in the "start" marker.
// Also note that the end marker could occur before the
// first sample. In that case it'll become a dot marker at
// the location of the end marker. Otherwise we'll use the
// time of the first sample as its start.
const start = Math.min(endTime, threadRange.start);
addMarker([rawMarkerIndex], {
start,
name: stringTable.getString(name),
end: endTime,
category,
threadId: markerThreadId,
data,
incomplete: true,
});
}
}
break;
default:
throw new Error('Unhandled marker phase type.');
}
}
const endOfThread = threadRange.end;
// Loop over "start" markers without any "end" markers.
for (const markerBucket of openIntervalMarkers.values()) {
for (const startIndex of markerBucket) {
const start = ensureExists(
rawMarkers.startTime[startIndex],
'Encountered a marker without a startTime. Eventually this needs to be handled ' +
'for phase-style markers.'
);
addMarker([startIndex], {
start,
end: Math.max(endOfThread, start),
name: stringTable.getString(rawMarkers.name[startIndex]),
data: rawMarkers.data[startIndex],
category: rawMarkers.category[startIndex],
threadId: rawMarkers.threadId ? rawMarkers.threadId[startIndex] : null,
incomplete: true,
});
}
}
for (const startIndex of openNetworkMarkers.values()) {
const startTime = ensureExists(
rawMarkers.startTime[startIndex],
'Network markers are assumed to always have a start time.'
);
addMarker([startIndex], {
start: startTime,
end: Math.max(endOfThread, startTime),
name: stringTable.getString(rawMarkers.name[startIndex]),
category: rawMarkers.category[startIndex],
threadId: rawMarkers.threadId ? rawMarkers.threadId[startIndex] : null,
data: rawMarkers.data[startIndex],
incomplete: true,
});
}
// And we also need to add the "last screenshot markers".
for (const previousScreenshotMarker of previousScreenshotMarkers.values()) {
const start = ensureExists(
rawMarkers.startTime[previousScreenshotMarker],
'Expected to find a CompositorScreenshot marker with a start time.'
);
addMarker([previousScreenshotMarker], {
start,
end: Math.max(endOfThread, start),
name: 'CompositorScreenshot',
category: rawMarkers.category[previousScreenshotMarker],
threadId: rawMarkers.threadId
? rawMarkers.threadId[previousScreenshotMarker]
: null,
data: rawMarkers.data[previousScreenshotMarker],
});
}
return { markers, markerIndexToRawMarkerIndexes };
}
/**
* This function filters markers from a thread's raw marker table using the
* range specified as parameter. It's not used by the normal marker filtering
* pipeline, but is used in profile comparison.
*/
export function filterRawMarkerTableToRange(
markerTable: RawMarkerTable,
derivedMarkerInfo: DerivedMarkerInfo,
rangeStart: number,
rangeEnd: number
): RawMarkerTable {
const newMarkerTable = getEmptyRawMarkerTable();
const newThreadId = [];
if (markerTable.threadId) {
newMarkerTable.threadId = newThreadId;
}
const filteredMarkerIndexes = filterRawMarkerTableIndexesToRange(
markerTable,
derivedMarkerInfo,
rangeStart,
rangeEnd
);
for (const index of filteredMarkerIndexes) {
newMarkerTable.startTime.push(markerTable.startTime[index]);
newMarkerTable.endTime.push(markerTable.endTime[index]);
newMarkerTable.phase.push(markerTable.phase[index]);
newMarkerTable.name.push(markerTable.name[index]);
newMarkerTable.data.push(markerTable.data[index]);
newMarkerTable.category.push(markerTable.category[index]);
if (markerTable.threadId) {
newThreadId.push(markerTable.threadId[index]);
}
newMarkerTable.length++;
}
return newMarkerTable;
}
/**
* This function filters a raw marker table to just the indexes that are in range.
* This is done by going the derived Marker[] list, and finding the original markers
* that make up that marker.
*/
export function filterRawMarkerTableIndexesToRange(
markerTable: RawMarkerTable,
derivedMarkerInfo: DerivedMarkerInfo,
rangeStart: number,
rangeEnd: number
): IndexIntoRawMarkerTable[] {
const { markers, markerIndexToRawMarkerIndexes } = derivedMarkerInfo;
const inRange: Set<IndexIntoRawMarkerTable> = new Set();
for (let markerIndex = 0; markerIndex < markers.length; markerIndex++) {
const { start, end } = markers[markerIndex];
if (end === null) {
if (start < rangeEnd && start >= rangeStart) {
for (const rawIndex of markerIndexToRawMarkerIndexes[markerIndex]) {
inRange.add(rawIndex);
}
}
} else {
if (start < rangeEnd && end >= rangeStart) {
for (const rawIndex of markerIndexToRawMarkerIndexes[markerIndex]) {
inRange.add(rawIndex);
}
}
}
}
return [...inRange].sort((a, b) => a - b);
}
/**
* This function filters markers from a thread's raw marker table using the
* range and marker indexes array specified as parameters.
*
* Uses `filterRawMarkerTableIndexesToRange` function and excludes
* markers in `markersToDelete` set.