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