blob: c926669503fa0521d12d3659aa20719bd1d62579 [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 {
Andreas Gampe74035032015-01-27 16:12:08 -080025 private static File createTempFile() throws Exception {
Sebastien Hertz270a0e12015-01-16 19:49:09 +010026 try {
Andreas Gampe74035032015-01-27 16:12:08 -080027 return File.createTempFile("test", ".trace");
28 } catch (IOException e) {
29 System.setProperty("java.io.tmpdir", "/data/local/tmp");
Sebastien Hertz270a0e12015-01-16 19:49:09 +010030 try {
Andreas Gampe74035032015-01-27 16:12:08 -080031 return File.createTempFile("test", ".trace");
32 } catch (IOException e2) {
Sebastien Hertz270a0e12015-01-16 19:49:09 +010033 System.setProperty("java.io.tmpdir", "/sdcard");
Andreas Gampe74035032015-01-27 16:12:08 -080034 return File.createTempFile("test", ".trace");
Sebastien Hertz270a0e12015-01-16 19:49:09 +010035 }
Andreas Gampe74035032015-01-27 16:12:08 -080036 }
37 }
38
39 public static void startDeoptimization() {
40 try {
41 File tempFile = createTempFile();
Sebastien Hertz270a0e12015-01-16 19:49:09 +010042 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
Andreas Gampe74035032015-01-27 16:12:08 -080054 public static void stopDeoptimization() {
Sebastien Hertz270a0e12015-01-16 19:49:09 +010055 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
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 }
87 public static void stopMethodTracing() throws Exception {
88 stopMethodTracingMethod.invoke(null);
89 }
90 public static int getMethodTracingMode() throws Exception {
91 return (int) getMethodTracingModeMethod.invoke(null);
92 }
93 }
94}