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