[RESTRICT AUTOMERGE]: [CTS] Fix oom catcher to remove false positives
Now with actual Android APIs.
Bug: 111403569
Test: run cts hostside tests and in the middle:
you: $ adb shell
device: $ oom(){ oom $1$1;};oom oom
device will reboot with message "lowmemorykiller detected; rebooting device."
Change-Id: I3a07415bea03422b8b205a08ed0c1e081182a589
diff --git a/apps/OomCatcher/Android.mk b/apps/OomCatcher/Android.mk
new file mode 100644
index 0000000..7f47e03
--- /dev/null
+++ b/apps/OomCatcher/Android.mk
@@ -0,0 +1,33 @@
+#
+# Copyright (C) 2018 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := OomCatcher
+
+LOCAL_SDK_VERSION := current
+
+LOCAL_COMPATIBILITY_SUITE := cts sts
+
+include $(BUILD_CTS_PACKAGE)
+
diff --git a/apps/OomCatcher/AndroidManifest.xml b/apps/OomCatcher/AndroidManifest.xml
new file mode 100644
index 0000000..25513e2
--- /dev/null
+++ b/apps/OomCatcher/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- Copyright (C) 2018 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.cts.oomcatcher"
+ android:versionCode="1"
+ android:versionName="1.0">
+
+ <application>
+ <activity android:name=".OomCatcher">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+</manifest>
diff --git a/apps/OomCatcher/src/com/android/cts/oomcatcher/OomCatcher.java b/apps/OomCatcher/src/com/android/cts/oomcatcher/OomCatcher.java
new file mode 100644
index 0000000..de4865e
--- /dev/null
+++ b/apps/OomCatcher/src/com/android/cts/oomcatcher/OomCatcher.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.oomcatcher;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.ComponentCallbacks2;
+import android.util.Log;
+
+public class OomCatcher extends Activity implements ComponentCallbacks2 {
+
+ private static final String LOG_TAG = "OomCatcher";
+
+ public void onTrimMemory(int level) {
+ switch (level) {
+ case TRIM_MEMORY_COMPLETE:
+ case TRIM_MEMORY_MODERATE:
+ case TRIM_MEMORY_BACKGROUND:
+ case TRIM_MEMORY_RUNNING_CRITICAL:
+ case TRIM_MEMORY_RUNNING_LOW:
+ case TRIM_MEMORY_RUNNING_MODERATE:
+ //fallthrough
+ onLowMemory();
+ break;
+ case TRIM_MEMORY_UI_HIDDEN:
+ default:
+ return;
+ }
+ }
+
+ public void onLowMemory() {
+ Log.i(LOG_TAG, "Low memory detected.");
+ }
+}
diff --git a/hostsidetests/security/AndroidTest.xml b/hostsidetests/security/AndroidTest.xml
index a5ee3c7..b140b69 100755
--- a/hostsidetests/security/AndroidTest.xml
+++ b/hostsidetests/security/AndroidTest.xml
@@ -157,6 +157,11 @@
<option name="append-bitness" value="true" />
</target_preparer>
+ <target_preparer class="com.android.compatibility.common.tradefed.targetprep.ApkInstaller">
+ <option name="cleanup-apks" value="true" />
+ <option name="test-file-name" value="OomCatcher.apk" />
+ </target_preparer>
+
<test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
<option name="jar" value="CtsSecurityHostTestCases.jar" />
<option name="runtime-hint" value="4m2s" />
diff --git a/hostsidetests/security/src/android/security/cts/SecurityTestCase.java b/hostsidetests/security/src/android/security/cts/SecurityTestCase.java
index 69566ea..3147857 100644
--- a/hostsidetests/security/src/android/security/cts/SecurityTestCase.java
+++ b/hostsidetests/security/src/android/security/cts/SecurityTestCase.java
@@ -36,6 +36,7 @@
import java.util.HashMap;
import com.android.ddmlib.MultiLineReceiver;
import com.android.ddmlib.Log;
+import com.android.ddmlib.TimeoutException;
public class SecurityTestCase extends DeviceTestCase {
@@ -45,8 +46,8 @@
private static final long LOW_MEMORY_DEVICE_THRESHOLD_KB = 1024 * 1024; // 1GB
private boolean isLowMemoryDevice = false;
- private static Map<ITestDevice, OomCatcher> oomCatchers = new HashMap<>();
- private static Map<ITestDevice, Long> totalMemories = new HashMap<>();
+ private static Map<String, OomCatcher> oomCatchers = new HashMap<>();
+ private static Map<String, Long> totalMemories = new HashMap<>();
private enum OomBehavior {
FAIL_AND_LOG, // normal behavior
PASS_AND_LOG, // skip tests that oom low memory devices
@@ -80,10 +81,10 @@
// Specifically time when app framework starts
// Singleton for caching device TotalMem to avoid and adb shell for every test.
- Long totalMemory = totalMemories.get(getDevice());
+ Long totalMemory = totalMemories.get(getDevice().getSerialNumber());
if (totalMemory == null) {
totalMemory = getMemTotal(getDevice());
- totalMemories.put(getDevice(), totalMemory);
+ totalMemories.put(getDevice().getSerialNumber(), totalMemory);
}
isLowMemoryDevice = totalMemory < LOW_MEMORY_DEVICE_THRESHOLD_KB;
@@ -102,11 +103,15 @@
}
// Singleton OOM detection in separate persistent threads for each device.
- OomCatcher oomCatcher = oomCatchers.get(getDevice());
+ OomCatcher oomCatcher = oomCatchers.get(getDevice().getSerialNumber());
if (oomCatcher == null || !oomCatcher.isAlive()) {
oomCatcher = new OomCatcher();
- oomCatchers.put(getDevice(), oomCatcher);
+ oomCatchers.put(getDevice().getSerialNumber(), oomCatcher);
oomCatcher.start();
+ synchronized(this) {
+ // Wait for the oom catcher thread to start listening.
+ this.wait(5000);
+ }
}
}
@@ -176,6 +181,8 @@
}
}
}
+
+ stopOomCatcher(getDevice().getSerialNumber());
}
public void assertMatches(String pattern, String input) throws Exception {
@@ -215,16 +222,33 @@
}
}
+ public static void stopOomCatcher(String serial) {
+ OomCatcher oomCatcher = oomCatchers.get(serial);
+ if (oomCatcher != null && oomCatcher.isAlive()) {
+ oomCatcher.interrupt();
+ }
+ }
+
class OomCatcher extends Thread {
+ OomCatcher() {
+ setDaemon(true);
+ }
+
@Override
public void run() {
MultiLineReceiver rcvr = new MultiLineReceiver() {
private boolean isCancelled = false;
public void processNewLines(String[] lines) {
+ synchronized(SecurityTestCase.this) {
+ // Let the main thread know that the listener has started so it can resume.
+ // Immediately after logcat is started there is data in the buffer so no
+ // waiting is expected.
+ SecurityTestCase.this.notify();
+ }
for (String line : lines) {
- if (Pattern.matches(".*lowmemorykiller.*", line)) {
+ if (Pattern.matches(".*Low memory detected.*", line)) {
// low memory detected, reboot device to clear memory and pass test
isCancelled = true;
Log.logAndDisplay(Log.LogLevel.INFO, LOG_TAG,
@@ -233,7 +257,8 @@
oomDetected = true;
}
try {
- getDevice().rebootUntilOnline();
+ AdbUtils.runCommandLine("reboot", getDevice());
+ getDevice().waitForDeviceOnline(60 * 2 * 1000); // 2 minutes
updateKernelStartTime();
} catch (Exception e) {
Log.e(LOG_TAG, e.toString());
@@ -249,8 +274,13 @@
};
try {
+ AdbUtils.runCommandLine("am force-stop com.android.cts.oomcatcher", getDevice());
AdbUtils.runCommandLine("logcat -c", getDevice());
- getDevice().executeShellCommand("logcat", rcvr);
+ AdbUtils.runCommandLine("am start com.android.cts.oomcatcher/.OomCatcher",
+ getDevice());
+ getDevice().executeShellCommand("logcat OomCatcher:V *:S", rcvr);
+ } catch (InterruptedException e) {
+ // thread stopped.
} catch (Exception e) {
Log.e(LOG_TAG, e.toString());
}