# HG changeset patch # User stefank # Date 1517235093 -3600 # Node ID d80d521e9cb14fc42b09aaa2b9eb40ebfffb9a9d # Parent f01f81fa12428b6c4333dce078f0baafe2319fbb 8196217: NMT: add_committed_regions doesn't merge succeeding regions Reviewed-by: zgu, pliden, coleenp diff -r f01f81fa1242 -r d80d521e9cb1 src/hotspot/share/services/virtualMemoryTracker.cpp --- a/src/hotspot/share/services/virtualMemoryTracker.cpp Mon Jan 29 11:55:40 2018 -0500 +++ b/src/hotspot/share/services/virtualMemoryTracker.cpp Mon Jan 29 15:11:33 2018 +0100 @@ -65,16 +65,26 @@ } if (rgn->adjacent_to(addr, size)) { - // special case to expand prior region if there is no next region - LinkedListNode* next = node->next(); - if (next == NULL && rgn->call_stack()->equals(stack)) { - VirtualMemorySummary::record_uncommitted_memory(rgn->size(), flag()); + if (rgn->call_stack()->equals(stack)) { // the two adjacent regions have the same call stack, merge them rgn->expand_region(addr, size); - VirtualMemorySummary::record_committed_memory(rgn->size(), flag()); + VirtualMemorySummary::record_committed_memory(size, flag()); + + // maybe merge with the next region + LinkedListNode* next_node = node->next(); + if (next_node != NULL) { + CommittedMemoryRegion* next = next_node->data(); + if (next->call_stack()->equals(stack) && rgn->adjacent_to(next->base(), next->size())) { + // the two adjacent regions have the same call stack, merge them + rgn->expand_region(next->base(), next->size()); + + // the merge next_node needs to be removed from the list + _committed_regions.remove(next_node); + } + } return true; } - } + } if (rgn->overlap_region(addr, size)) { // Clear a space for this region in the case it overlaps with any regions. @@ -87,10 +97,10 @@ node = node->next(); } - // New committed region - VirtualMemorySummary::record_committed_memory(size, flag()); - return add_committed_region(committed_rgn); - } + // New committed region + VirtualMemorySummary::record_committed_memory(size, flag()); + return add_committed_region(committed_rgn); +} void ReservedMemoryRegion::set_all_committed(bool b) { if (all_committed() != b) { diff -r f01f81fa1242 -r d80d521e9cb1 test/hotspot/jtreg/runtime/NMT/VirtualAllocCommitMerge.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/hotspot/jtreg/runtime/NMT/VirtualAllocCommitMerge.java Mon Jan 29 15:11:33 2018 +0100 @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Test merging of committed virtual memory and that we track it correctly + * @key nmt jcmd + * @library /test/lib + * @modules java.base/jdk.internal.misc + * java.management + * @build sun.hotspot.WhiteBox + * @run main ClassFileInstaller sun.hotspot.WhiteBox + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail VirtualAllocCommitMerge + * + */ + +import jdk.test.lib.process.ProcessTools; +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.JDKToolFinder; + +import sun.hotspot.WhiteBox; + +public class VirtualAllocCommitMerge { + + public static WhiteBox wb = WhiteBox.getWhiteBox(); + + public static void main(String args[]) throws Exception { + OutputAnalyzer output; + long commitSize = 128 * 1024; // 128KB + long reserveSize = 4 * 1024 * 1024; // 4096KB + long addr; + + String pid = Long.toString(ProcessTools.getProcessId()); + ProcessBuilder pb = new ProcessBuilder(); + + // reserve + addr = wb.NMTReserveMemory(reserveSize); + pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, + "VM.native_memory", "detail" }); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + long addrA = addr + (0 * commitSize); + long addrB = addr + (1 * commitSize); + long addrC = addr + (2 * commitSize); + long addrD = addr + (3 * commitSize); + long addrE = addr + (4 * commitSize); + + // Test discontigous areas + { + // commit ACE + wb.NMTCommitMemory(addrA, commitSize); + wb.NMTCommitMemory(addrC, commitSize); + wb.NMTCommitMemory(addrE, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "384KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, commitSize, "128KB"); + checkCommitted(output, addrC, commitSize, "128KB"); + checkCommitted(output, addrE, commitSize, "128KB"); + + // uncommit ACE + wb.NMTUncommitMemory(addrA, commitSize); + wb.NMTUncommitMemory(addrC, commitSize); + wb.NMTUncommitMemory(addrE, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + // Test contiguous areas + { + // commit AB + wb.NMTCommitMemory(addrA, commitSize); + wb.NMTCommitMemory(addrB, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "256KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 2 * commitSize, "256KB"); + + // uncommit AB + wb.NMTUncommitMemory(addrA, commitSize); + wb.NMTUncommitMemory(addrB, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + { + // commit BA + wb.NMTCommitMemory(addrB, commitSize); + wb.NMTCommitMemory(addrA, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "256KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 2 * commitSize, "256KB"); + + // uncommit AB + wb.NMTUncommitMemory(addrB, commitSize); + wb.NMTUncommitMemory(addrA, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + { + // commit ABC + wb.NMTCommitMemory(addrA, commitSize); + wb.NMTCommitMemory(addrB, commitSize); + wb.NMTCommitMemory(addrC, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "384KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 3 * commitSize, "384KB"); + + // uncommit + wb.NMTUncommitMemory(addrA, commitSize); + wb.NMTUncommitMemory(addrB, commitSize); + wb.NMTUncommitMemory(addrC, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + { + // commit ACB + wb.NMTCommitMemory(addrA, commitSize); + wb.NMTCommitMemory(addrC, commitSize); + wb.NMTCommitMemory(addrB, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "384KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 3 * commitSize, "384KB"); + + // uncommit + wb.NMTUncommitMemory(addrA, commitSize); + wb.NMTUncommitMemory(addrC, commitSize); + wb.NMTUncommitMemory(addrB, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + { + // commit BAC + wb.NMTCommitMemory(addrB, commitSize); + wb.NMTCommitMemory(addrA, commitSize); + wb.NMTCommitMemory(addrC, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "384KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 3 * commitSize, "384KB"); + + // uncommit + wb.NMTUncommitMemory(addrB, commitSize); + wb.NMTUncommitMemory(addrA, commitSize); + wb.NMTUncommitMemory(addrC, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + { + // commit BCA + wb.NMTCommitMemory(addrB, commitSize); + wb.NMTCommitMemory(addrC, commitSize); + wb.NMTCommitMemory(addrA, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "384KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 3 * commitSize, "384KB"); + + // uncommit + wb.NMTUncommitMemory(addrB, commitSize); + wb.NMTUncommitMemory(addrC, commitSize); + wb.NMTUncommitMemory(addrA, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + { + // commit CAB + wb.NMTCommitMemory(addrC, commitSize); + wb.NMTCommitMemory(addrA, commitSize); + wb.NMTCommitMemory(addrB, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "384KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 3 * commitSize, "384KB"); + + // uncommit + wb.NMTUncommitMemory(addrC, commitSize); + wb.NMTUncommitMemory(addrA, commitSize); + wb.NMTUncommitMemory(addrB, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + { + // commit CBA + wb.NMTCommitMemory(addrC, commitSize); + wb.NMTCommitMemory(addrB, commitSize); + wb.NMTCommitMemory(addrA, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "384KB"); + checkReserved(output, addr, reserveSize, "4096KB"); + + checkCommitted(output, addrA, 3 * commitSize, "384KB"); + + // uncommit + wb.NMTUncommitMemory(addrC, commitSize); + wb.NMTUncommitMemory(addrB, commitSize); + wb.NMTUncommitMemory(addrA, commitSize); + + output = new OutputAnalyzer(pb.start()); + checkReservedCommittedSummary(output, "4096KB", "0KB"); + } + + // release + wb.NMTReleaseMemory(addr, reserveSize); + output = new OutputAnalyzer(pb.start()); + output.shouldNotContain("Test (reserved="); + output.shouldNotMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*" + + Long.toHexString(addr + reserveSize) + "\\] reserved 4096KB for Test"); + } + + public static void checkReservedCommittedSummary(OutputAnalyzer output, String reservedString, String committedString) { + output.shouldContain("Test (reserved=" + reservedString + ", committed=" + committedString + ")"); + } + + public static void checkReserved(OutputAnalyzer output, long addr, long size, String sizeString) { + output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*" + + Long.toHexString(addr + size) + + "\\] reserved 4096KB for Test"); + } + + public static void checkCommitted(OutputAnalyzer output, long addr, long size, String sizeString) { + output.shouldMatch("\\[0x[0]*" + Long.toHexString(addr) + " - 0x[0]*" + + Long.toHexString(addr + size) + + "\\] committed " + sizeString + " from.*"); + } +}