Combine uids for CpuTimePerUidFreq atom

The puller for the atom frequently fails. We believe it is due to the
size of the pulled atoms. No metric can be correctly build from this
atom.

To reduce the size, we apply transformations that we would likely
perform when analyzing this data. We combine times for all shared app
gids together and combine times for the same apps. We also exclude
isolated uids which are frequently recycled and removed from the
underlying data source.

If these optimization do not help, we will need to introduce different
atoms.

Bug: 157535126
Test: cmd stats pull-source 10010
Change-Id: I0d9a18c78f456e1b0f9d7ad322f15354ead9f02b
Merged-In: I0d9a18c78f456e1b0f9d7ad322f15354ead9f02b
(cherry picked from commit e69ade3545864e7271806d071825ec37486d5d37)
diff --git a/core/java/android/os/UserHandle.java b/core/java/android/os/UserHandle.java
index b92fb47..c3051d6 100644
--- a/core/java/android/os/UserHandle.java
+++ b/core/java/android/os/UserHandle.java
@@ -223,6 +223,14 @@
     }
 
     /**
+     * Whether a UID belongs to a shared app gid.
+     * @hide
+     */
+    public static boolean isSharedAppGid(int uid) {
+        return getAppIdFromSharedAppGid(uid) != -1;
+    }
+
+    /**
      * Returns the user for a given uid.
      * @param uid A uid for an application running in a particular user.
      * @return A {@link UserHandle} for that user.
diff --git a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java
index 3b73c1e..96b53f0 100644
--- a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java
+++ b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java
@@ -31,6 +31,7 @@
 import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
 import static android.net.NetworkTemplate.getAllCollapsedRatTypes;
 import static android.os.Debug.getIonHeapsSizeKb;
+import static android.os.Process.LAST_SHARED_APPLICATION_GID;
 import static android.os.Process.getUidForPid;
 import static android.os.storage.VolumeInfo.TYPE_PRIVATE;
 import static android.os.storage.VolumeInfo.TYPE_PUBLIC;
@@ -1561,20 +1562,59 @@
     }
 
     int pullCpuTimePerUidFreqLocked(int atomTag, List<StatsEvent> pulledData) {
+        // Aggregate times for the same uids.
+        SparseArray<long[]> aggregated = new SparseArray<>();
         mCpuUidFreqTimeReader.readAbsolute((uid, cpuFreqTimeMs) -> {
-            for (int freqIndex = 0; freqIndex < cpuFreqTimeMs.length; ++freqIndex) {
-                if (cpuFreqTimeMs[freqIndex] >= MIN_CPU_TIME_PER_UID_FREQ) {
+            // For uids known to be aggregated from many entries allow mutating in place to avoid
+            // many copies. Otherwise, copy before aggregating.
+            boolean mutateInPlace = false;
+            if (UserHandle.isIsolated(uid)) {
+                // Skip individual isolated uids because they are recycled and quickly removed from
+                // the underlying data source.
+                return;
+            } else if (UserHandle.isSharedAppGid(uid)) {
+                // All shared app gids are accounted together.
+                uid = LAST_SHARED_APPLICATION_GID;
+                mutateInPlace = true;
+            } else if (UserHandle.isApp(uid)) {
+                // Apps are accounted under their app id.
+                uid = UserHandle.getAppId(uid);
+            }
+
+            long[] aggCpuFreqTimeMs = aggregated.get(uid);
+            if (aggCpuFreqTimeMs != null) {
+                if (!mutateInPlace) {
+                    aggCpuFreqTimeMs = Arrays.copyOf(aggCpuFreqTimeMs, cpuFreqTimeMs.length);
+                    aggregated.put(uid, aggCpuFreqTimeMs);
+                }
+                for (int freqIndex = 0; freqIndex < cpuFreqTimeMs.length; ++freqIndex) {
+                    aggCpuFreqTimeMs[freqIndex] += cpuFreqTimeMs[freqIndex];
+                }
+            } else {
+                if (mutateInPlace) {
+                    cpuFreqTimeMs = Arrays.copyOf(cpuFreqTimeMs, cpuFreqTimeMs.length);
+                }
+                aggregated.put(uid, cpuFreqTimeMs);
+            }
+        });
+
+        int size = aggregated.size();
+        for (int i = 0; i < size; ++i) {
+            int uid = aggregated.keyAt(i);
+            long[] aggCpuFreqTimeMs = aggregated.valueAt(i);
+            for (int freqIndex = 0; freqIndex < aggCpuFreqTimeMs.length; ++freqIndex) {
+                if (aggCpuFreqTimeMs[freqIndex] >= MIN_CPU_TIME_PER_UID_FREQ) {
                     StatsEvent e = StatsEvent.newBuilder()
                             .setAtomId(atomTag)
                             .writeInt(uid)
                             .addBooleanAnnotation(ANNOTATION_ID_IS_UID, true)
                             .writeInt(freqIndex)
-                            .writeLong(cpuFreqTimeMs[freqIndex])
+                            .writeLong(aggCpuFreqTimeMs[freqIndex])
                             .build();
                     pulledData.add(e);
                 }
             }
-        });
+        }
         return StatsManager.PULL_SUCCESS;
     }