From 66eb785a99c20c326db081d8c03bdb74ca375ea8 Mon Sep 17 00:00:00 2001 From: Amith Yamasani Date: Wed, 29 May 2019 14:09:53 -0700 Subject: [PATCH] Reduce calls to updateOomAdj() during unbind When unbinding a service, avoid repeatedly calling updateOomAdj() as doing it one time should be sufficient. Running the below CTS test repeatedly with and without the optimization resulted in CPU thread time changing from 1800ms to 1450ms. Test includes a mix of service bindings and activity launches, etc. Bug: 130684057 Test: atest CtsAppTestCases:ActivityManagerProcessStateTest Change-Id: Iadb3743ac1d8b506a22baded279f2b7db7c60aaf --- .../java/com/android/server/am/ActiveServices.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 90266f12a5ddf..830b3e0a0c15c 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -1946,8 +1946,6 @@ public final class ActiveServices { r.binding.service.app.hasClientActivities() || r.binding.service.app.treatLikeActivity, null); } - mAm.updateOomAdjLocked(r.binding.service.app, false, - OomAdjuster.OOM_ADJ_REASON_UNBIND_SERVICE); } } @@ -2906,15 +2904,15 @@ public final class ActiveServices { // Tell the service that it has been unbound. if (r.app != null && r.app.thread != null) { - for (int i=r.bindings.size()-1; i>=0; i--) { + boolean needOomAdj = false; + for (int i = r.bindings.size() - 1; i >= 0; i--) { IntentBindRecord ibr = r.bindings.valueAt(i); if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Bringing down binding " + ibr + ": hasBound=" + ibr.hasBound); if (ibr.hasBound) { try { bumpServiceExecutingLocked(r, false, "bring down unbind"); - mAm.updateOomAdjLocked(r.app, true, - OomAdjuster.OOM_ADJ_REASON_UNBIND_SERVICE); + needOomAdj = true; ibr.hasBound = false; ibr.requested = false; r.app.thread.scheduleUnbindService(r, @@ -2926,6 +2924,10 @@ public final class ActiveServices { } } } + if (needOomAdj) { + mAm.updateOomAdjLocked(r.app, true, + OomAdjuster.OOM_ADJ_REASON_UNBIND_SERVICE); + } } // Check to see if the service had been started as foreground, but being