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

Commit b3d89d6d authored by Jing Ji's avatar Jing Ji Committed by Android (Google) Code Review
Browse files

Merge "Avoid full oomAdjUpdate in unbinding services / provders etc."

parents b48e19a6 a744733c
Loading
Loading
Loading
Loading
+10 −8
Original line number Diff line number Diff line
@@ -2268,21 +2268,23 @@ public final class ActiveServices {
                    clist.remove(0);
                }

                if (r.binding.service.app != null) {
                    if (r.binding.service.app.whitelistManager) {
                        updateWhitelistManagerLocked(r.binding.service.app);
                final ProcessRecord app = r.binding.service.app;
                if (app != null) {
                    if (app.whitelistManager) {
                        updateWhitelistManagerLocked(app);
                    }
                    // This could have made the service less important.
                    if ((r.flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) {
                        r.binding.service.app.treatLikeActivity = true;
                        mAm.updateLruProcessLocked(r.binding.service.app,
                                r.binding.service.app.hasClientActivities()
                                || r.binding.service.app.treatLikeActivity, null);
                        app.treatLikeActivity = true;
                        mAm.updateLruProcessLocked(app,
                                app.hasClientActivities()
                                || app.treatLikeActivity, null);
                    }
                    mAm.enqueueOomAdjTargetLocked(app);
                }
            }

            mAm.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_UNBIND_SERVICE);
            mAm.updateOomAdjPendingTargetsLocked(OomAdjuster.OOM_ADJ_REASON_UNBIND_SERVICE);

        } finally {
            Binder.restoreCallingIdentity(origId);
+48 −14
Original line number Diff line number Diff line
@@ -5686,8 +5686,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                "setProcessLimit()");
        synchronized (this) {
            mConstants.setOverrideMaxCachedProcesses(max);
            trimApplicationsLocked(true, OomAdjuster.OOM_ADJ_REASON_PROCESS_END);
        }
        trimApplications(OomAdjuster.OOM_ADJ_REASON_PROCESS_END);
    }
    @Override
@@ -13995,7 +13995,8 @@ public class ActivityManagerService extends IActivityManager.Stub
                                r.resultAbort, false);
                        if (doNext) {
                            doTrim = true;
                            r.queue.processNextBroadcast(false);
                            r.queue.processNextBroadcastLocked(/* frommsg */ false,
                                    /* skipOomAdj */ true);
                        }
                    }
