Add BLE results counter to batterystats
Batterystats provides an API to count the number of BLE scan results.
Bug: 37720787
Test: runtest -x frameworks/base/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
Change-Id: Idcb7494b39e88dcbfbb3da1ebe90b8a2f8f4d55c
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index 235f24c..21b68ea 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -513,6 +513,7 @@
public abstract Timer getForegroundActivityTimer();
public abstract Timer getBluetoothScanTimer();
public abstract Timer getBluetoothScanBackgroundTimer();
+ public abstract Counter getBluetoothScanResultCounter();
// Note: the following times are disjoint. They can be added together to find the
// total time a uid has had any processes running at all.
@@ -3347,8 +3348,10 @@
final long actualTime = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
final long actualTimeBg = bleTimerBg != null ?
bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
+ final int resultCount = u.getBluetoothScanResultCounter() != null ?
+ u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
dumpLine(pw, uid, category, BLUETOOTH_MISC_DATA, totalTime, count,
- countBg, actualTime, actualTimeBg);
+ countBg, actualTime, actualTimeBg, resultCount);
}
}
@@ -4500,6 +4503,8 @@
final long actualTimeMs = bleTimer.getTotalDurationMsLocked(rawRealtimeMs);
final long actualTimeMsBg = bleTimerBg != null ?
bleTimerBg.getTotalDurationMsLocked(rawRealtimeMs) : 0;
+ final int resultCount = u.getBluetoothScanResultCounter() != null ?
+ u.getBluetoothScanResultCounter().getCountLocked(which) : 0;
sb.setLength(0);
sb.append(prefix);
@@ -4524,6 +4529,8 @@
sb.append(countBg);
sb.append(" times)");
}
+ sb.append("; Results count ");
+ sb.append(resultCount);
pw.println(sb.toString());
uidActivity = true;
}
diff --git a/core/java/com/android/internal/app/IBatteryStats.aidl b/core/java/com/android/internal/app/IBatteryStats.aidl
index 99a25fd..373f4bb 100644
--- a/core/java/com/android/internal/app/IBatteryStats.aidl
+++ b/core/java/com/android/internal/app/IBatteryStats.aidl
@@ -132,6 +132,7 @@
void noteBleScanStarted(in WorkSource ws);
void noteBleScanStopped(in WorkSource ws);
void noteResetBleScan();
+ void noteBleScanResult(in WorkSource ws);
HealthStatsParceler takeUidSnapshot(int uid);
HealthStatsParceler[] takeUidSnapshots(in int[] uid);
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index a582c2c..748272600 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -114,7 +114,7 @@
private static final int MAGIC = 0xBA757475; // 'BATSTATS'
// Current on-disk Parcel version
- private static final int VERSION = 155 + (USE_OLD_HISTORY ? 1000 : 0);
+ private static final int VERSION = 156 + (USE_OLD_HISTORY ? 1000 : 0);
// Maximum number of items we will record in the history.
private static final int MAX_HISTORY_ITEMS = 2000;
@@ -4704,6 +4704,14 @@
}
}
+ public void noteBluetoothScanResultFromSourceLocked(WorkSource ws) {
+ final int N = ws.size();
+ for (int i = 0; i < N; i++) {
+ int uid = mapUid(ws.get(i));
+ getUidStatsLocked(uid).noteBluetoothScanResultLocked();
+ }
+ }
+
private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
final long uptimeMillis, int uid) {
uid = mapUid(uid);
@@ -5421,6 +5429,7 @@
StopwatchTimer mCameraTurnedOnTimer;
StopwatchTimer mForegroundActivityTimer;
DualTimer mBluetoothScanTimer;
+ Counter mBluetoothScanResultCounter;
int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
StopwatchTimer[] mProcessStateTimer;
@@ -5864,6 +5873,17 @@
}
}
+ public Counter createBluetoothScanResultCounterLocked() {
+ if (mBluetoothScanResultCounter == null) {
+ mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase);
+ }
+ return mBluetoothScanResultCounter;
+ }
+
+ public void noteBluetoothScanResultLocked() {
+ createBluetoothScanResultCounterLocked().stepAtomic();
+ }
+
@Override
public void noteActivityResumedLocked(long elapsedRealtimeMs) {
// We always start, since we want multiple foreground PIDs to nest
@@ -6017,6 +6037,11 @@
return mBluetoothScanTimer.getSubTimer();
}
+ @Override
+ public Counter getBluetoothScanResultCounter() {
+ return mBluetoothScanResultCounter;
+ }
+
void makeProcessState(int i, Parcel in) {
if (i < 0 || i >= NUM_PROCESS_STATE) return;
@@ -6266,6 +6291,9 @@
active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
+ if (mBluetoothScanResultCounter != null) {
+ mBluetoothScanResultCounter.reset(false);
+ }
if (mProcessStateTimer != null) {
for (int i = 0; i < NUM_PROCESS_STATE; i++) {
@@ -6450,6 +6478,10 @@
mBluetoothScanTimer.detach();
mBluetoothScanTimer = null;
}
+ if (mBluetoothScanResultCounter != null) {
+ mBluetoothScanResultCounter.detach();
+ mBluetoothScanResultCounter = null;
+ }
if (mUserActivityCounters != null) {
for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
mUserActivityCounters[i].detach();
@@ -6620,6 +6652,12 @@
} else {
out.writeInt(0);
}
+ if (mBluetoothScanResultCounter != null) {
+ out.writeInt(1);
+ mBluetoothScanResultCounter.writeToParcel(out);
+ } else {
+ out.writeInt(0);
+ }
for (int i = 0; i < NUM_PROCESS_STATE; i++) {
if (mProcessStateTimer[i] != null) {
out.writeInt(1);
@@ -6850,6 +6888,11 @@
} else {
mBluetoothScanTimer = null;
}
+ if (in.readInt() != 0) {
+ mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
+ } else {
+ mBluetoothScanResultCounter = null;
+ }
mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
for (int i = 0; i < NUM_PROCESS_STATE; i++) {
if (in.readInt() != 0) {
@@ -10998,6 +11041,9 @@
if (in.readInt() != 0) {
u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
}
+ if (in.readInt() != 0) {
+ u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
+ }
u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
if (in.readInt() != 0) {
@@ -11391,6 +11437,12 @@
} else {
out.writeInt(0);
}
+ if (u.mBluetoothScanResultCounter != null) {
+ out.writeInt(1);
+ u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
+ } else {
+ out.writeInt(0);
+ }
for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
if (u.mProcessStateTimer[i] != null) {
out.writeInt(1);
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsNoteTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsNoteTest.java
new file mode 100644
index 0000000..1dced75
--- /dev/null
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsNoteTest.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.android.internal.os;
+
+import static android.os.BatteryStats.STATS_SINCE_CHARGED;
+
+import android.os.WorkSource;
+import android.support.test.filters.SmallTest;
+
+import junit.framework.TestCase;
+
+/**
+ * Test various BatteryStatsImpl noteStart methods.
+ */
+public class BatteryStatsNoteTest extends TestCase{
+ private static final int UID = 10500;
+ private static final WorkSource WS = new WorkSource(UID);
+
+ /** Test that BatteryStatsImpl.Uid.noteBluetoothScanResultLocked. */
+ @SmallTest
+ public void testNoteBluetoothScanResultLocked() throws Exception {
+ MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClocks());
+ bi.updateTimeBasesLocked(true, true, 0, 0);
+
+ bi.noteBluetoothScanResultFromSourceLocked(WS);
+ bi.noteBluetoothScanResultFromSourceLocked(WS);
+ assertEquals(2,
+ bi.getUidStats().get(UID).getBluetoothScanResultCounter()
+ .getCountLocked(STATS_SINCE_CHARGED));
+ }
+}
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
index 57d6934..3a16fcf 100644
--- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
@@ -15,6 +15,7 @@
BatteryStatsUidTest.class,
BatteryStatsSensorTest.class,
BatteryStatsBackgroundStatsTest.class,
+ BatteryStatsNoteTest.class,
})
public class BatteryStatsTests {
}
diff --git a/services/core/java/com/android/server/am/BatteryStatsService.java b/services/core/java/com/android/server/am/BatteryStatsService.java
index 983c975..7a46248 100644
--- a/services/core/java/com/android/server/am/BatteryStatsService.java
+++ b/services/core/java/com/android/server/am/BatteryStatsService.java
@@ -966,6 +966,14 @@
}
@Override
+ public void noteBleScanResult(WorkSource ws) {
+ enforceCallingPermission();
+ synchronized (mStats) {
+ mStats.noteBluetoothScanResultFromSourceLocked(ws);
+ }
+ }
+
+ @Override
public void noteWifiControllerActivity(WifiActivityEnergyInfo info) {
enforceCallingPermission();