VirtualAllocCommitUncommitRecommit.java 6.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
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
 * 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
28
 * @library /testlibrary /../../test/lib
29 30
 * @modules java.base/sun.misc
 *          java.management
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * @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;
47 48
        long commitSize = 128 * 1024; // 128KB
        long reserveSize = 4 * 1024 * 1024; // 4096KB
49 50 51 52 53 54 55 56 57 58 59
        long addr;

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

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

        output = new OutputAnalyzer(pb.start());
60
        output.shouldContain("Test (reserved=4096KB, committed=0KB)");
61 62 63
        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                           + Long.toHexString(addr + reserveSize)
                           + "\\] reserved 4096KB for Test");
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

        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());
79
        output.shouldContain("Test (reserved=4096KB, committed=512KB)");
80

81 82 83
        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                           + Long.toHexString(addr + reserveSize)
                           + "\\] reserved 4096KB for Test");
84 85 86 87 88
        // uncommit BC
        wb.NMTUncommitMemory(addrB, commitSize);
        wb.NMTUncommitMemory(addrC, commitSize);

        output = new OutputAnalyzer(pb.start());
89
        output.shouldContain("Test (reserved=4096KB, committed=256KB)");
90

91 92 93
        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                            + Long.toHexString(addr + reserveSize)
                            + "\\] reserved 4096KB for Test");
94 95 96 97 98 99

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

        output = new OutputAnalyzer(pb.start());
100
        output.shouldContain("Test (reserved=4096KB, committed=512KB)");
101 102 103
        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                           + Long.toHexString(addr + reserveSize)
                           + "\\] reserved 4096KB for Test");
104 105 106 107 108

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

        output = new OutputAnalyzer(pb.start());
109
        output.shouldContain("Test (reserved=4096KB, committed=384KB)");
110 111 112
        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                           + Long.toHexString(addr + reserveSize)
                           + "\\] reserved 4096KB for Test");
113 114 115 116 117 118 119

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

        output = new OutputAnalyzer(pb.start());
120
        output.shouldContain("Test (reserved=4096KB, committed=768KB)");
121 122 123
        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                           + Long.toHexString(addr + reserveSize)
                           + "\\] reserved 4096KB for Test");
124 125 126 127 128 129 130 131 132 133

        // 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());
134
        output.shouldContain("Test (reserved=4096KB, committed=0KB)");
135 136 137
        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
                           + Long.toHexString(addr + reserveSize)
                           + "\\] reserved 4096KB for Test");
138 139 140 141 142 143

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