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 {
main(String[] args)18   public static void main(String[] args) throws Exception {
19     System.loadLibrary(args[0]);
20     if (!hasJit()) {
21       return;
22     }
23     // Force initialization.
24     Foo.initialize();
25   }
26 
hasJitCompiledEntrypoint(Class<?> cls, String methodName)27   public native static boolean hasJitCompiledEntrypoint(Class<?> cls, String methodName);
hasJit()28   private native static boolean hasJit();
29 }
30 
31 class Foo {
32   // This method needs to be virtual for the test. Otherwise if it's a static method,
33   // the JIT compiler won't compile while its entrypoint is the resolution stub.
$noinline$hotMethod()34   void $noinline$hotMethod() {
35     for (int i = 0; i < array.length; ++i) {
36       array[i] = array;
37     }
38   }
39 
40   static {
41     array = new Object[10000];
42     while (!Main.hasJitCompiledEntrypoint(Foo.class, "$noinline$hotMethod")) {
43       new Foo().$noinline$hotMethod();
44       try {
45         // Sleep to give a chance for the JIT to compile `hotMethod`.
46         Thread.sleep(100);
47       } catch (Exception e) {
48         // Ignore
49       }
50     }
51   }
52 
initialize()53   static void initialize() {
54   }
55 
56   static Object[] array;
57 }
58