use of com.ibm.j9ddr.util.IteratorHelpers.IteratorFilter in project openj9 by eclipse.
the class JavaRuntimeTest method getHeapRootsTest.
@SuppressWarnings("unchecked")
@Test
public void getHeapRootsTest() {
boolean result = true;
String version = ((J9DDRImageProcess) ddrProcess).getVersion();
IteratorFilter ddrFilter = null;
if (version.equals("26")) {
ddrFilter = new IteratorFilter() {
// vm26 has a StringTable Cache that the root scanner scans but jextract(HeapIteratorAPI) does not
public boolean accept(Object obj) {
JavaReference ref = (JavaReference) obj;
if (ref.getDescription().equals("StringCacheTable")) {
return false;
}
return true;
}
};
}
if (version.equals("23")) {
ddrFilter = new IteratorFilter() {
boolean foundFirstContrivedThread = false;
public boolean accept(Object obj) {
try {
JavaReference ref = (JavaReference) obj;
Object ddrHeapRoot = ref.getTarget();
if (ref.getRootType() == JavaReference.HEAP_ROOT_THREAD) {
if (ddrHeapRoot instanceof JavaObject) {
JavaObject javaObject = (JavaObject) ddrHeapRoot;
String name = javaObject.getJavaClass().getName();
if (name.endsWith("Error") || name.endsWith("Exception") || name.equals("com/ibm/dtfj/tck/tests/javaruntime/TestJavaMonitor_ObjectMonitors$MonitorClass") || name.equals("com/ibm/dtfj/tck/harness/Configure$DumpThread")) {
// Ignore these since JExtract can't find them.
return false;
}
if (name.equals("com/ibm/dtfj/tck/tests/javaruntime/TestJavaThread_getStackFrames$ContrivedThread")) {
if (foundFirstContrivedThread) {
return false;
} else {
foundFirstContrivedThread = true;
}
}
}
}
return true;
} catch (DataUnavailable e) {
fail("bail");
return false;
} catch (CorruptDataException e) {
fail("bail");
return false;
}
}
};
}
for (int i = 0; i < ddrTestObjects.size(); i++) {
JavaRuntime localDDRRuntime = (JavaRuntime) ddrTestObjects.get(i);
JavaRuntime localJextractRuntime = (JavaRuntime) jextractTestObjects.get(i);
Object[] ddrHeapRootsArray = null;
Object[] jextractHeapRootsArray = null;
int threadCount = 0;
Iterator titer = localDDRRuntime.getThreads();
while (titer.hasNext()) {
threadCount++;
titer.next();
}
System.out.println("Thread count: " + threadCount);
try {
ddrHeapRootsArray = DTFJComparator.getList(Object.class, localDDRRuntime.getHeapRoots(), ddrFilter).toArray();
jextractHeapRootsArray = DTFJComparator.getList(Object.class, localJextractRuntime.getHeapRoots(), null).toArray();
} catch (InvalidObjectException e1) {
fail("Test setup failure");
}
Map<Long, List<Object>> map = new HashMap<Long, List<Object>>();
for (int j = 0; j < ddrHeapRootsArray.length; j++) {
long key = 0;
JavaReference ref = (JavaReference) ddrHeapRootsArray[j];
Object ddrHeapRoot = null;
try {
ddrHeapRoot = ref.getTarget();
} catch (DataUnavailable e) {
fail("bail");
} catch (CorruptDataException e) {
fail("bail");
}
if (ddrHeapRoot instanceof JavaObject) {
key = ((JavaObject) ddrHeapRoot).getID().getAddress();
} else if (ddrHeapRoot instanceof JavaClass) {
key = ((JavaClass) ddrHeapRoot).getID().getAddress();
} else {
fail("bail");
}
List bucket = map.get(key);
if (bucket == null) {
bucket = new ArrayList<Object>();
map.put(key, bucket);
}
bucket.add(ddrHeapRoot);
}
for (int j = 0; j < jextractHeapRootsArray.length; j++) {
JavaReference ref = (JavaReference) jextractHeapRootsArray[j];
Object heapRoot = null;
String name = null;
long key = 0;
try {
heapRoot = ref.getTarget();
if (heapRoot instanceof JavaObject) {
key = ((JavaObject) heapRoot).getID().getAddress();
name = "Instance of " + ((JavaObject) heapRoot).getJavaClass().getName();
} else if (heapRoot instanceof JavaClass) {
key = ((JavaClass) heapRoot).getID().getAddress();
name = ((JavaClass) heapRoot).getName();
} else {
fail("bail");
}
} catch (DataUnavailable e) {
fail("bail");
} catch (CorruptDataException e) {
fail("bail");
}
List bucket = map.get(key);
if (bucket == null) {
System.out.println(String.format("Jextract found %s at address: %s. Element #: %s that ddr does not know about", name, Long.toHexString(key), j));
result = false;
continue;
}
if (bucket.isEmpty()) {
fail("bail");
}
bucket.remove(0);
if (bucket.isEmpty()) {
map.remove(key);
}
}
// Whats left.
int count = 0;
Collection<List<Object>> remains = map.values();
if (remains.size() > 0) {
System.out.println("");
System.out.println("DDR found the following that JExtract did not know about");
}
Iterator<List<Object>> bucketIter = remains.iterator();
while (bucketIter.hasNext()) {
List<Object> bucket = bucketIter.next();
Iterator<Object> contentsIter = bucket.iterator();
while (contentsIter.hasNext()) {
Object obj = contentsIter.next();
System.out.println(String.format("%s) %s", ++count, obj));
}
}
}
assertTrue(result);
}
Aggregations