OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/resource_coordinator/coordination_unit/process_coordination_u
nit_impl.h" |
| 6 #include "base/process/process_handle.h" |
| 7 #include "base/process/process_metrics.h" |
| 8 #include "base/time/time.h" |
| 9 #include "services/resource_coordinator/public/cpp/coordination_unit_id.h" |
| 10 #include "services/service_manager/public/cpp/service_context_ref.h" |
| 11 |
| 12 #if defined(OS_MACOSX) |
| 13 #include "services/service_manager/public/cpp/standalone_service/mach_broker.h" |
| 14 #endif |
| 15 |
| 16 namespace resource_coordinator { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const int kCPUProfilingIntervalInSeconds = 5; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 ProcessCoordinationUnitImpl::ProcessCoordinationUnitImpl( |
| 25 const CoordinationUnitID& id, |
| 26 std::unique_ptr<service_manager::ServiceContextRef> service_ref) |
| 27 : CoordinationUnitImpl(id, std::move(service_ref)), cpu_usage_(-1.0) { |
| 28 // ProcessCoordinationUnit ids should correspond to its pid |
| 29 base::ProcessHandle pid = id.id; |
| 30 |
| 31 #if defined(OS_MACOSX) |
| 32 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics( |
| 33 pid, service_manager::MachBroker::GetInstance()->port_provider()); |
| 34 #else |
| 35 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics(pid); |
| 36 #endif |
| 37 |
| 38 ScheduleProcessCPUProfiling(base::TimeDelta()); |
| 39 } |
| 40 |
| 41 ProcessCoordinationUnitImpl::~ProcessCoordinationUnitImpl() {} |
| 42 |
| 43 void ProcessCoordinationUnitImpl::ScheduleProcessCPUProfiling( |
| 44 base::TimeDelta delay) { |
| 45 repeating_timer_.Start(FROM_HERE, delay, this, |
| 46 &ProcessCoordinationUnitImpl::MeasureProcessCPUUsage); |
| 47 } |
| 48 |
| 49 void ProcessCoordinationUnitImpl::MeasureProcessCPUUsage() { |
| 50 cpu_usage_ = process_metrics_->GetPlatformIndependentCPUUsage(); |
| 51 |
| 52 ScheduleProcessCPUProfiling( |
| 53 base::TimeDelta::FromSeconds(kCPUProfilingIntervalInSeconds)); |
| 54 } |
| 55 |
| 56 double ProcessCoordinationUnitImpl::GetCPUUsageForTesting() { |
| 57 return cpu_usage_; |
| 58 } |
| 59 |
| 60 } // namespace resource_coordinator |
OLD | NEW |