Another way to keep track number of foreground services in the device.
Add staic field sNumForegroundServices to keep track of number of foreground services and apps in the device. This field is used to report statsd atom LmkKillOccurred which can not hold AMS lock. Bug: 243768727 Test: statsd_testdrive 51 Change-Id: Id4e53ecf8902c8373e79537a2732586c8cb553d5
This commit is contained in:
@@ -192,6 +192,7 @@ import java.util.Comparator;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public final class ActiveServices {
|
public final class ActiveServices {
|
||||||
@@ -222,6 +223,11 @@ public final class ActiveServices {
|
|||||||
| ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
|
| ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
|
||||||
| ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION;
|
| ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION;
|
||||||
|
|
||||||
|
// Keep track of number of foreground services and number of apps that have foreground
|
||||||
|
// services in the device. This field is made to be directly accessed without holding AMS lock.
|
||||||
|
static final AtomicReference<Pair<Integer, Integer>> sNumForegroundServices =
|
||||||
|
new AtomicReference(new Pair<>(0, 0));
|
||||||
|
|
||||||
// Foreground service is stopped for unknown reason.
|
// Foreground service is stopped for unknown reason.
|
||||||
static final int FGS_STOP_REASON_UNKNOWN = 0;
|
static final int FGS_STOP_REASON_UNKNOWN = 0;
|
||||||
// Foreground service is stopped by app calling Service.stopForeground().
|
// Foreground service is stopped by app calling Service.stopForeground().
|
||||||
@@ -456,6 +462,7 @@ public final class ActiveServices {
|
|||||||
final ArrayList<ServiceRecord> mStartingBackground = new ArrayList<>();
|
final ArrayList<ServiceRecord> mStartingBackground = new ArrayList<>();
|
||||||
|
|
||||||
final ArrayMap<String, ActiveForegroundApp> mActiveForegroundApps = new ArrayMap<>();
|
final ArrayMap<String, ActiveForegroundApp> mActiveForegroundApps = new ArrayMap<>();
|
||||||
|
|
||||||
boolean mActiveForegroundAppsChanged;
|
boolean mActiveForegroundAppsChanged;
|
||||||
|
|
||||||
static final int MSG_BG_START_TIMEOUT = 1;
|
static final int MSG_BG_START_TIMEOUT = 1;
|
||||||
@@ -2027,6 +2034,7 @@ public final class ActiveServices {
|
|||||||
logFGSStateChangeLocked(r,
|
logFGSStateChangeLocked(r,
|
||||||
FrameworkStatsLog.FOREGROUND_SERVICE_STATE_CHANGED__STATE__ENTER,
|
FrameworkStatsLog.FOREGROUND_SERVICE_STATE_CHANGED__STATE__ENTER,
|
||||||
0, FGS_STOP_REASON_UNKNOWN);
|
0, FGS_STOP_REASON_UNKNOWN);
|
||||||
|
updateNumForegroundServicesLocked();
|
||||||
}
|
}
|
||||||
// Even if the service is already a FGS, we need to update the notification,
|
// Even if the service is already a FGS, we need to update the notification,
|
||||||
// so we need to call it again.
|
// so we need to call it again.
|
||||||
@@ -2118,6 +2126,7 @@ public final class ActiveServices {
|
|||||||
mAm.updateLruProcessLocked(r.app, false, null);
|
mAm.updateLruProcessLocked(r.app, false, null);
|
||||||
updateServiceForegroundLocked(r.app.mServices, true);
|
updateServiceForegroundLocked(r.app.mServices, true);
|
||||||
}
|
}
|
||||||
|
updateNumForegroundServicesLocked();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4836,6 +4845,7 @@ public final class ActiveServices {
|
|||||||
}
|
}
|
||||||
|
|
||||||
smap.ensureNotStartingBackgroundLocked(r);
|
smap.ensureNotStartingBackgroundLocked(r);
|
||||||
|
updateNumForegroundServicesLocked();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dropFgsNotificationStateLocked(ServiceRecord r) {
|
private void dropFgsNotificationStateLocked(ServiceRecord r) {
|
||||||
@@ -7040,6 +7050,10 @@ public final class ActiveServices {
|
|||||||
fgsStopReasonToString(fgsStopReason));
|
fgsStopReasonToString(fgsStopReason));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateNumForegroundServicesLocked() {
|
||||||
|
sNumForegroundServices.set(mAm.mProcessList.getNumForegroundServices());
|
||||||
|
}
|
||||||
|
|
||||||
boolean canAllowWhileInUsePermissionInFgsLocked(int callingPid, int callingUid,
|
boolean canAllowWhileInUsePermissionInFgsLocked(int callingPid, int callingUid,
|
||||||
String callingPackage) {
|
String callingPackage) {
|
||||||
return shouldAllowFgsWhileInUsePermissionLocked(callingPackage, callingPid, callingUid,
|
return shouldAllowFgsWhileInUsePermissionLocked(callingPackage, callingPid, callingUid,
|
||||||
|
|||||||
@@ -814,12 +814,14 @@ public final class ProcessList {
|
|||||||
< LmkdStatsReporter.KILL_OCCURRED_MSG_SIZE) {
|
< LmkdStatsReporter.KILL_OCCURRED_MSG_SIZE) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Pair<Integer, Integer> temp = getNumForegroundServices();
|
// Note: directly access
|
||||||
final int totalForegroundServices = temp.first;
|
// ActiveServices.sNumForegroundServices, do not try to
|
||||||
final int procsWithForegroundServices = temp.second;
|
// hold AMS lock here, otherwise it is a potential deadlock.
|
||||||
|
Pair<Integer, Integer> foregroundServices =
|
||||||
|
ActiveServices.sNumForegroundServices.get();
|
||||||
LmkdStatsReporter.logKillOccurred(inputData,
|
LmkdStatsReporter.logKillOccurred(inputData,
|
||||||
totalForegroundServices,
|
foregroundServices.first,
|
||||||
procsWithForegroundServices);
|
foregroundServices.second);
|
||||||
return true;
|
return true;
|
||||||
case LMK_STATE_CHANGED:
|
case LMK_STATE_CHANGED:
|
||||||
if (receivedLen
|
if (receivedLen
|
||||||
|
|||||||
Reference in New Issue
Block a user