Merge changes from topic "lmkd_ams" am: 360eb85898

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1702074

Change-Id: I83c23ee6717baf465048891f086cfc9f5c516634
This commit is contained in:
Vova Sharaienko
2021-05-11 17:52:28 +00:00
committed by Automerger Merge Worker
3 changed files with 64 additions and 51 deletions

View File

@@ -31,6 +31,8 @@ import com.android.internal.annotations.GuardedBy;
import libcore.io.IoUtils; import libcore.io.IoUtils;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -74,7 +76,7 @@ public class LmkdConnection {
* @param receivedLen Size of the data received * @param receivedLen Size of the data received
* @return True if the message has been handled correctly, false otherwise. * @return True if the message has been handled correctly, false otherwise.
*/ */
boolean handleUnsolicitedMessage(ByteBuffer dataReceived, int receivedLen); boolean handleUnsolicitedMessage(DataInputStream inputData, int receivedLen);
} }
private final MessageQueue mMsgQueue; private final MessageQueue mMsgQueue;
@@ -99,6 +101,10 @@ public class LmkdConnection {
private final ByteBuffer mInputBuf = private final ByteBuffer mInputBuf =
ByteBuffer.allocate(LMKD_REPLY_MAX_SIZE); ByteBuffer.allocate(LMKD_REPLY_MAX_SIZE);
// Input stream to parse the incoming data
private final DataInputStream mInputData = new DataInputStream(
new ByteArrayInputStream(mInputBuf.array()));
// object to protect mReplyBuf and to wait/notify when reply is received // object to protect mReplyBuf and to wait/notify when reply is received
private final Object mReplyBufLock = new Object(); private final Object mReplyBufLock = new Object();
@@ -190,26 +196,32 @@ public class LmkdConnection {
private void processIncomingData() { private void processIncomingData() {
int len = read(mInputBuf); int len = read(mInputBuf);
if (len > 0) { if (len > 0) {
synchronized (mReplyBufLock) { try {
if (mReplyBuf != null) { // reset InputStream to point into mInputBuf.array() begin
if (mListener.isReplyExpected(mReplyBuf, mInputBuf, len)) { mInputData.reset();
// copy into reply buffer synchronized (mReplyBufLock) {
mReplyBuf.put(mInputBuf.array(), 0, len); if (mReplyBuf != null) {
mReplyBuf.rewind(); if (mListener.isReplyExpected(mReplyBuf, mInputBuf, len)) {
// wakeup the waiting thread // copy into reply buffer
mReplyBufLock.notifyAll(); mReplyBuf.put(mInputBuf.array(), 0, len);
} else if (!mListener.handleUnsolicitedMessage(mInputBuf, len)) { mReplyBuf.rewind();
// received unexpected packet // wakeup the waiting thread
// treat this as an error mReplyBufLock.notifyAll();
mReplyBuf = null; } else if (!mListener.handleUnsolicitedMessage(mInputData, len)) {
mReplyBufLock.notifyAll(); // received unexpected packet
Slog.e(TAG, "Received an unexpected packet from lmkd"); // treat this as an error
mReplyBuf = null;
mReplyBufLock.notifyAll();
Slog.e(TAG, "Received an unexpected packet from lmkd");
}
} else if (!mListener.handleUnsolicitedMessage(mInputData, len)) {
// received asynchronous communication from lmkd
// but we don't recognize it.
Slog.w(TAG, "Received an unexpected packet from lmkd");
} }
} else if (!mListener.handleUnsolicitedMessage(mInputBuf, len)) {
// received asynchronous communication from lmkd
// but we don't recognize it.
Slog.w(TAG, "Received an unexpected packet from lmkd");
} }
} catch (IOException e) {
Slog.e(TAG, "Failed to parse lmkd data buffer. Size = " + len);
} }
} }
} }

View File

@@ -23,10 +23,8 @@ import android.util.Slog;
import com.android.internal.util.FrameworkStatsLog; import com.android.internal.util.FrameworkStatsLog;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
/** /**
* Activity manager communication with lmkd data handling and statsd atom logging * Activity manager communication with lmkd data handling and statsd atom logging
@@ -51,13 +49,8 @@ public final class LmkdStatsReporter {
* Logs the event when LMKD kills a process to reduce memory pressure. * Logs the event when LMKD kills a process to reduce memory pressure.
* Code: LMK_KILL_OCCURRED = 51 * Code: LMK_KILL_OCCURRED = 51
*/ */
public static void logKillOccurred(ByteBuffer dataReceived) { public static void logKillOccurred(DataInputStream inputData) {
DataInputStream inputData = new DataInputStream(
new ByteArrayInputStream(dataReceived.array()));
try { try {
//read first int which denotes the message type
final int msgType = inputData.readInt();
final long pgFault = inputData.readLong(); final long pgFault = inputData.readLong();
final long pgMajFault = inputData.readLong(); final long pgMajFault = inputData.readLong();
final long rssInBytes = inputData.readLong(); final long rssInBytes = inputData.readLong();

View File

@@ -125,6 +125,7 @@ import com.android.server.wm.WindowManagerService;
import dalvik.system.VMRuntime; import dalvik.system.VMRuntime;
import java.io.DataInputStream;
import java.io.File; import java.io.File;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
@@ -783,37 +784,44 @@ public final class ProcessList {
} }
@Override @Override
public boolean handleUnsolicitedMessage(ByteBuffer dataReceived, public boolean handleUnsolicitedMessage(DataInputStream inputData,
int receivedLen) { int receivedLen) {
if (receivedLen < 4) { if (receivedLen < 4) {
return false; return false;
} }
switch (dataReceived.getInt(0)) { try {
case LMK_PROCKILL: switch (inputData.readInt()) {
if (receivedLen != 12) { case LMK_PROCKILL:
if (receivedLen != 12) {
return false;
}
final int pid = inputData.readInt();
final int uid = inputData.readInt();
mAppExitInfoTracker.scheduleNoteLmkdProcKilled(pid, uid);
return true;
case LMK_KILL_OCCURRED:
if (receivedLen
< LmkdStatsReporter.KILL_OCCURRED_MSG_SIZE) {
return false;
}
LmkdStatsReporter.logKillOccurred(inputData);
return true;
case LMK_STATE_CHANGED:
if (receivedLen
!= LmkdStatsReporter.STATE_CHANGED_MSG_SIZE) {
return false;
}
final int state = inputData.readInt();
LmkdStatsReporter.logStateChanged(state);
return true;
default:
return false; return false;
} }
mAppExitInfoTracker.scheduleNoteLmkdProcKilled( } catch (IOException e) {
dataReceived.getInt(4), dataReceived.getInt(8)); Slog.e(TAG, "Invalid buffer data. Failed to log LMK_KILL_OCCURRED");
return true;
case LMK_KILL_OCCURRED:
if (receivedLen < LmkdStatsReporter.KILL_OCCURRED_MSG_SIZE) {
return false;
}
dataReceived.position(4);
LmkdStatsReporter.logKillOccurred(dataReceived);
return true;
case LMK_STATE_CHANGED:
if (receivedLen != LmkdStatsReporter.STATE_CHANGED_MSG_SIZE) {
return false;
}
LmkdStatsReporter.logStateChanged(
dataReceived.getInt(4));
return true;
default:
return false;
} }
return false;
} }
} }
); );