1 /*
2  * Copyright (C) 2016 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 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 
20 public class Main {
main(String[] args)21   public static void main(String[] args) throws Exception {
22     System.loadLibrary(args[0]);
23     Class<?> c = Class.forName("ErrClass");
24     Method m = c.getMethod("errMethod");
25 
26     // Print the counter before invokes. The golden file expects this to be 0.
27     int hotnessCounter = getHotnessCounter(c, "errMethod");
28     if (hotnessCounter != 0) {
29       throw new RuntimeException("Unexpected hotnessCounter: " + hotnessCounter);
30     }
31 
32     // Loop enough to make sure the interpreter reports invocations count.
33     long result = 0;
34     for (int i = 0; i < 10000; i++) {
35       try {
36         result += (Long)m.invoke(null);
37         hotnessCounter = getHotnessCounter(c, "errMethod");
38         if (hotnessCounter != 0) {
39           throw new RuntimeException(
40             "Unexpected hotnessCounter: " + hotnessCounter);
41         }
42 
43       } catch (InvocationTargetException e) {
44         if (!(e.getCause() instanceof NullPointerException)) {
45           throw e;
46         }
47       }
48     }
49 
50     // Not compilable methods should not increase their hotness counter.
51     if (hotnessCounter != 0) {
52       throw new RuntimeException("Unexpected hotnessCounter: " + hotnessCounter);
53     }
54   }
55 
getHotnessCounter(Class cls, String method_name)56   public static native int getHotnessCounter(Class cls, String method_name);
57 }
58