Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/BufferPoolMXBean/Basic.java
38828 views
/*1* Copyright (c) 2007, 2011, 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/* @test24* @bug 6606598 702417225* @summary Unit test for java.lang.management.BufferPoolMXBean26* @run main/othervm Basic27* @key randomness28*/2930import java.nio.ByteBuffer;31import java.nio.MappedByteBuffer;32import java.nio.file.Path;33import java.nio.file.Files;34import static java.nio.file.StandardOpenOption.*;35import java.nio.channels.FileChannel;36import java.lang.management.BufferPoolMXBean;37import java.lang.management.ManagementFactory;38import javax.management.MBeanServer;39import javax.management.ObjectName;40import java.lang.ref.WeakReference;41import java.util.*;4243public class Basic {4445// static fields to ensure buffers aren't GC'ed46static List<ByteBuffer> buffers;47static MappedByteBuffer mbb;4849// check counters50static void check(List<BufferPoolMXBean> pools,51int minBufferCount,52long minTotalCapacity)53{54int bufferCount = 0;55long totalCap = 0;56long totalMem = 0;57for (BufferPoolMXBean pool: pools) {58bufferCount += pool.getCount();59totalCap += pool.getTotalCapacity();60totalMem += pool.getMemoryUsed();61}62if (bufferCount < minBufferCount)63throw new RuntimeException("Count less than expected");64if (totalMem < minTotalCapacity)65throw new RuntimeException("Memory usage less than expected");66if (totalCap < minTotalCapacity)67throw new RuntimeException("Total capacity less than expected");68}6970public static void main(String[] args) throws Exception {71Random rand = new Random();7273// allocate a few direct buffers74int bufferCount = 5 + rand.nextInt(20);75buffers = new ArrayList<ByteBuffer>(bufferCount);76long totalCapacity = 0L;77for (int i=0; i<bufferCount; i++) {78int cap = 1024 + rand.nextInt(4096);79buffers.add( ByteBuffer.allocateDirect(cap) );80totalCapacity += cap;81}8283// create a mapped buffer84Path tmpfile = Files.createTempFile("blah", null);85tmpfile.toFile().deleteOnExit();86try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {87mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);88bufferCount++;89totalCapacity += mbb.capacity();90}9192// use platform MXBeans directly93List<BufferPoolMXBean> pools =94ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);95check(pools, bufferCount, totalCapacity);9697// use MBeanServer98MBeanServer server = ManagementFactory.getPlatformMBeanServer();99Set<ObjectName> mbeans = server.queryNames(100new ObjectName("java.nio:type=BufferPool,*"), null);101pools = new ArrayList<BufferPoolMXBean>();102for (ObjectName name: mbeans) {103BufferPoolMXBean pool = ManagementFactory104.newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);105pools.add(pool);106}107check(pools, bufferCount, totalCapacity);108109// attempt to unmap mapped buffer110WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);111mbb = null;112do {113System.gc();114Thread.sleep(250);115} while (ref.get() != null);116}117}118119120