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

Commit 04966c28 authored by Linux Build Service Account's avatar Linux Build Service Account Committed by Gerrit - the friendly Code Review server
Browse files

Merge "Performance: Postpone service restart during app launch."

parents a7077da5 bdd21bf3
Loading
Loading
Loading
Loading
+66 −1
Original line number Diff line number Diff line
@@ -121,6 +121,10 @@ public final class ActiveServices {
    // at the same time.
    final int mMaxStartingBackground;

    // Flag to reschedule the services during app launch. Disable by default.
    private static final boolean SERVICE_RESCHEDULE
            = SystemProperties.getBoolean("ro.am.reschedule_service", false);

    final SparseArray<ServiceMap> mServiceMap = new SparseArray<>();

    /**
@@ -1229,6 +1233,14 @@ public final class ActiveServices {
                        r.pendingStarts.add(0, si);
                        long dur = SystemClock.uptimeMillis() - si.deliveredTime;
                        dur *= 2;
                        if (SERVICE_RESCHEDULE && DEBUG_DELAYED_SERVICE) {
                            Slog.w(TAG,"Can add more delay !!!"
                               +" si.deliveredTime "+si.deliveredTime
                               +" dur "+dur
                               +" si.deliveryCount "+si.deliveryCount
                               +" si.doneExecutingCount "+si.doneExecutingCount
                               +" allowCancel "+allowCancel);
                        }
                        if (minDuration < dur) minDuration = dur;
                        if (resetTime < dur) resetTime = dur;
                    } else {
@@ -1241,6 +1253,13 @@ public final class ActiveServices {
            }

            r.totalRestartCount++;
            if (SERVICE_RESCHEDULE && DEBUG_DELAYED_SERVICE) {
                Slog.w(TAG,"r.name "+r.name+" N "+N+" minDuration "+minDuration
                       +" resetTime "+resetTime+" now "+now
                       +" r.restartDelay "+r.restartDelay
                       +" r.restartTime+resetTime "+(r.restartTime+resetTime)
                       +" allowCancel "+allowCancel);
            }
            if (r.restartDelay == 0) {
                r.restartCount++;
                r.restartDelay = minDuration;
@@ -1262,6 +1281,14 @@ public final class ActiveServices {
            }

            r.nextRestartTime = now + r.restartDelay;
            if (SERVICE_RESCHEDULE && DEBUG_DELAYED_SERVICE) {
                Slog.w(TAG,"r.name "+r.name+" N "+N+" minDuration "+minDuration
                       +" resetTime "+resetTime+" now "+now
                       +" r.restartDelay "+r.restartDelay
                       +" r.restartTime+resetTime "+(r.restartTime+resetTime)
                       +" r.nextRestartTime "+r.nextRestartTime
                       +" allowCancel "+allowCancel);
            }

            // Make sure that we don't end up restarting a bunch of services
            // all at the same time.
@@ -1304,6 +1331,15 @@ public final class ActiveServices {
        r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay;
        Slog.w(TAG, "Scheduling restart of crashed service "
                + r.shortName + " in " + r.restartDelay + "ms");

        if (SERVICE_RESCHEDULE && DEBUG_DELAYED_SERVICE) {
            for (int i=mRestartingServices.size()-1; i>=0; i--) {
                ServiceRecord r2 = mRestartingServices.get(i);
                Slog.w(TAG,"Restarting list - i "+i+" r2.nextRestartTime "
                           +r2.nextRestartTime+" r2.name "+r2.name);
            }
        }

        EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART,
                r.userId, r.shortName, r.restartDelay);

@@ -1315,7 +1351,31 @@ public final class ActiveServices {
            return;
        }
        try {
            if(SERVICE_RESCHEDULE == true) {
                boolean shouldDelay = false;
                ActivityRecord top_rc = null;
                ActivityStack stack = mAm.getFocusedStack();
                if(stack != null) {
                    top_rc = stack.topRunningActivityLocked(null);
                }
                if(top_rc != null) {
                    if(!top_rc.nowVisible && !r.shortName.contains(top_rc.packageName)) {
                        shouldDelay = true;
                    }
                }
                if(!shouldDelay) {
                    bringUpServiceLocked(r, r.intent.getIntent().getFlags(), r.createdFromFg, true);
                } else {
                    if (DEBUG_DELAYED_SERVICE) {
                        Slog.v(TAG, "Reschedule service restart due to app launch"
                              +" r.shortName "+r.shortName+" r.app = "+r.app);
                    }
                    r.resetRestartCounter();
                    scheduleServiceRestartLocked(r, true);
                }
            } else {
                bringUpServiceLocked(r, r.intent.getIntent().getFlags(), r.createdFromFg, true);
            }
        } catch (TransactionTooLargeException e) {
            // Ignore, it's been logged and nothing upstack cares.
        }
@@ -1538,6 +1598,11 @@ public final class ActiveServices {
                if (newService) {
                    app.services.remove(r);
                    r.app = null;
                    if (SERVICE_RESCHEDULE && DEBUG_DELAYED_SERVICE) {
                    Slog.w(TAG, " Failed to create Service !!!! ."
                           +"This will introduce huge delay...  "
                           +r.shortName + " in " + r.restartDelay + "ms");
                    }
                }

                // Retry.