Search in sources :

Example 16 with JavaRuntime

use of com.ibm.dtfj.java.JavaRuntime in project openj9 by eclipse.

the class DTFJUnitTest method buildContext.

private static TestContext buildContext(ImageFactory imageFactory, File coreFile) throws IOException {
    TestContext ctx = new DTFJUnitTest.TestContext();
    Image image = imageFactory.getImage(coreFile);
    ctx.setImage(image);
    Iterator<?> addressSpaceIt = image.getAddressSpaces();
    while (addressSpaceIt.hasNext()) {
        Object asObj = addressSpaceIt.next();
        if (asObj instanceof CorruptData) {
            System.err.println("Corrupt AddressSpace returned: " + asObj);
        } else if (asObj instanceof ImageAddressSpace) {
            ImageAddressSpace as = (ImageAddressSpace) asObj;
            ctx.setSpace(as);
            Iterator<?> processIterator = as.getProcesses();
            while (processIterator.hasNext()) {
                Object processObj = processIterator.next();
                if (processObj instanceof CorruptData) {
                    System.err.println("Corrupt ImageProcess returned: " + asObj);
                } else if (processObj instanceof ImageProcess) {
                    ImageProcess process = (ImageProcess) processObj;
                    ctx.setProcess(process);
                    Iterator<?> runtimeIterator = process.getRuntimes();
                    while (runtimeIterator.hasNext()) {
                        Object runtimeObj = runtimeIterator.next();
                        if (runtimeObj instanceof CorruptData) {
                            System.err.println("Corrupt ImageProcess returned: " + asObj);
                        } else if (runtimeObj instanceof JavaRuntime) {
                            JavaRuntime runtime = (JavaRuntime) runtimeObj;
                            ctx.setRuntime(runtime);
                            return ctx;
                        } else {
                            throw new ClassCastException("Unexpected type from Runtime iterator: " + runtimeObj.getClass() + ": " + runtimeObj);
                        }
                    }
                } else {
                    throw new ClassCastException("Unexpected type from Process iterator: " + processObj.getClass() + ": " + processObj);
                }
            }
        } else {
            throw new ClassCastException("Unexpected type from AddressSpace iterator: " + asObj.getClass() + ": " + asObj);
        }
    }
    throw new RuntimeException("Could not find a Java Runtime");
}
Also used : ImageAddressSpace(com.ibm.dtfj.image.ImageAddressSpace) JavaRuntime(com.ibm.dtfj.java.JavaRuntime) ImageProcess(com.ibm.dtfj.image.ImageProcess) Iterator(java.util.Iterator) CorruptData(com.ibm.dtfj.image.CorruptData) Image(com.ibm.dtfj.image.Image)

Example 17 with JavaRuntime

use of com.ibm.dtfj.java.JavaRuntime 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)

Example 18 with JavaRuntime

use of com.ibm.dtfj.java.JavaRuntime in project openj9 by eclipse.

the class JavaRuntimeTest method getHeapsTest.

@Test
public void getHeapsTest() {
    Iterator<Object> iter = ((JavaRuntime) ddrTestObjects.get(0)).getHeaps();
    while (iter.hasNext()) {
        JavaHeap heap = (JavaHeap) iter.next();
        Iterator<Object> objectIter = heap.getObjects();
        int count = 0;
        while (objectIter.hasNext()) {
            // JavaObject javaObject = (JavaObject) objectIter.next();
            // System.out.println(javaObject.get);
            count++;
            objectIter.next();
        }
        System.out.println(heap.getName() + ": " + count);
    }
    javaHeapComparator.testComparatorIteratorEquals(ddrTestObjects, jextractTestObjects, "getHeaps", JavaHeap.class);
}
Also used : JavaRuntime(com.ibm.dtfj.java.JavaRuntime) JavaHeap(com.ibm.dtfj.java.JavaHeap) JavaObject(com.ibm.dtfj.java.JavaObject) Test(org.junit.Test)

Example 19 with JavaRuntime

use of com.ibm.dtfj.java.JavaRuntime in project openj9 by eclipse.

the class DeadlockCommand method doCommand.

