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  */
  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 /testlibrary /../../test/lib
  29  * @build VirtualAllocCommitUncommitRecommit
  30  * @run main ClassFileInstaller jdk.testlib.WhiteBox
  31  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail VirtualAllocCommitUncommitRecommit
  32  *
  33  */
  34 
  35 import com.oracle.java.testlibrary.*;
  36 
  37 import jdk.testlib.WhiteBox;
  38 
  39 public class VirtualAllocCommitUncommitRecommit {
  40 
  41     public static WhiteBox wb = WhiteBox.getWhiteBox();
  42 
  43     public static void main(String args[]) throws Exception {
  44         OutputAnalyzer output;
  45         long commitSize = 128 * 1024; // 128KB
  46         long reserveSize = 4 * 1024 * 1024; // 4096KB
  47         long addr;
  48 
  49         String pid = Integer.toString(ProcessTools.getProcessId());
  50         ProcessBuilder pb = new ProcessBuilder();
  51 
  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         }
  58 
  59         // reserve
  60         addr = wb.NMTReserveMemory(reserveSize);
  61         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid,
  62                 "VM.native_memory", "detail" });
  63 
  64         output = new OutputAnalyzer(pb.start());
  65         output.shouldContain("Test (reserved=4096KB, committed=0KB)");
  66         if (has_nmt_detail) {
  67             output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
  68                     + Long.toHexString(addr + reserveSize)
  69                     + "\\] reserved 4096KB for Test");
  70         }
  71 
  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);
  78 
  79         // commit ABCD
  80         wb.NMTCommitMemory(addrA, commitSize);
  81         wb.NMTCommitMemory(addrB, commitSize);
  82         wb.NMTCommitMemory(addrC, commitSize);
  83         wb.NMTCommitMemory(addrD, commitSize);
  84 
  85         output = new OutputAnalyzer(pb.start());
  86         output.shouldContain("Test (reserved=4096KB, committed=512KB)");
  87 
  88         if (has_nmt_detail) {
  89             output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
  90                     + Long.toHexString(addr + reserveSize)
  91                     + "\\] reserved 4096KB for Test");
  92         }
  93         // uncommit BC
  94         wb.NMTUncommitMemory(addrB, commitSize);
  95         wb.NMTUncommitMemory(addrC, commitSize);
  96 
  97         output = new OutputAnalyzer(pb.start());
  98         output.shouldContain("Test (reserved=4096KB, committed=256KB)");
  99 
 100         if (has_nmt_detail) {
 101             output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
 102                     + Long.toHexString(addr + reserveSize)
 103                     + "\\] reserved 4096KB for Test");
 104         }
 105 
 106         // commit EF
 107         wb.NMTCommitMemory(addrE, commitSize);
 108         wb.NMTCommitMemory(addrF, commitSize);
 109 
 110         output = new OutputAnalyzer(pb.start());
 111         output.shouldContain("Test (reserved=4096KB, committed=512KB)");
 112         if (has_nmt_detail) {
 113             output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
 114                     + Long.toHexString(addr + reserveSize)
 115                     + "\\] reserved 4096KB for Test");
 116         }
 117 
 118         // uncommit A
 119         wb.NMTUncommitMemory(addrA, commitSize);
 120 
 121         output = new OutputAnalyzer(pb.start());
 122         output.shouldContain("Test (reserved=4096KB, committed=384KB)");
 123         if (has_nmt_detail) {
 124             output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
 125                     + Long.toHexString(addr + reserveSize)
 126                     + "\\] reserved 4096KB for Test");
 127         }
 128 
 129         // commit ABC
 130         wb.NMTCommitMemory(addrA, commitSize);
 131         wb.NMTCommitMemory(addrB, commitSize);
 132         wb.NMTCommitMemory(addrC, commitSize);
 133 
 134         output = new OutputAnalyzer(pb.start());
 135         output.shouldContain("Test (reserved=4096KB, committed=768KB)");
 136         if (has_nmt_detail) {
 137             output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
 138                     + Long.toHexString(addr + reserveSize)
 139                     + "\\] reserved 4096KB for Test");
 140         }
 141 
 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);
 149 
 150         output = new OutputAnalyzer(pb.start());
 151         output.shouldContain("Test (reserved=4096KB, committed=0KB)");
 152         if (has_nmt_detail) {
 153             output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
 154                     + Long.toHexString(addr + reserveSize)
 155                     + "\\] reserved 4096KB for Test");
 156         }
 157 
 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 4096KB for Test");
 164     }
 165 }