Skip to content
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

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion core/computed/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PageDependencyGraph {
if (data.fromTrace) {
const traceEngineResult = await TraceEngineResult.request({trace}, context);
const {graph} = await LanternPageDependencyGraph.createGraphFromTrace(
mainThreadEvents, trace, traceEngineResult, URL);
trace, traceEngineResult, URL);
return graph;
}

Expand Down
94 changes: 92 additions & 2 deletions core/lib/lantern/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,102 @@ 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 rendererPidToTid = new Map();
Copy link
Collaborator Author

@connorjclark connorjclark Jun 7, 2024

Choose a reason for hiding this comment

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

rendererPidToTid here gets constructed with different results than TraceProcessor was doing ...

consider the first test in core/test/computed/metrics/speed-index-test.js (should compute a simulated value) as an example, TraceProcessor only has one renderer in this map, but this version has two. the extra one is the initial about:blank renderer. this seems like something to avoid ... it happens b/c the about:blank is in topLevelRendererIds. I'm not yet sure how TraceProcessor ends up excluding it.

the result is more events end up in mainThreadEvents

Copy link
Collaborator Author

@connorjclark connorjclark Jun 7, 2024

Choose a reason for hiding this comment

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

ok I think const mainFramePids = Meta.mainFrameNavigations.map(nav => nav.pid); instead of using topLevelRendererIds kinda resolves this. Not sure what would happen w/o any navigation though .... so I'll fall back to topLevelRendererIds in that case.


for (const pid of Meta.topLevelRendererIds) {
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 this._filteredTraceSort(trace.traceEvents, e => rendererPidToTid.get(e.pid) === e.tid);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

w/o _filteredTraceSort(instead of just array.filter) no tests fail ... so is it needed?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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[]} */
Expand Down
16 changes: 2 additions & 14 deletions core/test/lib/lantern/metrics/metric-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {ProcessedTrace} from '../../../../computed/processed-trace.js';
import {TraceEngineResult} from '../../../../computed/trace-engine-result.js';
import {createProcessedNavigation} from '../../../../lib/lantern/lantern.js';
import {PageDependencyGraph} from '../../../../lib/lantern/page-dependency-graph.js';
Expand All @@ -17,18 +16,6 @@ import {getURLArtifactFromDevtoolsLog} from '../../../test-utils.js';

// TODO(15841): remove usage of Lighthouse code to create test data

/**
* @param {LH.Artifacts.TraceEngineResult} traceEngineResult
* @param {LH.Artifacts.URL} theURL
* @param {LH.Trace} trace
* @param {LH.Artifacts.ComputedContext} context
*/
async function createGraph(traceEngineResult, theURL, trace, context) {
const {mainThreadEvents} = await ProcessedTrace.request(trace, context);
return PageDependencyGraph.createGraphFromTrace(
mainThreadEvents, trace, traceEngineResult, theURL);
}

/**
* @param {{trace: LH.Trace, devtoolsLog: LH.DevtoolsLog, settings?: LH.Config.Settings, URL?: LH.Artifacts.URL}} opts
*/
Expand All @@ -39,7 +26,8 @@ async function getComputationDataFromFixture({trace, devtoolsLog, settings, URL}

const context = {settings, computedCache: new Map()};
const traceEngineResult = await TraceEngineResult.request({trace}, context);
const {graph, records} = await createGraph(traceEngineResult, URL, trace, context);
const {graph, records} =
await PageDependencyGraph.createGraphFromTrace(trace, traceEngineResult, URL);
const processedNavigation = createProcessedNavigation(traceEngineResult);
const networkAnalysis = NetworkAnalyzer.analyze(records);
const simulator = Simulator.createSimulator({...settings, networkAnalysis});
Expand Down
Loading