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 17import java.io.File; 18import java.io.IOException; 19import java.lang.reflect.Method; 20 21public class Main { 22 23 public static void main(String[] args) throws Exception { 24 System.loadLibrary(args[0]); 25 26 File file = null; 27 try { 28 file = createTempFile(); 29 // String codePath = getDexBaseLocation(); 30 String codePath = System.getenv("DEX_LOCATION") + "/595-profile-saving.jar"; 31 VMRuntime.registerAppInfo(file.getPath(), 32 System.getenv("DEX_LOCATION"), 33 new String[] {codePath}, 34 /* foreignProfileDir */ null); 35 36 int methodIdx = $opt$noinline$testProfile(); 37 ensureProfileProcessing(); 38 if (!presentInProfile(file.getPath(), methodIdx)) { 39 throw new RuntimeException("Method with index " + methodIdx + " not in the profile"); 40 } 41 } finally { 42 if (file != null) { 43 file.delete(); 44 } 45 } 46 } 47 48 public static int $opt$noinline$testProfile() { 49 if (doThrow) throw new Error(); 50 // Make sure we have a profile info for this method without the need to loop. 51 return ensureProfilingInfo("$opt$noinline$testProfile"); 52 } 53 54 // Return the dex method index. 55 public static native int ensureProfilingInfo(String methodName); 56 // Ensures the profile saver does its usual processing. 57 public static native void ensureProfileProcessing(); 58 // Checks if the profiles saver knows about the method. 59 public static native boolean presentInProfile(String profile, int methodIdx); 60 61 public static boolean doThrow = false; 62 private static final String TEMP_FILE_NAME_PREFIX = "dummy"; 63 private static final String TEMP_FILE_NAME_SUFFIX = "-file"; 64 65 static native String getProfileInfoDump( 66 String filename); 67 68 private static File createTempFile() throws Exception { 69 try { 70 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 71 } catch (IOException e) { 72 System.setProperty("java.io.tmpdir", "/data/local/tmp"); 73 try { 74 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 75 } catch (IOException e2) { 76 System.setProperty("java.io.tmpdir", "/sdcard"); 77 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); 78 } 79 } 80 } 81 82 private static class VMRuntime { 83 private static final Method registerAppInfoMethod; 84 static { 85 try { 86 Class<? extends Object> c = Class.forName("dalvik.system.VMRuntime"); 87 registerAppInfoMethod = c.getDeclaredMethod("registerAppInfo", 88 String.class, String.class, String[].class, String.class); 89 } catch (Exception e) { 90 throw new RuntimeException(e); 91 } 92 } 93 94 public static void registerAppInfo(String profile, String appDir, 95 String[] codePaths, String foreignDir) throws Exception { 96 registerAppInfoMethod.invoke(null, profile, appDir, codePaths, foreignDir); 97 } 98 } 99} 100