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