1 /*
2  * Copyright (C) 2015 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 // This file is a copy of 802-deoptimization/src/DeoptimizationController.java
18 // because run-test requires standalone individual test.
19 
20 import java.io.File;
21 import java.io.IOException;
22 import java.lang.reflect.Method;
23 
24 /**
25  * Controls deoptimization using dalvik.system.VMDebug class.
26  */
27 public class DeoptimizationController {
28   private static final String TEMP_FILE_NAME_PREFIX = "test";
29   private static final String TEMP_FILE_NAME_SUFFIX = ".trace";
30 
createTempFile()31   private static File createTempFile() throws Exception {
32     try {
33       return  File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
34     } catch (IOException e) {
35       System.setProperty("java.io.tmpdir", "/data/local/tmp");
36       try {
37         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
38       } catch (IOException e2) {
39         System.setProperty("java.io.tmpdir", "/sdcard");
40         return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
41       }
42     }
43   }
44 
startDeoptimization()45   public static void startDeoptimization() {
46     File tempFile = null;
47     try {
48       tempFile = createTempFile();
49       String tempFileName = tempFile.getPath();
50 
51       VMDebug.startMethodTracing(tempFileName, 0, 0, false, 1000);
52       if (VMDebug.getMethodTracingMode() == 0) {
53         throw new IllegalStateException("Not tracing.");
54       }
55     } catch (Exception exc) {
56       exc.printStackTrace(System.out);
57     } finally {
58       if (tempFile != null) {
59         tempFile.delete();
60       }
61     }
62   }
63 
stopDeoptimization()64   public static void stopDeoptimization() {
65     try {
66       VMDebug.stopMethodTracing();
67       if (VMDebug.getMethodTracingMode() != 0) {
68         throw new IllegalStateException("Still tracing.");
69       }
70     } catch (Exception exc) {
71       exc.printStackTrace(System.out);
72     }
73   }
74 
75   private static class VMDebug {
76     private static final Method startMethodTracingMethod;
77     private static final Method stopMethodTracingMethod;
78     private static final Method getMethodTracingModeMethod;
79 
80     static {
81       try {
82         Class<?> c = Class.forName("dalvik.system.VMDebug");
83         startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
84             Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
85         stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
86         getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
87       } catch (Exception e) {
88         throw new RuntimeException(e);
89       }
90     }
91 
startMethodTracing(String filename, int bufferSize, int flags, boolean samplingEnabled, int intervalUs)92     public static void startMethodTracing(String filename, int bufferSize, int flags,
93         boolean samplingEnabled, int intervalUs) throws Exception {
94       startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
95           intervalUs);
96     }
stopMethodTracing()97     public static void stopMethodTracing() throws Exception {
98       stopMethodTracingMethod.invoke(null);
99     }
getMethodTracingMode()100     public static int getMethodTracingMode() throws Exception {
101       return (int) getMethodTracingModeMethod.invoke(null);
102     }
103   }
104 }
105