Chromium Code Reviews| Index: chrome/browser/resource_coordinator/resource_coordinator_render_process_probe.cc |
| diff --git a/chrome/browser/resource_coordinator/resource_coordinator_render_process_probe.cc b/chrome/browser/resource_coordinator/resource_coordinator_render_process_probe.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3ec437b30cd46604202b8b29ae6928424fe86f0c |
| --- /dev/null |
| +++ b/chrome/browser/resource_coordinator/resource_coordinator_render_process_probe.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/resource_coordinator/resource_coordinator_render_process_probe.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/values.h" |
| +#include "build/build_config.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "services/resource_coordinator/public/cpp/resource_coordinator_interface.h" |
| +#include "services/resource_coordinator/public/interfaces/coordination_unit.mojom.h" |
| + |
| +#if defined(OS_MACOSX) |
| +#include "content/public/browser/browser_child_process_host.h" |
| +#endif |
| + |
| +namespace resource_coordinator { |
| + |
| +namespace { |
| + |
| +const int kMeasurementIntervalInSeconds = 5; |
|
lpy
2017/07/07 17:49:10
I would like to understand the reason we chose 5 s
|
| + |
| +base::LazyInstance<ResourceCoordinatorRenderProcessProbe>::DestructorAtExit |
| + g_probe = LAZY_INSTANCE_INITIALIZER; |
| + |
| +} // namespace |
| + |
| +ResourceCoordinatorRenderProcessProbe::ProcessMetricsState:: |
| + ProcessMetricsState() = default; |
| + |
| +ResourceCoordinatorRenderProcessProbe::ProcessMetricsState:: |
| + ~ProcessMetricsState() = default; |
| + |
| +ResourceCoordinatorRenderProcessProbe::ResourceCoordinatorRenderProcessProbe() = |
| + default; |
| + |
| +ResourceCoordinatorRenderProcessProbe:: |
| + ~ResourceCoordinatorRenderProcessProbe() = default; |
| + |
| +// static |
| +ResourceCoordinatorRenderProcessProbe* |
| +ResourceCoordinatorRenderProcessProbe::GetInstance() { |
| + return g_probe.Pointer(); |
| +} |
| + |
| +void ResourceCoordinatorRenderProcessProbe::StartGatherCycle() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + repeating_timer_.Start( |
| + FROM_HERE, base::TimeDelta(), this, |
| + &ResourceCoordinatorRenderProcessProbe::CollectAndSendMeasurements); |
| +} |
| + |
| +void ResourceCoordinatorRenderProcessProbe::CollectAndSendMeasurements() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + ++ticks_; |
| + |
| + for (content::RenderProcessHost::iterator rph_iter = |
| + content::RenderProcessHost::AllHostsIterator(); |
| + !rph_iter.IsAtEnd(); rph_iter.Advance()) { |
| + content::RenderProcessHost* host = rph_iter.GetCurrentValue(); |
| + base::ProcessHandle handle = host->GetHandle(); |
| + // Process may not be valid yet. |
| + if (handle == base::kNullProcessHandle) { |
| + continue; |
| + } |
| + |
| + auto& process_metric_state = process_metrics_states_[handle]; |
| + |
| + if (process_metric_state.metrics.get() == nullptr) { |
| +#if defined(OS_MACOSX) |
| + process_metric_state.metrics = base::ProcessMetrics::CreateProcessMetrics( |
| + handle, content::BrowserChildProcessHost::GetPortProvider()); |
| +#else |
| + process_metric_state.metrics = |
| + base::ProcessMetrics::CreateProcessMetrics(handle); |
| +#endif |
| + } |
| + |
| + process_metric_state.last_tick_active = ticks_; |
| + double cpu_usage = process_metric_state.metrics->GetCPUUsage(); |
| + |
| + host->GetProcessResourceCoordinator()->SetPropertyAndPropagate( |
| + mojom::PropertyType::kCPUUsage, |
| + base::MakeUnique<base::Value>(cpu_usage)); |
| + } |
| + |
| + // Remove dead render processes. If it was not measured at the current |tick_| |
| + // then it is assumed that it is no longer tracked. |
| + std::vector<base::ProcessHandle> dead_process_handles; |
| + for (auto& process_metric_state : process_metrics_states_) { |
| + if (process_metric_state.second.last_tick_active != ticks_) { |
| + dead_process_handles.push_back(process_metric_state.first); |
| + } |
| + } |
| + for (base::ProcessHandle handle : dead_process_handles) { |
| + process_metrics_states_.erase(handle); |
| + } |
| + |
| + repeating_timer_.Start( |
| + FROM_HERE, base::TimeDelta::FromSeconds(kMeasurementIntervalInSeconds), |
| + this, &ResourceCoordinatorRenderProcessProbe::CollectAndSendMeasurements); |
| +} |
| + |
| +} // namespace resource_coordinator |