Search in sources :

Example 6 with J9DDRImageProcess

use of com.ibm.j9ddr.view.dtfj.image.J9DDRImageProcess 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);
}
Also used : JavaRuntime(com.ibm.dtfj.java.JavaRuntime) HashMap(java.util.HashMap) J9DDRImageProcess(com.ibm.j9ddr.view.dtfj.image.J9DDRImageProcess) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) JavaReference(com.ibm.dtfj.java.JavaReference) Iterator(java.util.Iterator) DataUnavailable(com.ibm.dtfj.image.DataUnavailable) ArrayList(java.util.ArrayList) List(java.util.List) IteratorFilter(com.ibm.j9ddr.util.IteratorHelpers.IteratorFilter) JavaObject(com.ibm.dtfj.java.JavaObject) JavaClass(com.ibm.dtfj.java.JavaClass) JavaObject(com.ibm.dtfj.java.JavaObject) Test(org.junit.Test)

Aggregations

J9DDRImageProcess (com.ibm.j9ddr.view.dtfj.image.J9DDRImageProcess)6 DataUnavailable (com.ibm.dtfj.image.DataUnavailable)3 CorruptDataException (com.ibm.j9ddr.CorruptDataException)3 DDRInteractiveCommandException (com.ibm.j9ddr.tools.ddrinteractive.DDRInteractiveCommandException)3 ICore (com.ibm.j9ddr.corereaders.ICore)2 IProcess (com.ibm.j9ddr.corereaders.memory.IProcess)2 J9JavaVMPointer (com.ibm.j9ddr.vm29.pointer.generated.J9JavaVMPointer)2 J9RASPointer (com.ibm.j9ddr.vm29.pointer.generated.J9RASPointer)2 CorruptDataException (com.ibm.dtfj.image.CorruptDataException)1 ImageModule (com.ibm.dtfj.image.ImageModule)1 JavaClass (com.ibm.dtfj.java.JavaClass)1 JavaObject (com.ibm.dtfj.java.JavaObject)1 JavaReference (com.ibm.dtfj.java.JavaReference)1 JavaRuntime (com.ibm.dtfj.java.JavaRuntime)1 ILibraryDependentCore (com.ibm.j9ddr.corereaders.ILibraryDependentCore)1 Platform (com.ibm.j9ddr.corereaders.Platform)1 IteratorFilter (com.ibm.j9ddr.util.IteratorHelpers.IteratorFilter)1 J9DDRStubImageThread (com.ibm.j9ddr.view.dtfj.image.J9DDRStubImageThread)1 DTFJJavaRuntime (com.ibm.j9ddr.vm29.view.dtfj.java.DTFJJavaRuntime)1 IOException (java.io.IOException)1