Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/NMT/MallocTrackingVerify.java
32284 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 805483626* @summary Test to verify correctness of malloc tracking27* @key nmt jcmd28* @library /testlibrary /testlibrary/whitebox29* @build MallocTrackingVerify30* @run main ClassFileInstaller sun.hotspot.WhiteBox31* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail MallocTrackingVerify32*33*/3435import java.util.ArrayList;36import java.util.Random;3738import com.oracle.java.testlibrary.*;3940import sun.hotspot.WhiteBox;4142public class MallocTrackingVerify {43private static int MAX_ALLOC = 4 * 1024;4445static ArrayList<MallocMemory> mallocd_memory = new ArrayList<MallocMemory>();46static long mallocd_total = 0;47public static WhiteBox wb = WhiteBox.getWhiteBox();4849public static void main(String args[]) throws Exception {50OutputAnalyzer output;5152// Grab my own PID53String pid = Integer.toString(ProcessTools.getProcessId());54ProcessBuilder pb = new ProcessBuilder();5556Random random = new Random();57// Allocate small amounts of memory with random pseudo call stack58while (mallocd_total < MAX_ALLOC) {59int size = random.nextInt(31) + 1;60long addr = wb.NMTMallocWithPseudoStack(size, random.nextInt());61if (addr != 0) {62MallocMemory mem = new MallocMemory(addr, size);63mallocd_memory.add(mem);64mallocd_total += size;65} else {66System.out.println("Out of malloc memory");67break;68}69}7071pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary" });72output = new OutputAnalyzer(pb.start());73output.shouldContain("Test (reserved=4KB, committed=4KB)");7475// Free76for (MallocMemory mem : mallocd_memory) {77wb.NMTFree(mem.addr());78}7980// Run 'jcmd <pid> VM.native_memory summary', check for expected output81pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid,82"VM.native_memory", "summary" });83output = new OutputAnalyzer(pb.start());84output.shouldNotContain("Test (reserved=");85}8687static class MallocMemory {88private long addr;89private int size;9091MallocMemory(long addr, int size) {92this.addr = addr;93this.size = size;94}9596long addr() {97return this.addr;98}99100int size() {101return this.size;102}103}104}105106107