Tue, 09 Sep 2014 09:48:42 -0700
8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
Reviewed-by: ctornqvi, lfoltan
gtriantafill@7156 | 1 | /* |
gtriantafill@7156 | 2 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
gtriantafill@7156 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
gtriantafill@7156 | 4 | * |
gtriantafill@7156 | 5 | * This code is free software; you can redistribute it and/or modify it |
gtriantafill@7156 | 6 | * under the terms of the GNU General Public License version 2 only, as |
gtriantafill@7156 | 7 | * published by the Free Software Foundation. |
gtriantafill@7156 | 8 | * |
gtriantafill@7156 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
gtriantafill@7156 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
gtriantafill@7156 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
gtriantafill@7156 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
gtriantafill@7156 | 13 | * accompanied this code). |
gtriantafill@7156 | 14 | * |
gtriantafill@7156 | 15 | * You should have received a copy of the GNU General Public License version |
gtriantafill@7156 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
gtriantafill@7156 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
gtriantafill@7156 | 18 | * |
gtriantafill@7156 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
gtriantafill@7156 | 20 | * or visit www.oracle.com if you need additional information or have any |
gtriantafill@7156 | 21 | * questions. |
gtriantafill@7156 | 22 | */ |
gtriantafill@7156 | 23 | |
gtriantafill@7156 | 24 | /* |
gtriantafill@7156 | 25 | * @test |
gtriantafill@7156 | 26 | * @bug 8054836 |
gtriantafill@7156 | 27 | * @summary Test to verify correctness of malloc tracking |
gtriantafill@7156 | 28 | * @key nmt jcmd |
gtriantafill@7156 | 29 | * @library /testlibrary /testlibrary/whitebox |
gtriantafill@7156 | 30 | * @build MallocTrackingVerify |
gtriantafill@7156 | 31 | * @run main ClassFileInstaller sun.hotspot.WhiteBox |
gtriantafill@7156 | 32 | * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail MallocTrackingVerify |
gtriantafill@7156 | 33 | * |
gtriantafill@7156 | 34 | */ |
gtriantafill@7156 | 35 | |
gtriantafill@7156 | 36 | import java.util.ArrayList; |
gtriantafill@7156 | 37 | import java.util.Random; |
gtriantafill@7156 | 38 | |
gtriantafill@7156 | 39 | import com.oracle.java.testlibrary.*; |
gtriantafill@7156 | 40 | |
gtriantafill@7156 | 41 | import sun.hotspot.WhiteBox; |
gtriantafill@7156 | 42 | |
gtriantafill@7156 | 43 | public class MallocTrackingVerify { |
gtriantafill@7156 | 44 | private static int MAX_ALLOC = 4 * 1024; |
gtriantafill@7156 | 45 | |
gtriantafill@7156 | 46 | static ArrayList<MallocMemory> mallocd_memory = new ArrayList<MallocMemory>(); |
gtriantafill@7156 | 47 | static long mallocd_total = 0; |
gtriantafill@7156 | 48 | public static WhiteBox wb = WhiteBox.getWhiteBox(); |
gtriantafill@7156 | 49 | |
gtriantafill@7156 | 50 | public static void main(String args[]) throws Exception { |
gtriantafill@7156 | 51 | OutputAnalyzer output; |
gtriantafill@7156 | 52 | |
gtriantafill@7156 | 53 | // Grab my own PID |
gtriantafill@7156 | 54 | String pid = Integer.toString(ProcessTools.getProcessId()); |
gtriantafill@7156 | 55 | ProcessBuilder pb = new ProcessBuilder(); |
gtriantafill@7156 | 56 | |
gtriantafill@7156 | 57 | Random random = new Random(); |
gtriantafill@7156 | 58 | // Allocate small amounts of memory with random pseudo call stack |
gtriantafill@7156 | 59 | while (mallocd_total < MAX_ALLOC) { |
gtriantafill@7156 | 60 | int size = random.nextInt(31) + 1; |
gtriantafill@7156 | 61 | long addr = wb.NMTMallocWithPseudoStack(size, random.nextInt()); |
gtriantafill@7156 | 62 | if (addr != 0) { |
gtriantafill@7156 | 63 | MallocMemory mem = new MallocMemory(addr, size); |
gtriantafill@7156 | 64 | mallocd_memory.add(mem); |
gtriantafill@7156 | 65 | mallocd_total += size; |
gtriantafill@7156 | 66 | } else { |
gtriantafill@7156 | 67 | System.out.println("Out of malloc memory"); |
gtriantafill@7156 | 68 | break; |
gtriantafill@7156 | 69 | } |
gtriantafill@7156 | 70 | } |
gtriantafill@7156 | 71 | |
gtriantafill@7156 | 72 | pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary" }); |
gtriantafill@7156 | 73 | output = new OutputAnalyzer(pb.start()); |
gtriantafill@7156 | 74 | output.shouldContain("Test (reserved=4KB, committed=4KB)"); |
gtriantafill@7156 | 75 | |
gtriantafill@7156 | 76 | // Free |
gtriantafill@7156 | 77 | for (MallocMemory mem : mallocd_memory) { |
gtriantafill@7156 | 78 | wb.NMTFree(mem.addr()); |
gtriantafill@7156 | 79 | } |
gtriantafill@7156 | 80 | |
gtriantafill@7156 | 81 | // Run 'jcmd <pid> VM.native_memory summary', check for expected output |
gtriantafill@7156 | 82 | pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, |
gtriantafill@7156 | 83 | "VM.native_memory", "summary" }); |
gtriantafill@7156 | 84 | output = new OutputAnalyzer(pb.start()); |
gtriantafill@7156 | 85 | output.shouldNotContain("Test (reserved="); |
gtriantafill@7156 | 86 | } |
gtriantafill@7156 | 87 | |
gtriantafill@7156 | 88 | static class MallocMemory { |
gtriantafill@7156 | 89 | private long addr; |
gtriantafill@7156 | 90 | private int size; |
gtriantafill@7156 | 91 | |
gtriantafill@7156 | 92 | MallocMemory(long addr, int size) { |
gtriantafill@7156 | 93 | this.addr = addr; |
gtriantafill@7156 | 94 | this.size = size; |
gtriantafill@7156 | 95 | } |
gtriantafill@7156 | 96 | |
gtriantafill@7156 | 97 | long addr() { |
gtriantafill@7156 | 98 | return this.addr; |
gtriantafill@7156 | 99 | } |
gtriantafill@7156 | 100 | |
gtriantafill@7156 | 101 | int size() { |
gtriantafill@7156 | 102 | return this.size; |
gtriantafill@7156 | 103 | } |
gtriantafill@7156 | 104 | } |
gtriantafill@7156 | 105 | } |