From dfd3a0dfb3b35d73b75023a1447604ebd721af12 Mon Sep 17 00:00:00 2001 From: Zachary Marion Date: Sat, 16 Dec 2023 00:27:03 -0800 Subject: [PATCH] Fix bug in selectQueueToTakeFromNext for trace profiles (#450) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I have been taking a lot of profiles using the Hermes profiler, but I noticed that they sometimes to not show up properly. After debugging what exactly was going on, I realized it was because the logic in `selectQueueToTakeFromNext` only checks for name, instead of the key for the event. I had a bunch of events with the name `anonymous` that were getting improperly exited before they should have been due to this logic. This fix makes the code more robust if there are added "args" which differentiate an event from another (as is the case in Hermes profiles), however it would still be an issue if they key just defaults to the name. Example profile before: Screenshot 2023-12-15 at 12 54 04 AM What it should look like (in Perfetto): Screenshot 2023-12-15 at 8 51 38 AM After the fix: Screenshot 2023-12-15 at 12 54 29 AM --- .../simultaneous-anonymous-calls.json | 14 +++++ .../__snapshots__/trace-event.test.ts.snap | 62 +++++++++++++++++++ src/import/trace-event.test.ts | 4 ++ src/import/trace-event.ts | 4 +- 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 sample/profiles/trace-event/simultaneous-anonymous-calls.json diff --git a/sample/profiles/trace-event/simultaneous-anonymous-calls.json b/sample/profiles/trace-event/simultaneous-anonymous-calls.json new file mode 100644 index 000000000..4f878de53 --- /dev/null +++ b/sample/profiles/trace-event/simultaneous-anonymous-calls.json @@ -0,0 +1,14 @@ +[ + {"pid": 0, "tid": 0, "ph": "B", "name": "anonymous", "ts": 0}, + {"pid": 0, "tid": 0, "ph": "B", "name": "anonymous", "ts": 1, "args": { "parent": 1 }}, + {"pid": 0, "tid": 0, "ph": "B", "name": "function1", "ts": 1, "args": { "parent": 2 }}, + {"pid": 0, "tid": 0, "ph": "B", "name": "anonymous", "ts": 1, "args": { "parent": 3 }}, + {"pid": 0, "tid": 0, "ph": "E", "name": "anonymous", "ts": 3, "args": { "parent": 3 }}, + {"pid": 0, "tid": 0, "ph": "E", "name": "function1", "ts": 3, "args": { "parent": 2 }}, + {"pid": 0, "tid": 0, "ph": "E", "name": "anonymous", "ts": 3, "args": { "parent": 1 }}, + {"pid": 0, "tid": 0, "ph": "B", "name": "anonymous", "ts": 3, "args": { "parent": 1 }}, + {"pid": 0, "tid": 0, "ph": "B", "name": "anonymous", "ts": 3, "args": { "parent": 8 }}, + {"pid": 0, "tid": 0, "ph": "E", "name": "anonymous", "ts": 5, "args": { "parent": 8 }}, + {"pid": 0, "tid": 0, "ph": "E", "name": "anonymous", "ts": 5, "args": { "parent": 1 }}, + {"pid": 0, "tid": 0, "ph": "E", "name": "anonymous", "ts": 5} +] \ No newline at end of file diff --git a/src/import/__snapshots__/trace-event.test.ts.snap b/src/import/__snapshots__/trace-event.test.ts.snap index b5c3d4c4e..efc317ad3 100644 --- a/src/import/__snapshots__/trace-event.test.ts.snap +++ b/src/import/__snapshots__/trace-event.test.ts.snap @@ -172,6 +172,68 @@ exports[`importTraceEvents bad E events: indexToView 1`] = `0`; exports[`importTraceEvents bad E events: profileGroup.name 1`] = `"too-many-end-events.json"`; +exports[`importTraceEvents different number of start and end calls to same function at same timestamp 1`] = ` +Object { + "frames": Array [ + Frame { + "col": undefined, + "file": undefined, + "key": "anonymous", + "line": undefined, + "name": "anonymous", + "selfWeight": 1, + "totalWeight": 5, + }, + Frame { + "col": undefined, + "file": undefined, + "key": "anonymous {\\"parent\\":1}", + "line": undefined, + "name": "anonymous {\\"parent\\":1}", + "selfWeight": 0, + "totalWeight": 4, + }, + Frame { + "col": undefined, + "file": undefined, + "key": "function1 {\\"parent\\":2}", + "line": undefined, + "name": "function1 {\\"parent\\":2}", + "selfWeight": 0, + "totalWeight": 2, + }, + Frame { + "col": undefined, + "file": undefined, + "key": "anonymous {\\"parent\\":3}", + "line": undefined, + "name": "anonymous {\\"parent\\":3}", + "selfWeight": 2, + "totalWeight": 2, + }, + Frame { + "col": undefined, + "file": undefined, + "key": "anonymous {\\"parent\\":8}", + "line": undefined, + "name": "anonymous {\\"parent\\":8}", + "selfWeight": 2, + "totalWeight": 2, + }, + ], + "name": "pid 0, tid 0", + "stacks": Array [ + "anonymous 1.00µs", + "anonymous;anonymous {\\"parent\\":1};function1 {\\"parent\\":2};anonymous {\\"parent\\":3} 2.00µs", + "anonymous;anonymous {\\"parent\\":1};anonymous {\\"parent\\":8} 2.00µs", + ], +} +`; + +exports[`importTraceEvents different number of start and end calls to same function at same timestamp: indexToView 1`] = `0`; + +exports[`importTraceEvents different number of start and end calls to same function at same timestamp: profileGroup.name 1`] = `"simultaneous-anonymous-calls.json"`; + exports[`importTraceEvents end event with empty stack 1`] = ` Object { "frames": Array [ diff --git a/src/import/trace-event.test.ts b/src/import/trace-event.test.ts index 1fdd5f9b1..6a8e956c6 100644 --- a/src/import/trace-event.test.ts +++ b/src/import/trace-event.test.ts @@ -47,6 +47,10 @@ test('importTraceEvents bad E events', async () => { await checkProfileSnapshot('./sample/profiles/trace-event/too-many-end-events.json') }) +test('importTraceEvents different number of start and end calls to same function at same timestamp', async () => { + await checkProfileSnapshot('./sample/profiles/trace-event/simultaneous-anonymous-calls.json') +}) + test('importTraceEvents event re-ordering', async () => { await checkProfileSnapshot('./sample/profiles/trace-event/must-retain-original-order.json') }) diff --git a/src/import/trace-event.ts b/src/import/trace-event.ts index 0fa5ff573..642707075 100644 --- a/src/import/trace-event.ts +++ b/src/import/trace-event.ts @@ -103,12 +103,12 @@ function selectQueueToTakeFromNext( // If we got here, the 'B' event queue and the 'E' event queue have events at // the front with equal timestamps. - // If the front of the 'E' queue matches the front of the 'B' queue by name, + // If the front of the 'E' queue matches the front of the 'B' queue by key, // then it means we have a zero duration event. Process the 'B' queue first // to ensure it opens before we try to close it. // // Otherwise, process the 'E' queue first. - return bFront.name === eFront.name ? 'B' : 'E' + return frameInfoForEvent(bFront).key === frameInfoForEvent(eFront).key ? 'B' : 'E' } function convertToEventQueues(events: ImportableTraceEvent[]): [BTraceEvent[], ETraceEvent[]] {