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