• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 import java.io.File;
18 import java.io.IOException;
19 import java.lang.reflect.Method;
20 
21 public class Main {
22 
main(String[] args)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                                 new String[] {codePath});
33 
34       // Test that the profile saves an app method with a profiling info.
35       Method appMethod = Main.class.getDeclaredMethod("testAddMethodToProfile",
36           File.class, Method.class);
37       testAddMethodToProfile(file, appMethod);
38 
39       // Test that the profile saves a boot class path method with a profiling info.
40       Method bootMethod = File.class.getDeclaredMethod("delete");
41       if (bootMethod.getDeclaringClass().getClassLoader() != Object.class.getClassLoader()) {
42         System.out.println("Class loader does not match boot class");
43       }
44       testAddMethodToProfile(file, bootMethod);
45     } finally {
46       if (file != null) {
47         file.delete();
48       }
49     }
50   }
51 
testAddMethodToProfile(File file, Method m)52   static void testAddMethodToProfile(File file, Method m) {
53     // Make sure we have a profile info for this method without the need to loop.
54     ensureProfilingInfo(m);
55     // Make sure the profile gets saved.
56     ensureProfileProcessing();
57     // Verify that the profile was saved and contains the method.
58     if (!presentInProfile(file.getPath(), m)) {
59       throw new RuntimeException("Method with index " + m + " not in the profile");
60     }
61   }
62 
63   // Ensure a method has a profiling info.
ensureProfilingInfo(Method method)64   public static native void ensureProfilingInfo(Method method);
65   // Ensures the profile saver does its usual processing.
ensureProfileProcessing()66   public static native void ensureProfileProcessing();
67   // Checks if the profiles saver knows about the method.
presentInProfile(String profile, Method method)68   public static native boolean presentInProfile(String profile, Method method);
69 
70   private static final String TEMP_FILE_NAME_PREFIX = "dummy";
71   private static final String TEMP_FILE_NAME_SUFFIX = "-file";
72 
getProfileInfoDump( String filename)73   static native String getProfileInfoDump(
74       String filename);
75 
createTempFile()76   private static File createTempFile() throws Exception {
77     try {
78       return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
79     } catch (IOException e) {
80       System.setProperty("java.io.tmpdir", "/data/local/tmp");
81       try {
82         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
83       } catch (IOException e2) {
84         System.setProperty("java.io.tmpdir", "/sdcard");
85         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
86       }
87     }
88   }
89 
90   private static class VMRuntime {
91     private static final Method registerAppInfoMethod;
92     static {
93       try {
94         Class<? extends Object> c = Class.forName("dalvik.system.VMRuntime");
95         registerAppInfoMethod = c.getDeclaredMethod("registerAppInfo",
96             String.class, String[].class);
97       } catch (Exception e) {
98         throw new RuntimeException(e);
99       }
100     }
101 
registerAppInfo(String profile, String[] codePaths)102     public static void registerAppInfo(String profile, String[] codePaths)
103         throws Exception {
104       registerAppInfoMethod.invoke(null, profile, codePaths);
105     }
106   }
107 }
108