hotspot/test/runtime/NMT/MallocStressTest.java
author iignatyev
Wed, 07 Dec 2016 14:37:35 +0300
changeset 42640 09dba077f1e7
parent 40631 ed82623d7831
permissions -rw-r--r--
8078450: Implement consistent process for quarantine of tests Reviewed-by: dfazunen, dholmes, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25952
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     1
/*
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38089
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
25952
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     4
 *
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     7
 * published by the Free Software Foundation.
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     8
 *
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    13
 * accompanied this code).
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    14
 *
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    18
 *
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    21
 * questions.
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    22
 */
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    23
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    24
/*
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    25
 * @test
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    26
 * @summary Stress test for malloc tracking
26302
e3228e31a08d 8055765: Misplaced @key stress prevents MallocSiteHashOverflow.java and MallocStressTest.java tests from running
gtriantafill
parents: 25952
diff changeset
    27
 * @key nmt jcmd stress
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38089
diff changeset
    28
 * @library /test/lib
36851
03e2f4d0a421 8153737: Unsupported Module
chegar
parents: 33730
diff changeset
    29
 * @modules java.base/jdk.internal.misc
29678
dd2f3932c21e 8075586: Add @modules as needed to the open hotspot tests
ykantser
parents: 28190
diff changeset
    30
 *          java.management
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38089
diff changeset
    31
 * @build sun.hotspot.WhiteBox
25952
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    32
 * @run main ClassFileInstaller sun.hotspot.WhiteBox
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    33
 * @run main/othervm/timeout=600 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail MallocStressTest
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    34
 */
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    35
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    36
import java.util.concurrent.atomic.AtomicInteger;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    37
import java.util.ArrayList;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    38
import java.util.List;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    39
import java.util.Random;
40631
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38089
diff changeset
    40
import jdk.test.lib.JDKToolFinder;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38089
diff changeset
    41
import jdk.test.lib.Platform;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38089
diff changeset
    42
import jdk.test.lib.process.ProcessTools;
ed82623d7831 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 38089
diff changeset
    43
