• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     String methodName = "$noinline$hotnessCount";
38     int initialValue = getHotnessCounter(Main.class, methodName);
39     File file = null;
40     try {
41       file = createTempFile();
42       String codePath = System.getenv("DEX_LOCATION") + "/2230-profile-save-hotness.jar";
43       VMRuntime.registerAppInfo(
44           "test.app",
45           file.getPath(),
46           file.getPath(),
47           new String[] {codePath},
48           VMRuntime.CODE_PATH_TYPE_PRIMARY_APK);
49 
50       // Test that the profile saves an app method with a profiling info.
51       $noinline$hotnessCountWithLoop(100000);
52       ensureProfileProcessing();
53       Method appMethod = Main.class.getDeclaredMethod(methodName);
54       if (!presentInProfile(file.getPath(), appMethod)) {
55         System.out.println("App method not hot in profile " +
56                 getHotnessCounter(Main.class, methodName));
57       }
58       // Hardcoded assumption that the hotness value is zero.
59       if (getHotnessCounter(Main.class, methodName) != 0) {
60         System.out.println("Hotness should be zero " +
61                 getHotnessCounter(Main.class, methodName));
62       }
63       VMRuntime.resetJitCounters();
64       if (getHotnessCounter(Main.class, methodName) != initialValue) {
65         System.out.println(
66             "Expected " + initialValue +", got " + + getHotnessCounter(Main.class, methodName));
67       }
68     } finally {
69       if (file != null) {
70         file.delete();
71       }
72     }
73   }
74 
75   // Checks if the profiles saver has the method as hot/warm.
presentInProfile(String profile, Method method)76   public static native boolean presentInProfile(String profile, Method method);
77   // Ensures the profile saver does its usual processing.
ensureProfileProcessing()78   public static native void ensureProfileProcessing();
isAotCompiled(Class<?> cls, String methodName)79   public static native boolean isAotCompiled(Class<?> cls, String methodName);
getHotnessCounter(Class<?> cls, String methodName)80   public static native int getHotnessCounter(Class<?> cls, String methodName);
81 
82   private static final String TEMP_FILE_NAME_PREFIX = "temp";
83   private static final String TEMP_FILE_NAME_SUFFIX = "-file";
84 
createTempFile()85   private static File createTempFile() throws Exception {
86     try {
87       return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
88     } catch (IOException e) {
89       System.setProperty("java.io.tmpdir", "/data/local/tmp");
90       try {
91         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
92       } catch (IOException e2) {
93         System.setProperty("java.io.tmpdir", "/sdcard");
94         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
95       }
96     }
97   }
98 }
99