1 /* 2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 6606598 7024172 26 * @key randomness 27 * @summary Unit test for java.lang.management.BufferPoolMXBean 28 * 29 * @modules jdk.management 30 * 31 * @run main/othervm Basic 32 */ 33 34 import java.nio.ByteBuffer; 35 import java.nio.MappedByteBuffer; 36 import java.nio.file.Path; 37 import java.nio.file.Files; 38 import static java.nio.file.StandardOpenOption.*; 39 import java.nio.channels.FileChannel; 40 import java.lang.management.BufferPoolMXBean; 41 import java.lang.management.ManagementFactory; 42 import javax.management.MBeanServer; 43 import javax.management.ObjectName; 44 import java.lang.ref.WeakReference; 45 import java.util.*; 46 47 public class Basic { 48 49 // static fields to ensure buffers aren't GC'ed 50 static List<ByteBuffer> buffers; 51 static MappedByteBuffer mbb; 52 53 // check counters 54 static void check(List<BufferPoolMXBean> pools, 55 int minBufferCount, 56 long minTotalCapacity) 57 { 58 int bufferCount = 0; 59 long totalCap = 0; 60 long totalMem = 0; 61 for (BufferPoolMXBean pool: pools) { 62 bufferCount += pool.getCount(); 63 totalCap += pool.getTotalCapacity(); 64 totalMem += pool.getMemoryUsed(); 65 } 66 if (bufferCount < minBufferCount) 67 throw new RuntimeException("Count less than expected"); 68 if (totalMem < minTotalCapacity) 69 throw new RuntimeException("Memory usage less than expected"); 70 if (totalCap < minTotalCapacity) 71 throw new RuntimeException("Total capacity less than expected"); 72 } 73 74 public static void main(String[] args) throws Exception { 75 Random rand = new Random(); 76 77 // allocate a few direct buffers 78 int bufferCount = 5 + rand.nextInt(20); 79 buffers = new ArrayList<ByteBuffer>(bufferCount); 80 long totalCapacity = 0L; 81 for (int i=0; i<bufferCount; i++) { 82 int cap = 1024 + rand.nextInt(4096); 83 buffers.add( ByteBuffer.allocateDirect(cap) ); 84 totalCapacity += cap; 85 } 86 87 // create a mapped buffer 88 Path tmpfile = Files.createTempFile("blah", null); 89 tmpfile.toFile().deleteOnExit(); 90 try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) { 91 mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100); 92 bufferCount++; 93 totalCapacity += mbb.capacity(); 94 } 95 96 // use platform MXBeans directly 97 List<BufferPoolMXBean> pools = 98 ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); 99 check(pools, bufferCount, totalCapacity); 100 101 // use MBeanServer 102 MBeanServer server = ManagementFactory.getPlatformMBeanServer(); 103 Set<ObjectName> mbeans = server.queryNames( 104 new ObjectName("java.nio:type=BufferPool,*"), null); 105 pools = new ArrayList<BufferPoolMXBean>(); 106 for (ObjectName name: mbeans) { 107 BufferPoolMXBean pool = ManagementFactory 108 .newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class); 109 pools.add(pool); 110 } 111 check(pools, bufferCount, totalCapacity); 112 113 // attempt to unmap mapped buffer 114 WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb); 115 mbb = null; 116 do { 117 System.gc(); 118 Thread.sleep(250); 119 } while (ref.get() != null); 120 } 121 }