import jdk.test.lib.process.OutputAnalyzer;
25952
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    44
import sun.hotspot.WhiteBox;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    45
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    46
public class MallocStressTest {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    47
    private static int K = 1024;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    48
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    49
    // The stress test runs in three phases:
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    50
    // 1. alloc: A lot of malloc with fewer free, which simulates a burst memory allocation
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    51
    //    that is usually seen during startup or class loading.
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    52
    // 2. pause: Pause the test to check accuracy of native memory tracking
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    53
    // 3. release: Release all malloc'd memory and check native memory tracking result.
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    54
    public enum TestPhase {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    55
        alloc,
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    56
        pause,
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    57
        release
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    58
    };
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    59
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    60
    static TestPhase phase = TestPhase.alloc;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    61
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    62
    // malloc'd memory
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    63
    static ArrayList<MallocMemory>  mallocd_memory = new ArrayList<MallocMemory>();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    64
    static long                     mallocd_total  = 0;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    65
    static WhiteBox                 whiteBox;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    66
    static AtomicInteger            pause_count = new AtomicInteger();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    67
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    68
    static boolean                  is_64_bit_system;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    69
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    70
    private static boolean is_64_bit_system() { return is_64_bit_system; }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    71
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    72
    public static void main(String args[]) throws Exception {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    73
        is_64_bit_system = (Platform.is64bit());
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    74
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    75
        OutputAnalyzer output;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    76
        whiteBox = WhiteBox.getWhiteBox();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    77
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    78
        // Grab my own PID
38089
9e58203ef0e2 8153992: Some hotspot tests fail on compact2 due to an unnecessary test library dependency
akulyakh
parents: 36851
diff changeset
    79
        String pid = Long.toString(ProcessTools.getProcessId());
25952
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    80
        ProcessBuilder pb = new ProcessBuilder();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    81
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    82
        AllocThread[]   alloc_threads = new AllocThread[256];
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    83
        ReleaseThread[] release_threads = new ReleaseThread[64];
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    84
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    85
        int index;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    86
        // Create many allocation threads
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    87
        for (index = 0; index < alloc_threads.length; index ++) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    88
            alloc_threads[index] = new AllocThread();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    89
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    90
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    91
        // Fewer release threads
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    92
        for (index = 0; index < release_threads.length; index ++) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    93
            release_threads[index] = new ReleaseThread();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    94
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    95
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    96
        if (is_64_bit_system()) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    97
            sleep_wait(2*60*1000);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    98
        } else {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
    99
            sleep_wait(60*1000);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   100
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   101
        // pause the stress test
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   102
        phase = TestPhase.pause;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   103
        while (pause_count.intValue() <  alloc_threads.length + release_threads.length) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   104
            sleep_wait(10);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   105
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   106
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   107
        long mallocd_total_in_KB = (mallocd_total + K / 2) / K;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   108
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   109
        // Now check if the result from NMT matches the total memory allocated.
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   110
        String expected_test_summary = "Test (reserved=" + mallocd_total_in_KB +"KB, committed=" + mallocd_total_in_KB + "KB)";
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   111
        // Run 'jcmd <pid> VM.native_memory summary'
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   112
        pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary"});
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   113
        output = new OutputAnalyzer(pb.start());
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   114
        output.shouldContain(expected_test_summary);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   115
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   116
        // Release all allocated memory
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   117
        phase = TestPhase.release;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   118
        synchronized(mallocd_memory) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   119
            mallocd_memory.notifyAll();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   120
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   121
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   122
        // Join all threads
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   123
        for (index = 0; index < alloc_threads.length; index ++) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   124
            try {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   125
                alloc_threads[index].join();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   126
            } catch (InterruptedException e) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   127
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   128
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   129
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   130
        for (index = 0; index < release_threads.length; index ++) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   131
            try {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   132
                release_threads[index].join();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   133
            } catch (InterruptedException e) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   134
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   135
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   136
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   137
        // All test memory allocated should be released
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   138
        output = new OutputAnalyzer(pb.start());
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   139
        output.shouldNotContain("Test (reserved=");
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   140
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   141
        // Verify that tracking level has not been downgraded
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   142
        pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "statistics"});
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   143
        output = new OutputAnalyzer(pb.start());
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   144
        output.shouldNotContain("Tracking level has been downgraded due to lack of resources");
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   145
    }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   146
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   147
    private static void sleep_wait(int n) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   148
        try {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   149
            Thread.sleep(n);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   150
        } catch (InterruptedException e) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   151
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   152
    }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   153
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   154
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   155
    static class MallocMemory {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   156
        private long  addr;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   157
        private int   size;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   158
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   159
        MallocMemory(long addr, int size) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   160
            this.addr = addr;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   161
            this.size = size;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   162
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   163
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   164
        long addr()  { return this.addr; }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   165
        int  size()  { return this.size; }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   166
    }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   167
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   168
    static class AllocThread extends Thread {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   169
        AllocThread() {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   170
            this.setName("MallocThread");
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   171
            this.start();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   172
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   173
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   174
        // AllocThread only runs "Alloc" phase
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   175
        public void run() {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   176
            Random random = new Random();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   177
            while (MallocStressTest.phase == TestPhase.alloc) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   178
                int r = Math.abs(random.nextInt());
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   179
                // Only malloc small amount to avoid OOM
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   180
                int size = r % 32;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   181
                if (is_64_bit_system()) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   182
                    r = r % 32 * K;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   183
                } else {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   184
                    r = r % 64;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   185
                }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   186
                if (size == 0) size = 1;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   187
                long addr = MallocStressTest.whiteBox.NMTMallocWithPseudoStack(size, r);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   188
                if (addr != 0) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   189
                    MallocMemory mem = new MallocMemory(addr, size);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   190
                    synchronized(MallocStressTest.mallocd_memory) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   191
                        MallocStressTest.mallocd_memory.add(mem);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   192
                        MallocStressTest.mallocd_total += size;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   193
                    }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   194
                } else {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   195
                    System.out.println("Out of malloc memory");
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   196
                    break;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   197
                }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   198
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   199
            MallocStressTest.pause_count.incrementAndGet();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   200
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   201
    }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   202
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   203
    static class ReleaseThread extends Thread {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   204
        private Random random = new Random();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   205
        ReleaseThread() {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   206
            this.setName("ReleaseThread");
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   207
            this.start();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   208
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   209
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   210
        public void run() {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   211
            while(true) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   212
                switch(MallocStressTest.phase) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   213
                case alloc:
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   214
                    slow_release();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   215
                    break;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   216
                case pause:
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   217
                    enter_pause();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   218
                    break;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   219
                case release:
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   220
                    quick_release();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   221
                    return;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   222
                }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   223
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   224
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   225
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   226
        private void enter_pause() {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   227
            MallocStressTest.pause_count.incrementAndGet();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   228
            while (MallocStressTest.phase != MallocStressTest.TestPhase.release) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   229
                try {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   230
                    synchronized(MallocStressTest.mallocd_memory) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   231
                        MallocStressTest.mallocd_memory.wait(10);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   232
                    }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   233
                } catch (InterruptedException e) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   234
                }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   235
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   236
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   237
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   238
        private void quick_release() {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   239
            List<MallocMemory> free_list;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   240
            while (true) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   241
                synchronized(MallocStressTest.mallocd_memory) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   242
                    if (MallocStressTest.mallocd_memory.isEmpty()) return;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   243
                    int size =  Math.min(MallocStressTest.mallocd_memory.size(), 5000);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   244
                    List<MallocMemory> subList = MallocStressTest.mallocd_memory.subList(0, size);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   245
                    free_list = new ArrayList<MallocMemory>(subList);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   246
                    subList.clear();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   247
                }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   248
                for (int index = 0; index < free_list.size(); index ++) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   249
                    MallocMemory mem = free_list.get(index);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   250
                    MallocStressTest.whiteBox.NMTFree(mem.addr());
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   251
                }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   252
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   253
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   254
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   255
        private void slow_release() {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   256
            try {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   257
                Thread.sleep(10);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   258
            } catch (InterruptedException e) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   259
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   260
            synchronized(MallocStressTest.mallocd_memory) {
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   261
                if (MallocStressTest.mallocd_memory.isEmpty()) return;
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   262
                int n = Math.abs(random.nextInt()) % MallocStressTest.mallocd_memory.size();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   263
                MallocMemory mem = mallocd_memory.remove(n);
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   264
                MallocStressTest.whiteBox.NMTFree(mem.addr());
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   265
                MallocStressTest.mallocd_total -= mem.size();
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   266
            }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   267
        }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   268
    }
25c2dca04a31 8054952: [TESTBUG] Add missing NMT2 tests
gtriantafill
parents:
diff changeset
   269
}