1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2019 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import dalvik.system.VMRuntime; 18*795d594fSAndroid Build Coastguard Worker import java.io.File; 19*795d594fSAndroid Build Coastguard Worker import java.io.IOException; 20*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method; 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker public class Main { $noinline$hotnessCount()23*795d594fSAndroid Build Coastguard Worker public static void $noinline$hotnessCount() {} 24*795d594fSAndroid Build Coastguard Worker $noinline$hotnessCountWithLoop(int count)25*795d594fSAndroid Build Coastguard Worker public static void $noinline$hotnessCountWithLoop(int count) { 26*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < count; i++) { 27*795d594fSAndroid Build Coastguard Worker $noinline$hotnessCount(); 28*795d594fSAndroid Build Coastguard Worker } 29*795d594fSAndroid Build Coastguard Worker } 30*795d594fSAndroid Build Coastguard Worker main(String[] args)31*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) throws Exception { 32*795d594fSAndroid Build Coastguard Worker System.loadLibrary(args[0]); 33*795d594fSAndroid Build Coastguard Worker if (!isAotCompiled(Main.class, "main")) { 34*795d594fSAndroid Build Coastguard Worker return; 35*795d594fSAndroid Build Coastguard Worker } 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker String methodName = "$noinline$hotnessCount"; 38*795d594fSAndroid Build Coastguard Worker int initialValue = getHotnessCounter(Main.class, methodName); 39*795d594fSAndroid Build Coastguard Worker File file = null; 40*795d594fSAndroid Build Coastguard Worker try { 41*795d594fSAndroid Build Coastguard Worker file = createTempFile(); 42*795d594fSAndroid Build Coastguard Worker String codePath = System.getenv("DEX_LOCATION") + "/2230-profile-save-hotness.jar"; 43*795d594fSAndroid Build Coastguard Worker VMRuntime.registerAppInfo( 44*795d594fSAndroid Build Coastguard Worker "test.app", 45*795d594fSAndroid Build Coastguard Worker file.getPath(), 46*795d594fSAndroid Build Coastguard Worker file.getPath(), 47*795d594fSAndroid Build Coastguard Worker new String[] {codePath}, 48*795d594fSAndroid Build Coastguard Worker VMRuntime.CODE_PATH_TYPE_PRIMARY_APK); 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker // Test that the profile saves an app method that gets JITted. 51*795d594fSAndroid Build Coastguard Worker $noinline$hotnessCountWithLoop(100000); 52*795d594fSAndroid Build Coastguard Worker ensureProfileProcessing(); 53*795d594fSAndroid Build Coastguard Worker Method appMethod = Main.class.getDeclaredMethod(methodName); 54*795d594fSAndroid Build Coastguard Worker if (!presentInProfile(file.getPath(), appMethod)) { 55*795d594fSAndroid Build Coastguard Worker System.out.println("App method not hot in profile " + 56*795d594fSAndroid Build Coastguard Worker getHotnessCounter(Main.class, methodName)); 57*795d594fSAndroid Build Coastguard Worker } 58*795d594fSAndroid Build Coastguard Worker // Hardcoded assumption that the hotness value is zero. 59*795d594fSAndroid Build Coastguard Worker if (getHotnessCounter(Main.class, methodName) != 0) { 60*795d594fSAndroid Build Coastguard Worker System.out.println("Hotness should be zero " + 61*795d594fSAndroid Build Coastguard Worker getHotnessCounter(Main.class, methodName)); 62*795d594fSAndroid Build Coastguard Worker } 63*795d594fSAndroid Build Coastguard Worker VMRuntime.resetJitCounters(); 64*795d594fSAndroid Build Coastguard Worker if (getHotnessCounter(Main.class, methodName) != initialValue) { 65*795d594fSAndroid Build Coastguard Worker System.out.println( 66*795d594fSAndroid Build Coastguard Worker "Expected " + initialValue +", got " + + getHotnessCounter(Main.class, methodName)); 67*795d594fSAndroid Build Coastguard Worker } 68*795d594fSAndroid Build Coastguard Worker } finally { 69*795d594fSAndroid Build Coastguard Worker if (file != null) { 70*795d594fSAndroid Build Coastguard Worker file.delete(); 71*795d594fSAndroid Build Coastguard Worker } 72*795d594fSAndroid Build Coastguard Worker } 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker 75*795d594fSAndroid Build Coastguard Worker // Checks if the profile saver has the method as hot/warm. presentInProfile(String profile, Method method)76*795d594fSAndroid Build Coastguard Worker public static native boolean presentInProfile(String profile, Method method); 77*795d594fSAndroid Build Coastguard Worker // Ensures the profile saver does its usual processing. ensureProfileProcessing()78*795d594fSAndroid Build Coastguard Worker public static native void ensureProfileProcessing(); isAotCompiled(Class<?> cls, String methodName)79*795d594fSAndroid Build Coastguard Worker public static native boolean isAotCompiled(Class<?> cls, String methodName); getHotnessCounter(Class<?> cls, String methodName)80*795d594fSAndroid Build Coastguard Worker public static native int getHotnessCounter(Class<?> cls, String methodName); 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker private static final String TEMP_FILE_NAME_PREFIX = "temp"; 83*795d594fSAndroid Build Coastguard Worker private static final String TEMP_FILE_NAME_SUFFIX = "-file"; 84*795d594fSAndroid Build Coastguard Worker createTempFile()85*795d594fSAndroid Build Coastguard Worker private static File createTempFile() throws Exception { 86*795d594fSAndroid Build Coastguard Worker try { 87*795d594fSAndroid Build Coastguard Worker return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 88*795d594fSAndroid Build Coastguard Worker } catch (IOException e) { 89*795d594fSAndroid Build Coastguard Worker System.setProperty("java.io.tmpdir", "/data/local/tmp"); 90*795d594fSAndroid Build Coastguard Worker try { 91*795d594fSAndroid Build Coastguard Worker return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 92*795d594fSAndroid Build Coastguard Worker } catch (IOException e2) { 93*795d594fSAndroid Build Coastguard Worker System.setProperty("java.io.tmpdir", "/sdcard"); 94*795d594fSAndroid Build Coastguard Worker return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 95*795d594fSAndroid Build Coastguard Worker } 96*795d594fSAndroid Build Coastguard Worker } 97*795d594fSAndroid Build Coastguard Worker } 98*795d594fSAndroid Build Coastguard Worker } 99