blob: a8db0699eedb3828b6c76ad45cdebbfa9342b2f2 [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;
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -070020import java.util.Map;
Jeff Haod063d912014-09-08 09:38:18 -070021
22public class Main {
23 public static void main(String[] args) throws Exception {
24 String name = System.getProperty("java.vm.name");
25 if (!"Dalvik".equals(name)) {
26 System.out.println("This test is not supported on " + name);
27 return;
28 }
29 testMethodTracing();
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -070030 testRuntimeStat();
Jeff Haod063d912014-09-08 09:38:18 -070031 }
32
Andreas Gampe74035032015-01-27 16:12:08 -080033 private static File createTempFile() throws Exception {
Jeff Hao8cf89c42014-09-09 13:07:59 -070034 try {
Andreas Gampe74035032015-01-27 16:12:08 -080035 return File.createTempFile("test", ".trace");
Jeff Hao8cf89c42014-09-09 13:07:59 -070036 } catch (IOException e) {
Andreas Gampe74035032015-01-27 16:12:08 -080037 System.setProperty("java.io.tmpdir", "/data/local/tmp");
38 try {
39 return File.createTempFile("test", ".trace");
40 } catch (IOException e2) {
41 System.setProperty("java.io.tmpdir", "/sdcard");
42 return File.createTempFile("test", ".trace");
43 }
Jeff Haod063d912014-09-08 09:38:18 -070044 }
Andreas Gampe74035032015-01-27 16:12:08 -080045 }
46
47 private static void testMethodTracing() throws Exception {
48 File tempFile = createTempFile();
Jeff Hao8cf89c42014-09-09 13:07:59 -070049 tempFile.deleteOnExit();
50 String tempFileName = tempFile.getPath();
Jeff Haod063d912014-09-08 09:38:18 -070051
Jeff Haocbe15be2014-09-08 15:32:39 -070052 if (VMDebug.getMethodTracingMode() != 0) {
53 VMDebug.stopMethodTracing();
54 }
55
Jeff Haod063d912014-09-08 09:38:18 -070056 System.out.println("Confirm enable/disable");
57 System.out.println("status=" + VMDebug.getMethodTracingMode());
58 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
59 System.out.println("status=" + VMDebug.getMethodTracingMode());
60 VMDebug.stopMethodTracing();
61 System.out.println("status=" + VMDebug.getMethodTracingMode());
62 if (tempFile.length() == 0) {
63 System.out.println("ERROR: tracing output file is empty");
64 }
65
66 System.out.println("Confirm sampling");
67 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 1000);
68 System.out.println("status=" + VMDebug.getMethodTracingMode());
69 VMDebug.stopMethodTracing();
70 System.out.println("status=" + VMDebug.getMethodTracingMode());
71 if (tempFile.length() == 0) {
72 System.out.println("ERROR: sample tracing output file is empty");
73 }
74
75 System.out.println("Test starting when already started");
76 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
77 System.out.println("status=" + VMDebug.getMethodTracingMode());
78 VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
79 System.out.println("status=" + VMDebug.getMethodTracingMode());
80
81 System.out.println("Test stopping when already stopped");
82 VMDebug.stopMethodTracing();
83 System.out.println("status=" + VMDebug.getMethodTracingMode());
84 VMDebug.stopMethodTracing();
85 System.out.println("status=" + VMDebug.getMethodTracingMode());
86
87 System.out.println("Test tracing with empty filename");
88 try {
89 VMDebug.startMethodTracing("", 0, 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 tracing with bogus (< 1024 && != 0) filesize");
96 try {
97 VMDebug.startMethodTracing(tempFileName, 1000, 0, false, 0);
98 System.out.println("Should have thrown an exception");
99 } catch (Exception e) {
100 System.out.println("Got expected exception");
101 }
102
103 System.out.println("Test sampling with bogus (<= 0) interval");
104 try {
105 VMDebug.startMethodTracing(tempFileName, 0, 0, true, 0);
106 System.out.println("Should have thrown an exception");
107 } catch (Exception e) {
108 System.out.println("Got expected exception");
109 }
110
111 tempFile.delete();
112 }
113
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700114 private static void checkNumber(String s) throws Exception {
115 if (s == null) {
116 System.out.println("Got null string");
117 return;
118 }
119 long n = Long.valueOf(s);
120 if (n < 0) {
121 System.out.println("Got negative number " + n);
122 }
123 }
124
125 private static void checkHistogram(String s) throws Exception {
126 if (s == null || s.length() == 0) {
127 System.out.println("Got null or empty string");
128 return;
129 }
130 String[] buckets = s.split(",");
131 long last_key = 0;
132 for (int i = 0; i < buckets.length; ++i) {
133 String bucket = buckets[i];
134 if (bucket.length() == 0) {
135 System.out.println("Got empty bucket");
136 continue;
137 }
138 String[] kv = bucket.split(":");
139 if (kv.length != 2 || kv[0].length() == 0 || kv[1].length() == 0) {
140 System.out.println("Got bad bucket " + bucket);
141 continue;
142 }
143 long key = Long.valueOf(kv[0]);
144 long value = Long.valueOf(kv[1]);
145 if (key < 0 || value < 0) {
146 System.out.println("Got negative key or value " + bucket);
147 continue;
148 }
149 if (key < last_key) {
150 System.out.println("Got decreasing key " + bucket);
151 continue;
152 }
153 last_key = key;
154 }
155 }
156
157 private static void testRuntimeStat() throws Exception {
158 // Invoke at least one GC and wait for 20 seconds or so so we get at
159 // least one bucket in the histograms.
160 for (int i = 0; i < 20; ++i) {
161 Runtime.getRuntime().gc();
162 Thread.sleep(1000L);
163 }
164 String gc_count = VMDebug.getRuntimeStat("art.gc.gc-count");
165 String gc_time = VMDebug.getRuntimeStat("art.gc.gc-time");
166 String bytes_allocated = VMDebug.getRuntimeStat("art.gc.bytes-allocated");
167 String bytes_freed = VMDebug.getRuntimeStat("art.gc.bytes-freed");
168 String blocking_gc_count = VMDebug.getRuntimeStat("art.gc.blocking-gc-count");
169 String blocking_gc_time = VMDebug.getRuntimeStat("art.gc.blocking-gc-time");
170 String gc_count_rate_histogram = VMDebug.getRuntimeStat("art.gc.gc-count-rate-histogram");
171 String blocking_gc_count_rate_histogram =
172 VMDebug.getRuntimeStat("art.gc.blocking-gc-count-rate-histogram");
173 checkNumber(gc_count);
174 checkNumber(gc_time);
175 checkNumber(bytes_allocated);
176 checkNumber(bytes_freed);
177 checkNumber(blocking_gc_count);
178 checkNumber(blocking_gc_time);
179 checkHistogram(gc_count_rate_histogram);
180 checkHistogram(blocking_gc_count_rate_histogram);
181 }
182
183 private static void testRuntimeStats() throws Exception {
184 // Invoke at least one GC and wait for 20 seconds or so so we get at
185 // least one bucket in the histograms.
186 for (int i = 0; i < 20; ++i) {
187 Runtime.getRuntime().gc();
188 Thread.sleep(1000L);
189 }
190 Map<String, String> map = VMDebug.getRuntimeStats();
191 String gc_count = map.get("art.gc.gc-count");
192 String gc_time = map.get("art.gc.gc-time");
193 String bytes_allocated = map.get("art.gc.bytes-allocated");
194 String bytes_freed = map.get("art.gc.bytes-freed");
195 String blocking_gc_count = map.get("art.gc.blocking-gc-count");
196 String blocking_gc_time = map.get("art.gc.blocking-gc-time");
197 String gc_count_rate_histogram = map.get("art.gc.gc-count-rate-histogram");
198 String blocking_gc_count_rate_histogram =
199 map.get("art.gc.blocking-gc-count-rate-histogram");
200 checkNumber(gc_count);
201 checkNumber(gc_time);
202 checkNumber(bytes_allocated);
203 checkNumber(bytes_freed);
204 checkNumber(blocking_gc_count);
205 checkNumber(blocking_gc_time);
206 checkHistogram(gc_count_rate_histogram);
207 checkHistogram(blocking_gc_count_rate_histogram);
208 }
209
Jeff Haod063d912014-09-08 09:38:18 -0700210 private static class VMDebug {
211 private static final Method startMethodTracingMethod;
212 private static final Method stopMethodTracingMethod;
213 private static final Method getMethodTracingModeMethod;
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700214 private static final Method getRuntimeStatMethod;
215 private static final Method getRuntimeStatsMethod;
Jeff Haod063d912014-09-08 09:38:18 -0700216 static {
217 try {
218 Class c = Class.forName("dalvik.system.VMDebug");
219 startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
220 Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
221 stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
222 getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700223 getRuntimeStatMethod = c.getDeclaredMethod("getRuntimeStat", String.class);
224 getRuntimeStatsMethod = c.getDeclaredMethod("getRuntimeStats");
Jeff Haod063d912014-09-08 09:38:18 -0700225 } catch (Exception e) {
226 throw new RuntimeException(e);
227 }
228 }
229
230 public static void startMethodTracing(String filename, int bufferSize, int flags,
231 boolean samplingEnabled, int intervalUs) throws Exception {
232 startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
233 intervalUs);
234 }
235 public static void stopMethodTracing() throws Exception {
236 stopMethodTracingMethod.invoke(null);
237 }
238 public static int getMethodTracingMode() throws Exception {
239 return (int) getMethodTracingModeMethod.invoke(null);
240 }
Hiroshi Yamauchia1c9f012015-04-02 10:18:12 -0700241 public static String getRuntimeStat(String statName) throws Exception {
242 return (String) getRuntimeStatMethod.invoke(null, statName);
243 }
244 public static Map<String, String> getRuntimeStats() throws Exception {
245 return (Map<String, String>) getRuntimeStatsMethod.invoke(null);
246 }
Jeff Haod063d912014-09-08 09:38:18 -0700247 }
248}