Merge "Exclude inactive UIDs from PowerStats aggregation" into main
diff --git a/services/core/java/com/android/server/power/stats/processor/AggregatedPowerStats.java b/services/core/java/com/android/server/power/stats/processor/AggregatedPowerStats.java
index a783d54..53894a1 100644
--- a/services/core/java/com/android/server/power/stats/processor/AggregatedPowerStats.java
+++ b/services/core/java/com/android/server/power/stats/processor/AggregatedPowerStats.java
@@ -25,6 +25,7 @@
 import android.os.UserHandle;
 import android.text.format.DateFormat;
 import android.util.IndentingPrintWriter;
+import android.util.IntArray;
 import android.util.Slog;
 import android.util.SparseArray;
 import android.util.SparseBooleanArray;
@@ -347,7 +348,10 @@
 
         Set<Integer> uids = new HashSet<>();
         for (int i = 0; i < mPowerComponentStats.size(); i++) {
-            mPowerComponentStats.valueAt(i).collectUids(uids);
+            IntArray activeUids = mPowerComponentStats.valueAt(i).getActiveUids();
+            for (int j = activeUids.size() - 1; j >= 0; j--) {
+                uids.add(activeUids.get(j));
+            }
         }
 
         Integer[] allUids = uids.toArray(new Integer[uids.size()]);
diff --git a/services/core/java/com/android/server/power/stats/processor/BasePowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/BasePowerStatsProcessor.java
index d24ea83..1d35933 100644
--- a/services/core/java/com/android/server/power/stats/processor/BasePowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/BasePowerStatsProcessor.java
@@ -24,13 +24,12 @@
 
 import android.os.BatteryConsumer;
 import android.os.PersistableBundle;
+import android.util.IntArray;
 
 import com.android.internal.os.PowerStats;
 import com.android.server.power.stats.format.BasePowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.List;
 import java.util.function.DoubleSupplier;
 
 class BasePowerStatsProcessor extends PowerStatsProcessor {
@@ -125,11 +124,12 @@
         mCumulativeDischargeUah = 0;
         mCumulativeDischargeDurationMs = 0;
 
-        List<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-
-        long durationMs = timestampMs - mStartTimestamp;
-        if (!uids.isEmpty()) {
+        // Note that we are calling `getUids` rather than `getActiveUids`, because this Processor
+        // deals with duration rather than power estimation, so it needs to process *all* known
+        // UIDs, not just the ones that contributed PowerStats
+        IntArray uids = stats.getUids();
+        if (uids.size() != 0) {
+            long durationMs = timestampMs - mStartTimestamp;
             for (int i = uids.size() - 1; i >= 0; i--) {
                 long[] uidStats = new long[sStatsLayout.getUidStatsArrayLength()];
                 sStatsLayout.setUidUsageDuration(uidStats, durationMs);
diff --git a/services/core/java/com/android/server/power/stats/processor/BinaryStatePowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/BinaryStatePowerStatsProcessor.java
index 9fe7f3e..5eaaac9 100644
--- a/services/core/java/com/android/server/power/stats/processor/BinaryStatePowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/BinaryStatePowerStatsProcessor.java
@@ -20,6 +20,7 @@
 import android.os.BatteryStats;
 import android.os.PersistableBundle;
 import android.os.Process;
+import android.util.IntArray;
 
 import com.android.internal.os.PowerStats;
 import com.android.server.power.stats.UsageBasedPowerEstimator;
@@ -27,7 +28,6 @@
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -190,13 +190,13 @@
         }
 
         computeDevicePowerEstimates(stats, mPlan, mEnergyConsumerSupported);
-        combineDevicePowerEstimates(stats);
 
-        List<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-
-        computeUidActivityTotals(stats, uids);
-        computeUidPowerEstimates(stats, uids);
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
+            combineDevicePowerEstimates(stats);
+            computeUidActivityTotals(stats, uids);
+            computeUidPowerEstimates(stats, uids);
+        }
     }
 
     protected void computeDevicePowerEstimates(PowerComponentAggregatedPowerStats stats,
@@ -239,8 +239,7 @@
         }
     }
 
