Path: blob/master/test/jdk/java/lang/management/ManagementFactory/ValidateOpenTypes.java
41172 views
/*1* Copyright (c) 2004, 2016, 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 502453126* @summary Validate open types mapped for the MXBeans in the platform27* MBeanServer.28* @author Mandy Chung29*30* @compile ValidateOpenTypes.java31* @run main/othervm -verbose:gc ValidateOpenTypes32*/33import java.lang.management.*;34import javax.management.*;35import javax.management.openmbean.CompositeData;36import javax.management.openmbean.TabularData;37import static java.lang.management.ManagementFactory.*;38import java.util.List;39import java.util.Map;40import com.sun.management.GcInfo;4142public class ValidateOpenTypes {43private static MBeanServer server =44ManagementFactory.getPlatformMBeanServer();45private static ObjectName memory;46private static ObjectName thread;47private static ObjectName runtime;48private static ObjectName os;49private static ObjectName heapPool = null;50private static ObjectName nonHeapPool = null;5152public static void main(String[] argv) throws Exception {53memory = new ObjectName(MEMORY_MXBEAN_NAME);54runtime = new ObjectName(RUNTIME_MXBEAN_NAME);55thread = new ObjectName(THREAD_MXBEAN_NAME);56os = new ObjectName(OPERATING_SYSTEM_MXBEAN_NAME);5758List<MemoryPoolMXBean> pools = getMemoryPoolMXBeans();59for (MemoryPoolMXBean p : pools) {60if (heapPool == null &&61p.getType() == MemoryType.HEAP &&62p.isUsageThresholdSupported() &&63p.isCollectionUsageThresholdSupported()) {64heapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE +65",name=" + p.getName());66}67if (nonHeapPool == null &&68p.getType() == MemoryType.NON_HEAP &&69p.isUsageThresholdSupported()) {70nonHeapPool = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE +71",name=" + p.getName());72}73}7475// Check notification emitters76MyListener listener = new MyListener();77server.addNotificationListener(memory, listener, null, null);78server.removeNotificationListener(memory, listener);7980checkEnum();81checkList();82checkMap();83checkMemoryUsage();84checkThreadInfo();8586checkOS();87checkSunGC();8889System.out.println("Test passed.");90}9192private static void checkEnum() throws Exception {93String type = (String) server.getAttribute(heapPool, "Type");94if (!type.equals("HEAP")) {95throw new RuntimeException("TEST FAILED: " +96" incorrect memory type for " + heapPool);97}9899type = (String) server.getAttribute(nonHeapPool, "Type");100if (!type.equals("NON_HEAP")) {101throw new RuntimeException("TEST FAILED: " +102" incorrect memory type for " + nonHeapPool);103}104}105106private static final String OPTION = "-verbose:gc";107private static void checkList() throws Exception {108String[] args = (String[]) server.getAttribute(runtime,109"InputArguments");110if (args.length < 1) {111throw new RuntimeException("TEST FAILED: " +112" empty input arguments");113}114// check if -verbose:gc exists115boolean found = false;116for (String option : args) {117if (option.equals(OPTION)) {118found = true;119break;120}121}122if (!found) {123throw new RuntimeException("TEST FAILED: " +124"VM option " + OPTION + " not found");125}126}127128private static final String KEY1 = "test.property.key1";129private static final String VALUE1 = "test.property.value1";130private static final String KEY2 = "test.property.key2";131private static final String VALUE2 = "test.property.value2";132private static final String KEY3 = "test.property.key3";133private static void checkMap() throws Exception {134// Add new system properties135System.setProperty(KEY1, VALUE1);136System.setProperty(KEY2, VALUE2);137138TabularData props1 = (TabularData)139server.getAttribute(runtime, "SystemProperties");140141String value1 = getProperty(props1, KEY1);142if (value1 == null || !value1.equals(VALUE1)) {143throw new RuntimeException("TEST FAILED: " +144KEY1 + " property found" +145" with value = " + value1 +146" but expected to be " + VALUE1);147}148149String value2 = getProperty(props1, KEY2);150if (value2 == null || !value2.equals(VALUE2)) {151throw new RuntimeException("TEST FAILED: " +152KEY2 + " property found" +153" with value = " + value2 +154" but expected to be " + VALUE2);155}156157String value3 = getProperty(props1, KEY3);158if (value3 != null) {159throw new RuntimeException("TEST FAILED: " +160KEY3 + " property found" +161" but should not exist" );162}163}164private static String getProperty(TabularData td, String propName) {165CompositeData cd = td.get(new Object[] { propName});166if (cd != null) {167String key = (String) cd.get("key");168if (!propName.equals(key)) {169throw new RuntimeException("TEST FAILED: " +170key + " property found" +171" but expected to be " + propName);172}173return (String) cd.get("value");174}175return null;176}177178private static void checkMemoryUsage() throws Exception {179// sanity check to have non-negative usage180Object u1 = server.getAttribute(memory, "HeapMemoryUsage");181Object u2 = server.getAttribute(memory, "NonHeapMemoryUsage");182Object u3 = server.getAttribute(heapPool, "Usage");183Object u4 = server.getAttribute(nonHeapPool, "Usage");184if (getCommitted(u1) < 0 ||185getCommitted(u2) < 0 ||186getCommitted(u3) < 0 ||187getCommitted(u4) < 0) {188throw new RuntimeException("TEST FAILED: " +189" expected non-negative committed usage");190}191server.invoke(memory, "gc", new Object[0], new String[0]);192Object u5 = server.getAttribute(heapPool, "CollectionUsage");193if (getCommitted(u5) < 0) {194throw new RuntimeException("TEST FAILED: " +195" expected non-negative committed collected usage");196}197}198199private static long getCommitted(Object data) {200MemoryUsage u = MemoryUsage.from((CompositeData) data);201return u.getCommitted();202}203204private static void checkThreadInfo() throws Exception {205// assume all threads stay alive206long[] ids = (long[]) server.getAttribute(thread, "AllThreadIds");207Object result = server.invoke(thread,208"getThreadInfo",209new Object[] { ids },210new String[] { "[J" });211for (CompositeData cd : (CompositeData[]) result) {212printThreadInfo(cd);213}214215result = server.invoke(thread,216"getThreadInfo",217new Object[] { ids, new Integer(2) },218new String[] { "[J", "int" });219for (CompositeData cd : (CompositeData[]) result) {220printThreadInfo(cd);221}222223long id = Thread.currentThread().getId();224result = server.invoke(thread,225"getThreadInfo",226new Object[] { new Long(id) },227new String[] { "long" });228printThreadInfo((CompositeData) result);229230result = server.invoke(thread,231"getThreadInfo",232new Object[] { new Long(id), new Integer(2) },233new String[] { "long", "int" });234printThreadInfo((CompositeData) result);235}236237private static void printThreadInfo(CompositeData cd) {238ThreadInfo info = ThreadInfo.from(cd);239if (info == null) {240throw new RuntimeException("TEST FAILED: " +241" Null ThreadInfo");242}243244System.out.print(info.getThreadName());245System.out.print(" id=" + info.getThreadId());246System.out.println(" " + info.getThreadState());247248for (StackTraceElement s : info.getStackTrace()) {249System.out.println(s);250}251}252253private static void checkOS() throws Exception {254Integer cpus = (Integer) server.getAttribute(os, "AvailableProcessors");255System.out.println("# CPUs = " + cpus);256Long vmem = (Long) server.getAttribute(os, "CommittedVirtualMemorySize");257System.out.println("Committed virtual memory = " + vmem);258}259260private static void checkSunGC() throws Exception {261// Test com.sun.management proxy262List<GarbageCollectorMXBean> gcs = getGarbageCollectorMXBeans();263for (GarbageCollectorMXBean gc : gcs) {264ObjectName sunGc =265new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +266",name=" + gc.getName());267CompositeData cd = (CompositeData) server.getAttribute(sunGc, "LastGcInfo");268if (cd != null) {269System.out.println("GC statistic for : " + gc.getName());270printGcInfo(cd);271}272}273}274275private static void printGcInfo(CompositeData cd) throws Exception {276GcInfo info = GcInfo.from(cd);277System.out.print("GC #" + info.getId());278System.out.print(" start:" + info.getStartTime());279System.out.print(" end:" + info.getEndTime());280System.out.println(" (" + info.getDuration() + "ms)");281Map<String,MemoryUsage> usage = info.getMemoryUsageBeforeGc();282283for (Map.Entry<String,MemoryUsage> entry : usage.entrySet()) {284String poolname = entry.getKey();285MemoryUsage busage = entry.getValue();286MemoryUsage ausage = info.getMemoryUsageAfterGc().get(poolname);287if (ausage == null) {288throw new RuntimeException("After Gc Memory does not exist" +289" for " + poolname);290}291System.out.println("Usage for pool " + poolname);292System.out.println(" Before GC: " + busage);293System.out.println(" After GC: " + ausage);294}295}296297static class MyListener implements NotificationListener {298public void handleNotification(Notification notif, Object handback) {299return;300}301}302}303304305