Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 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 | |
| 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 Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 32 | new String[] {codePath}); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 33 | |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame^] | 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"); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 43 | } |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame^] | 44 | testAddMethodToProfile(file, bootMethod); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 45 | } finally { |
| 46 | if (file != null) { |
| 47 | file.delete(); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame^] | 52 | static void testAddMethodToProfile(File file, Method m) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 53 | // Make sure we have a profile info for this method without the need to loop. |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame^] | 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 | } |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame^] | 63 | // Ensure a method has a profiling info. |
| 64 | public static native void ensureProfilingInfo(Method method); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 65 | // 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 Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame^] | 68 | public static native boolean presentInProfile(String profile, Method method); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 69 | |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 70 | 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 Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 96 | String.class, String[].class); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 97 | } catch (Exception e) { |
| 98 | throw new RuntimeException(e); |
| 99 | } |
| 100 | } |
| 101 | |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 102 | public static void registerAppInfo(String profile, String[] codePaths) |
| 103 | throws Exception { |
| 104 | registerAppInfoMethod.invoke(null, profile, codePaths); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |