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(file.getPath(), new String[] {codePath}); 42 43 // Test that the profile saves an app method with a profiling info. 44 $noinline$hotnessCountWithLoop(10000); 45 ensureProfileProcessing(); 46 String methodName = "$noinline$hotnessCount"; 47 Method appMethod = Main.class.getDeclaredMethod(methodName); 48 if (!presentInProfile(file.getPath(), appMethod)) { 49 System.out.println("App method not hot in profile " + 50 getHotnessCounter(Main.class, methodName)); 51 } 52 if (getHotnessCounter(Main.class, methodName) == 0) { 53 System.out.println("Hotness should be non zero " + 54 getHotnessCounter(Main.class, methodName)); 55 } 56 VMRuntime.resetJitCounters(); 57 if (getHotnessCounter(Main.class, methodName) != 0) { 58 System.out.println("Hotness should be zero " + getHotnessCounter(Main.class, methodName)); 59 } 60 } finally { 61 if (file != null) { 62 file.delete(); 63 } 64 } 65 } 66 67 // Checks if the profiles saver has the method as hot/warm. presentInProfile(String profile, Method method)68 public static native boolean presentInProfile(String profile, Method method); 69 // Ensures the profile saver does its usual processing. ensureProfileProcessing()70 public static native void ensureProfileProcessing(); isAotCompiled(Class<?> cls, String methodName)71 public static native boolean isAotCompiled(Class<?> cls, String methodName); getHotnessCounter(Class<?> cls, String methodName)72 public static native int getHotnessCounter(Class<?> cls, String methodName); 73 74 private static final String TEMP_FILE_NAME_PREFIX = "dummy"; 75 private static final String TEMP_FILE_NAME_SUFFIX = "-file"; 76 createTempFile()77 private static File createTempFile() throws Exception { 78 try { 79 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 80 } catch (IOException e) { 81 System.setProperty("java.io.tmpdir", "/data/local/tmp"); 82 try { 83 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 84 } catch (IOException e2) { 85 System.setProperty("java.io.tmpdir", "/sdcard"); 86 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 87 } 88 } 89 } 90 } 91