VirtualAllocCommitUncommitRecommit.java 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/*
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @summary Test reserve/commit/uncommit/release of virtual memory and that we track it correctly
 * @key nmt jcmd
 * @library /testlibrary /testlibrary/whitebox
 * @build VirtualAllocCommitUncommitRecommit
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail VirtualAllocCommitUncommitRecommit
 *
 */

import com.oracle.java.testlibrary.*;

import sun.hotspot.WhiteBox;

public class VirtualAllocCommitUncommitRecommit {

    public static WhiteBox wb = WhiteBox.getWhiteBox();

    public static void main(String args[]) throws Exception {
        OutputAnalyzer output;
        long commitSize = 4 * 1024; // 4KB
        long reserveSize = 1024 * 1024; // 1024KB
        long addr;

        String pid = Integer.toString(ProcessTools.getProcessId());
        ProcessBuilder pb = new ProcessBuilder();

        boolean has_nmt_detail = wb.NMTIsDetailSupported();
        if (has_nmt_detail) {
            System.out.println("NMT detail support detected.");
        } else {
            System.out.println("NMT detail support not detected.");
        }

        // reserve
        addr = wb.NMTReserveMemory(reserveSize);
        pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid,
                "VM.native_memory", "detail" });

        output = new OutputAnalyzer(pb.start());
        output.shouldContain("Test (reserved=1024KB, committed=0KB)");
        if (has_nmt_detail) {
            output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                    + Long.toHexString(addr + reserveSize)
                    + "\\] reserved 1024KB for Test");
        }

        long addrA = addr;
        long addrB = addr + commitSize;
        long addrC = addr + (2 * commitSize);
        long addrD = addr + (3 * commitSize);
        long addrE = addr + (4 * commitSize);
        long addrF = addr + (5 * commitSize);

        // commit ABCD
        wb.NMTCommitMemory(addrA, commitSize);
        wb.NMTCommitMemory(addrB, commitSize);
        wb.NMTCommitMemory(addrC, commitSize);
        wb.NMTCommitMemory(addrD, commitSize);

        output = new OutputAnalyzer(pb.start());
        output.shouldContain("Test (reserved=1024KB, committed=16KB)");

        if (has_nmt_detail) {
            output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                    + Long.toHexString(addr + reserveSize)
                    + "\\] reserved 1024KB for Test");
        }
        // uncommit BC
        wb.NMTUncommitMemory(addrB, commitSize);
        wb.NMTUncommitMemory(addrC, commitSize);

        output = new OutputAnalyzer(pb.start());
        output.shouldContain("Test (reserved=1024KB, committed=8KB)");

        if (has_nmt_detail) {
            output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                    + Long.toHexString(addr + reserveSize)
                    + "\\] reserved 1024KB for Test");
        }

        // commit EF
        wb.NMTCommitMemory(addrE, commitSize);
        wb.NMTCommitMemory(addrF, commitSize);

        output = new OutputAnalyzer(pb.start());
        output.shouldContain("Test (reserved=1024KB, committed=16KB)");
        if (has_nmt_detail) {
            output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                    + Long.toHexString(addr + reserveSize)
                    + "\\] reserved 1024KB for Test");
        }

        // uncommit A
        wb.NMTUncommitMemory(addrA, commitSize);

        output = new OutputAnalyzer(pb.start());
        output.shouldContain("Test (reserved=1024KB, committed=12KB)");
        if (has_nmt_detail) {
            output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                    + Long.toHexString(addr + reserveSize)
                    + "\\] reserved 1024KB for Test");
        }

        // commit ABC
        wb.NMTCommitMemory(addrA, commitSize);
        wb.NMTCommitMemory(addrB, commitSize);
        wb.NMTCommitMemory(addrC, commitSize);

        output = new OutputAnalyzer(pb.start());
        output.shouldContain("Test (reserved=1024KB, committed=24KB)");
        if (has_nmt_detail) {
            output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                    + Long.toHexString(addr + reserveSize)
                    + "\\] reserved 1024KB for Test");
        }

        // uncommit ABCDEF
        wb.NMTUncommitMemory(addrA, commitSize);
        wb.NMTUncommitMemory(addrB, commitSize);
        wb.NMTUncommitMemory(addrC, commitSize);
        wb.NMTUncommitMemory(addrD, commitSize);
        wb.NMTUncommitMemory(addrE, commitSize);
        wb.NMTUncommitMemory(addrF, commitSize);

        output = new OutputAnalyzer(pb.start());
        output.shouldContain("Test (reserved=1024KB, committed=0KB)");
        if (has_nmt_detail) {
            output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                    + Long.toHexString(addr + reserveSize)
                    + "\\] reserved 1024KB for Test");
        }

        // release
        wb.NMTReleaseMemory(addr, reserveSize);
        output = new OutputAnalyzer(pb.start());
        output.shouldNotContain("Test (reserved=");
        output.shouldNotMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
163
                + Long.toHexString(addr + reserveSize) + "\\] reserved 1024KB for Test");
164 165
    }
}