PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/099-vmdebug/src/Main.java

https://gitlab.com/androidopensourceproject/platform-art
Java | 365 lines | 315 code | 31 blank | 19 comment | 34 complexity | f67bac12ff86ab592a501bf82ea24d0d MD5 | raw file
  1. /*
  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. import java.io.File;
  17. import java.io.IOException;
  18. import java.lang.reflect.Method;
  19. import java.util.Arrays;
  20. import java.util.ArrayList;
  21. import java.util.Map;
  22. public class Main {
  23. private static final String TEMP_FILE_NAME_PREFIX = "test";
  24. private static final String TEMP_FILE_NAME_SUFFIX = ".trace";
  25. public static void main(String[] args) throws Exception {
  26. String name = System.getProperty("java.vm.name");
  27. if (!"Dalvik".equals(name)) {
  28. System.out.println("This test is not supported on " + name);
  29. return;
  30. }
  31. testMethodTracing();
  32. testCountInstances();
  33. testGetInstances();
  34. testRuntimeStat();
  35. testRuntimeStats();
  36. }
  37. private static File createTempFile() throws Exception {
  38. try {
  39. return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
  40. } catch (IOException e) {
  41. System.setProperty("java.io.tmpdir", "/data/local/tmp");
  42. try {
  43. return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
  44. } catch (IOException e2) {
  45. System.setProperty("java.io.tmpdir", "/sdcard");
  46. return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX);
  47. }
  48. }
  49. }
  50. private static void testMethodTracing() throws Exception {
  51. File tempFile = null;
  52. try {
  53. tempFile = createTempFile();
  54. testMethodTracingToFile(tempFile);
  55. } finally {
  56. if (tempFile != null) {
  57. tempFile.delete();
  58. }
  59. }
  60. }
  61. private static void testMethodTracingToFile(File tempFile) throws Exception {
  62. String tempFileName = tempFile.getPath();
  63. if (VMDebug.getMethodTracingMode() != 0) {
  64. VMDebug.stopMethodTracing();
  65. }
  66. System.out.println("Confirm enable/disable");
  67. System.out.println("status=" + VMDebug.getMethodTracingMode());
  68. VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
  69. System.out.println("status=" + VMDebug.getMethodTracingMode());
  70. VMDebug.stopMethodTracing();
  71. System.out.println("status=" + VMDebug.getMethodTracingMode());
  72. if (tempFile.length() == 0) {
  73. System.out.println("ERROR: tracing output file is empty");
  74. }
  75. System.out.println("Confirm sampling");
  76. VMDebug.startMethodTracing(tempFileName, 0, 0, true, 1000);
  77. System.out.println("status=" + VMDebug.getMethodTracingMode());
  78. VMDebug.stopMethodTracing();
  79. System.out.println("status=" + VMDebug.getMethodTracingMode());
  80. if (tempFile.length() == 0) {
  81. System.out.println("ERROR: sample tracing output file is empty");
  82. }
  83. System.out.println("Test starting when already started");
  84. VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
  85. System.out.println("status=" + VMDebug.getMethodTracingMode());
  86. VMDebug.startMethodTracing(tempFileName, 0, 0, false, 0);
  87. System.out.println("status=" + VMDebug.getMethodTracingMode());
  88. System.out.println("Test stopping when already stopped");
  89. VMDebug.stopMethodTracing();
  90. System.out.println("status=" + VMDebug.getMethodTracingMode());
  91. VMDebug.stopMethodTracing();
  92. System.out.println("status=" + VMDebug.getMethodTracingMode());
  93. System.out.println("Test tracing with empty filename");
  94. try {
  95. VMDebug.startMethodTracing("", 0, 0, false, 0);
  96. System.out.println("Should have thrown an exception");
  97. } catch (Exception e) {
  98. System.out.println("Got expected exception");
  99. }
  100. System.out.println("Test tracing with bogus (< 1024 && != 0) filesize");
  101. try {
  102. VMDebug.startMethodTracing(tempFileName, 1000, 0, false, 0);
  103. System.out.println("Should have thrown an exception");
  104. } catch (Exception e) {
  105. System.out.println("Got expected exception");
  106. }
  107. System.out.println("Test sampling with bogus (<= 0) interval");
  108. try {
  109. VMDebug.startMethodTracing(tempFileName, 0, 0, true, 0);
  110. System.out.println("Should have thrown an exception");
  111. } catch (Exception e) {
  112. System.out.println("Got expected exception");
  113. }
  114. tempFile.delete();
  115. }
  116. private static void checkNumber(String s) throws Exception {
  117. if (s == null) {
  118. System.out.println("Got null string");
  119. return;
  120. }
  121. long n = Long.parseLong(s);
  122. if (n < 0) {
  123. System.out.println("Got negative number " + n);
  124. }
  125. }
  126. private static void checkHistogram(String s) throws Exception {
  127. if (s == null || s.length() == 0) {
  128. System.out.println("Got null or empty string");
  129. return;
  130. }
  131. String[] buckets = s.split(",");
  132. long last_key = 0;
  133. for (int i = 0; i < buckets.length; ++i) {
  134. String bucket = buckets[i];
  135. if (bucket.length() == 0) {
  136. System.out.println("Got empty bucket");
  137. continue;
  138. }
  139. String[] kv = bucket.split(":");
  140. if (kv.length != 2 || kv[0].length() == 0 || kv[1].length() == 0) {
  141. System.out.println("Got bad bucket " + bucket);
  142. continue;
  143. }
  144. long key = Long.parseLong(kv[0]);
  145. long value = Long.parseLong(kv[1]);
  146. if (key < 0 || value < 0) {
  147. System.out.println("Got negative key or value " + bucket);
  148. continue;
  149. }
  150. if (key < last_key) {
  151. System.out.println("Got decreasing key " + bucket);
  152. continue;
  153. }
  154. last_key = key;
  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. private static void testRuntimeStats() throws Exception {
  183. // Invoke at least one GC and wait for 20 seconds or so so we get at
  184. // least one bucket in the histograms.
  185. for (int i = 0; i < 20; ++i) {
  186. Runtime.getRuntime().gc();
  187. Thread.sleep(1000L);
  188. }
  189. Map<String, String> map = VMDebug.getRuntimeStats();
  190. String gc_count = map.get("art.gc.gc-count");
  191. String gc_time = map.get("art.gc.gc-time");
  192. String bytes_allocated = map.get("art.gc.bytes-allocated");
  193. String bytes_freed = map.get("art.gc.bytes-freed");
  194. String blocking_gc_count = map.get("art.gc.blocking-gc-count");
  195. String blocking_gc_time = map.get("art.gc.blocking-gc-time");
  196. String gc_count_rate_histogram = map.get("art.gc.gc-count-rate-histogram");
  197. String blocking_gc_count_rate_histogram =
  198. map.get("art.gc.blocking-gc-count-rate-histogram");
  199. checkNumber(gc_count);
  200. checkNumber(gc_time);
  201. checkNumber(bytes_allocated);
  202. checkNumber(bytes_freed);
  203. checkNumber(blocking_gc_count);
  204. checkNumber(blocking_gc_time);
  205. checkHistogram(gc_count_rate_histogram);
  206. checkHistogram(blocking_gc_count_rate_histogram);
  207. }
  208. static class ClassA { }
  209. static class ClassB { }
  210. static class ClassC extends ClassA { }
  211. private static void testCountInstances() throws Exception {
  212. ArrayList<Object> l = new ArrayList<Object>();
  213. l.add(new ClassA());
  214. l.add(new ClassB());
  215. l.add(new ClassA());
  216. l.add(new ClassC());
  217. Runtime.getRuntime().gc();
  218. System.out.println("Instances of ClassA " +
  219. VMDebug.countInstancesofClass(ClassA.class, false));
  220. System.out.println("Instances of ClassB " +
  221. VMDebug.countInstancesofClass(ClassB.class, false));
  222. System.out.println("Instances of null " + VMDebug.countInstancesofClass(null, false));
  223. System.out.println("Instances of ClassA assignable " +
  224. VMDebug.countInstancesofClass(ClassA.class, true));
  225. Class<?>[] classes = new Class<?>[] {ClassA.class, ClassB.class, null};
  226. long[] counts = VMDebug.countInstancesofClasses(classes, false);
  227. System.out.println("Array counts " + Arrays.toString(counts));
  228. counts = VMDebug.countInstancesofClasses(classes, true);
  229. System.out.println("Array counts assignable " + Arrays.toString(counts));
  230. }
  231. static class ClassD {
  232. public int mask;
  233. public ClassD(int mask) {
  234. this.mask = mask;
  235. }
  236. }
  237. static class ClassE extends ClassD {
  238. public ClassE(int mask) {
  239. super(mask);
  240. }
  241. }
  242. private static void testGetInstances() throws Exception {
  243. ArrayList<Object> l = new ArrayList<Object>();
  244. l.add(new ClassD(0x01));
  245. l.add(new ClassE(0x02));
  246. l.add(new ClassD(0x04));
  247. l.add(new ClassD(0x08));
  248. l.add(new ClassE(0x10));
  249. Runtime.getRuntime().gc();
  250. Class<?>[] classes = new Class<?>[] {ClassD.class, ClassE.class, null};
  251. Object[][] instances = VMDebug.getInstancesOfClasses(classes, false);
  252. int mask = 0;
  253. for (Object instance : instances[0]) {
  254. mask |= ((ClassD)instance).mask;
  255. }
  256. System.out.println("ClassD got " + instances[0].length + ", combined mask: " + mask);
  257. mask = 0;
  258. for (Object instance : instances[1]) {
  259. mask |= ((ClassD)instance).mask;
  260. }
  261. System.out.println("ClassE got " + instances[1].length + ", combined mask: " + mask);
  262. System.out.println("null got " + instances[2].length);
  263. instances = VMDebug.getInstancesOfClasses(classes, true);
  264. mask = 0;
  265. for (Object instance : instances[0]) {
  266. mask |= ((ClassD)instance).mask;
  267. }
  268. System.out.println("ClassD assignable got " + instances[0].length + ", combined mask: " + mask);
  269. mask = 0;
  270. for (Object instance : instances[1]) {
  271. mask |= ((ClassD)instance).mask;
  272. }
  273. System.out.println("ClassE assignable got " + instances[1].length + ", combined mask: " + mask);
  274. System.out.println("null assignable got " + instances[2].length);
  275. }
  276. private static class VMDebug {
  277. private static final Method startMethodTracingMethod;
  278. private static final Method stopMethodTracingMethod;
  279. private static final Method getMethodTracingModeMethod;
  280. private static final Method getRuntimeStatMethod;
  281. private static final Method getRuntimeStatsMethod;
  282. private static final Method countInstancesOfClassMethod;
  283. private static final Method countInstancesOfClassesMethod;
  284. private static final Method getInstancesOfClassesMethod;
  285. static {
  286. try {
  287. Class<?> c = Class.forName("dalvik.system.VMDebug");
  288. startMethodTracingMethod = c.getDeclaredMethod("startMethodTracing", String.class,
  289. Integer.TYPE, Integer.TYPE, Boolean.TYPE, Integer.TYPE);
  290. stopMethodTracingMethod = c.getDeclaredMethod("stopMethodTracing");
  291. getMethodTracingModeMethod = c.getDeclaredMethod("getMethodTracingMode");
  292. getRuntimeStatMethod = c.getDeclaredMethod("getRuntimeStat", String.class);
  293. getRuntimeStatsMethod = c.getDeclaredMethod("getRuntimeStats");
  294. countInstancesOfClassMethod = c.getDeclaredMethod("countInstancesOfClass",
  295. Class.class, Boolean.TYPE);
  296. countInstancesOfClassesMethod = c.getDeclaredMethod("countInstancesOfClasses",
  297. Class[].class, Boolean.TYPE);
  298. getInstancesOfClassesMethod = c.getDeclaredMethod("getInstancesOfClasses",
  299. Class[].class, Boolean.TYPE);
  300. } catch (Exception e) {
  301. throw new RuntimeException(e);
  302. }
  303. }
  304. public static void startMethodTracing(String filename, int bufferSize, int flags,
  305. boolean samplingEnabled, int intervalUs) throws Exception {
  306. startMethodTracingMethod.invoke(null, filename, bufferSize, flags, samplingEnabled,
  307. intervalUs);
  308. }
  309. public static void stopMethodTracing() throws Exception {
  310. stopMethodTracingMethod.invoke(null);
  311. }
  312. public static int getMethodTracingMode() throws Exception {
  313. return (int) getMethodTracingModeMethod.invoke(null);
  314. }
  315. public static String getRuntimeStat(String statName) throws Exception {
  316. return (String) getRuntimeStatMethod.invoke(null, statName);
  317. }
  318. public static Map<String, String> getRuntimeStats() throws Exception {
  319. return (Map<String, String>) getRuntimeStatsMethod.invoke(null);
  320. }
  321. public static long countInstancesofClass(Class<?> c, boolean assignable) throws Exception {
  322. return (long) countInstancesOfClassMethod.invoke(null, new Object[]{c, assignable});
  323. }
  324. public static long[] countInstancesofClasses(Class<?>[] classes, boolean assignable)
  325. throws Exception {
  326. return (long[]) countInstancesOfClassesMethod.invoke(
  327. null, new Object[]{classes, assignable});
  328. }
  329. public static Object[][] getInstancesOfClasses(Class<?>[] classes, boolean assignable) throws Exception {
  330. return (Object[][]) getInstancesOfClassesMethod.invoke(
  331. null, new Object[]{classes, assignable});
  332. }
  333. }
  334. }