Path: blob/master/test/jdk/javax/management/mxbean/MXBeanInteropTest1.java
41149 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 805886526* @summary Test all MXBeans available by default on the platform27* @author Olivier Lagneau28* @modules java.management.rmi29* @library /lib/testlibrary30* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanInteropTest131*/3233import java.util.Arrays;34import java.util.Iterator;35import java.util.Map;36import java.util.Set;3738import java.lang.management.ClassLoadingMXBean;39import java.lang.management.CompilationMXBean;40import java.lang.management.GarbageCollectorMXBean;41import java.lang.management.ManagementFactory;42import java.lang.management.MemoryMXBean;43import java.lang.management.MemoryManagerMXBean;44import java.lang.management.MemoryPoolMXBean;45import java.lang.management.OperatingSystemMXBean;46import java.lang.management.RuntimeMXBean;47import java.lang.management.ThreadMXBean;4849import javax.management.JMX;50import javax.management.MBeanAttributeInfo;51import javax.management.MBeanConstructorInfo;52import javax.management.MBeanServer;53import javax.management.MBeanServerFactory;54import javax.management.MBeanInfo;55import javax.management.MBeanNotificationInfo;56import javax.management.MBeanOperationInfo;57import javax.management.MBeanServerConnection;58import javax.management.ObjectName;59import javax.management.remote.JMXConnector;60import javax.management.remote.JMXConnectorFactory;61import javax.management.remote.JMXConnectorServer;62import javax.management.remote.JMXConnectorServerFactory;63import javax.management.remote.JMXServiceURL;6465public class MXBeanInteropTest1 {6667/*68* First Debug properties and arguments are collect in expected69* map (argName, value) format, then calls original test's run method.70*/71public static void main(String args[]) throws Exception {7273System.out.println("=================================================");7475// Parses parameters76Utils.parseDebugProperties();77Map<String, Object> map = Utils.parseParameters(args) ;7879// Run test80MXBeanInteropTest1 test = new MXBeanInteropTest1();81test.run(map);8283}8485public void run(Map<String, Object> args) {8687System.out.println("MXBeanInteropTest1::run: Start") ;88int errorCount = 0 ;8990try {91// JMX MbeanServer used inside single VM as if remote.92// MBeanServer mbs = MBeanServerFactory.newMBeanServer();93MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();9495JMXServiceURL url = new JMXServiceURL("rmi", null, 0);96JMXConnectorServer cs =97JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);98cs.start();99100JMXServiceURL addr = cs.getAddress();101JMXConnector cc = JMXConnectorFactory.connect(addr);102MBeanServerConnection mbsc = cc.getMBeanServerConnection();103104// Print out registered java.lang.management MXBeans found105// in the remote jvm.106printMBeans(mbsc) ;107108// For each possible kind of JDK 5 defined MXBean, we retrieve its109// MBeanInfo and print it and we call all getters and print110// their output.111errorCount += doClassLoadingMXBeanTest(mbsc) ;112errorCount += doMemoryMXBeanTest(mbsc) ;113errorCount += doThreadMXBeanTest(mbsc) ;114errorCount += doRuntimeMXBeanTest(mbsc) ;115errorCount += doOperatingSystemMXBeanTest(mbsc) ;116errorCount += doCompilationMXBeanTest(mbsc) ;117errorCount += doGarbageCollectorMXBeanTest(mbsc) ;118errorCount += doMemoryManagerMXBeanTest(mbsc) ;119errorCount += doMemoryPoolMXBeanTest(mbsc) ;120121// Terminate the JMX Client122cc.close();123124} catch(Exception e) {125Utils.printThrowable(e, true) ;126throw new RuntimeException(e);127}128129if ( errorCount == 0 ) {130System.out.println("MXBeanInteropTest1::run: Done without any error") ;131} else {132System.out.println("MXBeanInteropTest1::run: Done with "133+ errorCount134+ " error(s)") ;135throw new RuntimeException("errorCount = " + errorCount);136}137}138139/**140* Prints all MBeans of domain java.lang.141* They are MBeans related to the JSR 174 that defines142* package java.lang.management.143*/144private static void printMBeans(MBeanServerConnection mbsc) throws Exception {145ObjectName filterName = new ObjectName("java.lang:*");146Set<ObjectName> set = mbsc.queryNames(filterName, null);147148if ( set.size() == 0 ) {149throw new RuntimeException("(ERROR) No MBean found with filter "150+ filterName);151}152153System.out.println("---- MBeans found in domain java.lang :");154155for (Iterator<ObjectName> iter = set.iterator(); iter.hasNext(); ) {156System.out.println(iter.next().toString());157}158159System.out.println("\n") ;160}161162163private final int doClassLoadingMXBeanTest(MBeanServerConnection mbsc) {164int errorCount = 0 ;165System.out.println("---- ClassLoadingMXBean") ;166167try {168ObjectName classLoadingName =169new ObjectName(ManagementFactory.CLASS_LOADING_MXBEAN_NAME) ;170MBeanInfo mbInfo = mbsc.getMBeanInfo(classLoadingName);171errorCount += checkNonEmpty(mbInfo);172System.out.println("getMBeanInfo\t\t"173+ mbInfo);174ClassLoadingMXBean classLoading = null;175176classLoading = JMX.newMXBeanProxy(mbsc,177classLoadingName,178ClassLoadingMXBean.class) ;179System.out.println("getLoadedClassCount\t\t"180+ classLoading.getLoadedClassCount());181System.out.println("getTotalLoadedClassCount\t\t"182+ classLoading.getTotalLoadedClassCount());183System.out.println("getUnloadedClassCount\t\t"184+ classLoading.getUnloadedClassCount());185System.out.println("isVerbose\t\t"186+ classLoading.isVerbose());187188System.out.println("---- OK\n") ;189190} catch (Exception e) {191Utils.printThrowable(e, true) ;192errorCount++ ;193System.out.println("---- ERROR\n") ;194}195196return errorCount ;197}198199200private final int doMemoryMXBeanTest(MBeanServerConnection mbsc) {201int errorCount = 0 ;202System.out.println("---- MemoryMXBean") ;203204try {205ObjectName memoryName =206new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME) ;207MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryName);208errorCount += checkNonEmpty(mbInfo);209System.out.println("getMBeanInfo\t\t"210+ mbInfo);211MemoryMXBean memory = null ;212213memory =214JMX.newMXBeanProxy(mbsc,215memoryName,216MemoryMXBean.class,217true) ;218System.out.println("getMemoryHeapUsage\t\t"219+ memory.getHeapMemoryUsage());220System.out.println("getNonHeapMemoryHeapUsage\t\t"221+ memory.getNonHeapMemoryUsage());222System.out.println("getObjectPendingFinalizationCount\t\t"223+ memory.getObjectPendingFinalizationCount());224System.out.println("isVerbose\t\t"225+ memory.isVerbose());226227System.out.println("---- OK\n") ;228229} catch (Exception e) {230Utils.printThrowable(e, true) ;231errorCount++ ;232System.out.println("---- ERROR\n") ;233}234235return errorCount ;236}237238239private final int doThreadMXBeanTest(MBeanServerConnection mbsc) {240int errorCount = 0 ;241System.out.println("---- ThreadMXBean") ;242243try {244ObjectName threadName =245new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ;246MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName);247errorCount += checkNonEmpty(mbInfo);248System.out.println("getMBeanInfo\t\t" + mbInfo);249ThreadMXBean thread = null ;250251thread =252JMX.newMXBeanProxy(mbsc,253threadName,254ThreadMXBean.class) ;255System.out.println("findMonitorDeadlockedThreads\t\t"256+ thread.findMonitorDeadlockedThreads());257long[] threadIDs = thread.getAllThreadIds() ;258System.out.println("getAllThreadIds\t\t"259+ threadIDs);260261for ( long threadID : threadIDs ) {262System.out.println("getThreadInfo long\t\t"263+ thread.getThreadInfo(threadID));264System.out.println("getThreadInfo long, int\t\t"265+ thread.getThreadInfo(threadID, 2));266}267268System.out.println("getThreadInfo long[]\t\t"269+ thread.getThreadInfo(threadIDs));270System.out.println("getThreadInfo long[], int\t\t"271+ thread.getThreadInfo(threadIDs, 2));272System.out.println("getDaemonThreadCount\t\t"273+ thread.getDaemonThreadCount());274System.out.println("getPeakThreadCount\t\t"275+ thread.getPeakThreadCount());276System.out.println("getThreadCount\t\t"277+ thread.getThreadCount());278System.out.println("getTotalStartedThreadCount\t\t"279+ thread.getTotalStartedThreadCount());280boolean supported = thread.isThreadContentionMonitoringSupported() ;281System.out.println("isThreadContentionMonitoringSupported\t\t"282+ supported);283284if ( supported ) {285System.out.println("isThreadContentionMonitoringEnabled\t\t"286+ thread.isThreadContentionMonitoringEnabled());287}288289supported = thread.isThreadCpuTimeSupported() ;290System.out.println("isThreadCpuTimeSupported\t\t"291+ supported);292293if ( supported ) {294System.out.println("isThreadCpuTimeEnabled\t\t"295+ thread.isThreadCpuTimeEnabled());296297for (long id : threadIDs) {298System.out.println("getThreadCpuTime(" + id + ")\t\t"299+ thread.getThreadCpuTime(id));300System.out.println("getThreadUserTime(" + id + ")\t\t"301+ thread.getThreadUserTime(id));302}303}304305supported = thread.isCurrentThreadCpuTimeSupported() ;306System.out.println("isCurrentThreadCpuTimeSupported\t\t"307+ supported);308309if ( supported ) {310System.out.println("getCurrentThreadCpuTime\t\t"311+ thread.getCurrentThreadCpuTime());312System.out.println("getCurrentThreadUserTime\t\t"313+ thread.getCurrentThreadUserTime());314}315316thread.resetPeakThreadCount() ;317318System.out.println("---- OK\n") ;319} catch (Exception e) {320Utils.printThrowable(e, true) ;321errorCount++ ;322System.out.println("---- ERROR\n") ;323}324325return errorCount ;326}327328329private final int doRuntimeMXBeanTest(MBeanServerConnection mbsc) {330int errorCount = 0 ;331System.out.println("---- RuntimeMXBean") ;332333try {334ObjectName runtimeName =335new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME) ;336MBeanInfo mbInfo = mbsc.getMBeanInfo(runtimeName);337errorCount += checkNonEmpty(mbInfo);338System.out.println("getMBeanInfo\t\t" + mbInfo);339RuntimeMXBean runtime = null;340341runtime =342JMX.newMXBeanProxy(mbsc,343runtimeName,344RuntimeMXBean.class) ;345System.out.println("getClassPath\t\t"346+ runtime.getClassPath());347System.out.println("getInputArguments\t\t"348+ runtime.getInputArguments());349System.out.println("getLibraryPath\t\t"350+ runtime.getLibraryPath());351System.out.println("getManagementSpecVersion\t\t"352+ runtime.getManagementSpecVersion());353System.out.println("getName\t\t"354+ runtime.getName());355System.out.println("getSpecName\t\t"356+ runtime.getSpecName());357System.out.println("getSpecVendor\t\t"358+ runtime.getSpecVendor());359System.out.println("getSpecVersion\t\t"360+ runtime.getSpecVersion());361System.out.println("getStartTime\t\t"362+ runtime.getStartTime());363System.out.println("getSystemProperties\t\t"364+ runtime.getSystemProperties());365System.out.println("getUptime\t\t"366+ runtime.getUptime());367System.out.println("getVmName\t\t"368+ runtime.getVmName());369System.out.println("getVmVendor\t\t"370+ runtime.getVmVendor());371System.out.println("getVmVersion\t\t"372+ runtime.getVmVersion());373boolean supported = runtime.isBootClassPathSupported() ;374System.out.println("isBootClassPathSupported\t\t"375+ supported);376377if ( supported ) {378System.out.println("getBootClassPath\t\t"379+ runtime.getBootClassPath());380}381382System.out.println("---- OK\n") ;383} catch (Exception e) {384Utils.printThrowable(e, true) ;385errorCount++ ;386System.out.println("---- ERROR\n") ;387}388389return errorCount ;390}391392393private final int doOperatingSystemMXBeanTest(MBeanServerConnection mbsc) {394int errorCount = 0 ;395System.out.println("---- OperatingSystemMXBean") ;396397try {398ObjectName operationName =399new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME) ;400MBeanInfo mbInfo = mbsc.getMBeanInfo(operationName);401errorCount += checkNonEmpty(mbInfo);402System.out.println("getMBeanInfo\t\t" + mbInfo);403OperatingSystemMXBean operation = null ;404405operation =406JMX.newMXBeanProxy(mbsc,407operationName,408OperatingSystemMXBean.class) ;409System.out.println("getArch\t\t"410+ operation.getArch());411System.out.println("getAvailableProcessors\t\t"412+ operation.getAvailableProcessors());413System.out.println("getName\t\t"414+ operation.getName());415System.out.println("getVersion\t\t"416+ operation.getVersion());417418System.out.println("---- OK\n") ;419} catch (Exception e) {420Utils.printThrowable(e, true) ;421errorCount++ ;422System.out.println("---- ERROR\n") ;423}424425return errorCount ;426}427428429private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) {430int errorCount = 0 ;431System.out.println("---- CompilationMXBean") ;432433try {434ObjectName compilationName =435new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME);436437if ( mbsc.isRegistered(compilationName) ) {438MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName);439errorCount += checkNonEmpty(mbInfo);440System.out.println("getMBeanInfo\t\t" + mbInfo);441CompilationMXBean compilation = null ;442443compilation =444JMX.newMXBeanProxy(mbsc,445compilationName,446CompilationMXBean.class) ;447System.out.println("getName\t\t"448+ compilation.getName());449boolean supported =450compilation.isCompilationTimeMonitoringSupported() ;451System.out.println("isCompilationTimeMonitoringSupported\t\t"452+ supported);453454if ( supported ) {455System.out.println("getTotalCompilationTime\t\t"456+ compilation.getTotalCompilationTime());457}458}459460System.out.println("---- OK\n") ;461} catch (Exception e) {462Utils.printThrowable(e, true) ;463errorCount++ ;464System.out.println("---- ERROR\n") ;465}466467return errorCount ;468}469470471private final int doGarbageCollectorMXBeanTest(MBeanServerConnection mbsc) {472int errorCount = 0 ;473System.out.println("---- GarbageCollectorMXBean") ;474475try {476ObjectName filterName =477new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE478+ ",*");479Set<ObjectName> onSet = mbsc.queryNames(filterName, null);480481for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {482ObjectName garbageName = iter.next() ;483System.out.println("-------- " + garbageName) ;484MBeanInfo mbInfo = mbsc.getMBeanInfo(garbageName);485errorCount += checkNonEmpty(mbInfo);486System.out.println("getMBeanInfo\t\t" + mbInfo);487GarbageCollectorMXBean garbage = null ;488489garbage =490JMX.newMXBeanProxy(mbsc,491garbageName,492GarbageCollectorMXBean.class) ;493System.out.println("getCollectionCount\t\t"494+ garbage.getCollectionCount());495System.out.println("getCollectionTime\t\t"496+ garbage.getCollectionTime());497}498499System.out.println("---- OK\n") ;500} catch (Exception e) {501Utils.printThrowable(e, true) ;502errorCount++ ;503System.out.println("---- ERROR\n") ;504}505506return errorCount ;507}508509510private final int doMemoryManagerMXBeanTest(MBeanServerConnection mbsc) {511int errorCount = 0 ;512System.out.println("---- MemoryManagerMXBean") ;513514try {515ObjectName filterName =516new ObjectName(ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE517+ ",*");518Set<ObjectName> onSet = mbsc.queryNames(filterName, null);519520for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {521ObjectName memoryManagerName = iter.next() ;522System.out.println("-------- " + memoryManagerName) ;523MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryManagerName);524System.out.println("getMBeanInfo\t\t" + mbInfo);525errorCount += checkNonEmpty(mbInfo);526MemoryManagerMXBean memoryManager = null;527528memoryManager =529JMX.newMXBeanProxy(mbsc,530memoryManagerName,531MemoryManagerMXBean.class) ;532System.out.println("getMemoryPoolNames\t\t"533+ Arrays.deepToString(memoryManager.getMemoryPoolNames()));534System.out.println("getName\t\t"535+ memoryManager.getName());536System.out.println("isValid\t\t"537+ memoryManager.isValid());538}539540System.out.println("---- OK\n") ;541} catch (Exception e) {542Utils.printThrowable(e, true) ;543errorCount++ ;544System.out.println("---- ERROR\n") ;545}546547return errorCount ;548}549550551private final int doMemoryPoolMXBeanTest(MBeanServerConnection mbsc) {552int errorCount = 0 ;553System.out.println("---- MemoryPoolMXBean") ;554555try {556ObjectName filterName =557new ObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE558+ ",*");559Set<ObjectName> onSet = mbsc.queryNames(filterName, null);560561for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) {562ObjectName memoryPoolName = iter.next() ;563System.out.println("-------- " + memoryPoolName) ;564MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryPoolName);565errorCount += checkNonEmpty(mbInfo);566System.out.println("getMBeanInfo\t\t" + mbInfo);567MemoryPoolMXBean memoryPool = null;568569memoryPool =570JMX.newMXBeanProxy(mbsc,571memoryPoolName,572MemoryPoolMXBean.class,573true) ;574System.out.println("getCollectionUsage\t\t"575+ memoryPool.getCollectionUsage());576System.out.println("getMemoryManagerNames\t\t"577+ Arrays.deepToString(memoryPool.getMemoryManagerNames()));578System.out.println("getName\t\t"579+ memoryPool.getName());580System.out.println("getPeakUsage\t\t"581+ memoryPool.getPeakUsage());582System.out.println("getType\t\t"583+ memoryPool.getType());584System.out.println("getUsage\t\t"585+ memoryPool.getUsage());586System.out.println("isValid\t\t"587+ memoryPool.isValid());588boolean supported = memoryPool.isUsageThresholdSupported() ;589System.out.println("isUsageThresholdSupported\t\t"590+ supported);591592if ( supported ) {593System.out.println("getUsageThreshold\t\t"594+ memoryPool.getUsageThreshold());595System.out.println("isUsageThresholdExceeded\t\t"596+ memoryPool.isUsageThresholdExceeded());597System.out.println("getUsageThresholdCount\t\t"598+ memoryPool.getUsageThresholdCount());599}600601supported = memoryPool.isCollectionUsageThresholdSupported() ;602System.out.println("isCollectionUsageThresholdSupported\t\t"603+ supported);604605if ( supported ) {606System.out.println("getCollectionUsageThreshold\t\t"607+ memoryPool.getCollectionUsageThreshold());608System.out.println("getCollectionUsageThresholdCount\t\t"609+ memoryPool.getCollectionUsageThresholdCount());610System.out.println("isCollectionUsageThresholdExceeded\t\t"611+ memoryPool.isCollectionUsageThresholdExceeded());612}613614memoryPool.resetPeakUsage();615}616617System.out.println("---- OK\n") ;618} catch (Exception e) {619Utils.printThrowable(e, true) ;620errorCount++ ;621System.out.println("---- ERROR\n") ;622}623624return errorCount ;625}626627628private int checkNonEmpty(MBeanInfo mbi) {629if ( mbi.toString().length() == 0 ) {630System.out.println("(ERROR) MBeanInfo is empty !");631return 1;632} else {633return 0;634}635}636637}638639640