hotspot/test/runtime/NMT/MallocTrackingVerify.java
author thartmann
Tue, 28 Oct 2014 16:44:39 +0100
changeset 27431 5f970b31939c
parent 26690 905d970d7537
child 27615 d7caba003432
permissions -rw-r--r--
8062250: [TESTBUG] compiler/codecache/CheckSegmentedCodeCache.java fails after CodeCacheMinimumFreeSpace removal Summary: Adapted calculation of minimum code cache size after CodeCacheMinimumFreeSpace was removed by JDK-8046809. Reviewed-by: kvn, anoll
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
/*
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    29
 * @library /testlibrary /testlibrary/whitebox
26690
905d970d7537 8058504: [TESTBUG] Temporarily disable failing test runtime/NMT/MallocTrackingVerify.java
gtriantafill
parents: 26568
diff changeset
    30
 * @ignore 8058251
26568
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    31
 * @build MallocTrackingVerify
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    32
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    33
 * @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
    34
 *
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
import java.util.ArrayList;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    38
import java.util.Random;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    39
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    40
import com.oracle.java.testlibrary.*;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    41
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    42
import sun.hotspot.WhiteBox;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    43
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    44
public class MallocTrackingVerify {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    45
    private static int MAX_ALLOC = 4 * 1024;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    46
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    47
    static ArrayList<MallocMemory> mallocd_memory = new ArrayList<MallocMemory>();
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    48
    static long mallocd_total = 0;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    49
    public static WhiteBox wb = WhiteBox.getWhiteBox();
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    50
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    51
    public static void main(String args[]) throws Exception {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    52
        OutputAnalyzer output;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    53
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    54
        // Grab my own PID
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    55
        String pid = Integer.toString(ProcessTools.getProcessId());
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    56
        ProcessBuilder pb = new ProcessBuilder();
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    57
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    58
        Random random = new Random();
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    59
        // 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
    60
        while (mallocd_total < MAX_ALLOC) {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    61
            int size = random.nextInt(31) + 1;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    62
            long addr = wb.NMTMallocWithPseudoStack(size, random.nextInt());
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    63
            if (addr != 0) {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    64
                MallocMemory mem = new MallocMemory(addr, size);
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    65
                mallocd_memory.add(mem);
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    66
                mallocd_total += size;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    67
            } else {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    68
                System.out.println("Out of malloc memory");
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    69
                break;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    70
            }
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
        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
    74
        output = new OutputAnalyzer(pb.start());
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    75
        output.shouldContain("Test (reserved=4KB, committed=4KB)");
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    76
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    77
        // Free
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    78
        for (MallocMemory mem : mallocd_memory) {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    79
            wb.NMTFree(mem.addr());
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    80
        }
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
        // 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
    83
        pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid,
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    84
                "VM.native_memory", "summary" });
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    85
        output = new OutputAnalyzer(pb.start());
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    86
        output.shouldNotContain("Test (reserved=");
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    87
    }
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
    static class MallocMemory {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    90
        private long addr;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    91
        private int size;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    92
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    93
        MallocMemory(long addr, int size) {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    94
            this.addr = addr;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    95
            this.size = size;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    96
        }
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
        long addr() {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
    99
            return this.addr;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
   100
        }
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
        int size() {
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
   103
            return this.size;
65dbacf42e6d 8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
gtriantafill
parents:
diff changeset
   104
        }
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
}