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 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/process/process.h" |
| 11 #include "base/process/process_handle.h" |
| 12 #include "base/process/process_metrics.h" |
| 13 #include "base/time/time.h" |
| 14 |
| 15 #if defined(OS_MACOSX) |
| 16 #include "services/service_manager/public/cpp/standalone_service/mach_broker.h" |
| 17 #endif |
| 18 |
| 19 #if defined(OS_WIN) |
| 20 #include <windows.h> |
| 21 #endif |
| 22 |
| 23 namespace service_manager { |
| 24 class ServiceContextRef; |
| 25 } |
| 26 |
| 27 namespace resource_coordinator { |
| 28 |
| 29 struct CoordinationUnitID; |
| 30 |
| 31 namespace { |
| 32 |
| 33 const int kCPUProfilingIntervalInSeconds = 5; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 ProcessCoordinationUnitImpl::ProcessCoordinationUnitImpl( |
| 38 const CoordinationUnitID& id, |
| 39 std::unique_ptr<service_manager::ServiceContextRef> service_ref) |
| 40 : CoordinationUnitImpl(id, std::move(service_ref)), cpu_usage_(-1.0) { |
| 41 // ProcessCoordinationUnit ids should correspond to its pid |
| 42 base::ProcessId pid = id.id; |
| 43 #if defined(OS_WIN) |
| 44 base::Process process = |
| 45 base::Process::OpenWithAccess(pid, PROCESS_QUERY_INFORMATION); |
| 46 #else |
| 47 base::Process process = base::Process::Open(pid); |
| 48 #endif |
| 49 base::ProcessHandle process_handle = process.Handle(); |
| 50 |
| 51 #if defined(OS_MACOSX) |
| 52 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics( |
| 53 process_handle, |
| 54 service_manager::MachBroker::GetInstance()->port_provider()); |
| 55 #else |
| 56 process_metrics_ = base::ProcessMetrics::CreateProcessMetrics(process_handle); |
| 57 #endif |
| 58 |
| 59 repeating_timer_.Start(FROM_HERE, base::TimeDelta(), this, |
| 60 &ProcessCoordinationUnitImpl::MeasureProcessCPUUsage); |
| 61 } |
| 62 |
| 63 ProcessCoordinationUnitImpl::~ProcessCoordinationUnitImpl() = default; |
| 64 |
| 65 void ProcessCoordinationUnitImpl::MeasureProcessCPUUsage() { |
| 66 cpu_usage_ = process_metrics_->GetPlatformIndependentCPUUsage(); |
| 67 |
| 68 repeating_timer_.Start( |
| 69 FROM_HERE, base::TimeDelta::FromSeconds(kCPUProfilingIntervalInSeconds), |
| 70 this, &ProcessCoordinationUnitImpl::MeasureProcessCPUUsage); |
| 71 } |
| 72 |
| 73 double ProcessCoordinationUnitImpl::GetCPUUsageForTesting() { |
| 74 return cpu_usage_; |
| 75 } |
| 76 |
| 77 } // namespace resource_coordinator |
OLD | NEW |