Skip to content

Commit

Permalink
Only compute the max CPU delta per millisecond, not per interval.
Browse files Browse the repository at this point in the history
  • Loading branch information
mstange committed Dec 13, 2024
1 parent da31d2c commit c4c372e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
13 changes: 6 additions & 7 deletions src/profile-logic/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ function _computeMaxVariableCPUCyclesPerMs(
* Returns 1234567 * 3, i.e. "3703701 cycles per sample if each sample ticks at
* the declared 3ms interval and the CPU usage is at the observed maximum".
*/
export function computeMaxCPUDeltaPerInterval(profile: Profile): number | null {
export function computeMaxCPUDeltaPerMs(profile: Profile): number {
const sampleUnits = profile.meta.sampleUnits;
if (!sampleUnits) {
return null;
return 1;
}

const interval = profile.meta.interval;
Expand All @@ -86,21 +86,20 @@ export function computeMaxCPUDeltaPerInterval(profile: Profile): number | null {
switch (threadCPUDeltaUnit) {
case 'µs':
case 'ns': {
const cpuDeltaTimeUnitMultiplier =
getCpuDeltaTimeUnitMultiplier(threadCPUDeltaUnit);
return cpuDeltaTimeUnitMultiplier * interval;
const deltaUnitPerMs = getCpuDeltaTimeUnitMultiplier(threadCPUDeltaUnit);
return deltaUnitPerMs;
}
case 'variable CPU cycles': {
const maxThreadCPUDeltaPerMs = _computeMaxVariableCPUCyclesPerMs(
profile.threads,
interval
);
return maxThreadCPUDeltaPerMs * interval;
return maxThreadCPUDeltaPerMs;
}
default:
throw assertExhaustiveCheck(
threadCPUDeltaUnit,
'Unhandled threadCPUDelta unit in computeMaxCPUDeltaPerInterval.'
'Unhandled threadCPUDelta unit in computeMaxCPUDeltaPerMs.'
);
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/profile-logic/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ const AUDIO_THREAD_SAMPLE_SCORE_BOOST_FACTOR = 40;
export function computeThreadActivityScore(
profile: Profile,
thread: Thread,
maxCpuDeltaPerInterval: number | null
maxCpuDeltaPerMs: number
): ThreadActivityScore {
const isEssentialFirefoxThread = _isEssentialFirefoxThread(thread);
const isInParentProcess = thread.processType === 'default';
Expand All @@ -1186,7 +1186,7 @@ export function computeThreadActivityScore(
const sampleScore = _computeThreadSampleScore(
profile,
thread,
maxCpuDeltaPerInterval
maxCpuDeltaPerMs
);
const boostedSampleScore = isInterestingEvenWithMinimalActivity
? sampleScore * AUDIO_THREAD_SAMPLE_SCORE_BOOST_FACTOR
Expand Down Expand Up @@ -1225,12 +1225,12 @@ function _isFirefoxMediaThreadWhichIsUsuallyIdle(thread: Thread): boolean {
// If the thread does not have CPU delta information, we compute a
// "CPU-delta-like" number based on the number of samples which are in a
// non-idle category.
// If the profile has no cpu delta units, the return value is the number of
// non-idle samples.
// If the profile has no cpu delta units, the return value is based on the
// number of non-idle samples.
function _computeThreadSampleScore(
{ meta }: Profile,
{ samples, stackTable }: Thread,
maxCpuDeltaPerInterval: number | null
maxCpuDeltaPerMs: number
): number {
if (meta.sampleUnits && samples.threadCPUDelta) {
// Sum up all CPU deltas in this thread, to compute a total
Expand All @@ -1251,7 +1251,8 @@ function _computeThreadSampleScore(
(stack) =>
stack !== null && stackTable.category[stack] !== idleCategoryIndex
).length;
return nonIdleSampleCount * (maxCpuDeltaPerInterval ?? 1);
const maxCpuDeltaPerInterval = maxCpuDeltaPerMs * meta.interval;
return nonIdleSampleCount * maxCpuDeltaPerInterval;
}

function _findDefaultThread(threads: Thread[]): Thread | null {
Expand Down
15 changes: 1 addition & 14 deletions src/selectors/cpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@

import { createSelector } from 'reselect';

import {
getProfile,
getThreads,
getSampleUnits,
getMeta,
getCounter,
} from './profile';
import { computeMaxCPUDeltaPerInterval } from 'firefox-profiler/profile-logic/cpu';
import { getThreads, getSampleUnits, getMeta, getCounter } from './profile';

import type { Selector } from 'firefox-profiler/types';

Expand Down Expand Up @@ -43,9 +36,3 @@ export const getAreThereAnyProcessCPUCounters: Selector<boolean> =
counters !== null &&
counters.some((counter) => counter.category === 'CPU')
);

export const getMaxThreadCPUDeltaPerMs: Selector<number> = createSelector(
getProfile,
(profile) =>
(computeMaxCPUDeltaPerInterval(profile) ?? 0) * profile.meta.interval
);
16 changes: 7 additions & 9 deletions src/selectors/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,22 +718,20 @@ export const getHiddenTrackCount: Selector<HiddenTrackCount> = createSelector(
}
);

export const getMaxCPUDeltaPerInterval: Selector<number | null> =
createSelector(getProfile, CPU.computeMaxCPUDeltaPerInterval);
export const getMaxThreadCPUDeltaPerMs: Selector<number> = createSelector(
getProfile,
CPU.computeMaxCPUDeltaPerMs
);

export const getThreadActivityScores: Selector<Array<ThreadActivityScore>> =
createSelector(
getProfile,
getMaxCPUDeltaPerInterval,
(profile, maxCpuDeltaPerInterval) => {
getMaxThreadCPUDeltaPerMs,
(profile, maxCpuDeltaPerMs) => {
const { threads } = profile;

return threads.map((thread) =>
Tracks.computeThreadActivityScore(
profile,
thread,
maxCpuDeltaPerInterval
)
Tracks.computeThreadActivityScore(profile, thread, maxCpuDeltaPerMs)
);
}
);
Expand Down

0 comments on commit c4c372e

Please sign in to comment.