summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Jiakai Zhang <jiakaiz@google.com> 2024-01-15 11:19:45 +0000
committer Jiakai Zhang <jiakaiz@google.com> 2024-01-16 11:20:15 +0000
commitb9f45b0930a486b2a09220d7e269aba9758f3eaa (patch)
treefbb4e0dbf511c2222594fe9c238235587b33a8eb
parent466ed0f1e775edd8120fdea698d9a00603470a01 (diff)
Retry the test if GC still happens in 2271-profile-inline-cache.
Bug: 317505124 Test: - 1. Limit the test on a single little core. 2. art/test/testrunner/testrunner.py --target --64 test-art-target-run-test-ndebug-prebuild-jit-no-relocate-ntrace-gcstress-checkjni-picimage-ndebuggable-no-jvmti-cdex-fast-2271-profile-inline-cache64 Change-Id: I2f002f14a9d8ae2b2eb7dcd9746f13b55970b6fa
-rw-r--r--test/2271-profile-inline-cache/src/Main.java155
-rw-r--r--test/708-jit-cache-churn/jit.cc56
-rw-r--r--test/708-jit-cache-churn/src/JitCacheChurnTest.java4
-rw-r--r--test/708-jit-cache-churn/src/Main.java3
-rw-r--r--test/Android.bp1
-rw-r--r--test/common/runtime_state.cc32
6 files changed, 138 insertions, 113 deletions
diff --git a/test/2271-profile-inline-cache/src/Main.java b/test/2271-profile-inline-cache/src/Main.java
index 81ba535154..a95d4acd91 100644
--- a/test/2271-profile-inline-cache/src/Main.java
+++ b/test/2271-profile-inline-cache/src/Main.java
@@ -21,6 +21,12 @@ import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
+ private static File sFile = null;
+ private static Method sMethod1 = null;
+ private static Method sMethod2 = null;
+ private static Method sMethod3 = null;
+ private static Method sMethod4 = null;
+
public static void main(String[] args) throws Exception {
System.loadLibrary(args[0]);
@@ -29,69 +35,84 @@ public class Main {
return;
}
- File file = null;
- try {
- file = createTempFile();
- String codePath = System.getenv("DEX_LOCATION") + "/2271-profile-inline-cache.jar";
- VMRuntime.registerAppInfo("test.app", file.getPath(), file.getPath(),
- new String[] {codePath}, VMRuntime.CODE_PATH_TYPE_PRIMARY_APK);
-
- Derived1 derived1 = new Derived1();
- Derived2 derived2 = new Derived2();
-
- // This method is below the inline cache threshold.
- Method method1 = Main.class.getDeclaredMethod("$noinline$method1", Base.class);
- ensureJitBaselineCompiled(method1);
- // The baseline code doesn't update the inline cache if we are marking. Force a GC now
- // so that GC will unlikely take place during the method calls below.
- Runtime.getRuntime().gc();
+ sMethod1 = Main.class.getDeclaredMethod("$noinline$method1", Base.class);
+ sMethod2 = Main.class.getDeclaredMethod("$noinline$method2", Base.class);
+ sMethod3 = Main.class.getDeclaredMethod("$noinline$method3", Base.class);
+ sMethod4 = Main.class.getDeclaredMethod("$noinline$method4", Base.class);
+
+ sFile = createTempFile();
+ sFile.deleteOnExit();
+ String codePath = System.getenv("DEX_LOCATION") + "/2271-profile-inline-cache.jar";
+ VMRuntime.registerAppInfo("test.app", sFile.getPath(), sFile.getPath(),
+ new String[] {codePath}, VMRuntime.CODE_PATH_TYPE_PRIMARY_APK);
+
+ for (int i = 0; i < 10; i++) {
+ try {
+ test();
+ return;
+ } catch (ScopedAssertNoGc.NoGcAssertionFailure e) {
+ // This should rarely happen. When it happens, reset the state, delete the profile,
+ // and try again.
+ reset();
+ sFile.delete();
+ }
+ }
+
+ // The possibility of hitting this line only exists in theory, unless the test is wrong.
+ throw new RuntimeException("NoGcAssertionFailure occurred 10 times");
+ }
+
+ private static void test() throws Exception {
+ Derived1 derived1 = new Derived1();
+ Derived2 derived2 = new Derived2();
+
+ // This method is below the inline cache threshold.
+ ensureJitBaselineCompiled(sMethod1);
+ try (ScopedAssertNoGc noGc = new ScopedAssertNoGc()) {
$noinline$method1(derived1);
for (int i = 0; i < 2998; i++) {
$noinline$method1(derived2);
}
- checkMethodHasNoInlineCache(file, method1);
-
- // This method is right on the inline cache threshold.
- Method method2 = Main.class.getDeclaredMethod("$noinline$method2", Base.class);
- ensureJitBaselineCompiled(method2);
- // The baseline code doesn't update the inline cache if we are marking. Force a GC now
- // so that GC will unlikely take place during the method calls below.
- Runtime.getRuntime().gc();
+ }
+ checkMethodHasNoInlineCache(sFile, sMethod1);
+
+ // This method is right on the inline cache threshold.
+ ensureJitBaselineCompiled(sMethod2);
+ try (ScopedAssertNoGc noGc = new ScopedAssertNoGc()) {
$noinline$method2(derived1);
for (int i = 0; i < 2999; i++) {
$noinline$method2(derived2);
}
- checkMethodHasInlineCache(file, method2, Derived1.class, Derived2.class);
-
- // This method is above the inline cache threshold.
- Method method3 = Main.class.getDeclaredMethod("$noinline$method3", Base.class);
- ensureJitBaselineCompiled(method3);
- // The baseline code doesn't update the inline cache if we are marking. Force a GC now
- // so that GC will unlikely take place during the method calls below.
- Runtime.getRuntime().gc();
+ }
+ checkMethodHasInlineCache(sFile, sMethod2, Derived1.class, Derived2.class);
+
+ // This method is above the inline cache threshold.
+ ensureJitBaselineCompiled(sMethod3);
+ try (ScopedAssertNoGc noGc = new ScopedAssertNoGc()) {
for (int i = 0; i < 10000; i++) {
$noinline$method3(derived1);
}
for (int i = 0; i < 10000; i++) {
$noinline$method3(derived2);
}
- checkMethodHasInlineCache(file, method3, Derived1.class, Derived2.class);
-
- // This method is above the JIT threshold.
- Method method4 = Main.class.getDeclaredMethod("$noinline$method4", Base.class);
- ensureJitBaselineCompiled(method4);
- // The baseline code doesn't update the inline cache if we are marking. Force a GC now
- // so that GC will unlikely take place during the method calls below.
- Runtime.getRuntime().gc();
+ }
+ checkMethodHasInlineCache(sFile, sMethod3, Derived1.class, Derived2.class);
+
+ // This method is above the JIT threshold.
+ ensureJitBaselineCompiled(sMethod4);
+ try (ScopedAssertNoGc noGc = new ScopedAssertNoGc()) {
$noinline$method4(derived1);
$noinline$method4(derived2);
- ensureMethodJitCompiled(method4);
- checkMethodHasInlineCache(file, method4, Derived1.class, Derived2.class);
- } finally {
- if (file != null) {
- file.delete();
- }
}
+ ensureMethodJitCompiled(sMethod4);
+ checkMethodHasInlineCache(sFile, sMethod4, Derived1.class, Derived2.class);
+ }
+
+ private static void reset() {
+ removeJitCompiledMethod(sMethod1, false /* releaseMemory */);
+ removeJitCompiledMethod(sMethod2, false /* releaseMemory */);
+ removeJitCompiledMethod(sMethod3, false /* releaseMemory */);
+ removeJitCompiledMethod(sMethod4, false /* releaseMemory */);
}
public static void $noinline$method1(Base obj) {
@@ -152,6 +173,8 @@ public class Main {
public static native boolean hasInlineCacheInProfile(
String profile, Method method, Class<?>... targetTypes);
public static native boolean hasJit();
+ public static native int getCurrentGcNum();
+ public static native boolean removeJitCompiledMethod(Method method, boolean releaseMemory);
private static final String TEMP_FILE_NAME_PREFIX = "temp";
private static final String TEMP_FILE_NAME_SUFFIX = "-file";
@@ -170,13 +193,6 @@ public class Main {
}
}
- private static void sleep(long millis) {
- try {
- Thread.sleep(millis);
- } catch (InterruptedException ignored) {
- }
- }
-
private static class VMRuntime {
public static final int CODE_PATH_TYPE_PRIMARY_APK = 1 << 0;
private static final Method registerAppInfoMethod;
@@ -197,4 +213,37 @@ public class Main {
null, packageName, curProfile, refProfile, codePaths, codePathsType);
}
}
+
+ // This scope is intended to guard code that doesn't expect GC to take place. Because we can't
+ // really prevent GC in Java code (calling a native method that enters a GCCriticalSection will
+ // cause the runtime to hang forever when transitioning from native back to Java), this is a
+ // workaround that forces a GC at the beginning so that GC will unlikely take place within the
+ // scope. If a GC still takes place within the scope, this will throw NoGcAssertionFailure.
+ //
+ // The baseline code doesn't update the inline cache if we are marking, so we use this scope to
+ // guard calls to virtual methods for which we want inline cache to be updated.
+ private static class ScopedAssertNoGc implements AutoCloseable {
+ private final int mLastGcNum;
+
+ public ScopedAssertNoGc() {
+ System.gc();
+ mLastGcNum = getCurrentGcNum();
+ }
+
+ @Override
+ public void close() throws NoGcAssertionFailure {
+ int currentGcNum = getCurrentGcNum();
+ if (currentGcNum != mLastGcNum) {
+ throw new NoGcAssertionFailure(
+ String.format("GC happened within the scope (before: %d, after: %d)",
+ mLastGcNum, currentGcNum));
+ }
+ }
+
+ public static class NoGcAssertionFailure extends Exception {
+ public NoGcAssertionFailure(String message) {
+ super(message);
+ }
+ }
+ }
}
diff --git a/test/708-jit-cache-churn/jit.cc b/test/708-jit-cache-churn/jit.cc
deleted file mode 100644
index 1b80eb3c0c..0000000000
--- a/test/708-jit-cache-churn/jit.cc
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-#include "jni.h"
-
-#include "art_method.h"
-#include "jit/jit.h"
-#include "jit/jit_code_cache.h"
-#include "jni/jni_internal.h"
-#include "mirror/class.h"
-#include "runtime.h"
-#include "scoped_thread_state_change-inl.h"
-#include "thread_list.h"
-
-namespace art {
-
-extern "C" JNIEXPORT
-jboolean
-Java_JitCacheChurnTest_removeJitCompiledMethod(JNIEnv* env,
- jclass,
- jobject javaMethod,
- jboolean releaseMemory) {
- if (!Runtime::Current()->UseJitCompilation()) {
- return JNI_FALSE;
- }
-
- jit::Jit* jit = Runtime::Current()->GetJit();
- jit->WaitForCompilationToFinish(Thread::Current());
-
- ScopedObjectAccess soa(env);
- ArtMethod* method = ArtMethod::FromReflectedMethod(soa, javaMethod);
-
- jit::JitCodeCache* code_cache = jit->GetCodeCache();
-
- // Drop the shared mutator lock
- ScopedThreadSuspension selfSuspension(Thread::Current(), art::ThreadState::kNative);
- // Get exclusive mutator lock with suspend all.
- ScopedSuspendAll suspend("Removing JIT compiled method", /*long_suspend*/true);
- bool removed = code_cache->RemoveMethod(method, static_cast<bool>(releaseMemory));
- return removed ? JNI_TRUE : JNI_FALSE;
-}
-
-} // namespace art
diff --git a/test/708-jit-cache-churn/src/JitCacheChurnTest.java b/test/708-jit-cache-churn/src/JitCacheChurnTest.java
index abc5f35f70..8902d8db0f 100644
--- a/test/708-jit-cache-churn/src/JitCacheChurnTest.java
+++ b/test/708-jit-cache-churn/src/JitCacheChurnTest.java
@@ -244,7 +244,7 @@ public class JitCacheChurnTest {
System.err.println(e);
System.exit(-1);
}
- removeJitCompiledMethod(method, false);
+ Main.removeJitCompiledMethod(method, false);
}
private void removeJittedMethods(int mask) {
@@ -274,6 +274,4 @@ public class JitCacheChurnTest {
concurrentExecution.shutdown();
}
}
-
- private static native void removeJitCompiledMethod(Method method, boolean releaseMemory);
}
diff --git a/test/708-jit-cache-churn/src/Main.java b/test/708-jit-cache-churn/src/Main.java
index 0595aae506..4d11dae899 100644
--- a/test/708-jit-cache-churn/src/Main.java
+++ b/test/708-jit-cache-churn/src/Main.java
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+import java.lang.reflect.Method;
+
public class Main {
public static void main(String[] args) throws Exception {
@@ -28,4 +30,5 @@ public class Main {
static native boolean hasJit();
static native void ensureJitCompiled(Class<?> klass, String methodName);
+ static native void removeJitCompiledMethod(Method method, boolean releaseMemory);
}
diff --git a/test/Android.bp b/test/Android.bp
index fef4d99c2b..00c5f6be89 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -905,7 +905,6 @@ cc_defaults {
"667-jit-jni-stub/jit_jni_stub_test.cc",
"674-hiddenapi/hiddenapi.cc",
"692-vdex-inmem-loader/vdex_inmem_loader.cc",
- "708-jit-cache-churn/jit.cc",
"720-thread-priority/thread_priority.cc",
"800-smali/jni.cc",
"817-hiddenapi/test_native.cc",
diff --git a/test/common/runtime_state.cc b/test/common/runtime_state.cc
index 27f1ebad12..8e0507327e 100644
--- a/test/common/runtime_state.cc
+++ b/test/common/runtime_state.cc
@@ -566,4 +566,36 @@ extern "C" JNIEXPORT jboolean JNICALL Java_Main_hasInlineCacheInProfile(
return JNI_FALSE;
}
+extern "C" JNIEXPORT jint JNICALL Java_Main_getCurrentGcNum(JNIEnv* env, jclass) {
+ // Prevent any new GC before getting the current GC num.
+ ScopedObjectAccess soa(env);
+ gc::Heap* heap = Runtime::Current()->GetHeap();
+ heap->WaitForGcToComplete(gc::kGcCauseJitCodeCache, Thread::Current());
+ return heap->GetCurrentGcNum();
+}
+
+extern "C" JNIEXPORT jboolean Java_Main_removeJitCompiledMethod(JNIEnv* env,
+ jclass,
+ jobject java_method,
+ jboolean release_memory) {
+ if (!Runtime::Current()->UseJitCompilation()) {
+ return JNI_FALSE;
+ }
+
+ jit::Jit* jit = Runtime::Current()->GetJit();
+ jit->WaitForCompilationToFinish(Thread::Current());
+
+ ScopedObjectAccess soa(env);
+ ArtMethod* method = ArtMethod::FromReflectedMethod(soa, java_method);
+
+ jit::JitCodeCache* code_cache = jit->GetCodeCache();
+
+ // Drop the shared mutator lock.
+ ScopedThreadSuspension self_suspension(Thread::Current(), art::ThreadState::kNative);
+ // Get exclusive mutator lock with suspend all.
+ ScopedSuspendAll suspend("Removing JIT compiled method", /*long_suspend=*/true);
+ bool removed = code_cache->RemoveMethod(method, static_cast<bool>(release_memory));
+ return removed ? JNI_TRUE : JNI_FALSE;
+}
+
} // namespace art