public void doCommand() {
    SortedMap monitorNodes = new TreeMap();
    JavaRuntime jr = ctx.getRuntime();
    Iterator itMonitor = jr.getMonitors();
    int nodeListNum = 0;
    out.print("\n  deadlocks for runtime \n");
    // JVM dumps on Linux don't have all the image threads, so we now use JavaThreads
    while (itMonitor.hasNext()) {
        JavaMonitor monitor = (JavaMonitor) itMonitor.next();
        MonitorNode node = new MonitorNode(monitor);
        JavaThread owner = null;
        Long id = null;
        try {
            owner = monitor.getOwner();
        } catch (CorruptDataException e) {
            out.println("exception encountered while getting monitor owner: " + Exceptions.getCorruptDataExceptionString());
            return;
        }
        if (null == owner) {
            // the monitor's owner or the next monitor in a potential deadlock chain.
            continue;
        } else {
            JavaObject threadObject;
            try {
                threadObject = owner.getObject();
            } catch (CorruptDataException e) {
                out.println("exception encountered while getting owner's JavaObject: " + Exceptions.getCorruptDataExceptionString());
                return;
            }
            id = new Long(threadObject.getID().getAddress());
        }
        // Note: defect 133638, we used to give up here with an error if there was already
        // a monitor node in the table with the same key (thread). This is very common (a
        // thread owning multiple monitors). Longer term the intention is to replace this
        // algorithm with the one used in javacore, but for now we carry on to see if we can
        // still find a deadlock, with some node(s) discarded.
        monitorNodes.put(id, node);
    }
    // Step 1.b
    // Add the JUC locks, technically to find all of them you need to walk the whole
    // heap. But the active ones can be found by walking the thread list and looking
    // at the blocking objects. (Any others aren't blocking any threads anyway so aren't
    // interesting.
    Iterator itThread = jr.getThreads();
    while (itThread.hasNext()) {
        try {
            Object o = itThread.next();
            if (!(o instanceof JavaThread)) {
                continue;
            }
            JavaThread jt = (JavaThread) o;
            JavaThread owner = null;
            Long id = null;
            if ((jt.getState() & JavaThread.STATE_PARKED) != 0) {
                JavaObject lock = jt.getBlockingObject();
                MonitorNode node = new JUCMonitorNode(lock, jr);
                try {
                    owner = Utils.getParkBlockerOwner(lock, jr);
                    if (owner == null) {
                        continue;
                    }
                } catch (MemoryAccessException e) {
                    out.println("exception encountered while getting monitor owner: " + Exceptions.getCorruptDataExceptionString());
                    return;
                }
                JavaObject threadObject;
                try {
                    threadObject = owner.getObject();
                } catch (CorruptDataException e) {
                    out.println("exception encountered while getting owner's JavaObject: " + Exceptions.getCorruptDataExceptionString());
                    return;
                }
                id = new Long(threadObject.getID().getAddress());
                monitorNodes.put(id, node);
            }
        } catch (CorruptDataException cde) {
            out.println("\nwarning, corrupt data encountered during scan for java.util.concurrent locks...");
        } catch (DataUnavailable du) {
            out.println("\nwarning, data unavailable encountered during scan for java.util.concurrent locks...");
        }
    }
    Iterator values = monitorNodes.values().iterator();
    // enter waiter, set that waiter's MonitorNode's waitingOn to m1.
    while (values.hasNext()) {
        MonitorNode currNode = (MonitorNode) values.next();
        Iterator itWaiters = currNode.getEnterWaiters();
        while (itWaiters.hasNext()) {
            Object o = itWaiters.next();
            if (!(o instanceof JavaThread)) {
                continue;
            }
            JavaThread waiter = (JavaThread) o;
            JavaObject threadObject;
            Long id = null;
            try {
                threadObject = waiter.getObject();
            } catch (CorruptDataException e) {
                out.println("exception encountered while getting waiter's ImageThread: " + Exceptions.getCorruptDataExceptionString());
                return;
            }
            id = new Long(threadObject.getID().getAddress());
            MonitorNode waiterNode = (MonitorNode) monitorNodes.get(id);
            if (null != waiterNode) {
                waiterNode.waitingOn = currNode;
            }
        }
    }
    values = monitorNodes.values().iterator();
    int visit = 1;
    Vector lists = new Vector();
    while (values.hasNext()) {
        MonitorNode startNode = (MonitorNode) values.next();
        MonitorNode currNode = startNode;
        MonitorNode endNode;
        if (0 != startNode.visit) {
            continue;
        }
        while (true) {
            currNode.visit = visit;
            if (null == currNode.waitingOn) {
                currNode.deadlock = MonitorNode.NO_DEADLOCK;
                break;
            }
            if (isDeadlocked(currNode.waitingOn)) {
                // we've encountered a deadlocked node in the chain;
                // set branch deadlock for all nodes between startNode
                // and currNode
                endNode = currNode.waitingOn;
                currNode = startNode;
                NodeList branchList = null;
                while (currNode != endNode) {
                    if (null == branchList) {
                        branchList = new NodeList(currNode, nodeListNum++);
                    }
                    currNode.deadlock = MonitorNode.BRANCH_DEADLOCK;
                    currNode = currNode.waitingOn;
                    branchList.add(currNode);
                    if (currNode != endNode)
                        currNode.inList = branchList;
                }
                if (endNode.inList.isLoop()) {
                    lists.insertElementAt(branchList, lists.indexOf(endNode.inList));
                } else {
                    NodeList oldList = endNode.inList;
                    // FIXME: the below line will cause problems with at least
                    // one case that was not considered when attachOrSplit was
                    // coded: if a NodeList n1 has already been split and another
                    // NodeList n2 tries to attach to the end of n1, then n1 will
                    // allow n2 to attach to n1, while what n1 should really do is
                    // just return n2 and not allow n2 to attach to itself
                    NodeList split = endNode.inList.attachOrSplit(branchList, nodeListNum++);
                    if (null != split) {
                        lists.insertElementAt(split, lists.indexOf(oldList));
                        lists.insertElementAt(branchList, lists.indexOf(oldList));
                    }
                }
                break;
            }
            if (currNode.waitingOn.visit == visit) {
                // we've encountered a node in the same visit as the current
                // visit, ie. we've found a loop; first flag the whole loop
                // with a loop deadlock flag, then flag the rest of the nodes
                // in the chain with a branch deadlock
                endNode = currNode.waitingOn;
                currNode = endNode;
                NodeList loopList = new NodeList(currNode, nodeListNum++);
                lists.insertElementAt(loopList, 0);
                do {
                    currNode.deadlock = MonitorNode.LOOP_DEADLOCK;
                    currNode = currNode.waitingOn;
                    loopList.add(currNode);
                    currNode.inList = loopList;
                } while (currNode != endNode);
                currNode = startNode;
                NodeList branchList = null;
                while (currNode != endNode) {
                    if (null == branchList) {
                        branchList = new NodeList(currNode, nodeListNum++);
                        lists.insertElementAt(branchList, 0);
                    }
                    currNode.deadlock = MonitorNode.BRANCH_DEADLOCK;
                    currNode = currNode.waitingOn;
                    branchList.add(currNode);
                    if (currNode != endNode)
                        currNode.inList = branchList;
                }
                break;
            }
            currNode = currNode.waitingOn;
        }
        visit++;
    }
    if (lists.isEmpty()) {
        out.print("\n");
        out.print("\t no deadlocks detected");
        out.print("\n");
        return;
    }
    boolean lastListWasLoop = true;
    Iterator itList = lists.iterator();
    // Step 5. print the lists
    while (itList.hasNext()) {
        NodeList list = (NodeList) itList.next();
        if (list.isLoop()) {
            out.print("\n    deadlock loop:\n");
            lastListWasLoop = true;
        } else if (lastListWasLoop) {
            // && !list.isLoop()
            out.print("\n\n    deadlock branch(es):\n");
            lastListWasLoop = false;
        }
        out.print("\t  " + list.toString());
        out.print("\n");
    }
    out.print("\n");
}
Also used : JavaRuntime(com.ibm.dtfj.java.JavaRuntime) NodeList(com.ibm.jvm.dtfjview.commands.helpers.NodeList) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) TreeMap(java.util.TreeMap) JUCMonitorNode(com.ibm.jvm.dtfjview.commands.helpers.JUCMonitorNode) JavaMonitor(com.ibm.dtfj.java.JavaMonitor) JUCMonitorNode(com.ibm.jvm.dtfjview.commands.helpers.JUCMonitorNode) MonitorNode(com.ibm.jvm.dtfjview.commands.helpers.MonitorNode) JavaObject(com.ibm.dtfj.java.JavaObject) SortedMap(java.util.SortedMap) Iterator(java.util.Iterator) JavaThread(com.ibm.dtfj.java.JavaThread) DataUnavailable(com.ibm.dtfj.image.DataUnavailable) JavaObject(com.ibm.dtfj.java.JavaObject) MemoryAccessException(com.ibm.dtfj.image.MemoryAccessException) Vector(java.util.Vector)

