Skip to content

Commit

Permalink
feat: add cumulativeCPUUsage to AppMetrics and CPUUsage
Browse files Browse the repository at this point in the history
This allows apps to measure their CPU usage over any given period
without worrying about other calls affecting the output,
as they would with `percentCPUUsage`.
  • Loading branch information
rcombs committed Apr 18, 2024
1 parent b683754 commit 39d04bf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/api/structures/cpu-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
14 changes: 10 additions & 4 deletions shell/browser/api/electron_api_app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1276,10 +1276,16 @@ std::vector<gin_helper::Dictionary> 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()) {
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",
Expand Down
11 changes: 9 additions & 2 deletions shell/common/api/electron_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,15 @@ v8::Local<v8::Value> 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()) {
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
Expand Down
1 change: 1 addition & 0 deletions spec/api-app-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions spec/api-process-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
Expand Down Expand Up @@ -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');
});
});
Expand Down

0 comments on commit 39d04bf

Please sign in to comment.