| Vladimir Marko | da28305 | 2017-11-07 21:17:24 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | public class Main { |
| 18 | public static void main(String[] args) { |
| 19 | System.loadLibrary(args[0]); |
| 20 | if (!hasJit()) { |
| 21 | // Make the test pass if not using JIT. |
| 22 | return; |
| 23 | } |
| 24 | if (hasImage()) { |
| 25 | throw new Error("The `run` script should prevent this test from running with an image!"); |
| 26 | } |
| 27 | if (!isClassMoveable(String.class)) { |
| 28 | throw new Error("String.class not moveable despite running without image!"); |
| 29 | } |
| 30 | |
| 31 | // Make sure the Main.test() is JIT-compiled and then call it. |
| 32 | ensureJitCompiled(Main.class, "test"); |
| 33 | test(); |
| 34 | } |
| 35 | |
| 36 | public static void test() { |
| 37 | int length = 5; |
| 38 | |
| 39 | // Hide the type of these strings in an Object array, |
| 40 | // so that we treat them as Object for the String.equals() below. |
| 41 | Object[] array = new Object[length]; |
| 42 | for (int i = 0; i != length; ++i) { |
| 43 | array[i] = "V" + i; |
| 44 | } |
| 45 | |
| 46 | for (int count = 0; count != 128 * 1024; ++count) { |
| 47 | for (int i = 0; i != length; ++i) { |
| 48 | allocateAtLeast1KiB(); |
| 49 | assertTrue(("V" + i).equals(array[i])); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public static void allocateAtLeast1KiB() { |
| 55 | // Give GC more work by allocating Object arrays. |
| 56 | memory[allocationIndex] = new Object[1024 / 4]; |
| 57 | ++allocationIndex; |
| 58 | if (allocationIndex == memory.length) { |
| 59 | allocationIndex = 0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static void assertTrue(boolean value) { |
| 64 | if (!value) { |
| 65 | throw new Error("Assertion failed!"); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private native static boolean hasJit(); |
| 70 | private native static boolean hasImage(); |
| 71 | private native static boolean isClassMoveable(Class<?> cls); |
| 72 | private static native void ensureJitCompiled(Class<?> itf, String method_name); |
| 73 | |
| 74 | // We shall retain some allocated memory and release old allocations |
| 75 | // so that the GC has something to do. |
| 76 | public static Object[] memory = new Object[4096]; |
| 77 | public static int allocationIndex = 0; |
| 78 | } |