Tue, 09 Sep 2014 09:48:42 -0700
8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
Reviewed-by: ctornqvi, lfoltan
1 /*
2 * Copyright (c) 2014, 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 */
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 /testlibrary /testlibrary/whitebox
29 * @build VirtualAllocCommitUncommitRecommit
30 * @run main ClassFileInstaller sun.hotspot.WhiteBox
31 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail VirtualAllocCommitUncommitRecommit
32 *
33 */
35 import com.oracle.java.testlibrary.*;
37 import sun.hotspot.WhiteBox;
39 public class VirtualAllocCommitUncommitRecommit {
41 public static WhiteBox wb = WhiteBox.getWhiteBox();
43 public static void main(String args[]) throws Exception {
44 OutputAnalyzer output;
45 long commitSize = 4 * 1024; // 4KB
46 long reserveSize = 1024 * 1024; // 1024KB
47 long addr;
49 String pid = Integer.toString(ProcessTools.getProcessId());
50 ProcessBuilder pb = new ProcessBuilder();
52 boolean has_nmt_detail = wb.NMTIsDetailSupported();
53 if (has_nmt_detail) {
54 System.out.println("NMT detail support detected.");
55 } else {
56 System.out.println("NMT detail support not detected.");
57 }
59 // reserve
60 addr = wb.NMTReserveMemory(reserveSize);
61 pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid,
62 "VM.native_memory", "detail" });
64 output = new OutputAnalyzer(pb.start());
65 output.shouldContain("Test (reserved=1024KB, committed=0KB)");
66 if (has_nmt_detail) {
67 output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
68 + Long.toHexString(addr + reserveSize)
69 + "\\] reserved 1024KB for Test");
70 }
72 long addrA = addr;
73 long addrB = addr + commitSize;
74 long addrC = addr + (2 * commitSize);
75 long addrD = addr + (3 * commitSize);
76 long addrE = addr + (4 * commitSize);
77 long addrF = addr + (5 * commitSize);
79 // commit ABCD
80 wb.NMTCommitMemory(addrA, commitSize);
81 wb.NMTCommitMemory(addrB, commitSize);
82 wb.NMTCommitMemory(addrC, commitSize);
83 wb.NMTCommitMemory(addrD, commitSize);
85 output = new OutputAnalyzer(pb.start());
86 output.shouldContain("Test (reserved=1024KB, committed=16KB)");
88 if (has_nmt_detail) {
89 output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
90 + Long.toHexString(addr + reserveSize)
91 + "\\] reserved 1024KB for Test");
92 }
93 // uncommit BC
94 wb.NMTUncommitMemory(addrB, commitSize);
95 wb.NMTUncommitMemory(addrC, commitSize);
97 output = new OutputAnalyzer(pb.start());
98 output.shouldContain("Test (reserved=1024KB, committed=8KB)");
100 if (has_nmt_detail) {
101 output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
102 + Long.toHexString(addr + reserveSize)
103 + "\\] reserved 1024KB for Test");
104 }
106 // commit EF
107 wb.NMTCommitMemory(addrE, commitSize);
108 wb.NMTCommitMemory(addrF, commitSize);
110 output = new OutputAnalyzer(pb.start());
111 output.shouldContain("Test (reserved=1024KB, committed=16KB)");
112 if (has_nmt_detail) {
113 output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
114 + Long.toHexString(addr + reserveSize)
115 + "\\] reserved 1024KB for Test");
116 }
118 // uncommit A
119 wb.NMTUncommitMemory(addrA, commitSize);
121 output = new OutputAnalyzer(pb.start());
122 output.shouldContain("Test (reserved=1024KB, committed=12KB)");
123 if (has_nmt_detail) {
124 output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
125 + Long.toHexString(addr + reserveSize)
126 + "\\] reserved 1024KB for Test");
127 }
129 // commit ABC
130 wb.NMTCommitMemory(addrA, commitSize);
131 wb.NMTCommitMemory(addrB, commitSize);
132 wb.NMTCommitMemory(addrC, commitSize);
134 output = new OutputAnalyzer(pb.start());
135 output.shouldContain("Test (reserved=1024KB, committed=24KB)");
136 if (has_nmt_detail) {
137 output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
138 + Long.toHexString(addr + reserveSize)
139 + "\\] reserved 1024KB for Test");
140 }
142 // uncommit ABCDEF
143 wb.NMTUncommitMemory(addrA, commitSize);
144 wb.NMTUncommitMemory(addrB, commitSize);
145 wb.NMTUncommitMemory(addrC, commitSize);
146 wb.NMTUncommitMemory(addrD, commitSize);
147 wb.NMTUncommitMemory(addrE, commitSize);
148 wb.NMTUncommitMemory(addrF, commitSize);
150 output = new OutputAnalyzer(pb.start());
151 output.shouldContain("Test (reserved=1024KB, committed=0KB)");
152 if (has_nmt_detail) {
153 output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
154 + Long.toHexString(addr + reserveSize)
155 + "\\] reserved 1024KB for Test");
156 }
158 // release
159 wb.NMTReleaseMemory(addr, reserveSize);
160 output = new OutputAnalyzer(pb.start());
161 output.shouldNotContain("Test (reserved=");
162 output.shouldNotMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
163 + Long.toHexString(addr + reserveSize) + "\\] reserved 1024KB for Test");
164 }
165 }