-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core(lantern): extract main thread events w/o TraceProcessor #16049
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -785,12 +785,105 @@ class PageDependencyGraph { | |
} | ||
|
||
/** | ||
* @param {LH.TraceEvent[]} mainThreadEvents | ||
* Sorts and filters trace events by timestamp and respecting the nesting structure inherent to | ||
* parent/child event relationships. | ||
* | ||
* @param {LH.TraceEvent[]} traceEvents | ||
* @param {(e: LH.TraceEvent) => boolean} filter | ||
*/ | ||
static _filteredTraceSort(traceEvents, filter) { | ||
// create an array of the indices that we want to keep | ||
const indices = []; | ||
for (let srcIndex = 0; srcIndex < traceEvents.length; srcIndex++) { | ||
if (filter(traceEvents[srcIndex])) { | ||
indices.push(srcIndex); | ||
} | ||
} | ||
|
||
// Sort by ascending timestamp first. | ||
indices.sort((indexA, indexB) => traceEvents[indexA].ts - traceEvents[indexB].ts); | ||
|
||
// Now we find groups with equal timestamps and order them by their nesting structure. | ||
for (let i = 0; i < indices.length - 1; i++) { | ||
const ts = traceEvents[indices[i]].ts; | ||
const tsGroupIndices = [i]; | ||
for (let j = i + 1; j < indices.length; j++) { | ||
if (traceEvents[indices[j]].ts !== ts) break; | ||
tsGroupIndices.push(j); | ||
} | ||
|
||
// We didn't find any other events with the same timestamp, just keep going. | ||
if (tsGroupIndices.length === 1) continue; | ||
|
||
// Sort the group by other criteria and replace our index array with it. | ||
const finalIndexOrder = TraceProcessor._sortTimestampEventGroup( | ||
tsGroupIndices, | ||
indices, | ||
i, | ||
traceEvents | ||
); | ||
indices.splice(i, finalIndexOrder.length, ...finalIndexOrder); | ||
// We just sorted this set of identical timestamps, so skip over the rest of the group. | ||
// -1 because we already have i++. | ||
i += tsGroupIndices.length - 1; | ||
} | ||
|
||
// create a new array using the target indices from previous sort step | ||
const sorted = []; | ||
for (let i = 0; i < indices.length; i++) { | ||
sorted.push(traceEvents[indices[i]]); | ||
} | ||
|
||
return sorted; | ||
} | ||
|
||
/** | ||
* @param {LH.Trace} trace | ||
* @param {LH.Artifacts.TraceEngineResult} traceEngineResult | ||
* @return {LH.TraceEvent[]} | ||
*/ | ||
static _collectMainThreadEvents(trace, traceEngineResult) { | ||
const Meta = traceEngineResult.data.Meta; | ||
const mainFramePids = Meta.mainFrameNavigations.length | ||
? new Set(Meta.mainFrameNavigations.map(nav => nav.pid)) | ||
: Meta.topLevelRendererIds; | ||
|
||
const rendererPidToTid = new Map(); | ||
for (const pid of mainFramePids) { | ||
const threads = Meta.threadsInProcess.get(pid) ?? []; | ||
|
||
let found = false; | ||
for (const [tid, thread] of threads) { | ||
if (thread.args.name === 'CrRendererMain') { | ||
rendererPidToTid.set(pid, tid); | ||
found = true; | ||
break; | ||
} | ||
} | ||
|
||
if (found) continue; | ||
|
||
// `CrRendererMain` can be missing if chrome is launched with the `--single-process` flag. | ||
// In this case, page tasks will be run in the browser thread. | ||
for (const [tid, thread] of threads) { | ||
if (thread.args.name === 'CrBrowserMain') { | ||
rendererPidToTid.set(pid, tid); | ||
found = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return trace.traceEvents.filter(e => rendererPidToTid.get(e.pid) === e.tid); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. w/o There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's needed. I looked at how we use main thread events in lantern and we just look for top level CPU tasks. Those should really never have the same timestamp which seems to be the purpose of that function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems convincing |
||
|
||
/** | ||
* @param {LH.Trace} trace | ||
* @param {LH.Artifacts.TraceEngineResult} traceEngineResult | ||
* @param {LH.Artifacts.URL} URL | ||
*/ | ||
static async createGraphFromTrace(mainThreadEvents, trace, traceEngineResult, URL) { | ||
static async createGraphFromTrace(trace, traceEngineResult, URL) { | ||
const mainThreadEvents = this._collectMainThreadEvents(trace, traceEngineResult); | ||
const workerThreads = this._findWorkerThreads(trace); | ||
|
||
/** @type {Lantern.NetworkRequest[]} */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use
Meta.topLevelRendererIds
all the time? Seems like it represents what we want?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see #16049 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approving cuz I'm kinda just throwing ideas against the wall but I also saw
rendererProcessesByFrame
in the trace processor. It isn't fed by main frame navigations but it is fed byCommitLoad
which seems similar.