1 /*
2  * Copyright (C) 2019 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 dalvik.system.VMRuntime;
18 import java.io.File;
19 import java.io.IOException;
20 import java.lang.reflect.Method;
21 
22 public class Main {
$noinline$hotnessCount()23   public static void $noinline$hotnessCount() {}
24 
$noinline$hotnessCountWithLoop(int count)25   public static void $noinline$hotnessCountWithLoop(int count) {
26     for (int i = 0; i < count; i++) {
27       $noinline$hotnessCount();
28     }
29   }
30 
main(String[] args)31   public static void main(String[] args) throws Exception {
32     System.loadLibrary(args[0]);
33     if (!isAotCompiled(Main.class, "main")) {
34       return;
35     }
36 
37     File file = null;
38     try {
39       file = createTempFile();
40       String codePath = System.getenv("DEX_LOCATION") + "/2230-profile-save-hotness.jar";
41       VMRuntime.registerAppInfo(
42           "test.app",
43           file.getPath(),
44           file.getPath(),
45           new String[] {codePath},
46           VMRuntime.CODE_PATH_TYPE_PRIMARY_APK);
47 
48       // Test that the profile saves an app method with a profiling info.
49       $noinline$hotnessCountWithLoop(10000);
50       ensureProfileProcessing();
51       String methodName = "$noinline$hotnessCount";
52       Method appMethod = Main.class.getDeclaredMethod(methodName);
53       if (!presentInProfile(file.getPath(), appMethod)) {
54         System.out.println("App method not hot in profile " +
55                 getHotnessCounter(Main.class, methodName));
56       }
57       if (getHotnessCounter(Main.class, methodName) == 0) {
58         System.out.println("Hotness should be non zero " +
59                 getHotnessCounter(Main.class, methodName));
60       }
61       VMRuntime.resetJitCounters();
62       if (getHotnessCounter(Main.class, methodName) != 0) {
63         System.out.println("Hotness should be zero " + getHotnessCounter(Main.class, methodName));
64       }
65     } finally {
66       if (file != null) {
67         file.delete();
68       }
69     }
70   }
71 
72   // Checks if the profiles saver has the method as hot/warm.
presentInProfile(String profile, Method method)73   public static native boolean presentInProfile(String profile, Method method);
74   // Ensures the profile saver does its usual processing.
ensureProfileProcessing()75   public static native void ensureProfileProcessing();
isAotCompiled(Class<?> cls, String methodName)76   public static native boolean isAotCompiled(Class<?> cls, String methodName);
getHotnessCounter(Class<?> cls, String methodName)77   public static native int getHotnessCounter(Class<?> cls, String methodName);
78 
79   private static final String TEMP_FILE_NAME_PREFIX = "temp";
80   private static final String TEMP_FILE_NAME_SUFFIX = "-file";
81 
createTempFile()82   private static File createTempFile() throws Exception {
83     try {
84       return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
85     } catch (IOException e) {
86       System.setProperty("java.io.tmpdir", "/data/local/tmp");
87       try {
88         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
89       } catch (IOException e2) {
90         System.setProperty("java.io.tmpdir", "/sdcard");
91         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
92       }
93     }
94   }
95 }
96