xref: /aosp_15_r20/art/test/570-checker-osr/src/DeoptimizationController.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker // This file is a copy of 802-deoptimization/src/DeoptimizationController.java
18*795d594fSAndroid Build Coastguard Worker // because run-test requires standalone individual test.
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker import java.io.File;
21*795d594fSAndroid Build Coastguard Worker import java.io.IOException;
22*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method;
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker /**
25*795d594fSAndroid Build Coastguard Worker  * Controls deoptimization using dalvik.system.VMDebug class.
26*795d594fSAndroid Build Coastguard Worker  */
27*795d594fSAndroid Build Coastguard Worker public class DeoptimizationController {
28*795d594fSAndroid Build Coastguard Worker   private static final String TEMP_FILE_NAME_PREFIX = "test";
29*795d594fSAndroid Build Coastguard Worker   private static final String TEMP_FILE_NAME_SUFFIX = ".trace";
30*795d594fSAndroid Build Coastguard Worker 
createTempFile()31*795d594fSAndroid Build Coastguard Worker   private static File createTempFile() throws Exception {
32*795d594fSAndroid Build Coastguard Worker     try {
33*795d594fSAndroid Build Coastguard Worker       return  File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
34*795d594fSAndroid Build Coastguard Worker     } catch (IOException e) {
35*795d594fSAndroid Build Coastguard Worker       System.setProperty("java.io.tmpdir", "/data/local/tmp");
36*795d594fSAndroid Build Coastguard Worker       try {
37*795d594fSAndroid Build Coastguard Worker         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
38*795d594fSAndroid Build Coastguard Worker       } catch (IOException e2) {
39*795d594fSAndroid Build Coastguard Worker         System.setProperty("java.io.tmpdir", "/sdcard");
40*795d594fSAndroid Build Coastguard Worker         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
41*795d594fSAndroid Build Coastguard Worker       }
42*795d594fSAndroid Build Coastguard Worker     }
43*795d594fSAndroid Build Coastguard Worker   }
44*795d594fSAndroid Build Coastguard Worker 
startDeoptimization()45*795d594fSAndroid Build Coastguard Worker   public static void startDeoptimization() {
46*795d594fSAndroid Build Coastguard Worker     File tempFile = null;
47*795d594fSAndroid Build Coastguard Worker     try {
48*795d594fSAndroid Build Coastguard Worker       tempFile = createTempFile();
49*795d594fSAndroid Build Coastguard Worker       String tempFileName = tempFile.getPath();
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker       VMDebug.startMethodTracing(tempFileName, 0, 0, false, 1000);
52*795d594fSAndroid Build Coastguard Worker       if (VMDebug.getMethodTracingMode() == 0) {
53*795d594fSAndroid Build Coastguard Worker         throw new IllegalStateException("Not tracing.");
54*795d594fSAndroid Build Coastguard Worker       }
55*795d594fSAndroid Build Coastguard Worker     } catch (Exception exc) {
56*795d594fSAndroid Build Coastguard Worker       exc.printStackTrace(System.out);
57*795d594fSAndroid Build Coastguard Worker     } finally {
58*795d594fSAndroid Build Coastguard Worker       if (tempFile != null) {
59*795d594fSAndroid Build Coastguard Worker         tempFile.delete();
60*795d594fSAndroid Build Coastguard Worker       }
61*795d594fSAndroid Build Coastguard Worker     }
62*795d594fSAndroid Build Coastguard Worker   }
63*795d594fSAndroid Build Coastguard Worker 
stopDeoptimization()64*795d594fSAndroid Build Coastguard Worker   public static void stopDeoptimization() {
65*795d594fSAndroid Build Coastguard Worker     try {
66*795d594fSAndroid Build Coastguard Worker       VMDebug.stopMethodTracing();
67*795d594fSAndroid Build Coastguard Worker       if (VMDebug.getMethodTracingMode() != 0) {
68*795d594fSAndroid Build Coastguard Worker         throw new IllegalStateException("Still tracing.");
69*795d594fSAndroid Build Coastguard Worker       }
70*795d594fSAndroid Build Coastguard Worker     } catch (Exception exc) {
71*795d594fSAndroid Build Coastguard Worker       exc.printStackTrace(System.out);
72*795d594fSAndroid Build Coastguard Worker     }
73*795d594fSAndroid Build Coastguard Worker   }
74*795d594fSAndroid Build Coastguard Worker 
75*795d594fSAndroid Build Coastguard Worker   private static class VMDebug {
76*795d594fSAndroid Build Coastguard Worker     private static final Method startMethodTracingMethod;
77*795d594fSAndroid Build Coastguard Worker     private static final Method stopMethodTracingMethod;
78*795d594fSAndroid Build Coastguard Worker     private static final Method getMethodTracingModeMethod;
79*795d594fSAndroid Build Coastguard Worker 
80*795d594fSAndroid Build Coastguard Worker     static {
81*795d594fSAndroid Build Coastguard Worker       try {
82*795d594fSAndroid Build Coastguard Worker         Class<?> c = Class.forName("dalvik.system.VMDebug");
83*795d594fSAndroid Build Coastguard Worker         startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
84*795d594fSAndroid Build Coastguard Worker             Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
85*795d594fSAndroid Build Coastguard Worker         stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
86*795d594fSAndroid Build Coastguard Worker         getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
87*795d594fSAndroid Build Coastguard Worker       } catch (Exception e) {
88*795d594fSAndroid Build Coastguard Worker         throw new RuntimeException(e);
89*795d594fSAndroid Build Coastguard Worker       }
90*795d594fSAndroid Build Coastguard Worker     }
91*795d594fSAndroid Build Coastguard Worker 
startMethodTracing(String filename, int bufferSize, int flags, boolean samplingEnabled, int intervalUs)92*795d594fSAndroid Build Coastguard Worker     public static void startMethodTracing(String filename, int bufferSize, int flags,
93*795d594fSAndroid Build Coastguard Worker         boolean samplingEnabled, int intervalUs) throws Exception {
94*795d594fSAndroid Build Coastguard Worker       startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
95*795d594fSAndroid Build Coastguard Worker           intervalUs);
96*795d594fSAndroid Build Coastguard Worker     }
stopMethodTracing()97*795d594fSAndroid Build Coastguard Worker     public static void stopMethodTracing() throws Exception {
98*795d594fSAndroid Build Coastguard Worker       stopMethodTracingMethod.invoke(null);
99*795d594fSAndroid Build Coastguard Worker     }
getMethodTracingMode()100*795d594fSAndroid Build Coastguard Worker     public static int getMethodTracingMode() throws Exception {
101*795d594fSAndroid Build Coastguard Worker       return (int) getMethodTracingModeMethod.invoke(null);
102*795d594fSAndroid Build Coastguard Worker     }
103*795d594fSAndroid Build Coastguard Worker   }
104*795d594fSAndroid Build Coastguard Worker }
105