Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 76 additions & 4 deletions src/profile-logic/import/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ function getThreadInfo(
if (threadNameEvent) {
thread.name = threadNameEvent.args.name;
thread.isMainThread =
thread.name.startsWith('Cr') && thread.name.endsWith('Main');
(thread.name.startsWith('Cr') && thread.name.endsWith('Main')) ||
(!!chunk.pid && chunk.pid === chunk.tid);
}

const processNameEvent = findEvent<ProcessNameEvent>(
Expand Down Expand Up @@ -940,6 +941,14 @@ function extractMarkers(
},
];

// Map to store begin event detail field for pairing with end events.
// For async events (b/e), key is "pid:tid:id:name"
// For duration events (B/E), key is "pid:tid:name"
const beginEventDetail: Map<string, string> = new Map();
Copy link
Member

@canova canova Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was very surprised that we needed a map here, because normally we merge the payloads of start and end markers:

// 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 === null) {
return endData;
}
if (endData === null) {
return startData;
}
return {
...startData,
...endData,
};
}

And this should have been enough. So locally I tried to remove it and noticed that it actually regressed the state.

Then I accidentally opened a can of worms, because it looks like even though we don't have marker payloads in a chrome event, we still assign a dummy payload to it here:

const newData = {
...argData,
type: name,
category: event.cat,
};

It looks like it's just for the category, but wait, we don't actually show the category anywhere in the UI since we don't have a marker schema!
And the marker category itself is assigned to Other by default!

So because of the fact that we always assign a payload even when it doesn't have a payload is breaking this marker payload merge logic...

So even though I don't like this beginEventDetail map, since the chrome importer was broken before this PR, it's unfair to ask for a bigger change from you. I will merge this PR and then I will follow-up myself with proper fixes that:

  1. Put the category properly to the marker categories.
  2. Remove the dummy payload if the event doesn't have any data.
  3. Remove this beginEventDetail all together with all the logic around it.

Another issue I had with this map is that it wouldn't work well with the nested markers. I don't know if it's possible to have any from clang or any other tool, but I think that's a source of bugs too. But considering that this map will go away, we shouldn't really spend a lot of time on it.


// Track whether we've added the EventWithDetail schema
let hasEventWithDetailSchema = false;

for (const [name, events] of eventsByName.entries()) {
if (
name === 'Profile' ||
Expand Down Expand Up @@ -988,11 +997,35 @@ function extractMarkers(
const { thread } = threadInfo;
const { markers } = thread;
let argData:
| (object & { type2?: unknown; category2?: unknown })
| (object & { type2?: unknown; category2?: unknown; detail?: string })
| null = null;
if ('args' in event && event.args && typeof event.args === 'object') {
argData = event.args.data || null;
// Some trace events have args.data, but others have args fields directly
// (e.g., "Source" markers have args.detail).
if (event.args.data) {
argData = event.args.data;
} else if (
'detail' in event.args &&
typeof event.args.detail === 'string'
) {
argData = { detail: event.args.detail };
}
}

// For end events (E/e), try to use the detail from the corresponding begin event
if ((event.ph === 'E' || event.ph === 'e') && !argData) {
// Generate key for looking up the begin event detail
// For async events (b/e), use id; for duration events (B/E), use name only
const key =
event.ph === 'e' && 'id' in event
? `${event.pid}:${event.tid}:${event.id}:${name}`
: `${event.pid}:${event.tid}:${name}`;
Comment on lines +1019 to +1022
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we extract this key generation logic and use it in all 3 places?
Edit: After thinking more about that I wanna remove all these as per my other comment, but they are for a follow-up.

const detail = beginEventDetail.get(key);
if (detail) {
argData = { detail };
}
}

markers.name.push(stringTable.indexForString(name));
markers.category.push(otherCategoryIndex);

Expand All @@ -1003,9 +1036,32 @@ function extractMarkers(
argData.category2 = argData.category;
}

// Add EventWithDetail schema the first time we encounter a detail field
if (argData?.detail && !hasEventWithDetailSchema) {
profile.meta.markerSchema.push({
// Generic schema for Chrome trace event markers with a detail field.
// This is used when compiling with clang -ftime-trace=file.json, which
// generates Source markers, ParseDeclarationOrFunctionDefinition markers,
// and similar compiler events with file paths or location details.
name: 'EventWithDetail',
chartLabel: '{marker.data.detail}',
tooltipLabel: '{marker.name}: {marker.data.detail}',
tableLabel: '{marker.data.detail}',
display: ['marker-chart', 'marker-table'],
fields: [
{
key: 'detail',
label: 'Details',
format: 'string',
},
],
});
hasEventWithDetailSchema = true;
}

const newData = {
...argData,
type: name,
type: argData?.detail ? 'EventWithDetail' : name,
category: event.cat,
};

Expand All @@ -1026,13 +1082,29 @@ function extractMarkers(
markers.startTime.push(time);
markers.endTime.push(null);
markers.phase.push(INTERVAL_START);

// Store the detail field from begin event so it can be used for the corresponding end event
if (argData?.detail) {
const key =
event.ph === 'b' && 'id' in event
? `${event.pid}:${event.tid}:${event.id}:${name}`
: `${event.pid}:${event.tid}:${name}`;
beginEventDetail.set(key, argData.detail);
}
} else if (event.ph === 'E' || event.ph === 'e') {
// Duration or Async Event End
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.nso4gcezn7n1
// The 'E' and 'e' phase stand for "end", and is the Chrome equivalent of IntervalEnd.
markers.startTime.push(null);
markers.endTime.push(time);
markers.phase.push(INTERVAL_END);

// Clean up the stored begin event detail
const key =
event.ph === 'e' && 'id' in event
? `${event.pid}:${event.tid}:${event.id}:${name}`
: `${event.pid}:${event.tid}:${name}`;
beginEventDetail.delete(key);
} else {
// Instant Event
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.lenwiilchoxp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445793,7 +445793,7 @@ Object {
"resource": Array [],
"source": Array [],
},
"isMainThread": false,
"isMainThread": true,
"markers": Object {
"category": Array [
0,
Expand Down