1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2020 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 /*
  27  * This is a stress test for allocating from a single MetaspaceArena from
  28  *  multiple threads, optionally with reserve limit (mimicking the non-expandable CompressedClassSpace)
  29  * or commit limit (mimimcking MaxMetaspaceSize).
  30  *
  31  * The test threads will start to allocate from the Arena, and occasionally deallocate.
  32  * The threads run with a safety allocation max; if reached (or, if the underlying arena
  33  * hits either commit or reserve limit, if given) they will switch to deallocation and then
  34  * kind of float at the allocation ceiling, alternating between allocation and deallocation.
  35  *
  36  * We test with various flags, to exercise all 3 reclaim policies (none, balanced (default)
  37  * and aggessive) as well as one run with allocation guards enabled.
  38  *
  39  * We also set MetaspaceVerifyInterval very low to trigger many verifications in debug vm.
  40  *
  41  */
  42 
  43 
  44 /*
  45  * @test id=debug
  46  * @library /test/lib
  47  * @modules java.base/jdk.internal.misc
  48  *          java.management
  49  * @build sun.hotspot.WhiteBox
  50  * @key randomness
  51  * @requires (vm.debug == true)
  52  *
  53  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  54  *
  55  * @run main/othervm/timeout=400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI  -XX:VerifyMetaspaceInterval=10                                        TestMetaspaceAllocationMT1
  56  * @run main/othervm/timeout=400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI  -XX:VerifyMetaspaceInterval=10  -XX:MetaspaceReclaimPolicy=none       TestMetaspaceAllocationMT1
  57  * @run main/othervm/timeout=400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI  -XX:VerifyMetaspaceInterval=10  -XX:MetaspaceReclaimPolicy=aggressive TestMetaspaceAllocationMT1
  58  * @run main/othervm/timeout=400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI  -XX:VerifyMetaspaceInterval=10  -XX:+MetaspaceGuardAllocations        TestMetaspaceAllocationMT1
  59  *
  60  */
  61 
  62 /*
  63  * @test id=ndebug
  64  * @library /test/lib
  65  * @modules java.base/jdk.internal.misc
  66  *          java.management
  67  * @build sun.hotspot.WhiteBox
  68  * @key randomness
  69  * @requires (vm.debug == false)
  70  *
  71  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  72  *
  73  * @run main/othervm/timeout=400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI                                        TestMetaspaceAllocationMT1
  74  * @run main/othervm/timeout=400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI  -XX:MetaspaceReclaimPolicy=none       TestMetaspaceAllocationMT1
  75  * @run main/othervm/timeout=400 -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI  -XX:MetaspaceReclaimPolicy=aggressive TestMetaspaceAllocationMT1
  76  */
  77 
  78 public class TestMetaspaceAllocationMT1 {
  79 
  80     public static void main(String[] args) throws Exception {
  81 
  82         final long testAllocationCeiling = 1024 * 1024 * 8; // 8m words = 64M on 64bit
  83         final int numThreads = 4;
  84         final int seconds = 10;
  85 
  86         for (int i = 0; i < 3; i ++) {
  87 
  88             long commitLimit = (i == 1) ? 1024 * 256 : 0;
  89 
  90             // Note: reserve limit must be a multiple of Metaspace::reserve_alignment_words()
  91             //  (512 K)
  92             long reserveLimit = (i == 2) ? 1024 * 512 : 0;
  93 
  94             System.out.println("#### Test: ");
  95             System.out.println("#### testAllocationCeiling: " + testAllocationCeiling);
  96             System.out.println("#### numThreads: " + numThreads);
  97             System.out.println("#### seconds: " + seconds);
  98             System.out.println("#### commitLimit: " + commitLimit);
  99             System.out.println("#### reserveLimit: " + reserveLimit);
 100             System.out.println("#### ReclaimPolicy: " + Settings.settings().reclaimPolicy);
 101             System.out.println("#### guards: " + Settings.settings().usesAllocationGuards);
 102 
 103             MetaspaceTestContext context = new MetaspaceTestContext(commitLimit, reserveLimit);
 104             MetaspaceTestOneArenaManyThreads test = new MetaspaceTestOneArenaManyThreads(context, testAllocationCeiling, numThreads, seconds);
 105 
 106             try {
 107                 test.runTest();
 108             } catch (RuntimeException e) {
 109                 System.out.println(e);
 110                 context.printToTTY();
 111                 throw e;
 112             }
 113 
 114             context.destroy();
 115 
 116             System.out.println("#### Done. ####");
 117             System.out.println("###############");
 118 
 119         }
 120 
 121     }
 122 
 123 }