Example 20 with JavaRuntime

use of com.ibm.dtfj.java.JavaRuntime in project openj9 by eclipse.

the class HeapdumpCommand method doCommand.

public void doCommand(String[] args) {
    Set heapsToDump = new HashSet();
    _numberOfObjects = 0;
    _numberOfErrors = 0;
    _numberOfClasses = 0;
    if (ctx.hasPropertyBeenSet(VERBOSE_MODE_PROPERTY)) {
        _verbose = true;
    }
    JavaRuntime runtime = ctx.getRuntime();
    while (runtime != null) {
        ImageAddressSpace addressSpace = null;
        try {
            addressSpace = runtime.getJavaVM().getAddressSpace();
        } catch (CorruptDataException e) {
        }
        if (addressSpace == null) {
            out.println("Couldn't get handle on address space");
            break;
        }
        if (!heapArgumentsAreValid(runtime, heapsToDump)) {
            break;
        }
        String version = getVersionString(runtime);
        if (version.contains("IBM J9 2.3") || version.contains("IBM J9 2.4") || version.contains("IBM J9 2.5")) {
            // J9 JVMs versions prior to 2.6 have 16-bit hashcodes, later JVMs have 32-bit hashcodes
            _is32BitHash = false;
        } else {
            _is32BitHash = true;
        }
        boolean is64Bit = addressSpace.getCurrentProcess().getPointerSize() == 64;
        String filename = HeapDumpSettings.getFileName(ctx.getProperties());
        boolean phdFormat = HeapDumpSettings.areHeapDumpsPHD(ctx.getProperties());
        try {
            if (HeapDumpSettings.multipleHeapsInMultipleFiles(ctx.getProperties())) {
                dumpMultipleHeapsInSeparateFiles(runtime, version, is64Bit, phdFormat, filename, heapsToDump);
            } else {
                dumpMultipleHeapsInOneFile(runtime, version, is64Bit, phdFormat, filename, heapsToDump);
            }
            if (_numberOfErrors == 0) {
                out.print("\nSuccessfully wrote " + _numberOfObjects + " objects and " + _numberOfClasses + " classes\n");
            } else {
                out.print("\nWrote " + _numberOfObjects + " objects and " + _numberOfClasses + " classes and encountered " + _numberOfErrors + " errors." + "\n");
            }
        } catch (IOException ex) {
            out.println("I/O error writing dump:\n");
            StringWriter writer = new StringWriter();
            ex.printStackTrace(new PrintWriter(writer));
            out.println(writer.toString());
        }
        runtime = null;
    }
}
Also used : ImageAddressSpace(com.ibm.dtfj.image.ImageAddressSpace) JavaRuntime(com.ibm.dtfj.java.JavaRuntime) HashSet(java.util.HashSet) Set(java.util.Set) StringWriter(java.io.StringWriter) CorruptDataException(com.ibm.dtfj.image.CorruptDataException) IOException(java.io.IOException) HashSet(java.util.HashSet) PrintWriter(java.io.PrintWriter)

Aggregations

JavaRuntime (com.ibm.dtfj.java.JavaRuntime)27 Iterator (java.util.Iterator)13 JavaObject (com.ibm.dtfj.java.JavaObject)10 CorruptDataException (com.ibm.dtfj.image.CorruptDataException)8 ImageAddressSpace (com.ibm.dtfj.image.ImageAddressSpace)8 ImageProcess (com.ibm.dtfj.image.ImageProcess)8 CorruptData (com.ibm.dtfj.image.CorruptData)7 JavaClass (com.ibm.dtfj.java.JavaClass)6 DataUnavailable (com.ibm.dtfj.image.DataUnavailable)5 Image (com.ibm.dtfj.image.Image)5 JavaHeap (com.ibm.dtfj.java.JavaHeap)5 JavaThread (com.ibm.dtfj.java.JavaThread)5 File (java.io.File)4 HashMap (java.util.HashMap)4 ImageFactory (com.ibm.dtfj.image.ImageFactory)3 JavaClassLoader (com.ibm.dtfj.java.JavaClassLoader)3 JavaMonitor (com.ibm.dtfj.java.JavaMonitor)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ImageSection (com.ibm.dtfj.image.ImageSection)2