Path: blob/master/test/jdk/java/lang/management/BufferPoolMXBean/Basic.java
41153 views
/*1* Copyright (c) 2007, 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/* @test24* @bug 6606598 702417225* @summary Unit test for java.lang.management.BufferPoolMXBean26* @modules jdk.management27* @run main/othervm Basic28* @key randomness29*/3031import java.nio.ByteBuffer;32import java.nio.MappedByteBuffer;33import java.nio.file.Path;34import java.nio.file.Files;35import static java.nio.file.StandardOpenOption.*;36import java.nio.channels.FileChannel;37import java.lang.management.BufferPoolMXBean;38import java.lang.management.ManagementFactory;39import javax.management.MBeanServer;40import javax.management.ObjectName;41import java.lang.ref.WeakReference;42import java.util.*;4344public class Basic {4546// static fields to ensure buffers aren't GC'ed47static List<ByteBuffer> buffers;48static MappedByteBuffer mbb;4950// check counters51static void check(List<BufferPoolMXBean> pools,52int minBufferCount,53long minTotalCapacity)54{55int bufferCount = 0;56long totalCap = 0;57long totalMem = 0;58for (BufferPoolMXBean pool: pools) {59bufferCount += pool.getCount();60totalCap += pool.getTotalCapacity();61totalMem += pool.getMemoryUsed();62}63if (bufferCount < minBufferCount)64throw new RuntimeException("Count less than expected");65if (totalMem < minTotalCapacity)66throw new RuntimeException("Memory usage less than expected");67if (totalCap < minTotalCapacity)68throw new RuntimeException("Total capacity less than expected");69}7071public static void main(String[] args) throws Exception {72Random rand = new Random();7374// allocate a few direct buffers75int bufferCount = 5 + rand.nextInt(20);76buffers = new ArrayList<ByteBuffer>(bufferCount);77long totalCapacity = 0L;78for (int i=0; i<bufferCount; i++) {79int cap = 1024 + rand.nextInt(4096);80buffers.add( ByteBuffer.allocateDirect(cap) );81totalCapacity += cap;82}8384// create a mapped buffer85Path tmpfile = Files.createTempFile("blah", null);86tmpfile.toFile().deleteOnExit();87try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {88mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);89bufferCount++;90totalCapacity += mbb.capacity();91}9293// use platform MXBeans directly94List<BufferPoolMXBean> pools =95ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);96check(pools, bufferCount, totalCapacity);9798// use MBeanServer99MBeanServer server = ManagementFactory.getPlatformMBeanServer();100Set<ObjectName> mbeans = server.queryNames(101new ObjectName("java.nio:type=BufferPool,*"), null);102pools = new ArrayList<BufferPoolMXBean>();103for (ObjectName name: mbeans) {104BufferPoolMXBean pool = ManagementFactory105.newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);106pools.add(pool);107}108check(pools, bufferCount, totalCapacity);109110// attempt to unmap mapped buffer111WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);112mbb = null;113do {114System.gc();115Thread.sleep(250);116} while (ref.get() != null);117}118}119120121