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