1/*
2 * Copyright (c) 2014, 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/*
25 * @test
26 * @summary Test reserve/commit/uncommit/release of virtual memory and that we track it correctly
27 * @key nmt jcmd
28 * @library /test/lib
29 * @modules java.base/jdk.internal.misc
30 *          java.management
31 * @build sun.hotspot.WhiteBox
32 * @run main ClassFileInstaller sun.hotspot.WhiteBox
33 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail VirtualAllocCommitUncommitRecommit
34 *
35 */
36
37import jdk.test.lib.process.ProcessTools;
38import jdk.test.lib.process.OutputAnalyzer;
39import jdk.test.lib.JDKToolFinder;
40
41import sun.hotspot.WhiteBox;
42
43public class VirtualAllocCommitUncommitRecommit {
44
45    public static WhiteBox wb = WhiteBox.getWhiteBox();
46
47    public static void main(String args[]) throws Exception {
48        OutputAnalyzer output;
49        long commitSize = 128 * 1024; // 128KB
50        long reserveSize = 4 * 1024 * 1024; // 4096KB
51        long addr;
52
53        String pid = Long.toString(ProcessTools.getProcessId());
54        ProcessBuilder pb = new ProcessBuilder();
55
56        // reserve
57        addr = wb.NMTReserveMemory(reserveSize);
58        pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid,
59                "VM.native_memory", "detail" });
60
61        output = new OutputAnalyzer(pb.start());
62        output.shouldContain("Test (reserved=4096KB, committed=0KB)");
63        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
64                           + Long.toHexString(addr + reserveSize)
65                           + "\\] reserved 4096KB for Test");
66
67        long addrA = addr;
68        long addrB = addr + commitSize;
69        long addrC = addr + (2 * commitSize);
70        long addrD = addr + (3 * commitSize);
71        long addrE = addr + (4 * commitSize);
72        long addrF = addr + (5 * commitSize);
73
74        // commit ABCD
75        wb.NMTCommitMemory(addrA, commitSize);
76        wb.NMTCommitMemory(addrB, commitSize);
77        wb.NMTCommitMemory(addrC, commitSize);
78        wb.NMTCommitMemory(addrD, commitSize);
79
80        output = new OutputAnalyzer(pb.start());
81        output.shouldContain("Test (reserved=4096KB, committed=512KB)");
82
83        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
84                           + Long.toHexString(addr + reserveSize)
85                           + "\\] reserved 4096KB for Test");
86        // uncommit BC
87        wb.NMTUncommitMemory(addrB, commitSize);
88        wb.NMTUncommitMemory(addrC, commitSize);
89
90        output = new OutputAnalyzer(pb.start());
91        output.shouldContain("Test (reserved=4096KB, committed=256KB)");
92
93        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
94                            + Long.toHexString(addr + reserveSize)
95                            + "\\] reserved 4096KB for Test");
96
97        // commit EF
98        wb.NMTCommitMemory(addrE, commitSize);
99        wb.NMTCommitMemory(addrF, commitSize);
100
101        output = new OutputAnalyzer(pb.start());
102        output.shouldContain("Test (reserved=4096KB, committed=512KB)");
103        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
104                           + Long.toHexString(addr + reserveSize)
105                           + "\\] reserved 4096KB for Test");
106
107        // uncommit A
108        wb.NMTUncommitMemory(addrA, commitSize);
109
110        output = new OutputAnalyzer(pb.start());
111        output.shouldContain("Test (reserved=4096KB, committed=384KB)");
112        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
113                           + Long.toHexString(addr + reserveSize)
114                           + "\\] reserved 4096KB for Test");
115
116        // commit ABC
117        wb.NMTCommitMemory(addrA, commitSize);
118        wb.NMTCommitMemory(addrB, commitSize);
119        wb.NMTCommitMemory(addrC, commitSize);
120
121        output = new OutputAnalyzer(pb.start());
122        output.shouldContain("Test (reserved=4096KB, committed=768KB)");
123        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
124                           + Long.toHexString(addr + reserveSize)
125                           + "\\] reserved 4096KB for Test");
126
127        // uncommit ABCDEF
128        wb.NMTUncommitMemory(addrA, commitSize);
129        wb.NMTUncommitMemory(addrB, commitSize);
130        wb.NMTUncommitMemory(addrC, commitSize);
131        wb.NMTUncommitMemory(addrD, commitSize);
132        wb.NMTUncommitMemory(addrE, commitSize);
133        wb.NMTUncommitMemory(addrF, commitSize);
134
135        output = new OutputAnalyzer(pb.start());
136        output.shouldContain("Test (reserved=4096KB, committed=0KB)");
137        output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
138                           + Long.toHexString(addr + reserveSize)
139                           + "\\] reserved 4096KB for Test");
140
141        // release
142        wb.NMTReleaseMemory(addr, reserveSize);
143        output = new OutputAnalyzer(pb.start());
144        output.shouldNotContain("Test (reserved=");
145        output.shouldNotMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
146                + Long.toHexString(addr + reserveSize) + "\\] reserved 4096KB for Test");
147    }
148}
149