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