private void createRuntimeProps(MemberStateImpl memberState) { Runtime runtime = Runtime.getRuntime(); ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); ClassLoadingMXBean clMxBean = ManagementFactory.getClassLoadingMXBean(); MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean(); MemoryUsage heapMemory = memoryMxBean.getHeapMemoryUsage(); MemoryUsage nonHeapMemory = memoryMxBean.getNonHeapMemoryUsage(); Map<String, Long> map = new HashMap<String, Long>(); map.put( "runtime.availableProcessors", Integer.valueOf(runtime.availableProcessors()).longValue()); map.put("date.startTime", runtimeMxBean.getStartTime()); map.put("seconds.upTime", runtimeMxBean.getUptime()); map.put("memory.maxMemory", runtime.maxMemory()); map.put("memory.freeMemory", runtime.freeMemory()); map.put("memory.totalMemory", runtime.totalMemory()); map.put("memory.heapMemoryMax", heapMemory.getMax()); map.put("memory.heapMemoryUsed", heapMemory.getUsed()); map.put("memory.nonHeapMemoryMax", nonHeapMemory.getMax()); map.put("memory.nonHeapMemoryUsed", nonHeapMemory.getUsed()); map.put("runtime.totalLoadedClassCount", clMxBean.getTotalLoadedClassCount()); map.put( "runtime.loadedClassCount", Integer.valueOf(clMxBean.getLoadedClassCount()).longValue()); map.put("runtime.unloadedClassCount", clMxBean.getUnloadedClassCount()); map.put("runtime.totalStartedThreadCount", threadMxBean.getTotalStartedThreadCount()); map.put("runtime.threadCount", Integer.valueOf(threadMxBean.getThreadCount()).longValue()); map.put( "runtime.peakThreadCount", Integer.valueOf(threadMxBean.getPeakThreadCount()).longValue()); map.put( "runtime.daemonThreadCount", Integer.valueOf(threadMxBean.getDaemonThreadCount()).longValue()); memberState.setRuntimeProps(map); }
// 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(); } }
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"); }
/** * A total hack to see if there are any active Java3D threads running * * @return any java3d threads running */ private static boolean anyJava3dThreadsActive() { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long[] ids = threadBean.getAllThreadIds(); for (int i = 0; i < ids.length; i++) { ThreadInfo info = threadBean.getThreadInfo(ids[i], Integer.MAX_VALUE); if (info == null) { continue; } if (info.getThreadState() != Thread.State.RUNNABLE) { continue; } if (info.getThreadName().indexOf("J3D") >= 0) { return true; } } return false; }
public static void main(String[] args) throws Exception { // TODO Auto-generated method stub if (args.length != 4) { System.err.println("Please provide process id zabbix-host zabbix-port host-guid"); System.exit(-1); } String processPid = args[0]; String zabbixHost = args[1]; String zabbixPort = args[2]; String hostGuid = args[3]; VirtualMachine vm = VirtualMachine.attach(processPid); String connectorAddr = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress"); if (connectorAddr == null) { String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar"; vm.loadAgent(agent); connectorAddr = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress"); } JMXServiceURL serviceURL = new JMXServiceURL(connectorAddr); JMXConnector connector = JMXConnectorFactory.connect(serviceURL); MBeanServerConnection mbsc = connector.getMBeanServerConnection(); ObjectName objName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME); Set<ObjectName> mbeans = mbsc.queryNames(objName, null); for (ObjectName name : mbeans) { ThreadMXBean threadBean; threadBean = ManagementFactory.newPlatformMXBeanProxy(mbsc, name.toString(), ThreadMXBean.class); long threadIds[] = threadBean.getAllThreadIds(); for (long threadId : threadIds) { ThreadInfo threadInfo = threadBean.getThreadInfo(threadId); System.out.println(threadInfo.getThreadName() + " / " + threadInfo.getThreadState()); } } }
private long getCPUTimeMillis() { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); return bean.getCurrentThreadCpuTime() / 1000000; }
synchronized Result formatSummary() { Result result = new Result(); ProxyClient proxyClient = vmPanel.getProxyClient(); if (proxyClient.isDead()) { return null; } buf = new StringBuilder(); append("<table cellpadding=1>"); try { RuntimeMXBean rmBean = proxyClient.getRuntimeMXBean(); CompilationMXBean cmpMBean = proxyClient.getCompilationMXBean(); ThreadMXBean tmBean = proxyClient.getThreadMXBean(); MemoryMXBean memoryBean = proxyClient.getMemoryMXBean(); ClassLoadingMXBean clMBean = proxyClient.getClassLoadingMXBean(); OperatingSystemMXBean osMBean = proxyClient.getOperatingSystemMXBean(); com.sun.management.OperatingSystemMXBean sunOSMBean = proxyClient.getSunOperatingSystemMXBean(); append("<tr><td colspan=4>"); append("<center><b>" + Messages.SUMMARY_TAB_TAB_NAME + "</b></center>"); String dateTime = headerDateTimeFormat.format(System.currentTimeMillis()); append("<center>" + dateTime + "</center>"); append(newDivider); { // VM info append(newLeftTable); append(Messages.CONNECTION_NAME, vmPanel.getDisplayName()); append( Messages.VIRTUAL_MACHINE, Resources.format( Messages.SUMMARY_TAB_VM_VERSION, rmBean.getVmName(), rmBean.getVmVersion())); append(Messages.VENDOR, rmBean.getVmVendor()); append(Messages.NAME, rmBean.getName()); append(endTable); append(newRightTable); result.upTime = rmBean.getUptime(); append(Messages.UPTIME, formatTime(result.upTime)); if (sunOSMBean != null) { result.processCpuTime = sunOSMBean.getProcessCpuTime(); append(Messages.PROCESS_CPU_TIME, formatNanoTime(result.processCpuTime)); } if (cmpMBean != null) { append(Messages.JIT_COMPILER, cmpMBean.getName()); append( Messages.TOTAL_COMPILE_TIME, cmpMBean.isCompilationTimeMonitoringSupported() ? formatTime(cmpMBean.getTotalCompilationTime()) : Messages.UNAVAILABLE); } else { append(Messages.JIT_COMPILER, Messages.UNAVAILABLE); } append(endTable); } append(newDivider); { // Threads and Classes append(newLeftTable); int tlCount = tmBean.getThreadCount(); int tdCount = tmBean.getDaemonThreadCount(); int tpCount = tmBean.getPeakThreadCount(); long ttCount = tmBean.getTotalStartedThreadCount(); String[] strings1 = formatLongs( tlCount, tpCount, tdCount, ttCount); append(Messages.LIVE_THREADS, strings1[0]); append(Messages.PEAK, strings1[1]); append(Messages.DAEMON_THREADS, strings1[2]); append(Messages.TOTAL_THREADS_STARTED, strings1[3]); append(endTable); append(newRightTable); long clCount = clMBean.getLoadedClassCount(); long cuCount = clMBean.getUnloadedClassCount(); long ctCount = clMBean.getTotalLoadedClassCount(); String[] strings2 = formatLongs(clCount, cuCount, ctCount); append(Messages.CURRENT_CLASSES_LOADED, strings2[0]); append(Messages.TOTAL_CLASSES_LOADED, strings2[2]); append(Messages.TOTAL_CLASSES_UNLOADED, strings2[1]); append(null, ""); append(endTable); } append(newDivider); { // Memory MemoryUsage u = memoryBean.getHeapMemoryUsage(); append(newLeftTable); String[] strings1 = formatKByteStrings(u.getUsed(), u.getMax()); append(Messages.CURRENT_HEAP_SIZE, strings1[0]); append(Messages.MAXIMUM_HEAP_SIZE, strings1[1]); append(endTable); append(newRightTable); String[] strings2 = formatKByteStrings(u.getCommitted()); append(Messages.COMMITTED_MEMORY, strings2[0]); append( Messages.SUMMARY_TAB_PENDING_FINALIZATION_LABEL, Messages.SUMMARY_TAB_PENDING_FINALIZATION_VALUE, memoryBean.getObjectPendingFinalizationCount()); append(endTable); append(newTable); Collection<GarbageCollectorMXBean> garbageCollectors = proxyClient.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean garbageCollectorMBean : garbageCollectors) { String gcName = garbageCollectorMBean.getName(); long gcCount = garbageCollectorMBean.getCollectionCount(); long gcTime = garbageCollectorMBean.getCollectionTime(); append( Messages.GARBAGE_COLLECTOR, Resources.format( Messages.GC_INFO, gcName, gcCount, (gcTime >= 0) ? formatTime(gcTime) : Messages.UNAVAILABLE), 4); } append(endTable); } append(newDivider); { // Operating System info append(newLeftTable); String osName = osMBean.getName(); String osVersion = osMBean.getVersion(); String osArch = osMBean.getArch(); result.nCPUs = osMBean.getAvailableProcessors(); append(Messages.OPERATING_SYSTEM, osName + " " + osVersion); append(Messages.ARCHITECTURE, osArch); append(Messages.NUMBER_OF_PROCESSORS, result.nCPUs + ""); if (pathSeparator == null) { // Must use separator of remote OS, not File.pathSeparator // from this local VM. In the future, consider using // RuntimeMXBean to get the remote system property. pathSeparator = osName.startsWith("Windows ") ? ";" : ":"; } if (sunOSMBean != null) { String[] kbStrings1 = formatKByteStrings(sunOSMBean.getCommittedVirtualMemorySize()); String[] kbStrings2 = formatKByteStrings( sunOSMBean.getTotalPhysicalMemorySize(), sunOSMBean.getFreePhysicalMemorySize(), sunOSMBean.getTotalSwapSpaceSize(), sunOSMBean.getFreeSwapSpaceSize()); append(Messages.COMMITTED_VIRTUAL_MEMORY, kbStrings1[0]); append(endTable); append(newRightTable); append(Messages.TOTAL_PHYSICAL_MEMORY, kbStrings2[0]); append(Messages.FREE_PHYSICAL_MEMORY, kbStrings2[1]); append(Messages.TOTAL_SWAP_SPACE, kbStrings2[2]); append(Messages.FREE_SWAP_SPACE, kbStrings2[3]); } append(endTable); } append(newDivider); { // VM arguments and paths append(newTable); String args = ""; java.util.List<String> inputArguments = rmBean.getInputArguments(); for (String arg : inputArguments) { args += arg + " "; } append(Messages.VM_ARGUMENTS, args, 4); append(Messages.CLASS_PATH, rmBean.getClassPath(), 4); append(Messages.LIBRARY_PATH, rmBean.getLibraryPath(), 4); append( Messages.BOOT_CLASS_PATH, rmBean.isBootClassPathSupported() ? rmBean.getBootClassPath() : Messages.UNAVAILABLE, 4); append(endTable); } } catch (IOException e) { if (JConsole.isDebug()) { e.printStackTrace(); } proxyClient.markAsDead(); return null; } catch (UndeclaredThrowableException e) { if (JConsole.isDebug()) { e.printStackTrace(); } proxyClient.markAsDead(); return null; } append("</table>"); result.timeStamp = System.currentTimeMillis(); result.summary = buf.toString(); return result; }