1 /* 2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8058865 27 * @summary Test all MXBeans available by default on the platform 28 * @author Olivier Lagneau 29 * @modules java.management.rmi 30 * @library /lib/testlibrary 31 * @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanInteropTest1 32 */ 33 34 import java.util.Arrays; 35 import java.util.Iterator; 36 import java.util.Map; 37 import java.util.Set; 38 39 import java.lang.management.ClassLoadingMXBean; 40 import java.lang.management.CompilationMXBean; 41 import java.lang.management.GarbageCollectorMXBean; 42 import java.lang.management.ManagementFactory; 43 import java.lang.management.MemoryMXBean; 44 import java.lang.management.MemoryManagerMXBean; 45 import java.lang.management.MemoryPoolMXBean; 46 import java.lang.management.OperatingSystemMXBean; 47 import java.lang.management.RuntimeMXBean; 48 import java.lang.management.ThreadMXBean; 49 50 import javax.management.JMX; 51 import javax.management.MBeanAttributeInfo; 52 import javax.management.MBeanConstructorInfo; 53 import javax.management.MBeanServer; 54 import javax.management.MBeanServerFactory; 55 import javax.management.MBeanInfo; 56 import javax.management.MBeanNotificationInfo; 57 import javax.management.MBeanOperationInfo; 58 import javax.management.MBeanServerConnection; 59 import javax.management.ObjectName; 60 import javax.management.remote.JMXConnector; 61 import javax.management.remote.JMXConnectorFactory; 62 import javax.management.remote.JMXConnectorServer; 63 import javax.management.remote.JMXConnectorServerFactory; 64 import javax.management.remote.JMXServiceURL; 65 66 public class MXBeanInteropTest1 { 67 68 /* 69 * First Debug properties and arguments are collect in expected 70 * map (argName, value) format, then calls original test's run method. 71 */ 72 public static void main(String args[]) throws Exception { 73 74 System.out.println("================================================="); 75 76 // Parses parameters 77 Utils.parseDebugProperties(); 78 Map<String, Object> map = Utils.parseParameters(args) ; 79 80 // Run test 81 MXBeanInteropTest1 test = new MXBeanInteropTest1(); 82 test.run(map); 83 84 } 85 86 public void run(Map<String, Object> args) { 87 88 System.out.println("MXBeanInteropTest1::run: Start") ; 89 int errorCount = 0 ; 90 91 try { 92 // JMX MbeanServer used inside single VM as if remote. 93 // MBeanServer mbs = MBeanServerFactory.newMBeanServer(); 94 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 95 96 JMXServiceURL url = new JMXServiceURL("rmi", null, 0); 97 JMXConnectorServer cs = 98 JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); 99 cs.start(); 100 101 JMXServiceURL addr = cs.getAddress(); 102 JMXConnector cc = JMXConnectorFactory.connect(addr); 103 MBeanServerConnection mbsc = cc.getMBeanServerConnection(); 104 105 // Print out registered java.lang.management MXBeans found 106 // in the remote jvm. 107 printMBeans(mbsc) ; 108 109 // For each possible kind of JDK 5 defined MXBean, we retrieve its 110 // MBeanInfo and print it and we call all getters and print 111 // their output. 112 errorCount += doClassLoadingMXBeanTest(mbsc) ; 113 errorCount += doMemoryMXBeanTest(mbsc) ; 114 errorCount += doThreadMXBeanTest(mbsc) ; 115 errorCount += doRuntimeMXBeanTest(mbsc) ; 116 errorCount += doOperatingSystemMXBeanTest(mbsc) ; 117 errorCount += doCompilationMXBeanTest(mbsc) ; 118 errorCount += doGarbageCollectorMXBeanTest(mbsc) ; 119 errorCount += doMemoryManagerMXBeanTest(mbsc) ; 120 errorCount += doMemoryPoolMXBeanTest(mbsc) ; 121 122 // Terminate the JMX Client 123 cc.close(); 124 125 } catch(Exception e) { 126 Utils.printThrowable(e, true) ; 127 throw new RuntimeException(e); 128 } 129 130 if ( errorCount == 0 ) { 131 System.out.println("MXBeanInteropTest1::run: Done without any error") ; 132 } else { 133 System.out.println("MXBeanInteropTest1::run: Done with " 134 + errorCount 135 + " error(s)") ; 136 throw new RuntimeException("errorCount = " + errorCount); 137 } 138 } 139 140 /** 141 * Prints all MBeans of domain java.lang. 142 * They are MBeans related to the JSR 174 that defines 143 * package java.lang.management. 144 */ 145 private static void printMBeans(MBeanServerConnection mbsc) throws Exception { 146 ObjectName filterName = new ObjectName("java.lang:*"); 147 Set<ObjectName> set = mbsc.queryNames(filterName, null); 148 149 if ( set.size() == 0 ) { 150 throw new RuntimeException("(ERROR) No MBean found with filter " 151 + filterName); 152 } 153 154 System.out.println("---- MBeans found in domain java.lang :"); 155 156 for (Iterator<ObjectName> iter = set.iterator(); iter.hasNext(); ) { 157 System.out.println(iter.next().toString()); 158 } 159 160 System.out.println("\n") ; 161 } 162 163 164 private final int doClassLoadingMXBeanTest(MBeanServerConnection mbsc) { 165 int errorCount = 0 ; 166 System.out.println("---- ClassLoadingMXBean") ; 167 168 try { 169 ObjectName classLoadingName = 170 new ObjectName(ManagementFactory.CLASS_LOADING_MXBEAN_NAME) ; 171 MBeanInfo mbInfo = mbsc.getMBeanInfo(classLoadingName); 172 errorCount += checkNonEmpty(mbInfo); 173 System.out.println("getMBeanInfo\t\t" 174 + mbInfo); 175 ClassLoadingMXBean classLoading = null; 176 177 classLoading = JMX.newMXBeanProxy(mbsc, 178 classLoadingName, 179 ClassLoadingMXBean.class) ; 180 System.out.println("getLoadedClassCount\t\t" 181 + classLoading.getLoadedClassCount()); 182 System.out.println("getTotalLoadedClassCount\t\t" 183 + classLoading.getTotalLoadedClassCount()); 184 System.out.println("getUnloadedClassCount\t\t" 185 + classLoading.getUnloadedClassCount()); 186 System.out.println("isVerbose\t\t" 187 + classLoading.isVerbose()); 188 189 System.out.println("---- OK\n") ; 190 191 } catch (Exception e) { 192 Utils.printThrowable(e, true) ; 193 errorCount++ ; 194 System.out.println("---- ERROR\n") ; 195 } 196 197 return errorCount ; 198 } 199 200 201 private final int doMemoryMXBeanTest(MBeanServerConnection mbsc) { 202 int errorCount = 0 ; 203 System.out.println("---- MemoryMXBean") ; 204 205 try { 206 ObjectName memoryName = 207 new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME) ; 208 MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryName); 209 errorCount += checkNonEmpty(mbInfo); 210 System.out.println("getMBeanInfo\t\t" 211 + mbInfo); 212 MemoryMXBean memory = null ; 213 214 memory = 215 JMX.newMXBeanProxy(mbsc, 216 memoryName, 217 MemoryMXBean.class, 218 true) ; 219 System.out.println("getMemoryHeapUsage\t\t" 220 + memory.getHeapMemoryUsage()); 221 System.out.println("getNonHeapMemoryHeapUsage\t\t" 222 + memory.getNonHeapMemoryUsage()); 223 System.out.println("getObjectPendingFinalizationCount\t\t" 224 + memory.getObjectPendingFinalizationCount()); 225 System.out.println("isVerbose\t\t" 226 + memory.isVerbose()); 227 228 System.out.println("---- OK\n") ; 229 230 } catch (Exception e) { 231 Utils.printThrowable(e, true) ; 232 errorCount++ ; 233 System.out.println("---- ERROR\n") ; 234 } 235 236 return errorCount ; 237 } 238 239 240 private final int doThreadMXBeanTest(MBeanServerConnection mbsc) { 241 int errorCount = 0 ; 242 System.out.println("---- ThreadMXBean") ; 243 244 try { 245 ObjectName threadName = 246 new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME) ; 247 MBeanInfo mbInfo = mbsc.getMBeanInfo(threadName); 248 errorCount += checkNonEmpty(mbInfo); 249 System.out.println("getMBeanInfo\t\t" + mbInfo); 250 ThreadMXBean thread = null ; 251 252 thread = 253 JMX.newMXBeanProxy(mbsc, 254 threadName, 255 ThreadMXBean.class) ; 256 System.out.println("findMonitorDeadlockedThreads\t\t" 257 + thread.findMonitorDeadlockedThreads()); 258 long[] threadIDs = thread.getAllThreadIds() ; 259 System.out.println("getAllThreadIds\t\t" 260 + threadIDs); 261 262 for ( long threadID : threadIDs ) { 263 System.out.println("getThreadInfo long\t\t" 264 + thread.getThreadInfo(threadID)); 265 System.out.println("getThreadInfo long, int\t\t" 266 + thread.getThreadInfo(threadID, 2)); 267 } 268 269 System.out.println("getThreadInfo long[]\t\t" 270 + thread.getThreadInfo(threadIDs)); 271 System.out.println("getThreadInfo long[], int\t\t" 272 + thread.getThreadInfo(threadIDs, 2)); 273 System.out.println("getDaemonThreadCount\t\t" 274 + thread.getDaemonThreadCount()); 275 System.out.println("getPeakThreadCount\t\t" 276 + thread.getPeakThreadCount()); 277 System.out.println("getThreadCount\t\t" 278 + thread.getThreadCount()); 279 System.out.println("getTotalStartedThreadCount\t\t" 280 + thread.getTotalStartedThreadCount()); 281 boolean supported = thread.isThreadContentionMonitoringSupported() ; 282 System.out.println("isThreadContentionMonitoringSupported\t\t" 283 + supported); 284 285 if ( supported ) { 286 System.out.println("isThreadContentionMonitoringEnabled\t\t" 287 + thread.isThreadContentionMonitoringEnabled()); 288 } 289 290 supported = thread.isThreadCpuTimeSupported() ; 291 System.out.println("isThreadCpuTimeSupported\t\t" 292 + supported); 293 294 if ( supported ) { 295 System.out.println("isThreadCpuTimeEnabled\t\t" 296 + thread.isThreadCpuTimeEnabled()); 297 298 for (long id : threadIDs) { 299 System.out.println("getThreadCpuTime(" + id + ")\t\t" 300 + thread.getThreadCpuTime(id)); 301 System.out.println("getThreadUserTime(" + id + ")\t\t" 302 + thread.getThreadUserTime(id)); 303 } 304 } 305 306 supported = thread.isCurrentThreadCpuTimeSupported() ; 307 System.out.println("isCurrentThreadCpuTimeSupported\t\t" 308 + supported); 309 310 if ( supported ) { 311 System.out.println("getCurrentThreadCpuTime\t\t" 312 + thread.getCurrentThreadCpuTime()); 313 System.out.println("getCurrentThreadUserTime\t\t" 314 + thread.getCurrentThreadUserTime()); 315 } 316 317 thread.resetPeakThreadCount() ; 318 319 System.out.println("---- OK\n") ; 320 } catch (Exception e) { 321 Utils.printThrowable(e, true) ; 322 errorCount++ ; 323 System.out.println("---- ERROR\n") ; 324 } 325 326 return errorCount ; 327 } 328 329 330 private final int doRuntimeMXBeanTest(MBeanServerConnection mbsc) { 331 int errorCount = 0 ; 332 System.out.println("---- RuntimeMXBean") ; 333 334 try { 335 ObjectName runtimeName = 336 new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME) ; 337 MBeanInfo mbInfo = mbsc.getMBeanInfo(runtimeName); 338 errorCount += checkNonEmpty(mbInfo); 339 System.out.println("getMBeanInfo\t\t" + mbInfo); 340 RuntimeMXBean runtime = null; 341 342 runtime = 343 JMX.newMXBeanProxy(mbsc, 344 runtimeName, 345 RuntimeMXBean.class) ; 346 System.out.println("getClassPath\t\t" 347 + runtime.getClassPath()); 348 System.out.println("getInputArguments\t\t" 349 + runtime.getInputArguments()); 350 System.out.println("getLibraryPath\t\t" 351 + runtime.getLibraryPath()); 352 System.out.println("getManagementSpecVersion\t\t" 353 + runtime.getManagementSpecVersion()); 354 System.out.println("getName\t\t" 355 + runtime.getName()); 356 System.out.println("getSpecName\t\t" 357 + runtime.getSpecName()); 358 System.out.println("getSpecVendor\t\t" 359 + runtime.getSpecVendor()); 360 System.out.println("getSpecVersion\t\t" 361 + runtime.getSpecVersion()); 362 System.out.println("getStartTime\t\t" 363 + runtime.getStartTime()); 364 System.out.println("getSystemProperties\t\t" 365 + runtime.getSystemProperties()); 366 System.out.println("getUptime\t\t" 367 + runtime.getUptime()); 368 System.out.println("getVmName\t\t" 369 + runtime.getVmName()); 370 System.out.println("getVmVendor\t\t" 371 + runtime.getVmVendor()); 372 System.out.println("getVmVersion\t\t" 373 + runtime.getVmVersion()); 374 boolean supported = runtime.isBootClassPathSupported() ; 375 System.out.println("isBootClassPathSupported\t\t" 376 + supported); 377 378 if ( supported ) { 379 System.out.println("getBootClassPath\t\t" 380 + runtime.getBootClassPath()); 381 } 382 383 System.out.println("---- OK\n") ; 384 } catch (Exception e) { 385 Utils.printThrowable(e, true) ; 386 errorCount++ ; 387 System.out.println("---- ERROR\n") ; 388 } 389 390 return errorCount ; 391 } 392 393 394 private final int doOperatingSystemMXBeanTest(MBeanServerConnection mbsc) { 395 int errorCount = 0 ; 396 System.out.println("---- OperatingSystemMXBean") ; 397 398 try { 399 ObjectName operationName = 400 new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME) ; 401 MBeanInfo mbInfo = mbsc.getMBeanInfo(operationName); 402 errorCount += checkNonEmpty(mbInfo); 403 System.out.println("getMBeanInfo\t\t" + mbInfo); 404 OperatingSystemMXBean operation = null ; 405 406 operation = 407 JMX.newMXBeanProxy(mbsc, 408 operationName, 409 OperatingSystemMXBean.class) ; 410 System.out.println("getArch\t\t" 411 + operation.getArch()); 412 System.out.println("getAvailableProcessors\t\t" 413 + operation.getAvailableProcessors()); 414 System.out.println("getName\t\t" 415 + operation.getName()); 416 System.out.println("getVersion\t\t" 417 + operation.getVersion()); 418 419 System.out.println("---- OK\n") ; 420 } catch (Exception e) { 421 Utils.printThrowable(e, true) ; 422 errorCount++ ; 423 System.out.println("---- ERROR\n") ; 424 } 425 426 return errorCount ; 427 } 428 429 430 private final int doCompilationMXBeanTest(MBeanServerConnection mbsc) { 431 int errorCount = 0 ; 432 System.out.println("---- CompilationMXBean") ; 433 434 try { 435 ObjectName compilationName = 436 new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME); 437 438 if ( mbsc.isRegistered(compilationName) ) { 439 MBeanInfo mbInfo = mbsc.getMBeanInfo(compilationName); 440 errorCount += checkNonEmpty(mbInfo); 441 System.out.println("getMBeanInfo\t\t" + mbInfo); 442 CompilationMXBean compilation = null ; 443 444 compilation = 445 JMX.newMXBeanProxy(mbsc, 446 compilationName, 447 CompilationMXBean.class) ; 448 System.out.println("getName\t\t" 449 + compilation.getName()); 450 boolean supported = 451 compilation.isCompilationTimeMonitoringSupported() ; 452 System.out.println("isCompilationTimeMonitoringSupported\t\t" 453 + supported); 454 455 if ( supported ) { 456 System.out.println("getTotalCompilationTime\t\t" 457 + compilation.getTotalCompilationTime()); 458 } 459 } 460 461 System.out.println("---- OK\n") ; 462 } catch (Exception e) { 463 Utils.printThrowable(e, true) ; 464 errorCount++ ; 465 System.out.println("---- ERROR\n") ; 466 } 467 468 return errorCount ; 469 } 470 471 472 private final int doGarbageCollectorMXBeanTest(MBeanServerConnection mbsc) { 473 int errorCount = 0 ; 474 System.out.println("---- GarbageCollectorMXBean") ; 475 476 try { 477 ObjectName filterName = 478 new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE 479 + ",*"); 480 Set<ObjectName> onSet = mbsc.queryNames(filterName, null); 481 482 for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) { 483 ObjectName garbageName = iter.next() ; 484 System.out.println("-------- " + garbageName) ; 485 MBeanInfo mbInfo = mbsc.getMBeanInfo(garbageName); 486 errorCount += checkNonEmpty(mbInfo); 487 System.out.println("getMBeanInfo\t\t" + mbInfo); 488 GarbageCollectorMXBean garbage = null ; 489 490 garbage = 491 JMX.newMXBeanProxy(mbsc, 492 garbageName, 493 GarbageCollectorMXBean.class) ; 494 System.out.println("getCollectionCount\t\t" 495 + garbage.getCollectionCount()); 496 System.out.println("getCollectionTime\t\t" 497 + garbage.getCollectionTime()); 498 } 499 500 System.out.println("---- OK\n") ; 501 } catch (Exception e) { 502 Utils.printThrowable(e, true) ; 503 errorCount++ ; 504 System.out.println("---- ERROR\n") ; 505 } 506 507 return errorCount ; 508 } 509 510 511 private final int doMemoryManagerMXBeanTest(MBeanServerConnection mbsc) { 512 int errorCount = 0 ; 513 System.out.println("---- MemoryManagerMXBean") ; 514 515 try { 516 ObjectName filterName = 517 new ObjectName(ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE 518 + ",*"); 519 Set<ObjectName> onSet = mbsc.queryNames(filterName, null); 520 521 for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) { 522 ObjectName memoryManagerName = iter.next() ; 523 System.out.println("-------- " + memoryManagerName) ; 524 MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryManagerName); 525 System.out.println("getMBeanInfo\t\t" + mbInfo); 526 errorCount += checkNonEmpty(mbInfo); 527 MemoryManagerMXBean memoryManager = null; 528 529 memoryManager = 530 JMX.newMXBeanProxy(mbsc, 531 memoryManagerName, 532 MemoryManagerMXBean.class) ; 533 System.out.println("getMemoryPoolNames\t\t" 534 + Arrays.deepToString(memoryManager.getMemoryPoolNames())); 535 System.out.println("getName\t\t" 536 + memoryManager.getName()); 537 System.out.println("isValid\t\t" 538 + memoryManager.isValid()); 539 } 540 541 System.out.println("---- OK\n") ; 542 } catch (Exception e) { 543 Utils.printThrowable(e, true) ; 544 errorCount++ ; 545 System.out.println("---- ERROR\n") ; 546 } 547 548 return errorCount ; 549 } 550 551 552 private final int doMemoryPoolMXBeanTest(MBeanServerConnection mbsc) { 553 int errorCount = 0 ; 554 System.out.println("---- MemoryPoolMXBean") ; 555 556 try { 557 ObjectName filterName = 558 new ObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE 559 + ",*"); 560 Set<ObjectName> onSet = mbsc.queryNames(filterName, null); 561 562 for (Iterator<ObjectName> iter = onSet.iterator(); iter.hasNext(); ) { 563 ObjectName memoryPoolName = iter.next() ; 564 System.out.println("-------- " + memoryPoolName) ; 565 MBeanInfo mbInfo = mbsc.getMBeanInfo(memoryPoolName); 566 errorCount += checkNonEmpty(mbInfo); 567 System.out.println("getMBeanInfo\t\t" + mbInfo); 568 MemoryPoolMXBean memoryPool = null; 569 570 memoryPool = 571 JMX.newMXBeanProxy(mbsc, 572 memoryPoolName, 573 MemoryPoolMXBean.class, 574 true) ; 575 System.out.println("getCollectionUsage\t\t" 576 + memoryPool.getCollectionUsage()); 577 System.out.println("getMemoryManagerNames\t\t" 578 + Arrays.deepToString(memoryPool.getMemoryManagerNames())); 579 System.out.println("getName\t\t" 580 + memoryPool.getName()); 581 System.out.println("getPeakUsage\t\t" 582 + memoryPool.getPeakUsage()); 583 System.out.println("getType\t\t" 584 + memoryPool.getType()); 585 System.out.println("getUsage\t\t" 586 + memoryPool.getUsage()); 587 System.out.println("isValid\t\t" 588 + memoryPool.isValid()); 589 boolean supported = memoryPool.isUsageThresholdSupported() ; 590 System.out.println("isUsageThresholdSupported\t\t" 591 + supported); 592 593 if ( supported ) { 594 System.out.println("getUsageThreshold\t\t" 595 + memoryPool.getUsageThreshold()); 596 System.out.println("isUsageThresholdExceeded\t\t" 597 + memoryPool.isUsageThresholdExceeded()); 598 System.out.println("getUsageThresholdCount\t\t" 599 + memoryPool.getUsageThresholdCount()); 600 } 601 602 supported = memoryPool.isCollectionUsageThresholdSupported() ; 603 System.out.println("isCollectionUsageThresholdSupported\t\t" 604 + supported); 605 606 if ( supported ) { 607 System.out.println("getCollectionUsageThreshold\t\t" 608 + memoryPool.getCollectionUsageThreshold()); 609 System.out.println("getCollectionUsageThresholdCount\t\t" 610 + memoryPool.getCollectionUsageThresholdCount()); 611 System.out.println("isCollectionUsageThresholdExceeded\t\t" 612 + memoryPool.isCollectionUsageThresholdExceeded()); 613 } 614 615 memoryPool.resetPeakUsage(); 616 } 617 618 System.out.println("---- OK\n") ; 619 } catch (Exception e) { 620 Utils.printThrowable(e, true) ; 621 errorCount++ ; 622 System.out.println("---- ERROR\n") ; 623 } 624 625 return errorCount ; 626 } 627 628 629 private int checkNonEmpty(MBeanInfo mbi) { 630 if ( mbi.toString().length() == 0 ) { 631 System.out.println("(ERROR) MBeanInfo is empty !"); 632 return 1; 633 } else { 634 return 0; 635 } 636 } 637 638 }