blob: 18c0598befbc66ecd98a352bb7653770a2c048fc [file] [log] [blame]
Calin Juravlee5de54c2016-04-20 14:22:09 +01001/*
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(),
Calin Juravle77651c42017-03-03 18:04:02 -080032 new String[] {codePath});
Calin Juravlee5de54c2016-04-20 14:22:09 +010033
Mathieu Chartier885a7132017-06-10 14:35:11 -070034 // 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");
Calin Juravlee5de54c2016-04-20 14:22:09 +010043 }
Mathieu Chartier885a7132017-06-10 14:35:11 -070044 testAddMethodToProfile(file, bootMethod);
Calin Juravlee5de54c2016-04-20 14:22:09 +010045 } finally {
46 if (file != null) {
47 file.delete();
48 }
49 }
50 }
51
Mathieu Chartier885a7132017-06-10 14:35:11 -070052 static void testAddMethodToProfile(File file, Method m) {
Calin Juravlee5de54c2016-04-20 14:22:09 +010053 // Make sure we have a profile info for this method without the need to loop.
Mathieu Chartier885a7132017-06-10 14:35:11 -070054 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 }
Calin Juravlee5de54c2016-04-20 14:22:09 +010061 }
62
Mathieu Chartier885a7132017-06-10 14:35:11 -070063 // Ensure a method has a profiling info.
64 public static native void ensureProfilingInfo(Method method);
Calin Juravlee5de54c2016-04-20 14:22:09 +010065 // Ensures the profile saver does its usual processing.
66 public static native void ensureProfileProcessing();
67 // Checks if the profiles saver knows about the method.
Mathieu Chartier885a7132017-06-10 14:35:11 -070068 public static native boolean presentInProfile(String profile, Method method);
Calin Juravlee5de54c2016-04-20 14:22:09 +010069
Calin Juravlee5de54c2016-04-20 14:22:09 +010070 private static final String TEMP_FILE_NAME_PREFIX = "dummy";
71 private static final String TEMP_FILE_NAME_SUFFIX = "-file";
72
73 static native String getProfileInfoDump(
74 String filename);
75
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",
Calin Juravle77651c42017-03-03 18:04:02 -080096 String.class, String[].class);
Calin Juravlee5de54c2016-04-20 14:22:09 +010097 } catch (Exception e) {
98 throw new RuntimeException(e);
99 }
100 }
101
Calin Juravle77651c42017-03-03 18:04:02 -0800102 public static void registerAppInfo(String profile, String[] codePaths)
103 throws Exception {
104 registerAppInfoMethod.invoke(null, profile, codePaths);
Calin Juravlee5de54c2016-04-20 14:22:09 +0100105 }
106 }
107}