test/runtime/NMT/MallocStressTest.java

Tue, 09 Sep 2014 09:48:42 -0700

author
gtriantafill
date
Tue, 09 Sep 2014 09:48:42 -0700
changeset 7156
4edd7572c235
parent 7115
3f9ff5e261c6
permissions
-rw-r--r--

8054836: [TESTBUG] Test is needed to verify correctness of malloc tracking
Reviewed-by: ctornqvi, lfoltan

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

mercurial