test/runtime/NMT/MallocStressTest.java

Thu, 04 Sep 2014 10:14:37 -0400

author
gtriantafill
date
Thu, 04 Sep 2014 10:14:37 -0400
changeset 7110
6640f982c1be
parent 7108
017b0145f20c
child 7115
3f9ff5e261c6
permissions
-rw-r--r--

8054711: [TESTBUG] Enable NMT2 tests after NMT2 is integrated
Summary: enable tests for NMT2
Reviewed-by: ctornqvi, zgu

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

mercurial