Skip to content

Commit 6f7af03

Browse files
committed
Process AMS events in NetworkPolicy handler.
When a process changes foreground status or dies, NetworkPolicy updates its internal state with a lock held. In cases where there is contention, this can block the AMS handler and prevent other events, such as broadcasts, from being dispatched. This change moves the incoming AMS events to an existing internal NetworkPolicy handler thread, where they can execute without blocking AMS. Bug: 5497544 Change-Id: Ie0c620a620fd9f0f4eb02af510bd819efa4deb6a
1 parent d5b25ec commit 6f7af03

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

services/java/com/android/server/net/NetworkPolicyManagerService.java

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
186186

187187
private static final long TIME_CACHE_MAX_AGE = DAY_IN_MILLIS;
188188

189-
private static final int MSG_RULES_CHANGED = 0x1;
190-
private static final int MSG_METERED_IFACES_CHANGED = 0x2;
189+
private static final int MSG_RULES_CHANGED = 1;
190+
private static final int MSG_METERED_IFACES_CHANGED = 2;
191+
private static final int MSG_FOREGROUND_ACTIVITIES_CHANGED = 3;
192+
private static final int MSG_PROCESS_DIED = 4;
191193

192194
private final Context mContext;
193195
private final IActivityManager mActivityManager;
@@ -335,37 +337,13 @@ public void systemReady() {
335337
private IProcessObserver mProcessObserver = new IProcessObserver.Stub() {
336338
@Override
337339
public void onForegroundActivitiesChanged(int pid, int uid, boolean foregroundActivities) {
338-
// only someone like AMS should only be calling us
339-
mContext.enforceCallingOrSelfPermission(MANAGE_APP_TOKENS, TAG);
340-
341-
synchronized (mRulesLock) {
342-
// because a uid can have multiple pids running inside, we need to
343-
// remember all pid states and summarize foreground at uid level.
344-
345-
// record foreground for this specific pid
346-
SparseBooleanArray pidForeground = mUidPidForeground.get(uid);
347-
if (pidForeground == null) {
348-
pidForeground = new SparseBooleanArray(2);
349-
mUidPidForeground.put(uid, pidForeground);
350-
}
351-
pidForeground.put(pid, foregroundActivities);
352-
computeUidForegroundLocked(uid);
353-
}
340+
mHandler.obtainMessage(MSG_FOREGROUND_ACTIVITIES_CHANGED,
341+
pid, uid, foregroundActivities).sendToTarget();
354342
}
355343

356344
@Override
357345
public void onProcessDied(int pid, int uid) {
358-
// only someone like AMS should only be calling us
359-
mContext.enforceCallingOrSelfPermission(MANAGE_APP_TOKENS, TAG);
360-
361-
synchronized (mRulesLock) {
362-
// clear records and recompute, when they exist
363-
final SparseBooleanArray pidForeground = mUidPidForeground.get(uid);
364-
if (pidForeground != null) {
365-
pidForeground.delete(pid);
366-
computeUidForegroundLocked(uid);
367-
}
368-
}
346+
mHandler.obtainMessage(MSG_PROCESS_DIED, pid, uid).sendToTarget();
369347
}
370348
};
371349

@@ -1469,6 +1447,40 @@ public boolean handleMessage(Message msg) {
14691447
mListeners.finishBroadcast();
14701448
return true;
14711449
}
1450+
case MSG_FOREGROUND_ACTIVITIES_CHANGED: {
1451+
final int pid = msg.arg1;
1452+
final int uid = msg.arg2;
1453+
final boolean foregroundActivities = (Boolean) msg.obj;
1454+
1455+
synchronized (mRulesLock) {
1456+
// because a uid can have multiple pids running inside, we need to
1457+
// remember all pid states and summarize foreground at uid level.
1458+
1459+
// record foreground for this specific pid
1460+
SparseBooleanArray pidForeground = mUidPidForeground.get(uid);
1461+
if (pidForeground == null) {
1462+
pidForeground = new SparseBooleanArray(2);
1463+
mUidPidForeground.put(uid, pidForeground);
1464+
}
1465+
pidForeground.put(pid, foregroundActivities);
1466+
computeUidForegroundLocked(uid);
1467+
}
1468+
return true;
1469+
}
1470+
case MSG_PROCESS_DIED: {
1471+
final int pid = msg.arg1;
1472+
final int uid = msg.arg2;
1473+
1474+
synchronized (mRulesLock) {
1475+
// clear records and recompute, when they exist
1476+
final SparseBooleanArray pidForeground = mUidPidForeground.get(uid);
1477+
if (pidForeground != null) {
1478+
pidForeground.delete(pid);
1479+
computeUidForegroundLocked(uid);
1480+
}
1481+
}
1482+
return true;
1483+
}
14721484
default: {
14731485
return false;
14741486
}

0 commit comments

Comments
 (0)