Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aaf134e0 authored by Richard Gaywood's avatar Richard Gaywood
Browse files

Puller for PendingIntentController

Populate a new stats atom with information about PendingIntents; per-uid: (1) the total number of intents and (2) the total size of any extras bundled with the intents.

Change-Id: I04e6b368a76623c80ead6f846baf85b1a6c5ce70
Test: statsd_testdrive 10151
Bug: 217162457
parent 6da7744f
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -831,4 +831,9 @@ public abstract class ActivityManagerInternal {
     * Restart android.
     */
    public abstract void restart();

    /**
     * Returns some summary statistics of the current PendingIntent queue - sizes and counts.
     */
    public abstract List<PendingIntentStats> getPendingIntentStats();
}
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 android.app;

/**
 * Data about pending intents - size and count, per UID that sent the intent.
 * @hide
 */
public class PendingIntentStats {
    public final int uid;
    public final int count;
    public final int sizeKb;

    public PendingIntentStats(int uid, int count, int sizeKb) {
        this.uid = uid;
        this.count = count;
        this.sizeKb = sizeKb;
    }
}
+10 −0
Original line number Diff line number Diff line
@@ -9208,6 +9208,16 @@ public class Intent implements Parcelable, Cloneable {
                : null;
    }

    /**
     * Returns the total size of the extras in bytes, or 0 if no extras are present.
     * @hide
     */
    public int getExtrasTotalSize() {
        return (mExtras != null)
                ? mExtras.getSize()
                : 0;
    }

    /**
     * @return Whether {@link #maybeStripForHistory} will return an lightened intent or
     * return itself as-is.
+6 −0
Original line number Diff line number Diff line
@@ -188,6 +188,7 @@ import android.app.Instrumentation;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.PendingIntentStats;
import android.app.ProcessMemoryState;
import android.app.ProfilerInfo;
import android.app.SyncNotedAppOp;
@@ -15951,6 +15952,11 @@ public class ActivityManagerService extends IActivityManager.Stub
    public final class LocalService extends ActivityManagerInternal
            implements ActivityManagerLocal {
        @Override
        public List<PendingIntentStats> getPendingIntentStats() {
            return mPendingIntentController.dumpPendingIntentStatsForStatsd();
        }
        @Override
        public Pair<String, String> getAppProfileStatsForDebugging(long time, int lines) {
            return mAppProfiler.getAppProfileStatsForDebugging(time, lines);
+51 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.app.Activity;
import android.app.ActivityManagerInternal;
import android.app.AppGlobals;
import android.app.PendingIntent;
import android.app.PendingIntentStats;
import android.content.IIntentSender;
import android.content.Intent;
import android.os.Binder;
@@ -60,6 +61,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Helper class for {@link ActivityManagerService} responsible for managing pending intents.
@@ -421,6 +423,55 @@ public class PendingIntentController {
        }
    }

    /**
     * Provides some stats tracking of the current state of the PendingIntent queue.
     *
     * Data about the pending intent queue is intended to be used for memory impact tracking.
     * Returned data (one per uid) will consist of instances of PendingIntentStats containing
     * (I) number of PendingIntents and (II) total size of all bundled extras in the PIs.
     *
     * @hide
     */
    public List<PendingIntentStats> dumpPendingIntentStatsForStatsd() {
        List<PendingIntentStats> pendingIntentStats = new ArrayList<>();

        synchronized (mLock) {
            if (mIntentSenderRecords.size() > 0) {
                // First, aggregate PendingIntent data by package uid.
                final SparseIntArray countsByUid = new SparseIntArray();
                final SparseIntArray bundleSizesByUid = new SparseIntArray();

                for (WeakReference<PendingIntentRecord> reference : mIntentSenderRecords.values()) {
                    if (reference == null || reference.get() == null) {
                        continue;
                    }
                    PendingIntentRecord record = reference.get();
                    int index = countsByUid.indexOfKey(record.uid);

                    if (index < 0) { // ie. the key was not found
                        countsByUid.put(record.uid, 1);
                        bundleSizesByUid.put(record.uid,
                                record.key.requestIntent.getExtrasTotalSize());
                    } else {
                        countsByUid.put(record.uid, countsByUid.valueAt(index) + 1);
                        bundleSizesByUid.put(record.uid,
                                bundleSizesByUid.valueAt(index)
                                + record.key.requestIntent.getExtrasTotalSize());
                    }
                }

                // Now generate the output.
                for (int i = 0, size = countsByUid.size(); i < size; i++) {
                    pendingIntentStats.add(new PendingIntentStats(
                            countsByUid.keyAt(i),
                            countsByUid.valueAt(i),
                            /* NB: int conversion here */ bundleSizesByUid.valueAt(i) / 1024));
                }
            }
        }
        return pendingIntentStats;
    }

    /**
     * Increment the number of the PendingIntentRecord for the given uid, log a warning
     * if there are too many for this uid already.
Loading