Commit ff277570 authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

android: Move battery metrics impl from chrome/ to components/

This moves the majority of AndroidBatteryMetrics from chrome/ to
components/, so that we can reuse it for WebView/WebLayer in the future.

The only aspect that remains in chrome/ is the detection of the app's
visibility state, since this will have to be done some other way for
WebView.

Bug: 1102048
Change-Id: Ia8391fd5efd80411ac970c559cdf5db988ba9ea4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485794
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819432}
parent be4d2b1c
...@@ -2413,8 +2413,6 @@ static_library("browser") { ...@@ -2413,8 +2413,6 @@ static_library("browser") {
"android/background_task_scheduler/chrome_background_task_factory.h", "android/background_task_scheduler/chrome_background_task_factory.h",
"android/background_task_scheduler/proxy_native_task.cc", "android/background_task_scheduler/proxy_native_task.cc",
"android/background_task_scheduler/proxy_native_task.h", "android/background_task_scheduler/proxy_native_task.h",
"android/battery/android_battery_metrics.cc",
"android/battery/android_battery_metrics.h",
"android/bookmarks/bookmark_bridge.cc", "android/bookmarks/bookmark_bridge.cc",
"android/bookmarks/bookmark_bridge.h", "android/bookmarks/bookmark_bridge.h",
"android/bookmarks/partner_bookmarks_reader.cc", "android/bookmarks/partner_bookmarks_reader.cc",
...@@ -3186,6 +3184,7 @@ static_library("browser") { ...@@ -3186,6 +3184,7 @@ static_library("browser") {
"//components/payments/content/android", "//components/payments/content/android",
"//components/payments/content/android:jni_headers", "//components/payments/content/android:jni_headers",
"//components/permissions/android:native", "//components/permissions/android:native",
"//components/power_metrics",
"//components/query_tiles", "//components/query_tiles",
"//components/resources:android_resources", "//components/resources:android_resources",
"//components/resources:components_resources", "//components/resources:components_resources",
......
...@@ -216,6 +216,7 @@ include_rules = [ ...@@ -216,6 +216,7 @@ include_rules = [
"+components/permissions", "+components/permissions",
"+components/pdf/browser", "+components/pdf/browser",
"+components/policy", "+components/policy",
"+components/power_metrics",
"+components/pref_registry", "+components/pref_registry",
"+components/prefs", "+components/prefs",
"+components/prerender", "+components/prerender",
......
...@@ -22,10 +22,30 @@ BatteryMetrics::BatteryMonitorBinder& GetBinderOverride() { ...@@ -22,10 +22,30 @@ BatteryMetrics::BatteryMonitorBinder& GetBinderOverride() {
return *binder; return *binder;
} }
#if defined(OS_ANDROID)
bool IsAppVisible(base::android::ApplicationState state) {
return state == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES;
}
#endif // defined(OS_ANDROID)
} // namespace } // namespace
BatteryMetrics::BatteryMetrics() { BatteryMetrics::BatteryMetrics() {
StartRecording(); StartRecording();
#if defined(OS_ANDROID)
// On Android, also track the battery capacity drain while Chrome is the
// foreground activity.
app_state_listener_ =
base::android::ApplicationStatusListener::New(base::BindRepeating(
[](BatteryMetrics* metrics, base::android::ApplicationState state) {
metrics->android_metrics_.OnAppVisibilityChanged(
IsAppVisible(state));
},
base::Unretained(this)));
android_metrics_.OnAppVisibilityChanged(
IsAppVisible(base::android::ApplicationStatusListener::GetState()));
#endif // defined(OS_ANDROID)
} }
BatteryMetrics::~BatteryMetrics() = default; BatteryMetrics::~BatteryMetrics() = default;
......
...@@ -17,7 +17,8 @@ ...@@ -17,7 +17,8 @@
#include "services/device/public/mojom/battery_status.mojom-forward.h" #include "services/device/public/mojom/battery_status.mojom-forward.h"
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
#include "chrome/browser/android/battery/android_battery_metrics.h" #include "base/android/application_status_listener.h"
#include "components/power_metrics/android_battery_metrics.h"
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
// Records metrics around battery usage on all platforms. Connects to // Records metrics around battery usage on all platforms. Connects to
...@@ -55,7 +56,8 @@ class BatteryMetrics { ...@@ -55,7 +56,8 @@ class BatteryMetrics {
mojo::Remote<device::mojom::BatteryMonitor> battery_monitor_; mojo::Remote<device::mojom::BatteryMonitor> battery_monitor_;
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
AndroidBatteryMetrics android_metrics_; power_metrics::AndroidBatteryMetrics android_metrics_;
std::unique_ptr<base::android::ApplicationStatusListener> app_state_listener_;
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
......
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
if (is_android) {
static_library("power_metrics") {
sources = [
"android_battery_metrics.cc",
"android_battery_metrics.h",
]
deps = [
"//base",
"//net",
]
}
}
include_rules = [
"+net/android",
]
eseckler@chromium.org
skyostil@chromium.org
khokhlov@google.com
# COMPONENT: Speed>Metrics
...@@ -2,17 +2,19 @@ ...@@ -2,17 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/android/battery/android_battery_metrics.h" #include "components/power_metrics/android_battery_metrics.h"
#include "base/android/radio_utils.h" #include "base/android/radio_utils.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/optional.h"
#include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor.h"
#include "net/android/network_library.h" #include "net/android/network_library.h"
#include "net/android/traffic_stats.h" #include "net/android/traffic_stats.h"
namespace power_metrics {
namespace { namespace {
void Report30SecondRadioUsage(int64_t tx_bytes, int64_t rx_bytes) { void Report30SecondRadioUsage(int64_t tx_bytes, int64_t rx_bytes) {
...@@ -115,10 +117,7 @@ void ReportAveragedDrain(int capacity_consumed, ...@@ -115,10 +117,7 @@ void ReportAveragedDrain(int capacity_consumed,
constexpr base::TimeDelta AndroidBatteryMetrics::kMetricsInterval; constexpr base::TimeDelta AndroidBatteryMetrics::kMetricsInterval;
AndroidBatteryMetrics::AndroidBatteryMetrics() AndroidBatteryMetrics::AndroidBatteryMetrics()
: app_state_listener_(base::android::ApplicationStatusListener::New( : app_visible_(false),
base::BindRepeating(&AndroidBatteryMetrics::OnAppStateChanged,
base::Unretained(this)))),
app_state_(base::android::ApplicationStatusListener::GetState()),
on_battery_power_(base::PowerMonitor::IsOnBatteryPower()) { on_battery_power_(base::PowerMonitor::IsOnBatteryPower()) {
base::PowerMonitor::AddObserver(this); base::PowerMonitor::AddObserver(this);
UpdateMetricsEnabled(); UpdateMetricsEnabled();
...@@ -128,10 +127,9 @@ AndroidBatteryMetrics::~AndroidBatteryMetrics() { ...@@ -128,10 +127,9 @@ AndroidBatteryMetrics::~AndroidBatteryMetrics() {
base::PowerMonitor::RemoveObserver(this); base::PowerMonitor::RemoveObserver(this);
} }
void AndroidBatteryMetrics::OnAppStateChanged( void AndroidBatteryMetrics::OnAppVisibilityChanged(bool visible) {
base::android::ApplicationState state) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
app_state_ = state; app_visible_ = visible;
UpdateMetricsEnabled(); UpdateMetricsEnabled();
} }
...@@ -144,12 +142,10 @@ void AndroidBatteryMetrics::OnPowerStateChange(bool on_battery_power) { ...@@ -144,12 +142,10 @@ void AndroidBatteryMetrics::OnPowerStateChange(bool on_battery_power) {
void AndroidBatteryMetrics::UpdateMetricsEnabled() { void AndroidBatteryMetrics::UpdateMetricsEnabled() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// We want to attribute battery drain to Chrome while it is in the foreground. // We want to attribute battery drain to chromium while the embedding app is
// Battery drain will only be reflected in remaining battery capacity when the // visible. Battery drain will only be reflected in remaining battery capacity
// device is not on a charger. // when the device is not on a charger.
bool should_be_enabled = bool should_be_enabled = app_visible_ && on_battery_power_;
app_state_ == base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES &&
on_battery_power_;
if (should_be_enabled && !metrics_timer_.IsRunning()) { if (should_be_enabled && !metrics_timer_.IsRunning()) {
// Capture first capacity measurement and enable the repeating timer. // Capture first capacity measurement and enable the repeating timer.
...@@ -237,3 +233,5 @@ void AndroidBatteryMetrics::CaptureAndReportMetrics() { ...@@ -237,3 +233,5 @@ void AndroidBatteryMetrics::CaptureAndReportMetrics() {
bool AndroidBatteryMetrics::IsMeasuringDrainExclusively() const { bool AndroidBatteryMetrics::IsMeasuringDrainExclusively() const {
return observed_capacity_drops_ >= 2; return observed_capacity_drops_ >= 2;
} }
} // namespace power_metrics
...@@ -2,44 +2,47 @@ ...@@ -2,44 +2,47 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_ANDROID_BATTERY_ANDROID_BATTERY_METRICS_H_ #ifndef COMPONENTS_POWER_METRICS_ANDROID_BATTERY_METRICS_H_
#define CHROME_BROWSER_ANDROID_BATTERY_ANDROID_BATTERY_METRICS_H_ #define COMPONENTS_POWER_METRICS_ANDROID_BATTERY_METRICS_H_
#include "base/android/application_status_listener.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/power_monitor/power_observer.h" #include "base/power_monitor/power_observer.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
// Records metrics around battery usage on Android. namespace power_metrics {
// Records metrics around battery usage on Android. The metrics are only tracked
// while the device is not charging and the app is visible (embedder should call
// OnAppVisibilityChanged()). This class is not thread-safe.
class AndroidBatteryMetrics : public base::PowerObserver { class AndroidBatteryMetrics : public base::PowerObserver {
public: public:
AndroidBatteryMetrics(); AndroidBatteryMetrics();
~AndroidBatteryMetrics() override; ~AndroidBatteryMetrics() override;
// base::PowerObserver implementation: // Should be called by the embedder when the embedder app becomes visible or
void OnPowerStateChange(bool on_battery_power) override; // invisible.
void OnAppVisibilityChanged(bool visible);
private: private:
// Called by base::android::ApplicationStatusListener. // base::PowerObserver implementation:
void OnAppStateChanged(base::android::ApplicationState); void OnPowerStateChange(bool on_battery_power) override;
void UpdateMetricsEnabled(); void UpdateMetricsEnabled();
void CaptureAndReportMetrics(); void CaptureAndReportMetrics();
void UpdateAndReportRadio(); void UpdateAndReportRadio();
// Whether or not we've seen at least two consecutive capacity drops while // Whether or not we've seen at least two consecutive capacity drops while
// Chrome was the foreground app. Battery drain reported prior to this could // the embedding app was visible. Battery drain reported prior to this could
// be caused by a different app. // be caused by a different app.
bool IsMeasuringDrainExclusively() const; bool IsMeasuringDrainExclusively() const;
// Battery drain is captured and reported periodically in this interval while // Battery drain is captured and reported periodically in this interval while
// the device is on battery power and Chrome is the foreground activity. // the device is on battery power and the app is visible.
static constexpr base::TimeDelta kMetricsInterval = static constexpr base::TimeDelta kMetricsInterval =
base::TimeDelta::FromSeconds(30); base::TimeDelta::FromSeconds(30);
std::unique_ptr<base::android::ApplicationStatusListener> app_state_listener_; bool app_visible_;
base::android::ApplicationState app_state_;
bool on_battery_power_; bool on_battery_power_;
int last_remaining_capacity_uah_ = 0; int last_remaining_capacity_uah_ = 0;
int64_t last_tx_bytes_ = -1; int64_t last_tx_bytes_ = -1;
...@@ -47,8 +50,7 @@ class AndroidBatteryMetrics : public base::PowerObserver { ...@@ -47,8 +50,7 @@ class AndroidBatteryMetrics : public base::PowerObserver {
base::RepeatingTimer metrics_timer_; base::RepeatingTimer metrics_timer_;
int skipped_timers_ = 0; int skipped_timers_ = 0;
// Number of consecutive charge drops seen while the app has been in the // Number of consecutive charge drops seen while the app has been visible.
// foreground.
int observed_capacity_drops_ = 0; int observed_capacity_drops_ = 0;
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
...@@ -56,4 +58,6 @@ class AndroidBatteryMetrics : public base::PowerObserver { ...@@ -56,4 +58,6 @@ class AndroidBatteryMetrics : public base::PowerObserver {
DISALLOW_COPY_AND_ASSIGN(AndroidBatteryMetrics); DISALLOW_COPY_AND_ASSIGN(AndroidBatteryMetrics);
}; };
#endif // CHROME_BROWSER_ANDROID_BATTERY_ANDROID_BATTERY_METRICS_H_ } // namespace power_metrics
#endif // COMPONENTS_POWER_METRICS_ANDROID_BATTERY_METRICS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment