| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/arc/metrics/arc_metrics_service.h" | 5 #include "components/arc/metrics/arc_metrics_service.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "chromeos/dbus/session_manager_client.h" | 13 #include "chromeos/dbus/session_manager_client.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 const int kRequestProcessListPeriodInMinutes = 5; | 17 const int kRequestProcessListPeriodInMinutes = 5; |
| 16 const char kArcProcessNamePrefix[] = "org.chromium.arc."; | 18 const char kArcProcessNamePrefix[] = "org.chromium.arc."; |
| 17 const char kGmsProcessNamePrefix[] = "com.google.android.gms"; | 19 const char kGmsProcessNamePrefix[] = "com.google.android.gms"; |
| 18 const char kBootProgressEnableScreen[] = "boot_progress_enable_screen"; | 20 const char kBootProgressEnableScreen[] = "boot_progress_enable_screen"; |
| 19 | 21 |
| 20 } // namespace | 22 } // namespace |
| 21 | 23 |
| 22 namespace arc { | 24 namespace arc { |
| 23 | 25 |
| 24 ArcMetricsService::ArcMetricsService(ArcBridgeService* bridge_service) | 26 ArcMetricsService::ArcMetricsService(ArcBridgeService* bridge_service) |
| 25 : ArcService(bridge_service), binding_(this), weak_ptr_factory_(this) { | 27 : ArcService(bridge_service), |
| 26 arc_bridge_service()->AddObserver(this); | 28 binding_(this), |
| 29 process_observer_(this), |
| 30 weak_ptr_factory_(this) { |
| 31 arc_bridge_service()->metrics()->AddObserver(this); |
| 32 arc_bridge_service()->process()->AddObserver(&process_observer_); |
| 27 oom_kills_monitor_.Start(); | 33 oom_kills_monitor_.Start(); |
| 28 } | 34 } |
| 29 | 35 |
| 30 ArcMetricsService::~ArcMetricsService() { | 36 ArcMetricsService::~ArcMetricsService() { |
| 31 DCHECK(CalledOnValidThread()); | 37 DCHECK(CalledOnValidThread()); |
| 32 arc_bridge_service()->RemoveObserver(this); | 38 arc_bridge_service()->process()->RemoveObserver(&process_observer_); |
| 39 arc_bridge_service()->metrics()->RemoveObserver(this); |
| 33 } | 40 } |
| 34 | 41 |
| 35 bool ArcMetricsService::CalledOnValidThread() { | 42 bool ArcMetricsService::CalledOnValidThread() { |
| 36 // Make sure access to the Chrome clipboard is happening in the UI thread. | 43 // Make sure access to the Chrome clipboard is happening in the UI thread. |
| 37 return thread_checker_.CalledOnValidThread(); | 44 return thread_checker_.CalledOnValidThread(); |
| 38 } | 45 } |
| 39 | 46 |
| 40 void ArcMetricsService::OnMetricsInstanceReady() { | 47 void ArcMetricsService::OnInstanceReady() { |
| 41 VLOG(2) << "Start metrics service."; | 48 VLOG(2) << "Start metrics service."; |
| 42 // Retrieve ARC start time from session manager. | 49 // Retrieve ARC start time from session manager. |
| 43 chromeos::SessionManagerClient* session_manager_client = | 50 chromeos::SessionManagerClient* session_manager_client = |
| 44 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); | 51 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); |
| 45 session_manager_client->GetArcStartTime( | 52 session_manager_client->GetArcStartTime( |
| 46 base::Bind(&ArcMetricsService::OnArcStartTimeRetrieved, | 53 base::Bind(&ArcMetricsService::OnArcStartTimeRetrieved, |
| 47 weak_ptr_factory_.GetWeakPtr())); | 54 weak_ptr_factory_.GetWeakPtr())); |
| 48 } | 55 } |
| 49 | 56 |
| 50 void ArcMetricsService::OnMetricsInstanceClosed() { | 57 void ArcMetricsService::OnInstanceClosed() { |
| 51 VLOG(2) << "Close metrics service."; | 58 VLOG(2) << "Close metrics service."; |
| 52 DCHECK(CalledOnValidThread()); | 59 DCHECK(CalledOnValidThread()); |
| 53 if (binding_.is_bound()) | 60 if (binding_.is_bound()) |
| 54 binding_.Unbind(); | 61 binding_.Unbind(); |
| 55 } | 62 } |
| 56 | 63 |
| 57 void ArcMetricsService::OnProcessInstanceReady() { | 64 void ArcMetricsService::OnProcessInstanceReady() { |
| 58 VLOG(2) << "Start updating process list."; | 65 VLOG(2) << "Start updating process list."; |
| 59 timer_.Start( | 66 timer_.Start(FROM_HERE, |
| 60 FROM_HERE, | 67 base::TimeDelta::FromMinutes(kRequestProcessListPeriodInMinutes), |
| 61 base::TimeDelta::FromMinutes(kRequestProcessListPeriodInMinutes), | 68 this, &ArcMetricsService::RequestProcessList); |
| 62 this, | |
| 63 &ArcMetricsService::RequestProcessList); | |
| 64 } | 69 } |
| 65 | 70 |
| 66 void ArcMetricsService::OnProcessInstanceClosed() { | 71 void ArcMetricsService::OnProcessInstanceClosed() { |
| 67 VLOG(2) << "Stop updating process list."; | 72 VLOG(2) << "Stop updating process list."; |
| 68 timer_.Stop(); | 73 timer_.Stop(); |
| 69 } | 74 } |
| 70 | 75 |
| 71 void ArcMetricsService::RequestProcessList() { | 76 void ArcMetricsService::RequestProcessList() { |
| 72 mojom::ProcessInstance* process_instance = | 77 mojom::ProcessInstance* process_instance = |
| 73 arc_bridge_service()->process_instance(); | 78 arc_bridge_service()->process()->instance(); |
| 74 if (!process_instance) { | 79 if (!process_instance) { |
| 75 LOG(ERROR) << "No process instance found before RequestProcessList"; | 80 LOG(ERROR) << "No process instance found before RequestProcessList"; |
| 76 return; | 81 return; |
| 77 } | 82 } |
| 78 | 83 |
| 79 VLOG(2) << "RequestProcessList"; | 84 VLOG(2) << "RequestProcessList"; |
| 80 process_instance->RequestProcessList( | 85 process_instance->RequestProcessList(base::Bind( |
| 81 base::Bind(&ArcMetricsService::ParseProcessList, | 86 &ArcMetricsService::ParseProcessList, weak_ptr_factory_.GetWeakPtr())); |
| 82 weak_ptr_factory_.GetWeakPtr())); | |
| 83 } | 87 } |
| 84 | 88 |
| 85 void ArcMetricsService::ParseProcessList( | 89 void ArcMetricsService::ParseProcessList( |
| 86 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes) { | 90 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes) { |
| 87 int running_app_count = 0; | 91 int running_app_count = 0; |
| 88 for (const auto& process : processes) { | 92 for (const auto& process : processes) { |
| 89 const mojo::String& process_name = process->process_name; | 93 const mojo::String& process_name = process->process_name; |
| 90 const mojom::ProcessState& process_state = process->process_state; | 94 const mojom::ProcessState& process_state = process->process_state; |
| 91 | 95 |
| 92 // Processes like the ARC launcher and intent helper are always running | 96 // Processes like the ARC launcher and intent helper are always running |
| 93 // and not counted as apps running by users. With the same reasoning, | 97 // and not counted as apps running by users. With the same reasoning, |
| 94 // GMS (Google Play Services) and its related processes are skipped as | 98 // GMS (Google Play Services) and its related processes are skipped as |
| 95 // well. The process_state check below filters out system processes, | 99 // well. The process_state check below filters out system processes, |
| 96 // services, apps that are cached because they've run before. | 100 // services, apps that are cached because they've run before. |
| 97 if (base::StartsWith(process_name.get(), kArcProcessNamePrefix, | 101 if (base::StartsWith(process_name.get(), kArcProcessNamePrefix, |
| 98 base::CompareCase::SENSITIVE) || | 102 base::CompareCase::SENSITIVE) || |
| 99 base::StartsWith(process_name.get(), kGmsProcessNamePrefix, | 103 base::StartsWith(process_name.get(), kGmsProcessNamePrefix, |
| 100 base::CompareCase::SENSITIVE) || | 104 base::CompareCase::SENSITIVE) || |
| 101 process_state != mojom::ProcessState::TOP) { | 105 process_state != mojom::ProcessState::TOP) { |
| 102 VLOG(2) << "Skipped " << process_name << " " << process_state; | 106 VLOG(2) << "Skipped " << process_name << " " << process_state; |
| 103 } else { | 107 } else { |
| 104 ++running_app_count; | 108 ++running_app_count; |
| 105 } | 109 } |
| 106 } | 110 } |
| 107 | 111 |
| 108 UMA_HISTOGRAM_COUNTS_100("Arc.AppCount", running_app_count); | 112 UMA_HISTOGRAM_COUNTS_100("Arc.AppCount", running_app_count); |
| 109 } | 113 } |
| 110 | 114 |
| 111 void ArcMetricsService::OnArcStartTimeRetrieved( | 115 void ArcMetricsService::OnArcStartTimeRetrieved( |
| 112 bool success, base::TimeTicks arc_start_time) { | 116 bool success, |
| 117 base::TimeTicks arc_start_time) { |
| 113 DCHECK(CalledOnValidThread()); | 118 DCHECK(CalledOnValidThread()); |
| 114 if (!success) { | 119 if (!success) { |
| 115 LOG(ERROR) << "Failed to retrieve ARC start timeticks."; | 120 LOG(ERROR) << "Failed to retrieve ARC start timeticks."; |
| 116 return; | 121 return; |
| 117 } | 122 } |
| 123 if (!arc_bridge_service()->metrics()->instance()) { |
| 124 LOG(ERROR) << "ARC metrics instance went away while retrieving start time."; |
| 125 return; |
| 126 } |
| 118 | 127 |
| 119 // The binding of host interface is deferred until the ARC start time is | 128 // The binding of host interface is deferred until the ARC start time is |
| 120 // retrieved here because it prevents race condition of the ARC start | 129 // retrieved here because it prevents race condition of the ARC start |
| 121 // time availability in ReportBootProgress(). | 130 // time availability in ReportBootProgress(). |
| 122 if (!binding_.is_bound()) { | 131 if (!binding_.is_bound()) { |
| 123 mojom::MetricsHostPtr host_ptr; | 132 mojom::MetricsHostPtr host_ptr; |
| 124 binding_.Bind(mojo::GetProxy(&host_ptr)); | 133 binding_.Bind(mojo::GetProxy(&host_ptr)); |
| 125 arc_bridge_service()->metrics_instance()->Init(std::move(host_ptr)); | 134 arc_bridge_service()->metrics()->instance()->Init(std::move(host_ptr)); |
| 126 } | 135 } |
| 127 arc_start_time_ = arc_start_time; | 136 arc_start_time_ = arc_start_time; |
| 128 VLOG(2) << "ARC start @" << arc_start_time_; | 137 VLOG(2) << "ARC start @" << arc_start_time_; |
| 129 } | 138 } |
| 130 | 139 |
| 131 void ArcMetricsService::ReportBootProgress( | 140 void ArcMetricsService::ReportBootProgress( |
| 132 mojo::Array<arc::mojom::BootProgressEventPtr> events) { | 141 mojo::Array<arc::mojom::BootProgressEventPtr> events) { |
| 133 DCHECK(CalledOnValidThread()); | 142 DCHECK(CalledOnValidThread()); |
| 134 int64_t arc_start_time_in_ms = | 143 int64_t arc_start_time_in_ms = |
| 135 (arc_start_time_ - base::TimeTicks()).InMilliseconds(); | 144 (arc_start_time_ - base::TimeTicks()).InMilliseconds(); |
| 136 for (const auto& event : events) { | 145 for (const auto& event : events) { |
| 137 VLOG(2) << "Report boot progress event:" | 146 VLOG(2) << "Report boot progress event:" << event->event << "@" |
| 138 << event->event << "@" << event->uptimeMillis; | 147 << event->uptimeMillis; |
| 139 std::string title = "Arc." + event->event.get(); | 148 std::string title = "Arc." + event->event.get(); |
| 140 base::TimeDelta elapsed_time = base::TimeDelta::FromMilliseconds( | 149 base::TimeDelta elapsed_time = base::TimeDelta::FromMilliseconds( |
| 141 event->uptimeMillis - arc_start_time_in_ms); | 150 event->uptimeMillis - arc_start_time_in_ms); |
| 142 // Note: This leaks memory, which is expected behavior. | 151 // Note: This leaks memory, which is expected behavior. |
| 143 base::HistogramBase* histogram = | 152 base::HistogramBase* histogram = base::Histogram::FactoryTimeGet( |
| 144 base::Histogram::FactoryTimeGet( | 153 title, base::TimeDelta::FromMilliseconds(1), |
| 145 title, | 154 base::TimeDelta::FromSeconds(30), 50, |
| 146 base::TimeDelta::FromMilliseconds(1), | 155 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 147 base::TimeDelta::FromSeconds(30), | |
| 148 50, | |
| 149 base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 150 histogram->AddTime(elapsed_time); | 156 histogram->AddTime(elapsed_time); |
| 151 if (event->event.get().compare(kBootProgressEnableScreen) == 0) | 157 if (event->event.get().compare(kBootProgressEnableScreen) == 0) |
| 152 UMA_HISTOGRAM_CUSTOM_TIMES("Arc.AndroidBootTime", | 158 UMA_HISTOGRAM_CUSTOM_TIMES("Arc.AndroidBootTime", elapsed_time, |
| 153 elapsed_time, | |
| 154 base::TimeDelta::FromMilliseconds(1), | 159 base::TimeDelta::FromMilliseconds(1), |
| 155 base::TimeDelta::FromSeconds(30), | 160 base::TimeDelta::FromSeconds(30), 50); |
| 156 50); | |
| 157 } | 161 } |
| 158 } | 162 } |
| 159 | 163 |
| 164 ArcMetricsService::ProcessObserver::ProcessObserver( |
| 165 ArcMetricsService* arc_metrics_service) |
| 166 : arc_metrics_service_(arc_metrics_service) {} |
| 167 |
| 168 ArcMetricsService::ProcessObserver::~ProcessObserver() = default; |
| 169 |
| 170 void ArcMetricsService::ProcessObserver::OnInstanceReady() { |
| 171 arc_metrics_service_->OnProcessInstanceReady(); |
| 172 } |
| 173 |
| 174 void ArcMetricsService::ProcessObserver::OnInstanceClosed() { |
| 175 arc_metrics_service_->OnProcessInstanceClosed(); |
| 176 } |
| 177 |
| 160 } // namespace arc | 178 } // namespace arc |
| OLD | NEW |