-    private void computeUidActivityTotals(PowerComponentAggregatedPowerStats stats,
-            List<Integer> uids) {
+    private void computeUidActivityTotals(PowerComponentAggregatedPowerStats stats, IntArray uids) {
         for (int i = mPlan.uidStateEstimates.size() - 1; i >= 0; i--) {
             UidStateEstimate uidStateEstimate = mPlan.uidStateEstimates.get(i);
             Intermediates intermediates =
@@ -259,8 +258,7 @@
         }
     }
 
-    private void computeUidPowerEstimates(PowerComponentAggregatedPowerStats stats,
-            List<Integer> uids) {
+    private void computeUidPowerEstimates(PowerComponentAggregatedPowerStats stats, IntArray uids) {
         for (int i = mPlan.uidStateEstimates.size() - 1; i >= 0; i--) {
             UidStateEstimate uidStateEstimate = mPlan.uidStateEstimates.get(i);
             Intermediates intermediates =
diff --git a/services/core/java/com/android/server/power/stats/processor/BluetoothPowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/BluetoothPowerStatsProcessor.java
index 4c1a0db..9e8b553 100644
--- a/services/core/java/com/android/server/power/stats/processor/BluetoothPowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/BluetoothPowerStatsProcessor.java
@@ -16,12 +16,13 @@
 
 package com.android.server.power.stats.processor;
 
+import android.util.IntArray;
+
 import com.android.internal.os.PowerProfile;
 import com.android.internal.os.PowerStats;
 import com.android.server.power.stats.UsageBasedPowerEstimator;
 import com.android.server.power.stats.format.BluetoothPowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.List;
 
 class BluetoothPowerStatsProcessor extends PowerStatsProcessor {
@@ -118,18 +119,19 @@
 
         combineDeviceStateEstimates();
 
-        ArrayList<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-        if (!uids.isEmpty()) {
-            for (int uid : uids) {
-                for (int i = 0; i < mPlan.uidStateEstimates.size(); i++) {
-                    computeUidActivityTotals(stats, uid, mPlan.uidStateEstimates.get(i));
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
+            for (int i = uids.size() - 1; i >= 0; i--) {
+                int uid = uids.get(i);
+                for (int j = 0; j < mPlan.uidStateEstimates.size(); j++) {
+                    computeUidActivityTotals(stats, uid, mPlan.uidStateEstimates.get(j));
                 }
             }
 
-            for (int uid : uids) {
-                for (int i = 0; i < mPlan.uidStateEstimates.size(); i++) {
-                    computeUidPowerEstimates(stats, uid, mPlan.uidStateEstimates.get(i));
+            for (int i = uids.size() - 1; i >= 0; i--) {
+                int uid = uids.get(i);
+                for (int j = 0; j < mPlan.uidStateEstimates.size(); j++) {
+                    computeUidPowerEstimates(stats, uid, mPlan.uidStateEstimates.get(j));
                 }
             }
         }
diff --git a/services/core/java/com/android/server/power/stats/processor/CpuPowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/CpuPowerStatsProcessor.java
index 17ceca6..b442dc2 100644
--- a/services/core/java/com/android/server/power/stats/processor/CpuPowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/CpuPowerStatsProcessor.java
@@ -19,6 +19,7 @@
 import android.annotation.Nullable;
 import android.os.BatteryConsumer;
 import android.util.ArraySet;
+import android.util.IntArray;
 import android.util.Log;
 
 import com.android.internal.os.CpuScalingPolicies;
@@ -27,7 +28,6 @@
 import com.android.server.power.stats.format.CpuPowerStatsLayout;
 import com.android.server.power.stats.format.WakelockPowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -189,12 +189,12 @@
         estimatePowerByDeviceState(stats, intermediates, wakelockStats);
         combineDeviceStateEstimates();
 
-        ArrayList<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-        if (!uids.isEmpty()) {
-            for (int uid : uids) {
-                for (int i = 0; i < mPlan.uidStateEstimates.size(); i++) {
-                    estimateUidPowerConsumption(stats, uid, mPlan.uidStateEstimates.get(i),
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
+            for (int i = uids.size() - 1; i >= 0; i--) {
+                int uid = uids.get(i);
+                for (int j = 0; j < mPlan.uidStateEstimates.size(); j++) {
+                    estimateUidPowerConsumption(stats, uid, mPlan.uidStateEstimates.get(j),
                             wakelockStats);
                 }
             }
diff --git a/services/core/java/com/android/server/power/stats/processor/CustomEnergyConsumerPowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/CustomEnergyConsumerPowerStatsProcessor.java
index 76adc47..05865e2 100644
--- a/services/core/java/com/android/server/power/stats/processor/CustomEnergyConsumerPowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/CustomEnergyConsumerPowerStatsProcessor.java
@@ -16,10 +16,11 @@
 
 package com.android.server.power.stats.processor;
 
+import android.util.IntArray;
+
 import com.android.internal.os.PowerStats;
 import com.android.server.power.stats.format.EnergyConsumerPowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.List;
 
 class CustomEnergyConsumerPowerStatsProcessor extends PowerStatsProcessor {
@@ -40,10 +41,8 @@
 
         computeDevicePowerEstimates(stats);
 
-        List<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-
-        if (!uids.isEmpty()) {
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
             computeUidPowerEstimates(stats, uids);
         }
     }
@@ -62,7 +61,7 @@
     }
 
     private void computeUidPowerEstimates(PowerComponentAggregatedPowerStats stats,
-            List<Integer> uids) {
+            IntArray uids) {
         for (int i = mPlan.uidStateEstimates.size() - 1; i >= 0; i--) {
             UidStateEstimate uidStateEstimate = mPlan.uidStateEstimates.get(i);
             List<UidStateProportionalEstimate> proportionalEstimates =
diff --git a/services/core/java/com/android/server/power/stats/processor/MobileRadioPowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/MobileRadioPowerStatsProcessor.java
index b4c40de8..c5db19b 100644
--- a/services/core/java/com/android/server/power/stats/processor/MobileRadioPowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/MobileRadioPowerStatsProcessor.java
@@ -19,6 +19,7 @@
 import android.telephony.CellSignalStrength;
 import android.telephony.ModemActivityInfo;
 import android.telephony.ServiceState;
+import android.util.IntArray;
 import android.util.Log;
 import android.util.Slog;
 import android.util.SparseArray;
@@ -29,7 +30,6 @@
 import com.android.server.power.stats.UsageBasedPowerEstimator;
 import com.android.server.power.stats.format.MobileRadioPowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -198,18 +198,19 @@
 
         combineDeviceStateEstimates();
 
-        ArrayList<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-        if (!uids.isEmpty()) {
-            for (int uid : uids) {
-                for (int i = 0; i < mPlan.uidStateEstimates.size(); i++) {
-                    computeUidRxTxTotals(stats, uid, mPlan.uidStateEstimates.get(i));
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
+            for (int i = uids.size() - 1; i >= 0; i--) {
+                int uid = uids.get(i);
+                for (int j = 0; j < mPlan.uidStateEstimates.size(); j++) {
+                    computeUidRxTxTotals(stats, uid, mPlan.uidStateEstimates.get(j));
                 }
             }
 
-            for (int uid : uids) {
-                for (int i = 0; i < mPlan.uidStateEstimates.size(); i++) {
-                    computeUidPowerEstimates(stats, uid, mPlan.uidStateEstimates.get(i));
+            for (int i = uids.size() - 1; i >= 0; i--) {
+                int uid = uids.get(i);
+                for (int j = 0; j < mPlan.uidStateEstimates.size(); j++) {
+                    computeUidPowerEstimates(stats, uid, mPlan.uidStateEstimates.get(j));
                 }
             }
         }
diff --git a/services/core/java/com/android/server/power/stats/processor/PowerComponentAggregatedPowerStats.java b/services/core/java/com/android/server/power/stats/processor/PowerComponentAggregatedPowerStats.java
index d4f8fd9..33baad8 100644
--- a/services/core/java/com/android/server/power/stats/processor/PowerComponentAggregatedPowerStats.java
+++ b/services/core/java/com/android/server/power/stats/processor/PowerComponentAggregatedPowerStats.java
@@ -21,6 +21,7 @@
 import android.os.BatteryStats;
 import android.os.UserHandle;
 import android.util.IndentingPrintWriter;
+import android.util.IntArray;
 import android.util.Slog;
 import android.util.SparseArray;
 
@@ -34,7 +35,6 @@
 import java.io.IOException;
 import java.io.StringWriter;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.function.IntConsumer;
 
 /**
@@ -77,6 +77,7 @@
     private static class UidStats {
         public int[] states;
         public MultiStateStats stats;
+        public boolean hasPowerStats;
         public boolean updated;
     }
 
@@ -200,6 +201,7 @@
         if (uidStats.stats == null) {
             createUidStats(uidStats, mPowerStatsTimestamp);
         }
+        uidStats.hasPowerStats = true;
         uidStats.stats.setStats(states, values);
     }
 
@@ -240,6 +242,7 @@
             }
             uidStats.stats.increment(powerStats.uidStats.valueAt(i), timestampMs);
             uidStats.updated = true;
+            uidStats.hasPowerStats = true;
         }
 
         // For UIDs not mentioned in the PowerStats object, we must assume a 0 increment.
@@ -267,6 +270,7 @@
         mStateStats.clear();
         for (int i = mUidStats.size() - 1; i >= 0; i--) {
             mUidStats.valueAt(i).stats = null;
+            mUidStats.valueAt(i).hasPowerStats = false;
         }
     }
 
@@ -290,12 +294,26 @@
         return uidStats;
     }
 
-    void collectUids(Collection<Integer> uids) {
+    IntArray getUids() {
+        IntArray uids = new IntArray(mUidStats.size());
         for (int i = mUidStats.size() - 1; i >= 0; i--) {
-            if (mUidStats.valueAt(i).stats != null) {
+            UidStats uidStats = mUidStats.valueAt(i);
+            if (uidStats.stats != null) {
                 uids.add(mUidStats.keyAt(i));
             }
         }
+        return uids;
+    }
+
+    IntArray getActiveUids() {
+        IntArray uids = new IntArray(mUidStats.size());
+        for (int i = mUidStats.size() - 1; i >= 0; i--) {
+            UidStats uidStats = mUidStats.valueAt(i);
+            if (uidStats.hasPowerStats) {
+                uids.add(mUidStats.keyAt(i));
+            }
+        }
+        return uids;
     }
 
     boolean getDeviceStats(long[] outValues, int[] deviceStates) {
@@ -516,6 +534,7 @@
                         if (uidStats.stats == null) {
                             createUidStats(uidStats, UNKNOWN);
                         }
+                        uidStats.hasPowerStats = true;
                         if (!uidStats.stats.readFromXml(parser)) {
                             return false;
                         }
diff --git a/services/core/java/com/android/server/power/stats/processor/PowerStatsExporter.java b/services/core/java/com/android/server/power/stats/processor/PowerStatsExporter.java
index 177d129..634415e 100644
--- a/services/core/java/com/android/server/power/stats/processor/PowerStatsExporter.java
+++ b/services/core/java/com/android/server/power/stats/processor/PowerStatsExporter.java
@@ -21,6 +21,7 @@
 import android.os.BatteryConsumer;
 import android.os.BatteryUsageStats;
 import android.os.UidBatteryConsumer;
+import android.util.IntArray;
 import android.util.Slog;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -31,7 +32,6 @@
 import com.android.server.power.stats.format.BasePowerStatsLayout;
 import com.android.server.power.stats.format.PowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -180,8 +180,7 @@
             }
         }
         if (layout.isUidPowerAttributionSupported()) {
-            populateBatteryConsumers(batteryUsageStatsBuilder,
-                    powerComponentStats, layout);
+            populateBatteryConsumers(batteryUsageStatsBuilder, powerComponentStats, layout);
         }
 
         populateBatteryLevelInfo(batteryUsageStatsBuilder, batteryLevelInfo);
@@ -258,6 +257,11 @@
             BatteryUsageStats.Builder batteryUsageStatsBuilder,
             PowerComponentAggregatedPowerStats powerComponentStats,
             PowerStatsLayout layout) {
+        IntArray uids = powerComponentStats.getUids();
+        if (uids.size() == 0) {
+            return;
+        }
+
         AggregatedPowerStatsConfig.PowerComponent powerComponent = powerComponentStats.getConfig();
         PowerStats.Descriptor descriptor = powerComponentStats.getPowerStatsDescriptor();
         long[] uidStats = new long[descriptor.uidStatsArrayLength];
@@ -273,8 +277,6 @@
             breakDownByProcState = false;
         }
 
-        ArrayList<Integer> uids = new ArrayList<>();
-        powerComponentStats.collectUids(uids);
         for (int screenState = 0; screenState < BatteryConsumer.SCREEN_STATE_COUNT; screenState++) {
             if (batteryUsageStatsBuilder.isScreenStateDataNeeded()) {
                 if (screenState == BatteryConsumer.SCREEN_STATE_UNSPECIFIED) {
@@ -303,7 +305,7 @@
     private void populateUidBatteryConsumers(
             BatteryUsageStats.Builder batteryUsageStatsBuilder,
             PowerComponentAggregatedPowerStats powerComponentStats, PowerStatsLayout layout,
-            List<Integer> uids, AggregatedPowerStatsConfig.PowerComponent powerComponent,
+            IntArray uids, AggregatedPowerStatsConfig.PowerComponent powerComponent,
             long[] uidStats, boolean breakDownByProcState,
             @BatteryConsumer.ScreenState int screenState,
             @BatteryConsumer.PowerState int powerState) {
@@ -319,7 +321,8 @@
         long[] durationByProcState =
                 new long[breakDownByProcState ? BatteryConsumer.PROCESS_STATE_COUNT : 1];
         double powerAllApps = 0;
-        for (int uid : uids) {
+        for (int i = uids.size() - 1; i >= 0; i--) {
+            int uid = uids.get(i);
             UidBatteryConsumer.Builder builder =
                     batteryUsageStatsBuilder.getOrCreateUidBatteryConsumerBuilder(uid);
 
diff --git a/services/core/java/com/android/server/power/stats/processor/ScreenPowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/ScreenPowerStatsProcessor.java
index 8e7498f..5a8e8b4 100644
--- a/services/core/java/com/android/server/power/stats/processor/ScreenPowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/ScreenPowerStatsProcessor.java
@@ -27,6 +27,7 @@
 import static com.android.server.power.stats.processor.AggregatedPowerStatsConfig.STATE_SCREEN;
 
 import android.os.BatteryStats;
+import android.util.IntArray;
 import android.util.Slog;
 
 import com.android.internal.os.PowerProfile;
@@ -34,7 +35,6 @@
 import com.android.server.power.stats.UsageBasedPowerEstimator;
 import com.android.server.power.stats.format.ScreenPowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.List;
 
 class ScreenPowerStatsProcessor extends PowerStatsProcessor {
@@ -116,10 +116,8 @@
         computeDevicePowerEstimates(stats);
         combineDeviceStateEstimates();
 
-        List<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-
-        if (!uids.isEmpty()) {
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
             computeUidPowerEstimates(stats, uids);
         }
         mPlan.resetIntermediates();
@@ -197,7 +195,7 @@
     }
 
     private void computeUidPowerEstimates(PowerComponentAggregatedPowerStats stats,
-            List<Integer> uids) {
+            IntArray uids) {
         int[] uidStateValues = new int[stats.getConfig().getUidStateConfig().length];
         uidStateValues[STATE_SCREEN] = SCREEN_STATE_ON;
         uidStateValues[STATE_PROCESS_STATE] = PROCESS_STATE_UNSPECIFIED;
diff --git a/services/core/java/com/android/server/power/stats/processor/SensorPowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/SensorPowerStatsProcessor.java
index 0bb028b..ba728d3 100644
--- a/services/core/java/com/android/server/power/stats/processor/SensorPowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/SensorPowerStatsProcessor.java
@@ -21,6 +21,7 @@
 import android.os.BatteryConsumer;
 import android.os.BatteryStats;
 import android.os.PersistableBundle;
+import android.util.IntArray;
 import android.util.Slog;
 import android.util.SparseArray;
 
@@ -28,7 +29,6 @@
 import com.android.server.power.stats.format.PowerStatsLayout;
 import com.android.server.power.stats.format.SensorPowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.function.Supplier;
@@ -207,11 +207,11 @@
             mPlan = new PowerEstimationPlan(stats.getConfig());
         }
 
-        List<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-
-        computeUidPowerEstimates(stats, uids);
-        computeDevicePowerEstimates(stats);
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
+            computeUidPowerEstimates(stats, uids);
+            computeDevicePowerEstimates(stats);
+        }
 
         mPlan.resetIntermediates();
     }
@@ -239,9 +239,7 @@
         mLastUpdateTimestamp = timestamp;
     }
 
-    private void computeUidPowerEstimates(
-            PowerComponentAggregatedPowerStats stats,
-            List<Integer> uids) {
+    private void computeUidPowerEstimates(PowerComponentAggregatedPowerStats stats, IntArray uids) {
         List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
         int[] uidSensorDurationPositions = new int[sensorList.size()];
         double[] sensorPower = new double[sensorList.size()];
@@ -292,8 +290,7 @@
         }
     }
 
-    private void computeDevicePowerEstimates(
-            PowerComponentAggregatedPowerStats stats) {
+    private void computeDevicePowerEstimates(PowerComponentAggregatedPowerStats stats) {
         for (int i = mPlan.combinedDeviceStateEstimations.size() - 1; i >= 0; i--) {
             CombinedDeviceStateEstimate estimation =
                     mPlan.combinedDeviceStateEstimations.get(i);
diff --git a/services/core/java/com/android/server/power/stats/processor/WifiPowerStatsProcessor.java b/services/core/java/com/android/server/power/stats/processor/WifiPowerStatsProcessor.java
index 0df01cf..f1797bd 100644
--- a/services/core/java/com/android/server/power/stats/processor/WifiPowerStatsProcessor.java
+++ b/services/core/java/com/android/server/power/stats/processor/WifiPowerStatsProcessor.java
@@ -16,6 +16,7 @@
 
 package com.android.server.power.stats.processor;
 
+import android.util.IntArray;
 import android.util.Slog;
 
 import com.android.internal.os.PowerProfile;
@@ -23,7 +24,6 @@
 import com.android.server.power.stats.UsageBasedPowerEstimator;
 import com.android.server.power.stats.format.WifiPowerStatsLayout;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
@@ -148,18 +148,19 @@
 
         combineDeviceStateEstimates();
 
-        ArrayList<Integer> uids = new ArrayList<>();
-        stats.collectUids(uids);
-        if (!uids.isEmpty()) {
-            for (int uid : uids) {
-                for (int i = 0; i < mPlan.uidStateEstimates.size(); i++) {
-                    computeUidActivityTotals(stats, uid, mPlan.uidStateEstimates.get(i));
+        IntArray uids = stats.getActiveUids();
+        if (uids.size() != 0) {
+            for (int i = uids.size() - 1; i >= 0; i--) {
+                int uid = uids.get(i);
+                for (int j = 0; j < mPlan.uidStateEstimates.size(); j++) {
+                    computeUidActivityTotals(stats, uid, mPlan.uidStateEstimates.get(j));
                 }
             }
 
-            for (int uid : uids) {
-                for (int i = 0; i < mPlan.uidStateEstimates.size(); i++) {
-                    computeUidPowerEstimates(stats, uid, mPlan.uidStateEstimates.get(i));
+            for (int i = uids.size() - 1; i >= 0; i--) {
+                int uid = uids.get(i);
+                for (int j = 0; j < mPlan.uidStateEstimates.size(); j++) {
+                    computeUidPowerEstimates(stats, uid, mPlan.uidStateEstimates.get(j));
                 }
             }
         }
diff --git a/services/tests/powerstatstests/src/com/android/server/power/stats/processor/CpuPowerStatsProcessorTest.java b/services/tests/powerstatstests/src/com/android/server/power/stats/processor/CpuPowerStatsProcessorTest.java
index cb9d9b1..dcddf06 100644
--- a/services/tests/powerstatstests/src/com/android/server/power/stats/processor/CpuPowerStatsProcessorTest.java
+++ b/services/tests/powerstatstests/src/com/android/server/power/stats/processor/CpuPowerStatsProcessorTest.java
@@ -34,6 +34,7 @@
 import android.os.BatteryConsumer;
 import android.os.PersistableBundle;
 import android.platform.test.ravenwood.RavenwoodRule;
+import android.util.IntArray;
 import android.util.LongArray;
 
 import androidx.test.filters.SmallTest;
@@ -51,7 +52,6 @@
 import org.junit.runner.RunWith;
 
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
@@ -315,8 +315,12 @@
         }
 
         @Override
-        void collectUids(Collection<Integer> uids) {
-            uids.addAll(mUids);
+        IntArray getActiveUids() {
+            IntArray uids = new IntArray();
+            for (Integer uid : mUids) {
+                uids.add(uid);
+            }
+            return uids;
         }
 
         void verifyPowerEstimates() {