// Return thread IDs of deadlocked threads or null if any. // It finds deadlocks involving only monitors if it's a Tiger VM. // Otherwise, it finds deadlocks involving both monitors and // the concurrent locks. public long[] findDeadlockedThreads() throws IOException { ThreadMXBean tm = getThreadMXBean(); if (supportsLockUsage && tm.isSynchronizerUsageSupported()) { return tm.findDeadlockedThreads(); } else { return tm.findMonitorDeadlockedThreads(); } }
private long[] findDeadlockedThreads(ThreadMXBean mbean) { // JDK 1.5 only supports the findMonitorDeadlockedThreads() // method, so you need to comment out the following three lines if (mbean.isSynchronizerUsageSupported()) { return mbean.findDeadlockedThreads(); } return mbean.findMonitorDeadlockedThreads(); }
@IgnoreJRERequirement public static ThreadInfo[] getThreadInfos() { ThreadMXBean mbean = ManagementFactory.getThreadMXBean(); return mbean.getThreadInfo( mbean.getAllThreadIds(), mbean.isObjectMonitorUsageSupported(), mbean.isSynchronizerUsageSupported()); }
public static String threadDump() { StringBuilder threadDump = new StringBuilder(); ThreadMXBean threadMx = ManagementFactory.getThreadMXBean(); if (threadMx.isObjectMonitorUsageSupported() && threadMx.isSynchronizerUsageSupported()) { // Print lock info if, and only if, both object monitor usage and synchronizer usage are // supported. dumpThreadInfo(threadDump, true, threadMx); } else { dumpThreadInfo(threadDump, false, threadMx); } return threadDump.toString(); }
public static void main(String[] argv) throws Exception { mbean = newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, ThreadMXBean.class); if (!mbean.isSynchronizerUsageSupported()) { System.out.println("Monitoring of synchronizer usage not supported"); return; } thread.setDaemon(true); thread.start(); // wait until myThread acquires mutex and lock owner is set. while (!(mutex.isLocked() && mutex.getLockOwner() == thread)) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } } long[] ids = new long[] {thread.getId()}; // validate the local access ThreadInfo[] infos = getThreadMXBean().getThreadInfo(ids, true, true); if (infos.length != 1) { throw new RuntimeException( "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1."); } thread.checkThreadInfo(infos[0]); // validate the remote access infos = mbean.getThreadInfo(ids, true, true); if (infos.length != 1) { throw new RuntimeException( "Returned ThreadInfo[] of length=" + infos.length + ". Expected to be 1."); } thread.checkThreadInfo(infos[0]); boolean found = false; infos = mbean.dumpAllThreads(true, true); for (ThreadInfo ti : infos) { if (ti.getThreadId() == thread.getId()) { thread.checkThreadInfo(ti); found = true; } } if (!found) { throw new RuntimeException("No ThreadInfo found for MyThread"); } System.out.println("Test passed"); }
/* (non-Javadoc) * @see org.epics.pvaccess.server.plugins.BeaconServerStatusProvider#getServerStatusData() */ public PVField getServerStatusData() { status .getIntField("connections") .put(context.getTransportRegistry().numberOfActiveTransports()); status.getLongField("allocatedMemory").put(Runtime.getRuntime().totalMemory()); status.getLongField("freeMemory").put(Runtime.getRuntime().freeMemory()); ThreadMXBean threadMBean = ManagementFactory.getThreadMXBean(); status.getIntField("threads").put(threadMBean.getThreadCount()); final long[] deadlocks = threadMBean.isSynchronizerUsageSupported() ? threadMBean.findDeadlockedThreads() : threadMBean.findMonitorDeadlockedThreads(); status.getIntField("deadlocks").put((deadlocks != null) ? deadlocks.length : 0); OperatingSystemMXBean osMBean = ManagementFactory.getOperatingSystemMXBean(); status.getDoubleField("averageSystemLoad").put(osMBean.getSystemLoadAverage()); return status; }
/** * Retrieve a JVM information text formatted using the given StringManager. * * @param requestedSm the StringManager to use * @return the formatted JVM information text */ private static String getVMInfo(StringManager requestedSm) { StringBuilder sb = new StringBuilder(); synchronized (timeformat) { sb.append(timeformat.format(new Date())); } sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoRuntime")); sb.append(":" + CRLF); sb.append(INDENT1 + "vmName: " + runtimeMXBean.getVmName() + CRLF); sb.append(INDENT1 + "vmVersion: " + runtimeMXBean.getVmVersion() + CRLF); sb.append(INDENT1 + "vmVendor: " + runtimeMXBean.getVmVendor() + CRLF); sb.append(INDENT1 + "specName: " + runtimeMXBean.getSpecName() + CRLF); sb.append(INDENT1 + "specVersion: " + runtimeMXBean.getSpecVersion() + CRLF); sb.append(INDENT1 + "specVendor: " + runtimeMXBean.getSpecVendor() + CRLF); sb.append( INDENT1 + "managementSpecVersion: " + runtimeMXBean.getManagementSpecVersion() + CRLF); sb.append(INDENT1 + "name: " + runtimeMXBean.getName() + CRLF); sb.append(INDENT1 + "startTime: " + runtimeMXBean.getStartTime() + CRLF); sb.append(INDENT1 + "uptime: " + runtimeMXBean.getUptime() + CRLF); sb.append( INDENT1 + "isBootClassPathSupported: " + runtimeMXBean.isBootClassPathSupported() + CRLF); sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoOs")); sb.append(":" + CRLF); sb.append(INDENT1 + "name: " + operatingSystemMXBean.getName() + CRLF); sb.append(INDENT1 + "version: " + operatingSystemMXBean.getVersion() + CRLF); sb.append(INDENT1 + "architecture: " + operatingSystemMXBean.getArch() + CRLF); sb.append( INDENT1 + "availableProcessors: " + operatingSystemMXBean.getAvailableProcessors() + CRLF); sb.append( INDENT1 + "systemLoadAverage: " + operatingSystemMXBean.getSystemLoadAverage() + CRLF); sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoThreadMxBean")); sb.append(":" + CRLF); sb.append( INDENT1 + "isCurrentThreadCpuTimeSupported: " + threadMXBean.isCurrentThreadCpuTimeSupported() + CRLF); sb.append( INDENT1 + "isThreadCpuTimeSupported: " + threadMXBean.isThreadCpuTimeSupported() + CRLF); sb.append(INDENT1 + "isThreadCpuTimeEnabled: " + threadMXBean.isThreadCpuTimeEnabled() + CRLF); sb.append( INDENT1 + "isObjectMonitorUsageSupported: " + threadMXBean.isObjectMonitorUsageSupported() + CRLF); sb.append( INDENT1 + "isSynchronizerUsageSupported: " + threadMXBean.isSynchronizerUsageSupported() + CRLF); sb.append( INDENT1 + "isThreadContentionMonitoringSupported: " + threadMXBean.isThreadContentionMonitoringSupported() + CRLF); sb.append( INDENT1 + "isThreadContentionMonitoringEnabled: " + threadMXBean.isThreadContentionMonitoringEnabled() + CRLF); sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoThreadCounts")); sb.append(":" + CRLF); sb.append(INDENT1 + "daemon: " + threadMXBean.getDaemonThreadCount() + CRLF); sb.append(INDENT1 + "total: " + threadMXBean.getThreadCount() + CRLF); sb.append(INDENT1 + "peak: " + threadMXBean.getPeakThreadCount() + CRLF); sb.append(INDENT1 + "totalStarted: " + threadMXBean.getTotalStartedThreadCount() + CRLF); sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoStartup")); sb.append(":" + CRLF); for (String arg : runtimeMXBean.getInputArguments()) { sb.append(INDENT1 + arg + CRLF); } sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoPath")); sb.append(":" + CRLF); sb.append(INDENT1 + "bootClassPath: " + runtimeMXBean.getBootClassPath() + CRLF); sb.append(INDENT1 + "classPath: " + runtimeMXBean.getClassPath() + CRLF); sb.append(INDENT1 + "libraryPath: " + runtimeMXBean.getLibraryPath() + CRLF); sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoClassLoading")); sb.append(":" + CRLF); sb.append(INDENT1 + "loaded: " + classLoadingMXBean.getLoadedClassCount() + CRLF); sb.append(INDENT1 + "unloaded: " + classLoadingMXBean.getUnloadedClassCount() + CRLF); sb.append(INDENT1 + "totalLoaded: " + classLoadingMXBean.getTotalLoadedClassCount() + CRLF); sb.append(INDENT1 + "isVerbose: " + classLoadingMXBean.isVerbose() + CRLF); sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoClassCompilation")); sb.append(":" + CRLF); sb.append(INDENT1 + "name: " + compilationMXBean.getName() + CRLF); sb.append( INDENT1 + "totalCompilationTime: " + compilationMXBean.getTotalCompilationTime() + CRLF); sb.append( INDENT1 + "isCompilationTimeMonitoringSupported: " + compilationMXBean.isCompilationTimeMonitoringSupported() + CRLF); sb.append(CRLF); for (MemoryManagerMXBean mbean : memoryManagerMXBeans) { sb.append(requestedSm.getString("diagnostics.vmInfoMemoryManagers", mbean.getName())); sb.append(":" + CRLF); sb.append(INDENT1 + "isValid: " + mbean.isValid() + CRLF); sb.append(INDENT1 + "mbean.getMemoryPoolNames: " + CRLF); String[] names = mbean.getMemoryPoolNames(); Arrays.sort(names); for (String name : names) { sb.append(INDENT2 + name + CRLF); } sb.append(CRLF); } for (GarbageCollectorMXBean mbean : garbageCollectorMXBeans) { sb.append(requestedSm.getString("diagnostics.vmInfoGarbageCollectors", mbean.getName())); sb.append(":" + CRLF); sb.append(INDENT1 + "isValid: " + mbean.isValid() + CRLF); sb.append(INDENT1 + "mbean.getMemoryPoolNames: " + CRLF); String[] names = mbean.getMemoryPoolNames(); Arrays.sort(names); for (String name : names) { sb.append(INDENT2 + name + CRLF); } sb.append(INDENT1 + "getCollectionCount: " + mbean.getCollectionCount() + CRLF); sb.append(INDENT1 + "getCollectionTime: " + mbean.getCollectionTime() + CRLF); sb.append(CRLF); } sb.append(requestedSm.getString("diagnostics.vmInfoMemory")); sb.append(":" + CRLF); sb.append(INDENT1 + "isVerbose: " + memoryMXBean.isVerbose() + CRLF); sb.append( INDENT1 + "getObjectPendingFinalizationCount: " + memoryMXBean.getObjectPendingFinalizationCount() + CRLF); sb.append(formatMemoryUsage("heap", memoryMXBean.getHeapMemoryUsage())); sb.append(formatMemoryUsage("non-heap", memoryMXBean.getNonHeapMemoryUsage())); sb.append(CRLF); for (MemoryPoolMXBean mbean : memoryPoolMXBeans) { sb.append(requestedSm.getString("diagnostics.vmInfoMemoryPools", mbean.getName())); sb.append(":" + CRLF); sb.append(INDENT1 + "isValid: " + mbean.isValid() + CRLF); sb.append(INDENT1 + "getType: " + mbean.getType() + CRLF); sb.append(INDENT1 + "mbean.getMemoryManagerNames: " + CRLF); String[] names = mbean.getMemoryManagerNames(); Arrays.sort(names); for (String name : names) { sb.append(INDENT2 + name + CRLF); } sb.append(INDENT1 + "isUsageThresholdSupported: " + mbean.isUsageThresholdSupported() + CRLF); try { sb.append(INDENT1 + "isUsageThresholdExceeded: " + mbean.isUsageThresholdExceeded() + CRLF); } catch (UnsupportedOperationException ex) { // IGNORE } sb.append( INDENT1 + "isCollectionUsageThresholdSupported: " + mbean.isCollectionUsageThresholdSupported() + CRLF); try { sb.append( INDENT1 + "isCollectionUsageThresholdExceeded: " + mbean.isCollectionUsageThresholdExceeded() + CRLF); } catch (UnsupportedOperationException ex) { // IGNORE } try { sb.append(INDENT1 + "getUsageThreshold: " + mbean.getUsageThreshold() + CRLF); } catch (UnsupportedOperationException ex) { // IGNORE } try { sb.append(INDENT1 + "getUsageThresholdCount: " + mbean.getUsageThresholdCount() + CRLF); } catch (UnsupportedOperationException ex) { // IGNORE } try { sb.append( INDENT1 + "getCollectionUsageThreshold: " + mbean.getCollectionUsageThreshold() + CRLF); } catch (UnsupportedOperationException ex) { // IGNORE } try { sb.append( INDENT1 + "getCollectionUsageThresholdCount: " + mbean.getCollectionUsageThresholdCount() + CRLF); } catch (UnsupportedOperationException ex) { // IGNORE } sb.append(formatMemoryUsage("current", mbean.getUsage())); sb.append(formatMemoryUsage("collection", mbean.getCollectionUsage())); sb.append(formatMemoryUsage("peak", mbean.getPeakUsage())); sb.append(CRLF); } sb.append(requestedSm.getString("diagnostics.vmInfoSystem")); sb.append(":" + CRLF); Map<String, String> props = runtimeMXBean.getSystemProperties(); ArrayList<String> keys = new ArrayList<String>(props.keySet()); Collections.sort(keys); for (String prop : keys) { sb.append(INDENT1 + prop + ": " + props.get(prop) + CRLF); } sb.append(CRLF); sb.append(requestedSm.getString("diagnostics.vmInfoLogger")); sb.append(":" + CRLF); List<String> loggers = loggingMXBean.getLoggerNames(); Collections.sort(loggers); for (String logger : loggers) { sb.append( INDENT1 + logger + ": level=" + loggingMXBean.getLoggerLevel(logger) + ", parent=" + loggingMXBean.getParentLoggerName(logger) + CRLF); } sb.append(CRLF); return sb.toString(); }