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