blob: d6e662d04d59aa14cbb62ad31d6b564d33b03959 [file] [log] [blame]
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001/*
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
17import java.io.File;
18import java.io.IOException;
19import java.lang.reflect.Method;
20
21/**
22 * Controls deoptimization using dalvik.system.VMDebug class.
23 */
24public class DeoptimizationController {
Sebastien Hertzef3b1772015-06-30 13:57:39 +020025 private static final String TEMP_FILE_NAME_PREFIX = "test";
26 private static final String TEMP_FILE_NAME_SUFFIX = ".trace";
27
Andreas Gampe74035032015-01-27 16:12:08 -080028 private static File createTempFile() throws Exception {
Sebastien Hertz270a0e12015-01-16 19:49:09 +010029 try {
Sebastien Hertzef3b1772015-06-30 13:57:39 +020030 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
Andreas Gampe74035032015-01-27 16:12:08 -080031 } catch (IOException e) {
32 System.setProperty("java.io.tmpdir", "/data/local/tmp");
Sebastien Hertz270a0e12015-01-16 19:49:09 +010033 try {
Sebastien Hertzef3b1772015-06-30 13:57:39 +020034 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
Andreas Gampe74035032015-01-27 16:12:08 -080035 } catch (IOException e2) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +010036 System.setProperty("java.io.tmpdir", "/sdcard");
Sebastien Hertzef3b1772015-06-30 13:57:39 +020037 return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
Sebastien Hertz270a0e12015-01-16 19:49:09 +010038 }
Andreas Gampe74035032015-01-27 16:12:08 -080039 }
40 }
41
42 public static void startDeoptimization() {
Sebastien Hertzef3b1772015-06-30 13:57:39 +020043 File tempFile = null;
Andreas Gampe74035032015-01-27 16:12:08 -080044 try {
Sebastien Hertzef3b1772015-06-30 13:57:39 +020045 tempFile = createTempFile();
Sebastien Hertz270a0e12015-01-16 19:49:09 +010046 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 Hertzef3b1772015-06-30 13:57:39 +020054 } finally {
55 if (tempFile != null) {
56 tempFile.delete();
57 }
Sebastien Hertz270a0e12015-01-16 19:49:09 +010058 }
59 }
60
Andreas Gampe74035032015-01-27 16:12:08 -080061 public static void stopDeoptimization() {
Sebastien Hertz270a0e12015-01-16 19:49:09 +010062 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}