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