feat: add cumulativeCPUUsage to AppMetrics and CPUUsage (#41819)

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`.
This commit is contained in:
rcombs 2024-06-11 07:38:03 -07:00 committed by GitHub
parent f35a755086
commit d8e4579e3c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 25 additions and 6 deletions

View file

@ -1270,10 +1270,17 @@ 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();
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",