Skip to content

Commit f0e5983

Browse files
committed
Select the default selected thread by the thread activity
Previously we were only getting the first tab process main thread as the selected thread, which wasn't the track we want most of the time.
1 parent c3f48b7 commit f0e5983

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed

src/actions/receive-profile.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ export function finalizeFullProfileView(
319319
const selectedThreadIndexes = initializeSelectedThreadIndex(
320320
maybeSelectedThreadIndexes,
321321
getVisibleThreads(tracksWithOrder, hiddenTracks),
322-
profile
322+
profile,
323+
threadActivityScores
323324
);
324325

325326
let timelineType = null;
@@ -1549,7 +1550,8 @@ export function changeTabFilter(tabID: TabID | null): ThunkAction<void> {
15491550
const selectedThreadIndexes = initializeSelectedThreadIndex(
15501551
null, // maybeSelectedThreadIndexes
15511552
getVisibleThreads(tracksWithOrder, hiddenTracks),
1552-
profile
1553+
profile,
1554+
threadActivityScores
15531555
);
15541556

15551557
// If the currently selected tab is only visible when the selected track

src/profile-logic/profile-data.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import type {
9595
TabID,
9696
} from 'firefox-profiler/types';
9797
import type { CallNodeInfo, SuffixOrderIndex } from './call-node-info';
98+
import type { ThreadActivityScore } from './tracks';
9899

99100
/**
100101
* Various helpers for dealing with the profile as a data structure.
@@ -1286,8 +1287,12 @@ export function getTimeRangeIncludingAllThreads(
12861287
return completeRange;
12871288
}
12881289

1289-
export function defaultThreadOrder(threads: RawThread[]): ThreadIndex[] {
1290-
const threadOrder = threads.map((thread, i) => i);
1290+
export function defaultThreadOrder(
1291+
visibleThreadIndexes: ThreadIndex[],
1292+
threads: RawThread[],
1293+
threadActivityScores: Array<ThreadActivityScore>
1294+
): ThreadIndex[] {
1295+
const threadOrder = [...visibleThreadIndexes];
12911296

12921297
// Note: to have a consistent behavior independant of the sorting algorithm,
12931298
// we need to be careful that the comparator function is consistent:
@@ -1299,7 +1304,10 @@ export function defaultThreadOrder(threads: RawThread[]): ThreadIndex[] {
12991304
const nameB = threads[b].name;
13001305

13011306
if (nameA === nameB) {
1302-
return a - b;
1307+
return (
1308+
threadActivityScores[b].boostedSampleScore -
1309+
threadActivityScores[a].boostedSampleScore
1310+
);
13031311
}
13041312

13051313
// Put the compositor/renderer thread last.

src/profile-logic/tracks.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,15 @@ export function initializeGlobalTrackOrder(
745745
export function initializeSelectedThreadIndex(
746746
selectedThreadIndexes: Set<ThreadIndex> | null,
747747
visibleThreadIndexes: ThreadIndex[],
748-
profile: Profile
748+
profile: Profile,
749+
threadActivityScores: Array<ThreadActivityScore>
749750
): Set<ThreadIndex> {
750751
if (selectedThreadIndexes === null) {
751-
return getDefaultSelectedThreadIndexes(visibleThreadIndexes, profile);
752+
return getDefaultSelectedThreadIndexes(
753+
visibleThreadIndexes,
754+
profile,
755+
threadActivityScores
756+
);
752757
}
753758

754759
// Filter out hidden threads from the set of selected threads.
@@ -758,7 +763,11 @@ export function initializeSelectedThreadIndex(
758763
);
759764
if (visibleSelectedThreadIndexes.size === 0) {
760765
// No selected threads were visible. Fall back to default selection.
761-
return getDefaultSelectedThreadIndexes(visibleThreadIndexes, profile);
766+
return getDefaultSelectedThreadIndexes(
767+
visibleThreadIndexes,
768+
profile,
769+
threadActivityScores
770+
);
762771
}
763772
return visibleSelectedThreadIndexes;
764773
}
@@ -767,7 +776,8 @@ export function initializeSelectedThreadIndex(
767776
// order.
768777
function getDefaultSelectedThreadIndexes(
769778
visibleThreadIndexes: ThreadIndex[],
770-
profile: Profile
779+
profile: Profile,
780+
threadActivityScores: Array<ThreadActivityScore>
771781
): Set<ThreadIndex> {
772782
if (profile.meta.initialSelectedThreads !== undefined) {
773783
return new Set(
@@ -785,10 +795,11 @@ function getDefaultSelectedThreadIndexes(
785795
})
786796
);
787797
}
788-
const visibleThreads = visibleThreadIndexes.map(
789-
(threadIndex) => profile.threads[threadIndex]
798+
const defaultThread = _findDefaultThread(
799+
visibleThreadIndexes,
800+
profile.threads,
801+
threadActivityScores
790802
);
791-
const defaultThread = _findDefaultThread(visibleThreads);
792803
const defaultThreadIndex = profile.threads.indexOf(defaultThread);
793804
if (defaultThreadIndex === -1) {
794805
throw new Error('Expected to find a thread index to select.');
@@ -1309,16 +1320,30 @@ function _computeThreadSampleScore(
13091320
return nonIdleSampleCount * referenceCPUDeltaPerInterval;
13101321
}
13111322

1312-
function _findDefaultThread(threads: RawThread[]): RawThread | null {
1323+
function _findDefaultThread(
1324+
visibleThreadIndexes: ThreadIndex[],
1325+
threads: RawThread[],
1326+
threadActivityScores: Array<ThreadActivityScore>
1327+
): RawThread | null {
13131328
if (threads.length === 0) {
13141329
// Tests may have no threads.
13151330
return null;
13161331
}
1317-
const contentThreadId = threads.findIndex(
1318-
(thread) => thread.name === 'GeckoMain' && thread.processType === 'tab'
1332+
1333+
const threadOrder = defaultThreadOrder(
1334+
visibleThreadIndexes,
1335+
threads,
1336+
threadActivityScores
13191337
);
1338+
1339+
// Try to find a tab process with the highest activity score. If it can't
1340+
// find one, select the first thread with the highest one.
13201341
const defaultThreadIndex =
1321-
contentThreadId !== -1 ? contentThreadId : defaultThreadOrder(threads)[0];
1342+
threadOrder.find(
1343+
(threadIndex) =>
1344+
threads[threadIndex].name === 'GeckoMain' &&
1345+
threads[threadIndex].processType === 'tab'
1346+
) ?? threadOrder[0];
13221347

13231348
return threads[defaultThreadIndex];
13241349
}

src/test/store/receive-profile.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ describe('actions/receive-profile', function () {
265265

266266
store.dispatch(viewProfile(profile));
267267
expect(getHumanReadableTracks(store.getState())).toEqual([
268-
'hide [thread GeckoMain tab]',
269268
'show [thread GeckoMain tab] SELECTED',
270269
'hide [thread GeckoMain tab]',
270+
'hide [thread GeckoMain tab]',
271271
]);
272272
});
273273

@@ -396,8 +396,8 @@ describe('actions/receive-profile', function () {
396396

397397
store.dispatch(viewProfile(profile));
398398
expect(getHumanReadableTracks(store.getState())).toEqual([
399-
'hide [thread GeckoMain tab]',
400399
'show [thread GeckoMain tab] SELECTED',
400+
'hide [thread GeckoMain tab]',
401401
]);
402402
});
403403

0 commit comments

Comments
 (0)