• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.FileDescriptor;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.lang.reflect.Method;
22 
23 public class Main {
24     private static final String TEMP_FILE_NAME_PREFIX = "test";
25     private static final String TEMP_FILE_NAME_SUFFIX = ".trace";
26     private static File file;
27 
main(String[] args)28     public static void main(String[] args) throws Exception {
29         System.loadLibrary(args[0]);
30         String name = System.getProperty("java.vm.name");
31         if (!"Dalvik".equals(name)) {
32             System.out.println("This test is not supported on " + name);
33             return;
34         }
35 
36         if (VMDebug.getMethodTracingMode() != 0) {
37             VMDebug.$noinline$stopMethodTracing();
38         }
39 
40         Main m = new Main();
41         boolean isAot_main_before = isAotCompiled(Main.class, "main");
42         boolean isAot_callOuter_before = isAotCompiled(Main.class, "callOuterFunction");
43 
44         file = createTempFile();
45         FileOutputStream out_file = new FileOutputStream(file);
46         VMDebug.startMethodTracing(file.getPath(), out_file.getFD(), /*buffer_size=*/0, /*flags=*/0,
47                 /*sampling=*/false, /*sampling_interval*/ 0, /*streaming=*/true);
48         m.$noinline$doSomeWork();
49 
50         VMDebug.$noinline$stopMethodTracing();
51         out_file.close();
52         file.delete();
53 
54         boolean isAot_main_after = isAotCompiled(Main.class, "main");
55         boolean isAot_callOuter_after = isAotCompiled(Main.class, "callOuterFunction");
56         if (isAot_main_before != isAot_main_after) {
57             throw new Exception("AOT code for main not restored after method tracing? "
58                     + isAot_main_before + " " + isAot_main_after);
59         }
60 
61         if (isAot_callOuter_before != isAot_callOuter_after) {
62             throw new Exception("AOT code for callOuter not restored after method tracing? "
63                     + isAot_callOuter_before + " " + isAot_callOuter_after);
64         }
65     }
66 
createTempFile()67     private static File createTempFile() throws Exception {
68         try {
69             return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
70         } catch (IOException e) {
71             System.setProperty("java.io.tmpdir", "/data/local/tmp");
72             try {
73                 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
74             } catch (IOException e2) {
75                 System.setProperty("java.io.tmpdir", "/sdcard");
76                 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
77             }
78         }
79     }
80 
callOuterFunction()81     public void callOuterFunction() {
82         callLeafFunction();
83     }
84 
callLeafFunction()85     public void callLeafFunction() {}
86 
$noinline$doSomeWork()87     public void $noinline$doSomeWork() {
88         callOuterFunction();
89         callLeafFunction();
90     }
91 
92     private static class VMDebug {
93         private static final Method startMethodTracingMethod;
94         private static final Method stopMethodTracingMethod;
95         private static final Method getMethodTracingModeMethod;
96         static {
97             try {
98                 Class<?> c = Class.forName("dalvik.system.VMDebug");
99                 startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
100                         FileDescriptor.class, Integer.TYPE, Integer.TYPE, Boolean.TYPE,
101                         Integer.TYPE, Boolean.TYPE);
102                 stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
103                 getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
104             } catch (Exception e) {
105                 throw new RuntimeException(e);
106             }
107         }
108 
startMethodTracing(String filename, FileDescriptor fd, int bufferSize, int flags, boolean samplingEnabled, int intervalUs, boolean streaming)109         public static void startMethodTracing(String filename, FileDescriptor fd, int bufferSize,
110                 int flags, boolean samplingEnabled, int intervalUs, boolean streaming)
111                 throws Exception {
112             startMethodTracingMethod.invoke(
113                     null, filename, fd, bufferSize, flags, samplingEnabled, intervalUs, streaming);
114         }
$noinline$stopMethodTracing()115         public static void $noinline$stopMethodTracing() throws Exception {
116             stopMethodTracingMethod.invoke(null);
117         }
getMethodTracingMode()118         public static int getMethodTracingMode() throws Exception {
119             return (int) getMethodTracingModeMethod.invoke(null);
120         }
121     }
122 
isAotCompiled(Class<?> cls, String methodName)123     private native static boolean isAotCompiled(Class<?> cls, String methodName);
124 }
125