blob: e2c04dbfd323f66255590d7e62e6748134d4e447 [file] [log] [blame]
Jeff Haod063d912014-09-08 09:38:18 -07001/*
2 * Copyright (C) 2014 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
21public class Main {
22 public static void main(String[] args) throws Exception {
23 String name = System.getProperty("java.vm.name");
24 if (!"Dalvik".equals(name)) {
25 System.out.println("This test is not supported on " + name);
26 return;
27 }
28 testMethodTracing();
29 }
30
31 private static void testMethodTracing() throws Exception {
32 String tempFileName;
33 if (new File("/tmp").isDirectory()) {
34 tempFileName = "/tmp/test.trace";
35 } else if (new File("/sdcard").isDirectory()) {
36 tempFileName = "/sdcard/test.trace";
37 } else {
38 System.out.println("Can't find proper output directory for trace file");
39 return;
40 }
41 File tempFile = new File(tempFileName);
42 tempFile.delete();
43
Jeff Haocbe15be2014-09-08 15:32:39 -070044 if (VMDebug.getMethodTracingMode() != 0) {
45 VMDebug.stopMethodTracing();
46 }
47
Jeff Haod063d912014-09-08 09:38:18 -070048 System.out.println("Confirm enable/disable");
49 System.out.println("status=" + VMDebug.getMethodTracingMode());
50 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
51 System.out.println("status=" + VMDebug.getMethodTracingMode());
52 VMDebug.stopMethodTracing();
53 System.out.println("status=" + VMDebug.getMethodTracingMode());
54 if (tempFile.length() == 0) {
55 System.out.println("ERROR: tracing output file is empty");
56 }
57
58 System.out.println("Confirm sampling");
59 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 1000);
60 System.out.println("status=" + VMDebug.getMethodTracingMode());
61 VMDebug.stopMethodTracing();
62 System.out.println("status=" + VMDebug.getMethodTracingMode());
63 if (tempFile.length() == 0) {
64 System.out.println("ERROR: sample tracing output file is empty");
65 }
66
67 System.out.println("Test starting when already started");
68 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
69 System.out.println("status=" + VMDebug.getMethodTracingMode());
70 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
71 System.out.println("status=" + VMDebug.getMethodTracingMode());
72
73 System.out.println("Test stopping when already stopped");
74 VMDebug.stopMethodTracing();
75 System.out.println("status=" + VMDebug.getMethodTracingMode());
76 VMDebug.stopMethodTracing();
77 System.out.println("status=" + VMDebug.getMethodTracingMode());
78
79 System.out.println("Test tracing with empty filename");
80 try {
81 VMDebug.startMethodTracing("", 0, 0, false, 0);
82 System.out.println("Should have thrown an exception");
83 } catch (Exception e) {
84 System.out.println("Got expected exception");
85 }
86
87 System.out.println("Test tracing with bogus (< 1024 && != 0) filesize");
88 try {
89 VMDebug.startMethodTracing(tempFileName, 1000, 0, false, 0);
90 System.out.println("Should have thrown an exception");
91 } catch (Exception e) {
92 System.out.println("Got expected exception");
93 }
94
95 System.out.println("Test sampling with bogus (<= 0) interval");
96 try {
97 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 0);
98 System.out.println("Should have thrown an exception");
99 } catch (Exception e) {
100 System.out.println("Got expected exception");
101 }
102
103 tempFile.delete();
104 }
105
106 private static class VMDebug {
107 private static final Method startMethodTracingMethod;
108 private static final Method stopMethodTracingMethod;
109 private static final Method getMethodTracingModeMethod;
110 static {
111 try {
112 Class c = Class.forName("dalvik.system.VMDebug");
113 startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
114 Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
115 stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
116 getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
117 } catch (Exception e) {
118 throw new RuntimeException(e);
119 }
120 }
121
122 public static void startMethodTracing(String filename, int bufferSize, int flags,
123 boolean samplingEnabled, int intervalUs) throws Exception {
124 startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
125 intervalUs);
126 }
127 public static void stopMethodTracing() throws Exception {
128 stopMethodTracingMethod.invoke(null);
129 }
130 public static int getMethodTracingMode() throws Exception {
131 return (int) getMethodTracingModeMethod.invoke(null);
132 }
133 }
134}