| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "services/resource_coordinator/coordination_unit/process_coordination_u
nit_impl.h" | 5 #include "services/resource_coordinator/coordination_unit/process_coordination_u
nit_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/process/process.h" | 10 #include "base/process/process.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const int kCPUProfilingIntervalInSeconds = 5; | 33 const int kCPUProfilingIntervalInSeconds = 5; |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 ProcessCoordinationUnitImpl::ProcessCoordinationUnitImpl( | 37 ProcessCoordinationUnitImpl::ProcessCoordinationUnitImpl( |
| 38 const CoordinationUnitID& id, | 38 const CoordinationUnitID& id, |
| 39 std::unique_ptr<service_manager::ServiceContextRef> service_ref) | 39 std::unique_ptr<service_manager::ServiceContextRef> service_ref) |
| 40 : CoordinationUnitImpl(id, std::move(service_ref)), cpu_usage_(-1.0) { | 40 : CoordinationUnitImpl(id, std::move(service_ref)) { |
| 41 // ProcessCoordinationUnit ids should correspond to its pid | 41 // ProcessCoordinationUnit ids should correspond to its pid |
| 42 base::ProcessId pid = id.id; | 42 base::ProcessId pid = id.id; |
| 43 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| 44 base::Process process = | 44 base::Process process = |
| 45 base::Process::OpenWithAccess(pid, PROCESS_QUERY_INFORMATION); | 45 base::Process::OpenWithAccess(pid, PROCESS_QUERY_INFORMATION); |
| 46 #else | 46 #else |
| 47 base::Process process = base::Process::Open(pid); | 47 base::Process process = base::Process::Open(pid); |
| 48 #endif | 48 #endif |
| 49 base::ProcessHandle process_handle = process.Handle(); | 49 base::ProcessHandle process_handle = process.Handle(); |
| 50 | 50 |
| 51 #if defined(OS_MACOSX) | 51 #if defined(OS_MACOSX) |
| 52 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics( | 52 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics( |
| 53 process_handle, | 53 process_handle, |
| 54 service_manager::MachBroker::GetInstance()->port_provider()); | 54 service_manager::MachBroker::GetInstance()->port_provider()); |
| 55 #else | 55 #else |
| 56 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics(process_handle); | 56 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics(process_handle); |
| 57 #endif | 57 #endif |
| 58 | 58 |
| 59 repeating_timer_.Start(FROM_HERE, base::TimeDelta(), this, | 59 repeating_timer_.Start(FROM_HERE, base::TimeDelta(), this, |
| 60 &ProcessCoordinationUnitImpl::MeasureProcessCPUUsage); | 60 &ProcessCoordinationUnitImpl::MeasureProcessCPUUsage); |
| 61 } | 61 } |
| 62 | 62 |
| 63 ProcessCoordinationUnitImpl::~ProcessCoordinationUnitImpl() = default; | 63 ProcessCoordinationUnitImpl::~ProcessCoordinationUnitImpl() = default; |
| 64 | 64 |
| 65 void ProcessCoordinationUnitImpl::MeasureProcessCPUUsage() { | 65 void ProcessCoordinationUnitImpl::MeasureProcessCPUUsage() { |
| 66 cpu_usage_ = process_metrics_->GetPlatformIndependentCPUUsage(); | 66 double cpu_usage = process_metrics_->GetPlatformIndependentCPUUsage(); |
| 67 SetProperty(mojom::PropertyType::kProcessCPUUsage, base::Value(cpu_usage)); |
| 67 | 68 |
| 68 repeating_timer_.Start( | 69 repeating_timer_.Start( |
| 69 FROM_HERE, base::TimeDelta::FromSeconds(kCPUProfilingIntervalInSeconds), | 70 FROM_HERE, base::TimeDelta::FromSeconds(kCPUProfilingIntervalInSeconds), |
| 70 this, &ProcessCoordinationUnitImpl::MeasureProcessCPUUsage); | 71 this, &ProcessCoordinationUnitImpl::MeasureProcessCPUUsage); |
| 71 } | 72 } |
| 72 | 73 |
| 73 double ProcessCoordinationUnitImpl::GetCPUUsageForTesting() { | |
| 74 return cpu_usage_; | |
| 75 } | |
| 76 | |
| 77 } // namespace resource_coordinator | 74 } // namespace resource_coordinator |
| OLD | NEW |