Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/metaspace/TestPerfCountersAndMemoryPools.java
32284 views
/*1* Copyright (c) 2013, 2014, 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*/2223import java.util.List;24import java.lang.management.*;2526import com.oracle.java.testlibrary.*;27import static com.oracle.java.testlibrary.Asserts.*;2829/* @test TestPerfCountersAndMemoryPools30* @bug 802347631* @library /testlibrary32* @requires vm.gc=="Serial" | vm.gc=="null"33* @summary Tests that a MemoryPoolMXBeans and PerfCounters for metaspace34* report the same data.35* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UseSerialGC -XX:+UsePerfData TestPerfCountersAndMemoryPools36* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UseSerialGC -XX:+UsePerfData TestPerfCountersAndMemoryPools37*/38public class TestPerfCountersAndMemoryPools {39public static void main(String[] args) throws Exception {40checkMemoryUsage("Metaspace", "sun.gc.metaspace");4142if (InputArguments.contains("-XX:+UseCompressedKlassPointers") && Platform.is64bit()) {43checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace");44}45}4647private static MemoryPoolMXBean getMemoryPool(String memoryPoolName) {48List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();49for (MemoryPoolMXBean pool : pools) {50if (pool.getName().equals(memoryPoolName)) {51return pool;52}53}5455throw new RuntimeException("Excpted to find a memory pool with name " +56memoryPoolName);57}5859private static void checkMemoryUsage(String memoryPoolName, String perfNS)60throws Exception {61MemoryPoolMXBean pool = getMemoryPool(memoryPoolName);6263// Must do a GC to update performance counters64System.gc();65assertEQ(getMinCapacity(perfNS), pool.getUsage().getInit());6667// Must do a second GC to update the perfomance counters again, since68// the call pool.getUsage().getInit() could have allocated some69// metadata.70System.gc();71assertEQ(getUsed(perfNS), pool.getUsage().getUsed());72assertEQ(getCapacity(perfNS), pool.getUsage().getCommitted());73}7475private static long getMinCapacity(String ns) throws Exception {76return PerfCounters.findByName(ns + ".minCapacity").longValue();77}7879private static long getCapacity(String ns) throws Exception {80return PerfCounters.findByName(ns + ".capacity").longValue();81}8283private static long getUsed(String ns) throws Exception {84return PerfCounters.findByName(ns + ".used").longValue();85}86}878889