@@ -14008,14 +14009,14 @@ public class ActivityManagerService extends IActivityManager.Stub
                        rl.receiver.asBinder().unlinkToDeath(rl, 0);
                    }
                }
            }
                // If we actually concluded any broadcasts, we might now be able
                // to trim the recipients' apps from our working set
                if (doTrim) {
                trimApplications(OomAdjuster.OOM_ADJ_REASON_FINISH_RECEIVER);
                    trimApplicationsLocked(false, OomAdjuster.OOM_ADJ_REASON_FINISH_RECEIVER);
                    return;
                }
            }
        } finally {
            Binder.restoreCallingIdentity(origId);
@@ -15145,7 +15146,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                    r.queue.processNextBroadcastLocked(/*fromMsg=*/ false, /*skipOomAdj=*/ true);
                }
                // updateOomAdjLocked() will be done here
                trimApplicationsLocked(OomAdjuster.OOM_ADJ_REASON_FINISH_RECEIVER);
                trimApplicationsLocked(false, OomAdjuster.OOM_ADJ_REASON_FINISH_RECEIVER);
            }
        } finally {
@@ -16155,6 +16156,32 @@ public class ActivityManagerService extends IActivityManager.Stub
        return mOomAdjuster.updateOomAdjLocked(app, oomAdjAll, oomAdjReason);
    }
    /**
     * Enqueue the given process into a todo list, and the caller should
     * call {@link #updateOomAdjPendingTargetsLocked} to kick off a pass of the oom adj update.
     */
    @GuardedBy("this")
    void enqueueOomAdjTargetLocked(ProcessRecord app) {
        mOomAdjuster.enqueueOomAdjTargetLocked(app);
    }
    /**
     * Remove the given process into a todo list.
     */
    @GuardedBy("this")
    void removeOomAdjTargetLocked(ProcessRecord app, boolean procDied) {
        mOomAdjuster.removeOomAdjTargetLocked(app, procDied);
    }
    /**
     * Kick off an oom adj update pass for the pending targets which are enqueued via
     * {@link #enqueueOomAdjTargetLocked}.
     */
    @GuardedBy("this")
    void updateOomAdjPendingTargetsLocked(String oomAdjReason) {
        mOomAdjuster.updateOomAdjPendingTargetsLocked(oomAdjReason);
    }
    static final class ProcStatsRunnable implements Runnable {
        private final ActivityManagerService mService;
        private final ProcessStatsService mProcessStats;
@@ -16563,16 +16590,17 @@ public class ActivityManagerService extends IActivityManager.Stub
        mOomAdjuster.setUidTempWhitelistStateLocked(uid, onWhitelist);
    }
    final void trimApplications(String oomAdjReason) {
    private void trimApplications(boolean forceFullOomAdj, String oomAdjReason) {
        synchronized (this) {
            trimApplicationsLocked(oomAdjReason);
            trimApplicationsLocked(forceFullOomAdj, oomAdjReason);
        }
    }
    @GuardedBy("this")
    final void trimApplicationsLocked(String oomAdjReason) {
    private void trimApplicationsLocked(boolean forceFullOomAdj, String oomAdjReason) {
        // First remove any unused application processes whose package
        // has been removed.
        boolean didSomething = false;
        for (int i = mProcessList.mRemovedProcesses.size() - 1; i >= 0; i--) {
            final ProcessRecord app = mProcessList.mRemovedProcesses.get(i);
            if (!app.hasActivitiesOrRecentTasks()
@@ -16594,6 +16622,7 @@ public class ActivityManagerService extends IActivityManager.Stub
                        // Ignore exceptions.
                    }
                }
                didSomething = true;
                cleanUpApplicationRecordLocked(app, false, true, -1, false /*replacingPid*/);
                mProcessList.mRemovedProcesses.remove(i);
@@ -16606,7 +16635,12 @@ public class ActivityManagerService extends IActivityManager.Stub
        // Now update the oom adj for all processes. Don't skip this, since other callers
        // might be depending on it.
        if (didSomething || forceFullOomAdj) {
            updateOomAdjLocked(oomAdjReason);
        } else {
            // Process any pending oomAdj targets, it'll be a no-op if nothing is pending.
            updateOomAdjPendingTargetsLocked(oomAdjReason);
        }
    }
    /** This method sends the specified signal to each of the persistent apps */
@@ -17411,7 +17445,7 @@ public class ActivityManagerService extends IActivityManager.Stub
        @Override
        public void trimApplications() {
            ActivityManagerService.this.trimApplications(OomAdjuster.OOM_ADJ_REASON_ACTIVITY);
            ActivityManagerService.this.trimApplications(true, OomAdjuster.OOM_ADJ_REASON_ACTIVITY);
        }
        public void killProcessesForRemovedTask(ArrayList<Object> procsToKill) {
+22 −17
Original line number Diff line number Diff line
@@ -281,7 +281,7 @@ public final class BroadcastQueue {
    }

    private final void processCurBroadcastLocked(BroadcastRecord r,
            ProcessRecord app, boolean skipOomAdj) throws RemoteException {
            ProcessRecord app) throws RemoteException {
        if (DEBUG_BROADCAST)  Slog.v(TAG_BROADCAST,
                "Process cur broadcast " + r + " for app " + app);
        if (app.thread == null) {
@@ -297,9 +297,11 @@ public final class BroadcastQueue {
        app.curReceivers.add(r);
        app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER);
        mService.mProcessList.updateLruProcessLocked(app, false, null);
        if (!skipOomAdj) {
            mService.updateOomAdjLocked(app, OomAdjuster.OOM_ADJ_REASON_NONE);
        }
        // Make sure the oom adj score is updated before delivering the broadcast.
        // Force an update, even if there are other pending requests, overall it still saves time,
        // because time(updateOomAdj(N apps)) <= N * time(updateOomAdj(1 app)).
        mService.enqueueOomAdjTargetLocked(app);
        mService.updateOomAdjPendingTargetsLocked(OomAdjuster.OOM_ADJ_REASON_START_RECEIVER);

        // Tell the application to launch this receiver.
        r.intent.setComponent(r.curComponent);
@@ -340,7 +342,7 @@ public final class BroadcastQueue {
            }
            try {
                mPendingBroadcast = null;
                processCurBroadcastLocked(br, app, false);
                processCurBroadcastLocked(br, app);
                didSomething = true;
            } catch (Exception e) {
                Slog.w(TAG, "Exception in new application when starting receiver "
@@ -501,6 +503,7 @@ public final class BroadcastQueue {
        r.intent.setComponent(null);
        if (r.curApp != null && r.curApp.curReceivers.contains(r)) {
            r.curApp.curReceivers.remove(r);
            mService.enqueueOomAdjTargetLocked(r.curApp);
        }
        if (r.curFilter != null) {
            r.curFilter.receiverList.curBroadcast = null;
@@ -562,7 +565,7 @@ public final class BroadcastQueue {
                Slog.i(TAG, "Resuming delayed broadcast");
                br.curComponent = null;
                br.state = BroadcastRecord.IDLE;
                processNextBroadcast(false);
                processNextBroadcastLocked(false, false);
            }
        }
    }
@@ -604,7 +607,7 @@ public final class BroadcastQueue {
    }

    private void deliverToRegisteredReceiverLocked(BroadcastRecord r,
            BroadcastFilter filter, boolean ordered, int index, boolean skipOomAdj) {
            BroadcastFilter filter, boolean ordered, int index) {
        boolean skip = false;
        if (!mService.validateAssociationAllowedLocked(r.callerPackage, r.callingUid,
                filter.packageName, filter.owningUid)) {
@@ -790,12 +793,11 @@ public final class BroadcastQueue {
                // are already core system stuff so don't matter for this.
                r.curApp = filter.receiverList.app;
                filter.receiverList.app.curReceivers.add(r);
                if (!skipOomAdj) {
                    mService.updateOomAdjLocked(r.curApp, true,
                mService.enqueueOomAdjTargetLocked(r.curApp);
                mService.updateOomAdjPendingTargetsLocked(
                        OomAdjuster.OOM_ADJ_REASON_START_RECEIVER);
            }
        }
        }
        try {
            if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST,
                    "Delivering to " + filter + " : " + r);
@@ -827,6 +829,8 @@ public final class BroadcastQueue {
                filter.receiverList.app.removeAllowBackgroundActivityStartsToken(r);
                if (ordered) {
                    filter.receiverList.app.curReceivers.remove(r);
                    // Something wrong, its oom adj could be downgraded, but not in a hurry.
                    mService.enqueueOomAdjTargetLocked(r.curApp);
                }
            }
            // And BroadcastRecord state related to ordered delivery, if appropriate
@@ -946,7 +950,7 @@ public final class BroadcastQueue {
        return true;
    }

    final void processNextBroadcast(boolean fromMsg) {
    private void processNextBroadcast(boolean fromMsg) {
        synchronized (mService) {
            processNextBroadcastLocked(fromMsg, false);
        }
@@ -990,7 +994,7 @@ public final class BroadcastQueue {
                        "Delivering non-ordered on [" + mQueueName + "] to registered "
                        + target + ": " + r);
                deliverToRegisteredReceiverLocked(r,
                        (BroadcastFilter) target, false, i, skipOomAdj);
                        (BroadcastFilter) target, false, i);
            }
            addBroadcastToHistoryLocked(r);
            if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast ["
@@ -1046,7 +1050,8 @@ public final class BroadcastQueue {
                    // If we had finished the last ordered broadcast, then
                    // make sure all processes have correct oom and sched
                    // adjustments.
                    mService.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_START_RECEIVER);
                    mService.updateOomAdjPendingTargetsLocked(
                            OomAdjuster.OOM_ADJ_REASON_START_RECEIVER);
                }

                // when we have no more ordered broadcast on this queue, stop logging
@@ -1288,7 +1293,7 @@ public final class BroadcastQueue {
                    "Delivering ordered ["
                    + mQueueName + "] to registered "
                    + filter + ": " + r);
            deliverToRegisteredReceiverLocked(r, filter, r.ordered, recIdx, skipOomAdj);
            deliverToRegisteredReceiverLocked(r, filter, r.ordered, recIdx);
            if (r.receiver == null || !r.ordered) {
                // The receiver has already finished, so schedule to
                // process the next one.
@@ -1616,7 +1621,7 @@ public final class BroadcastQueue {
                app.addPackage(info.activityInfo.packageName,
                        info.activityInfo.applicationInfo.longVersionCode, mService.mProcessStats);
                maybeAddAllowBackgroundActivityStartsToken(app, r);
                processCurBroadcastLocked(r, app, skipOomAdj);
                processCurBroadcastLocked(r, app);
                return;
            } catch (RemoteException e) {
                Slog.w(TAG, "Exception when sending broadcast to "
@@ -1750,7 +1755,7 @@ public final class BroadcastQueue {
                    ? r.curComponent.flattenToShortString() : "(null)"));
            r.curComponent = null;
            r.state = BroadcastRecord.IDLE;
            processNextBroadcast(false);
            processNextBroadcastLocked(false, false);
            return;
        }

+4 −2
Original line number Diff line number Diff line
@@ -698,7 +698,8 @@ public class ContentProviderHelper {
                    throw new NullPointerException("connection is null");
                }
                if (decProviderCountLocked(conn, null, null, stable)) {
                    mService.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_REMOVE_PROVIDER);
                    mService.updateOomAdjLocked(conn.provider.proc,
                            OomAdjuster.OOM_ADJ_REASON_REMOVE_PROVIDER);
                }
            }
        } finally {
@@ -734,7 +735,8 @@ public class ContentProviderHelper {
            ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp, userId);
            if (localCpr.hasExternalProcessHandles()) {
                if (localCpr.removeExternalProcessHandleLocked(token)) {
                    mService.updateOomAdjLocked(OomAdjuster.OOM_ADJ_REASON_REMOVE_PROVIDER);
                    mService.updateOomAdjLocked(localCpr.proc,
                            OomAdjuster.OOM_ADJ_REASON_REMOVE_PROVIDER);
                } else {
                    Slog.e(TAG, "Attempt to remove content provider " + localCpr
                            + " with no external reference for token: " + token + ".");
+175 −9

File changed.

Preview size limit exceeded, changes collapsed.

Loading