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