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