gtriantafill@7156: /* gtriantafill@7156: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. gtriantafill@7156: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. gtriantafill@7156: * gtriantafill@7156: * This code is free software; you can redistribute it and/or modify it gtriantafill@7156: * under the terms of the GNU General Public License version 2 only, as gtriantafill@7156: * published by the Free Software Foundation. gtriantafill@7156: * gtriantafill@7156: * This code is distributed in the hope that it will be useful, but WITHOUT gtriantafill@7156: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or gtriantafill@7156: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License gtriantafill@7156: * version 2 for more details (a copy is included in the LICENSE file that gtriantafill@7156: * accompanied this code). gtriantafill@7156: * gtriantafill@7156: * You should have received a copy of the GNU General Public License version gtriantafill@7156: * 2 along with this work; if not, write to the Free Software Foundation, gtriantafill@7156: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. gtriantafill@7156: * gtriantafill@7156: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA gtriantafill@7156: * or visit www.oracle.com if you need additional information or have any gtriantafill@7156: * questions. gtriantafill@7156: */ gtriantafill@7156: gtriantafill@7156: /* gtriantafill@7156: * @test gtriantafill@7156: * @bug 8054836 gtriantafill@7156: * @summary Test to verify correctness of malloc tracking gtriantafill@7156: * @key nmt jcmd gtriantafill@7156: * @library /testlibrary /testlibrary/whitebox gtriantafill@7156: * @build MallocTrackingVerify gtriantafill@7156: * @run main ClassFileInstaller sun.hotspot.WhiteBox gtriantafill@7156: * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail MallocTrackingVerify gtriantafill@7156: * gtriantafill@7156: */ gtriantafill@7156: gtriantafill@7156: import java.util.ArrayList; gtriantafill@7156: import java.util.Random; gtriantafill@7156: gtriantafill@7156: import com.oracle.java.testlibrary.*; gtriantafill@7156: gtriantafill@7156: import sun.hotspot.WhiteBox; gtriantafill@7156: gtriantafill@7156: public class MallocTrackingVerify { gtriantafill@7156: private static int MAX_ALLOC = 4 * 1024; gtriantafill@7156: gtriantafill@7156: static ArrayList mallocd_memory = new ArrayList(); gtriantafill@7156: static long mallocd_total = 0; gtriantafill@7156: public static WhiteBox wb = WhiteBox.getWhiteBox(); gtriantafill@7156: gtriantafill@7156: public static void main(String args[]) throws Exception { gtriantafill@7156: OutputAnalyzer output; gtriantafill@7156: gtriantafill@7156: // Grab my own PID gtriantafill@7156: String pid = Integer.toString(ProcessTools.getProcessId()); gtriantafill@7156: ProcessBuilder pb = new ProcessBuilder(); gtriantafill@7156: gtriantafill@7156: Random random = new Random(); gtriantafill@7156: // Allocate small amounts of memory with random pseudo call stack gtriantafill@7156: while (mallocd_total < MAX_ALLOC) { gtriantafill@7156: int size = random.nextInt(31) + 1; gtriantafill@7156: long addr = wb.NMTMallocWithPseudoStack(size, random.nextInt()); gtriantafill@7156: if (addr != 0) { gtriantafill@7156: MallocMemory mem = new MallocMemory(addr, size); gtriantafill@7156: mallocd_memory.add(mem); gtriantafill@7156: mallocd_total += size; gtriantafill@7156: } else { gtriantafill@7156: System.out.println("Out of malloc memory"); gtriantafill@7156: break; gtriantafill@7156: } gtriantafill@7156: } gtriantafill@7156: gtriantafill@7156: pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary" }); gtriantafill@7156: output = new OutputAnalyzer(pb.start()); gtriantafill@7156: output.shouldContain("Test (reserved=4KB, committed=4KB)"); gtriantafill@7156: gtriantafill@7156: // Free gtriantafill@7156: for (MallocMemory mem : mallocd_memory) { gtriantafill@7156: wb.NMTFree(mem.addr()); gtriantafill@7156: } gtriantafill@7156: gtriantafill@7156: // Run 'jcmd VM.native_memory summary', check for expected output gtriantafill@7156: pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, gtriantafill@7156: "VM.native_memory", "summary" }); gtriantafill@7156: output = new OutputAnalyzer(pb.start()); gtriantafill@7156: output.shouldNotContain("Test (reserved="); gtriantafill@7156: } gtriantafill@7156: gtriantafill@7156: static class MallocMemory { gtriantafill@7156: private long addr; gtriantafill@7156: private int size; gtriantafill@7156: gtriantafill@7156: MallocMemory(long addr, int size) { gtriantafill@7156: this.addr = addr; gtriantafill@7156: this.size = size; gtriantafill@7156: } gtriantafill@7156: gtriantafill@7156: long addr() { gtriantafill@7156: return this.addr; gtriantafill@7156: } gtriantafill@7156: gtriantafill@7156: int size() { gtriantafill@7156: return this.size; gtriantafill@7156: } gtriantafill@7156: } gtriantafill@7156: }