515N/A/*
3261N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
515N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
515N/A *
515N/A * This code is free software; you can redistribute it and/or modify it
515N/A * under the terms of the GNU General Public License version 2 only, as
515N/A * published by the Free Software Foundation.
515N/A *
515N/A * This code is distributed in the hope that it will be useful, but WITHOUT
515N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
515N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
515N/A * version 2 for more details (a copy is included in the LICENSE file that
515N/A * accompanied this code).
515N/A *
515N/A * You should have received a copy of the GNU General Public License version
515N/A * 2 along with this work; if not, write to the Free Software Foundation,
515N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
515N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
515N/A */
515N/A
515N/A/* @test
3887N/A * @bug 6606598 7024172
3887N/A * @summary Unit test for java.lang.management.BufferPoolMXBean
2546N/A * @run main/othervm Basic
515N/A */
515N/A
515N/Aimport java.nio.ByteBuffer;
515N/Aimport java.nio.MappedByteBuffer;
3887N/Aimport java.nio.file.Path;
3887N/Aimport java.nio.file.Files;
3887N/Aimport static java.nio.file.StandardOpenOption.*;
515N/Aimport java.nio.channels.FileChannel;
3887N/Aimport java.lang.management.BufferPoolMXBean;
515N/Aimport java.lang.management.ManagementFactory;
515N/Aimport javax.management.MBeanServer;
515N/Aimport javax.management.ObjectName;
3887N/Aimport java.lang.ref.WeakReference;
515N/Aimport java.util.*;
515N/A
515N/Apublic class Basic {
515N/A
515N/A // static fields to ensure buffers aren't GC'ed
515N/A static List<ByteBuffer> buffers;
515N/A static MappedByteBuffer mbb;
515N/A
515N/A // check counters
515N/A static void check(List<BufferPoolMXBean> pools,
515N/A int minBufferCount,
515N/A long minTotalCapacity)
515N/A {
515N/A int bufferCount = 0;
515N/A long totalCap = 0;
515N/A long totalMem = 0;
515N/A for (BufferPoolMXBean pool: pools) {
515N/A bufferCount += pool.getCount();
515N/A totalCap += pool.getTotalCapacity();
515N/A totalMem += pool.getMemoryUsed();
515N/A }
515N/A if (bufferCount < minBufferCount)
515N/A throw new RuntimeException("Count less than expected");
515N/A if (totalMem < minTotalCapacity)
515N/A throw new RuntimeException("Memory usage less than expected");
515N/A if (totalCap < minTotalCapacity)
515N/A throw new RuntimeException("Total capacity less than expected");
515N/A }
515N/A
515N/A public static void main(String[] args) throws Exception {
515N/A Random rand = new Random();
515N/A
515N/A // allocate a few direct buffers
515N/A int bufferCount = 5 + rand.nextInt(20);
515N/A buffers = new ArrayList<ByteBuffer>(bufferCount);
515N/A long totalCapacity = 0L;
515N/A for (int i=0; i<bufferCount; i++) {
515N/A int cap = 1024 + rand.nextInt(4096);
515N/A buffers.add( ByteBuffer.allocateDirect(cap) );
515N/A totalCapacity += cap;
515N/A }
515N/A
3887N/A // create a mapped buffer
3887N/A Path tmpfile = Files.createTempFile("blah", null);
3887N/A tmpfile.toFile().deleteOnExit();
3887N/A try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {
3887N/A mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);
3887N/A bufferCount++;
3887N/A totalCapacity += mbb.capacity();
3887N/A }
515N/A
3887N/A // use platform MXBeans directly
515N/A List<BufferPoolMXBean> pools =
515N/A ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
515N/A check(pools, bufferCount, totalCapacity);
515N/A
3887N/A // use MBeanServer
515N/A MBeanServer server = ManagementFactory.getPlatformMBeanServer();
515N/A Set<ObjectName> mbeans = server.queryNames(
515N/A new ObjectName("java.nio:type=BufferPool,*"), null);
515N/A pools = new ArrayList<BufferPoolMXBean>();
515N/A for (ObjectName name: mbeans) {
515N/A BufferPoolMXBean pool = ManagementFactory
515N/A .newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);
515N/A pools.add(pool);
515N/A }
515N/A check(pools, bufferCount, totalCapacity);
3887N/A
3887N/A // attempt to unmap mapped buffer
3887N/A WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);
3887N/A mbb = null;
3887N/A do {
3887N/A System.gc();
3887N/A Thread.sleep(250);
3887N/A } while (ref.get() != null);
515N/A }
515N/A}