test/hotspot/jtreg/runtime/NMT/VirtualAllocCommitMerge.java
changeset 48818 ec4a84ba2aaf
parent 48817 65a0cf59bfd8
child 48819 ee513596f3ee
equal deleted inserted replaced
48817:65a0cf59bfd8 48818:ec4a84ba2aaf
     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 merging of committed 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 VirtualAllocCommitMerge
       
    34  *
       
    35  */
       
    36 
       
    37 import jdk.test.lib.process.ProcessTools;
       
    38 import jdk.test.lib.process.OutputAnalyzer;
       
    39 import jdk.test.lib.JDKToolFinder;
       
    40 
       
    41 import sun.hotspot.WhiteBox;
       
    42 
       
    43 public class VirtualAllocCommitMerge {
       
    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         checkReservedCommittedSummary(output, "4096KB", "0KB");
       
    63         checkReserved(output, addr, reserveSize, "4096KB");
       
    64 
       
    65         long addrA = addr + (0 * commitSize);
       
    66         long addrB = addr + (1 * commitSize);
       
    67         long addrC = addr + (2 * commitSize);
       
    68         long addrD = addr + (3 * commitSize);
       
    69         long addrE = addr + (4 * commitSize);
       
    70 
       
    71         // Test discontigous areas
       
    72         {
       
    73             // commit ACE
       
    74             wb.NMTCommitMemory(addrA, commitSize);
       
    75             wb.NMTCommitMemory(addrC, commitSize);
       
    76             wb.NMTCommitMemory(addrE, commitSize);
       
    77 
       
    78             output = new OutputAnalyzer(pb.start());
       
    79             checkReservedCommittedSummary(output, "4096KB", "384KB");
       
    80             checkReserved(output, addr, reserveSize, "4096KB");
       
    81 
       
    82             checkCommitted(output, addrA, commitSize, "128KB");
       
    83             checkCommitted(output, addrC, commitSize, "128KB");
       
    84             checkCommitted(output, addrE, commitSize, "128KB");
       
    85 
       
    86             // uncommit ACE
       
    87             wb.NMTUncommitMemory(addrA, commitSize);
       
    88             wb.NMTUncommitMemory(addrC, commitSize);
       
    89             wb.NMTUncommitMemory(addrE, commitSize);
       
    90 
       
    91             output = new OutputAnalyzer(pb.start());
       
    92             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
    93         }
       
    94 
       
    95         // Test contiguous areas
       
    96         {
       
    97             // commit AB
       
    98             wb.NMTCommitMemory(addrA, commitSize);
       
    99             wb.NMTCommitMemory(addrB, commitSize);
       
   100 
       
   101             output = new OutputAnalyzer(pb.start());
       
   102             checkReservedCommittedSummary(output, "4096KB", "256KB");
       
   103             checkReserved(output, addr, reserveSize, "4096KB");
       
   104 
       
   105             checkCommitted(output, addrA, 2 * commitSize, "256KB");
       
   106 
       
   107             // uncommit AB
       
   108             wb.NMTUncommitMemory(addrA, commitSize);
       
   109             wb.NMTUncommitMemory(addrB, commitSize);
       
   110 
       
   111             output = new OutputAnalyzer(pb.start());
       
   112             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   113         }
       
   114 
       
   115         {
       
   116             // commit BA
       
   117             wb.NMTCommitMemory(addrB, commitSize);
       
   118             wb.NMTCommitMemory(addrA, commitSize);
       
   119 
       
   120             output = new OutputAnalyzer(pb.start());
       
   121             checkReservedCommittedSummary(output, "4096KB", "256KB");
       
   122             checkReserved(output, addr, reserveSize, "4096KB");
       
   123 
       
   124             checkCommitted(output, addrA, 2 * commitSize, "256KB");
       
   125 
       
   126             // uncommit AB
       
   127             wb.NMTUncommitMemory(addrB, commitSize);
       
   128             wb.NMTUncommitMemory(addrA, commitSize);
       
   129 
       
   130             output = new OutputAnalyzer(pb.start());
       
   131             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   132         }
       
   133 
       
   134         {
       
   135             // commit ABC
       
   136             wb.NMTCommitMemory(addrA, commitSize);
       
   137             wb.NMTCommitMemory(addrB, commitSize);
       
   138             wb.NMTCommitMemory(addrC, commitSize);
       
   139 
       
   140             output = new OutputAnalyzer(pb.start());
       
   141             checkReservedCommittedSummary(output, "4096KB", "384KB");
       
   142             checkReserved(output, addr, reserveSize, "4096KB");
       
   143 
       
   144             checkCommitted(output, addrA, 3 * commitSize, "384KB");
       
   145 
       
   146             // uncommit
       
   147             wb.NMTUncommitMemory(addrA, commitSize);
       
   148             wb.NMTUncommitMemory(addrB, commitSize);
       
   149             wb.NMTUncommitMemory(addrC, commitSize);
       
   150 
       
   151             output = new OutputAnalyzer(pb.start());
       
   152             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   153         }
       
   154 
       
   155         {
       
   156             // commit ACB
       
   157             wb.NMTCommitMemory(addrA, commitSize);
       
   158             wb.NMTCommitMemory(addrC, commitSize);
       
   159             wb.NMTCommitMemory(addrB, commitSize);
       
   160 
       
   161             output = new OutputAnalyzer(pb.start());
       
   162             checkReservedCommittedSummary(output, "4096KB", "384KB");
       
   163             checkReserved(output, addr, reserveSize, "4096KB");
       
   164 
       
   165             checkCommitted(output, addrA, 3 * commitSize, "384KB");
       
   166 
       
   167             // uncommit
       
   168             wb.NMTUncommitMemory(addrA, commitSize);
       
   169             wb.NMTUncommitMemory(addrC, commitSize);
       
   170             wb.NMTUncommitMemory(addrB, commitSize);
       
   171 
       
   172             output = new OutputAnalyzer(pb.start());
       
   173             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   174         }
       
   175 
       
   176         {
       
   177             // commit BAC
       
   178             wb.NMTCommitMemory(addrB, commitSize);
       
   179             wb.NMTCommitMemory(addrA, commitSize);
       
   180             wb.NMTCommitMemory(addrC, commitSize);
       
   181 
       
   182             output = new OutputAnalyzer(pb.start());
       
   183             checkReservedCommittedSummary(output, "4096KB", "384KB");
       
   184             checkReserved(output, addr, reserveSize, "4096KB");
       
   185 
       
   186             checkCommitted(output, addrA, 3 * commitSize, "384KB");
       
   187 
       
   188             // uncommit
       
   189             wb.NMTUncommitMemory(addrB, commitSize);
       
   190             wb.NMTUncommitMemory(addrA, commitSize);
       
   191             wb.NMTUncommitMemory(addrC, commitSize);
       
   192 
       
   193             output = new OutputAnalyzer(pb.start());
       
   194             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   195         }
       
   196 
       
   197         {
       
   198             // commit BCA
       
   199             wb.NMTCommitMemory(addrB, commitSize);
       
   200             wb.NMTCommitMemory(addrC, commitSize);
       
   201             wb.NMTCommitMemory(addrA, commitSize);
       
   202 
       
   203             output = new OutputAnalyzer(pb.start());
       
   204             checkReservedCommittedSummary(output, "4096KB", "384KB");
       
   205             checkReserved(output, addr, reserveSize, "4096KB");
       
   206 
       
   207             checkCommitted(output, addrA, 3 * commitSize, "384KB");
       
   208 
       
   209             // uncommit
       
   210             wb.NMTUncommitMemory(addrB, commitSize);
       
   211             wb.NMTUncommitMemory(addrC, commitSize);
       
   212             wb.NMTUncommitMemory(addrA, commitSize);
       
   213 
       
   214             output = new OutputAnalyzer(pb.start());
       
   215             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   216         }
       
   217 
       
   218         {
       
   219             // commit CAB
       
   220             wb.NMTCommitMemory(addrC, commitSize);
       
   221             wb.NMTCommitMemory(addrA, commitSize);
       
   222             wb.NMTCommitMemory(addrB, commitSize);
       
   223 
       
   224             output = new OutputAnalyzer(pb.start());
       
   225             checkReservedCommittedSummary(output, "4096KB", "384KB");
       
   226             checkReserved(output, addr, reserveSize, "4096KB");
       
   227 
       
   228             checkCommitted(output, addrA, 3 * commitSize, "384KB");
       
   229 
       
   230             // uncommit
       
   231             wb.NMTUncommitMemory(addrC, commitSize);
       
   232             wb.NMTUncommitMemory(addrA, commitSize);
       
   233             wb.NMTUncommitMemory(addrB, commitSize);
       
   234 
       
   235             output = new OutputAnalyzer(pb.start());
       
   236             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   237         }
       
   238 
       
   239         {
       
   240             // commit CBA
       
   241             wb.NMTCommitMemory(addrC, commitSize);
       
   242             wb.NMTCommitMemory(addrB, commitSize);
       
   243             wb.NMTCommitMemory(addrA, commitSize);
       
   244 
       
   245             output = new OutputAnalyzer(pb.start());
       
   246             checkReservedCommittedSummary(output, "4096KB", "384KB");
       
   247             checkReserved(output, addr, reserveSize, "4096KB");
       
   248 
       
   249             checkCommitted(output, addrA, 3 * commitSize, "384KB");
       
   250 
       
   251             // uncommit
       
   252             wb.NMTUncommitMemory(addrC, commitSize);
       
   253             wb.NMTUncommitMemory(addrB, commitSize);
       
   254             wb.NMTUncommitMemory(addrA, commitSize);
       
   255 
       
   256             output = new OutputAnalyzer(pb.start());
       
   257             checkReservedCommittedSummary(output, "4096KB", "0KB");
       
   258         }
       
   259 
       
   260         // release
       
   261         wb.NMTReleaseMemory(addr, reserveSize);
       
   262         output = new OutputAnalyzer(pb.start());
       
   263         output.shouldNotContain("Test (reserved=");
       
   264         output.shouldNotMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
       
   265                 + Long.toHexString(addr + reserveSize) + "\\] reserved 4096KB for Test");
       
   266     }
       
   267 
       
   268     public static void checkReservedCommittedSummary(OutputAnalyzer output, String reservedString, String committedString) {
       
   269         output.shouldContain("Test (reserved=" + reservedString + ", committed=" + committedString + ")");
       
   270     }
       
   271 
       
   272     public static void checkReserved(OutputAnalyzer output, long addr, long size, String sizeString) {
       
   273         output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
       
   274                            + Long.toHexString(addr + size)
       
   275                            + "\\] reserved 4096KB for Test");
       
   276     }
       
   277 
       
   278     public static void checkCommitted(OutputAnalyzer output, long addr, long size, String sizeString) {
       
   279         output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*"
       
   280                            + Long.toHexString(addr + size)
       
   281                            + "\\] committed " + sizeString + " from.*");
       
   282     }
       
   283 }