diff --git a/docs/api/structures/cpu-usage.md b/docs/api/structures/cpu-usage.md index 00c267533a1e..645da762bab6 100644 --- a/docs/api/structures/cpu-usage.md +++ b/docs/api/structures/cpu-usage.md @@ -2,6 +2,8 @@ * `percentCPUUsage` number - Percentage of CPU used since the last call to getCPUUsage. First call returns 0. +* `cumulativeCPUUsage` number (optional) - Total seconds of CPU time used since process + startup. * `idleWakeupsPerSecond` number - The number of average idle CPU wakeups per second since the last call to getCPUUsage. First call returns 0. Will always return 0 on Windows. diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index 1b51aae4c45b..b9ab944b8de0 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -1270,10 +1270,17 @@ std::vector App::GetAppMetrics(v8::Isolate* isolate) { auto pid_dict = gin_helper::Dictionary::CreateEmpty(isolate); auto cpu_dict = gin_helper::Dictionary::CreateEmpty(isolate); - double usage = - process_metric.second->metrics->GetPlatformIndependentCPUUsage() - .value_or(0); - cpu_dict.Set("percentCPUUsage", usage / processor_count); + // Default usage percentage to 0 for compatibility + double usagePercent = 0; + if (auto usage = process_metric.second->metrics->GetCumulativeCPUUsage(); + usage.has_value()) { + cpu_dict.Set("cumulativeCPUUsage", usage->InSecondsF()); + usagePercent = + process_metric.second->metrics->GetPlatformIndependentCPUUsage( + *usage); + } + + cpu_dict.Set("percentCPUUsage", usagePercent / processor_count); #if !BUILDFLAG(IS_WIN) cpu_dict.Set("idleWakeupsPerSecond", diff --git a/shell/common/api/electron_bindings.cc b/shell/common/api/electron_bindings.cc index 1a13f3979cf6..929f467b3b81 100644 --- a/shell/common/api/electron_bindings.cc +++ b/shell/common/api/electron_bindings.cc @@ -274,8 +274,15 @@ v8::Local ElectronBindings::GetCPUUsage( v8::Isolate* isolate) { auto dict = gin_helper::Dictionary::CreateEmpty(isolate); int processor_count = base::SysInfo::NumberOfProcessors(); - double usage = metrics->GetPlatformIndependentCPUUsage().value_or(0); - dict.Set("percentCPUUsage", usage / processor_count); + + // Default usage percentage to 0 for compatibility + double usagePercent = 0; + if (auto usage = metrics->GetCumulativeCPUUsage(); usage.has_value()) { + dict.Set("cumulativeCPUUsage", usage->InSecondsF()); + usagePercent = metrics->GetPlatformIndependentCPUUsage(*usage); + } + + dict.Set("percentCPUUsage", usagePercent / processor_count); // NB: This will throw NOTIMPLEMENTED() on Windows // For backwards compatibility, we'll return 0 diff --git a/spec/api-app-spec.ts b/spec/api-app-spec.ts index 4b3f9d185c13..491f1cc859b1 100644 --- a/spec/api-app-spec.ts +++ b/spec/api-app-spec.ts @@ -1442,6 +1442,7 @@ describe('app module', () => { types.push(entry.type); expect(entry.cpu).to.have.ownProperty('percentCPUUsage').that.is.a('number'); + expect(entry.cpu).to.have.ownProperty('cumulativeCPUUsage').that.is.a('number'); expect(entry.cpu).to.have.ownProperty('idleWakeupsPerSecond').that.is.a('number'); expect(entry.memory).to.have.property('workingSetSize').that.is.greaterThan(0); diff --git a/spec/api-process-spec.ts b/spec/api-process-spec.ts index 909f6f5244de..3a9a068cee8c 100644 --- a/spec/api-process-spec.ts +++ b/spec/api-process-spec.ts @@ -26,6 +26,7 @@ describe('process module', () => { it('returns a cpu usage object', async () => { const cpuUsage = await w.webContents.executeJavaScript('process.getCPUUsage()'); expect(cpuUsage.percentCPUUsage).to.be.a('number'); + expect(cpuUsage.cumulativeCPUUsage).to.be.a('number'); expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number'); }); }); @@ -124,6 +125,7 @@ describe('process module', () => { it('returns a cpu usage object', () => { const cpuUsage = process.getCPUUsage(); expect(cpuUsage.percentCPUUsage).to.be.a('number'); + expect(cpuUsage.cumulativeCPUUsage).to.be.a('number'); expect(cpuUsage.idleWakeupsPerSecond).to.be.a('number'); }); });