1 /*
   2  * Copyright (c) 2015, 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 commits of overlapping regions of memory.
  27  * @key nmt jcmd
  28  * @library /testlibrary /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  * @build   CommitOverlappingRegions
  32  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  34  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail CommitOverlappingRegions
  35  */
  36 
  37 import jdk.test.lib.*;
  38 import sun.hotspot.WhiteBox;
  39 
  40 public class CommitOverlappingRegions {
  41     public static WhiteBox wb = WhiteBox.getWhiteBox();
  42     public static void main(String args[]) throws Exception {
  43         OutputAnalyzer output;
  44 
  45         long size = 32 * 1024;
  46         int pagesize = wb.getVMPageSize();
  47         if (size < pagesize) { size = pagesize; }  // Should be aligned.
  48         long sizek = size / 1024;
  49 
  50         long addr = wb.NMTReserveMemory(8*size);
  51 
  52         String pid = Long.toString(ProcessTools.getProcessId());
  53         ProcessBuilder pb = new ProcessBuilder();
  54 
  55         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "detail"});
  56         System.out.println("Address is " + Long.toHexString(addr));
  57 
  58         // Start: . . . . . . . .
  59         output = new OutputAnalyzer(pb.start());
  60         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=0KB)");
  61 
  62         // Committing: * * * . . . . .
  63         // Region:     * * * . . . . .
  64         // Expected Total: 3 x sizek KB
  65         wb.NMTCommitMemory(addr + 0*size, 3*size);
  66 
  67         // Committing: . . . . * * * .
  68         // Region:     * * * . * * * .
  69         // Expected Total: 6 x sizek KB
  70         wb.NMTCommitMemory(addr + 4*size, 3*size);
  71 
  72         // Check output after first 2 commits.
  73         output = new OutputAnalyzer(pb.start());
  74         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 6*sizek + "KB)");
  75 
  76         // Committing: . . * * * . . .
  77         // Region:     * * * * * * * .
  78         // Expected Total: 7 x sizek KB
  79         wb.NMTCommitMemory(addr + 2*size, 3*size);
  80 
  81         // Check output after overlapping commit.
  82         output = new OutputAnalyzer(pb.start());
  83         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 7*sizek + "KB)");
  84 
  85         // Uncommitting: * * * * * * * *
  86         // Region:       . . . . . . . .
  87         // Expected Total: 0 x sizek KB
  88         wb.NMTUncommitMemory(addr + 0*size, 8*size);
  89         output = new OutputAnalyzer(pb.start());
  90         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=0KB)");
  91 
  92         // Committing: * * . . . . . .
  93         // Region:     * * . . . . . .
  94         // Expected Total: 2 x sizek KB
  95         wb.NMTCommitMemory(addr + 0*size, 2*size);
  96         output = new OutputAnalyzer(pb.start());
  97         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 2*sizek + "KB)");
  98 
  99         // Committing: . * * * . . . .
 100         // Region:     * * * * . . . .
 101         // Expected Total: 4 x sizek KB
 102         wb.NMTCommitMemory(addr + 1*size, 3*size);
 103         output = new OutputAnalyzer(pb.start());
 104         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 4*sizek + "KB)");
 105 
 106         // Uncommitting: * * * . . . . .
 107         // Region:       . . . * . . . .
 108         // Expected Total: 1 x sizek KB
 109         wb.NMTUncommitMemory(addr + 0*size, 3*size);
 110         output = new OutputAnalyzer(pb.start());
 111         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 1*sizek + "KB)");
 112 
 113         // Committing: . . . * * . . .
 114         // Region:     . . . * * . . .
 115         // Expected Total: 2 x sizek KB
 116         wb.NMTCommitMemory(addr + 3*size, 2*size);
 117         System.out.println("Address is " + Long.toHexString(addr + 3*size));
 118         output = new OutputAnalyzer(pb.start());
 119         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 2*sizek + "KB)");
 120 
 121         // Committing: . . . . * * . .
 122         // Region:     . . . * * * . .
 123         // Expected Total: 3 x sizek KB
 124         wb.NMTCommitMemory(addr + 4*size, 2*size);
 125         output = new OutputAnalyzer(pb.start());
 126         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 3*sizek + "KB)");
 127 
 128         // Committing: . . . . . * * .
 129         // Region:     . . . * * * * .
 130         // Expected Total: 4 x sizek KB
 131         wb.NMTCommitMemory(addr + 5*size, 2*size);
 132         output = new OutputAnalyzer(pb.start());
 133         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 4*sizek + "KB)");
 134 
 135         // Committing: . . . . . . * *
 136         // Region:     . . . * * * * *
 137         // Expected Total: 5 x sizek KB
 138         wb.NMTCommitMemory(addr + 6*size, 2*size);
 139         output = new OutputAnalyzer(pb.start());
 140         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=" + 5*sizek + "KB)");
 141 
 142         // Uncommitting: * * * * * * * *
 143         // Region:       . . . . . . . .
 144         // Expected Total: 0 x sizek KB
 145         wb.NMTUncommitMemory(addr + 0*size, 8*size);
 146         output = new OutputAnalyzer(pb.start());
 147         output.shouldContain("Test (reserved=" + 8*sizek + "KB, committed=0KB)");
 148     }
 149 }