privatestaticint doSomeCpuWork(int n) { int numFactors = 0; for (long i = 2; i * i <= n; i++) { if (n % i == 0) {
numFactors++;
}
} return numFactors;
}
try {
VMDebug.startMethodTracingDdms(1000, 0, false, 0);
System.out.println("Should have thrown an exception");
} catch (Exception e) {
System.out.println("Got expected exception");
}
System.out.println("Test sampling with bogus (<= 0) interval"); try {
VMDebug.startMethodTracing(tempFileName, 0, 0, true, 0);
System.out.println("Should have thrown an exception");
} catch (Exception e) {
System.out.println("Got expected exception");
} try {
VMDebug.startMethodTracingDdms(0, 0, true, 0);
System.out.println("Should have thrown an exception");
} catch (Exception e) {
System.out.println("Got expected exception");
}
tempFile.delete();
}
privatestaticvoid checkNumber(String s) throws Exception { if (s == null) {
System.out.println("Got null string"); return;
} long n = Long.parseLong(s); if (n < 0) {
System.out.println("Got negative number " + n);
}
}
privatestaticvoid checkBiggerThanZero(int i) throws Exception { if (i <= 0) {
System.out.println("Got zero or smaller " + i);
}
}
privatestaticvoid checkZero(int i) throws Exception { if (i != 0) {
System.out.println("Got non-zero result after reset " + i);
}
}
privatestaticvoid checkHistogram(String s) throws Exception { if (s == null || s.length() == 0) {
System.out.println("Got null or empty string"); return;
}
String[] buckets = s.split(","); long last_key = 0; for (int i = 0; i < buckets.length; ++i) {
String bucket = buckets[i]; if (bucket.length() == 0) {
System.out.println("Got empty bucket"); continue;
}
String[] kv = bucket.split(":"); if (kv.length != 2 || kv[0].length() == 0 || kv[1].length() == 0) {
System.out.println("Got bad bucket " + bucket); continue;
} long key = Long.parseLong(kv[0]); long value = Long.parseLong(kv[1]); if (key < 0 || value < 0) {
System.out.println("Got negative key or value " + bucket); continue;
} if (key < last_key) {
System.out.println("Got decreasing key " + bucket); continue;
}
last_key = key;
}
}
privatestaticvoid testRuntimeStat() throws Exception { // Invoke at least one GC and wait for 20 seconds or so so we get at // least one bucket in the histograms. for (int i = 0; i < 20; ++i) {
Runtime.getRuntime().gc(); Thread.sleep(1000L);
}
String gc_count = VMDebug.getRuntimeStat("art.gc.gc-count");
String gc_time = VMDebug.getRuntimeStat("art.gc.gc-time");
String bytes_allocated = VMDebug.getRuntimeStat("art.gc.bytes-allocated");
String bytes_freed = VMDebug.getRuntimeStat("art.gc.bytes-freed");
String blocking_gc_count = VMDebug.getRuntimeStat("art.gc.blocking-gc-count");
String blocking_gc_time = VMDebug.getRuntimeStat("art.gc.blocking-gc-time");
String gc_count_rate_histogram = VMDebug.getRuntimeStat("art.gc.gc-count-rate-histogram");
String blocking_gc_count_rate_histogram =
VMDebug.getRuntimeStat("art.gc.blocking-gc-count-rate-histogram");
checkNumber(gc_count);
checkNumber(gc_time);
checkNumber(bytes_allocated);
checkNumber(bytes_freed);
checkNumber(blocking_gc_count);
checkNumber(blocking_gc_time);
checkHistogram(gc_count_rate_histogram);
checkHistogram(blocking_gc_count_rate_histogram);
}
privatestaticvoid testRuntimeStats() throws Exception { // Invoke at least one GC and wait for 20 seconds or so so we get at // least one bucket in the histograms. for (int i = 0; i < 20; ++i) {
Runtime.getRuntime().gc(); Thread.sleep(1000L);
}
Map<String, String> map = VMDebug.getRuntimeStats();
String gc_count = map.get("art.gc.gc-count");
String gc_time = map.get("art.gc.gc-time");
String bytes_allocated = map.get("art.gc.bytes-allocated");
String bytes_freed = map.get("art.gc.bytes-freed");
String blocking_gc_count = map.get("art.gc.blocking-gc-count");
String blocking_gc_time = map.get("art.gc.blocking-gc-time");
String gc_count_rate_histogram = map.get("art.gc.gc-count-rate-histogram");
String blocking_gc_count_rate_histogram =
map.get("art.gc.blocking-gc-count-rate-histogram");
checkNumber(gc_count);
checkNumber(gc_time);
checkNumber(bytes_allocated);
checkNumber(bytes_freed);
checkNumber(blocking_gc_count);
checkNumber(blocking_gc_time);
checkHistogram(gc_count_rate_histogram);
checkHistogram(blocking_gc_count_rate_histogram);
}
ClassA a1 = new ClassA();
Object obj1 = new Object();
Runtime.getRuntime().gc();
int alloc_objects = VMDebug.getAllocCount(KIND_ALLOCATED_OBJECTS); int alloc_bytes = VMDebug.getAllocCount(KIND_ALLOCATED_BYTES); int freed_objects = VMDebug.getAllocCount(KIND_FREED_OBJECTS); int freed_bytes = VMDebug.getAllocCount(KIND_FREED_BYTES);
checkBiggerThanZero(alloc_objects);
checkBiggerThanZero(alloc_bytes);
checkBiggerThanZero(freed_objects);
checkBiggerThanZero(freed_bytes);
privatestaticvoid testDebuggerDetails() throws Exception { long start_cpu_time = VMDebug.threadCpuTimeNanos(); boolean debugger_connected = VMDebug.isDebuggerConnected(); boolean debugging_enabled = VMDebug.isDebuggingEnabled(); long last_activity = VMDebug.lastDebuggerActivity(); if (debugger_connected && last_activity < 0) {
System.out.println("Last debugging activity expected but not found");
} if (!debugger_connected && last_activity != -1) {
System.out.println("Found unexpected last activity");
}
VMDebug.dumpHprofDataDdms();
VMDebug.dumpReferenceTables(); // According to the spec the returned value is only meaningful when compared with a previous // value. So make sure there is some work done between the two calls. for (int i = 0; i < 1000; i++) { int n = ThreadLocalRandom.current().nextInt(10000, 1000000); int num_factors = doSomeCpuWork(n); // A check so compiler doesn't optimize away the cpu work as dead code. if (num_factors > n/2) {
System.out.println("Should never happen!"); break;
}
} long end_cpu_time = VMDebug.threadCpuTimeNanos(); // The threadCpuTimeNanos is allowed to return negative values. According to the spec -1 is // a special value to indicate a failure. While we don't have explicit checks for this it // should be usually valid. We shouldn't return negative values from our implementation. if (end_cpu_time == -1 && start_cpu_time == -1) {
System.out.println("CPU time is not supported "
+ "start_time: " + start_cpu_time + "end_time: " + end_cpu_time);
} long cpu_time = end_cpu_time - start_cpu_time; if (cpu_time <= 0) {
System.out.println("Incorrect CPU thread time " + cpu_time + "start_time: "
+ start_cpu_time + "end_time: " + end_cpu_time);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.