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

Improve how we compute the power consumption numbers from the Firefox Profiler output #2220

Merged
merged 4 commits into from
Dec 9, 2024
Merged
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
66 changes: 52 additions & 14 deletions lib/firefox/geckoProfiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,69 @@ import { BrowserError } from '../support/errors.js';
const delay = ms => new Promise(res => setTimeout(res, ms));
const log = intel.getLogger('browsertime.firefox');

// Return power usage in Wh
function computePowerSum(counter) {
// Return power usage in Wh for a single counter.
function computeCounterPowerSum(counter) {
let sum = 0;
// Older Firefoxes see https://github.com/sitespeedio/sitespeed.io/issues/3944#issuecomment-1871090793
if (counter.sample_groups) {
for (const groups of counter.sample_groups) {
const countIndex = groups.samples.schema.count;
for (const sample of groups.samples.data) {
sum += sample[countIndex];
// NOTE: We intentionally ignore the first index of the counters as they
// might contain incorrect values.
for (
let sampleIndex = 1;
sampleIndex < groups.samples.data.length;
sampleIndex++
) {
sum += groups.samples.data[sampleIndex][countIndex];
}
}
} else {
const countIndex = counter.samples.schema.count;
for (const sample of counter.samples.data) {
sum += sample[countIndex];
// NOTE: We intentionally ignore the first index of the counters as they
// might contain incorrect values.
for (
let sampleIndex = 1;
sampleIndex < counter.samples.data.length;
sampleIndex++
) {
sum += counter.samples.data[sampleIndex][countIndex];
}
}

return sum * 1e-12;
}

// Return the power usage by the whole profile including the sub processes.
// The return value is in Wh value.
function computeFullProfilePower(profile) {
let power = 0;
for (const counter of profile.counters) {
if (
counter.category === 'power' &&
// If power counters starts with "Power:", it means that there are several
// individual components that Firefox records. For Linux and Windows,
// intel CPUs might produce 'CPU package' to show the whole power
// consumption of the CPU. But on top of that it also shows some individual
// components like, DRAM, iGPU, individual CPU cores etc. We don't want
// to include them as it might produce double the power values it normally consumes.
//
// Linux track names: https://searchfox.org/mozilla-central/rev/5ad94327fc03d0e5bc6aa81c0d7d113c2dccf947/tools/profiler/core/PowerCounters-linux.cpp#48-70
// Windows track names: https://searchfox.org/mozilla-central/rev/5ad94327fc03d0e5bc6aa81c0d7d113c2dccf947/tools/profiler/core/PowerCounters-win.cpp#32-55
(!counter.name.startsWith('Power:') ||
counter.name === 'Power: CPU package')
) {
power += computeCounterPowerSum(counter);
}
}

for (const subprocessProfile of profile.processes) {
power += computeFullProfilePower(subprocessProfile);
}

return power;
}

/**
* Timeout a promise after ms. Use promise.race to compete
* about the timeout and the promise.
Expand Down Expand Up @@ -213,20 +256,15 @@ export class GeckoProfiler {
geckoProfilerDefaults.features
);
if (chosenFeatures.includes('power')) {
log.info('Collecting CPU power consumtion');
log.info('Collecting CPU power consumption');
const profile = JSON.parse(
await storageManager.readData(
`geckoProfile-${index}.json`,
path.join(pathToFolder(url, options))
)
);
let power = 0;
for (const counter of profile.counters) {
if (counter.category === 'power') {
power += computePowerSum(counter);
}
}
result.powerConsumption = Number(power);

result.powerConsumption = computeFullProfilePower(profile);
} else {
log.warning(
'Missing power setting in geckoProfilerParams.features so power will not be collected'
Expand Down
Loading