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

Commit 633a81af authored by Suyog Sarda's avatar Suyog Sarda Committed by Kishore Srivenkata Ganesh Bolisetty
Browse files

Performance: Postpone service restart during app launch.

In the android framework, when the service gets killed,
it will be rescheduled. Postpone the service restart if
the app is in process of startup. By Default this feature
is disabled. It can be enabled from build.prop of target.

Change-Id: Ia86edf027e20df2c7aa6f34e3aa293f7a8e6f1fa
parent d82f7799
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.