Path: blob/master/test/hotspot/jtreg/gc/metaspace/TestPerfCountersAndMemoryPools.java
40942 views
/*1* Copyright (c) 2013, 2019, 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*/2223package gc.metaspace;2425import java.util.List;26import java.lang.management.*;2728import jdk.test.lib.Platform;29import static jdk.test.lib.Asserts.*;30import gc.testlibrary.PerfCounters;3132/* @test TestPerfCountersAndMemoryPools33* @bug 802347634* @library /test/lib /35* @requires vm.gc.Serial36* @summary Tests that a MemoryPoolMXBeans and PerfCounters for metaspace37* report the same data.38* @modules java.base/jdk.internal.misc39* java.management40* jdk.internal.jvmstat/sun.jvmstat.monitor41* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint gc.metaspace.TestPerfCountersAndMemoryPools42* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint gc.metaspace.TestPerfCountersAndMemoryPools43*/44public class TestPerfCountersAndMemoryPools {45public static void main(String[] args) throws Exception {46checkMemoryUsage("Metaspace", "sun.gc.metaspace");4748if (InputArguments.contains("-XX:+UseCompressedClassPointers") && Platform.is64bit()) {49checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace");50}51}5253private static MemoryPoolMXBean getMemoryPool(String memoryPoolName) {54List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();55for (MemoryPoolMXBean pool : pools) {56if (pool.getName().equals(memoryPoolName)) {57return pool;58}59}6061throw new RuntimeException("Excpted to find a memory pool with name " +62memoryPoolName);63}6465private static void checkMemoryUsage(String memoryPoolName, String perfNS)66throws Exception {67MemoryPoolMXBean pool = getMemoryPool(memoryPoolName);6869// First, call all the methods to let them allocate their own slab of metadata70getMinCapacity(perfNS);71getCapacity(perfNS);72getUsed(perfNS);73pool.getUsage().getInit();74pool.getUsage().getUsed();75pool.getUsage().getCommitted();76assertEQ(1L, 1L, "Make assert load");7778// Must do a GC to update performance counters79System.gc();80assertEQ(getMinCapacity(perfNS), pool.getUsage().getInit(), "MinCapacity out of sync");8182// Adding a second GC due to metadata allocations caused by getting the83// initial size from the pool. This is needed when running with -Xcomp.84System.gc();85assertEQ(getUsed(perfNS), pool.getUsage().getUsed(), "Used out of sync");86assertEQ(getCapacity(perfNS), pool.getUsage().getCommitted(), "Committed out of sync");87}8889private static long getMinCapacity(String ns) throws Exception {90return PerfCounters.findByName(ns + ".minCapacity").longValue();91}9293private static long getCapacity(String ns) throws Exception {94return PerfCounters.findByName(ns + ".capacity").longValue();95}9697private static long getUsed(String ns) throws Exception {98return PerfCounters.findByName(ns + ".used").longValue();99}100}101102103