Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 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 { |
Sebastien Hertz | ef3b177 | 2015-06-30 13:57:39 +0200 | [diff] [blame] | 25 | private static final String TEMP_FILE_NAME_PREFIX = "test"; |
| 26 | private static final String TEMP_FILE_NAME_SUFFIX = ".trace"; |
| 27 | |
Andreas Gampe | 7403503 | 2015-01-27 16:12:08 -0800 | [diff] [blame] | 28 | private static File createTempFile() throws Exception { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 29 | try { |
Sebastien Hertz | ef3b177 | 2015-06-30 13:57:39 +0200 | [diff] [blame] | 30 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
Andreas Gampe | 7403503 | 2015-01-27 16:12:08 -0800 | [diff] [blame] | 31 | } catch (IOException e) { |
| 32 | System.setProperty("java.io.tmpdir", "/data/local/tmp"); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 33 | try { |
Sebastien Hertz | ef3b177 | 2015-06-30 13:57:39 +0200 | [diff] [blame] | 34 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
Andreas Gampe | 7403503 | 2015-01-27 16:12:08 -0800 | [diff] [blame] | 35 | } catch (IOException e2) { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 36 | System.setProperty("java.io.tmpdir", "/sdcard"); |
Sebastien Hertz | ef3b177 | 2015-06-30 13:57:39 +0200 | [diff] [blame] | 37 | return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 38 | } |
Andreas Gampe | 7403503 | 2015-01-27 16:12:08 -0800 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
| 42 | public static void startDeoptimization() { |
Sebastien Hertz | ef3b177 | 2015-06-30 13:57:39 +0200 | [diff] [blame] | 43 | File tempFile = null; |
Andreas Gampe | 7403503 | 2015-01-27 16:12:08 -0800 | [diff] [blame] | 44 | try { |
Sebastien Hertz | ef3b177 | 2015-06-30 13:57:39 +0200 | [diff] [blame] | 45 | tempFile = createTempFile(); |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 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); |
Sebastien Hertz | ef3b177 | 2015-06-30 13:57:39 +0200 | [diff] [blame] | 54 | } finally { |
| 55 | if (tempFile != null) { |
| 56 | tempFile.delete(); |
| 57 | } |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Andreas Gampe | 7403503 | 2015-01-27 16:12:08 -0800 | [diff] [blame] | 61 | public static void stopDeoptimization() { |
Sebastien Hertz | 270a0e1 | 2015-01-16 19:49:09 +0100 | [diff] [blame] | 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 | } |