blob: c031c07a0f15ad69fd90c351edfb8fa1808042e3 [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 {
25 public static void startDeoptomization() {
26 try {
27 File tempFile;
28 try {
29 tempFile = File.createTempFile("test", ".trace");
30 } catch (IOException e) {
31 System.setProperty("java.io.tmpdir", "/sdcard");
32 tempFile = File.createTempFile("test", ".trace");
33 }
34 tempFile.deleteOnExit();
35 String tempFileName = tempFile.getPath();
36
37 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 1000);
38 if (VMDebug.getMethodTracingMode() == 0) {
39 throw new IllegalStateException("Not tracing.");
40 }
41 } catch (Exception exc) {
42 exc.printStackTrace(System.err);
43 }
44 }
45
46 public static void stopDeoptomization() {
47 try {
48 VMDebug.stopMethodTracing();
49 if (VMDebug.getMethodTracingMode() != 0) {
50 throw new IllegalStateException("Still tracing.");
51 }
52 } catch (Exception exc) {
53 exc.printStackTrace(System.err);
54 }
55 }
56
57 private static class VMDebug {
58 private static final Method startMethodTracingMethod;
59 private static final Method stopMethodTracingMethod;
60 private static final Method getMethodTracingModeMethod;
61
62 static {
63 try {
64 Class<?> c = Class.forName("dalvik.system.VMDebug");
65 startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
66 Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
67 stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
68 getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
69 } catch (Exception e) {
70 throw new RuntimeException(e);
71 }
72 }
73
74 public static void startMethodTracing(String filename, int bufferSize, int flags,
75 boolean samplingEnabled, int intervalUs) throws Exception {
76 startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
77 intervalUs);
78 }
79 public static void stopMethodTracing() throws Exception {
80 stopMethodTracingMethod.invoke(null);
81 }
82 public static int getMethodTracingMode() throws Exception {
83 return (int) getMethodTracingModeMethod.invoke(null);
84 }
85 }
86}