1 /*
  2  * Copyright Amazon.com Inc. 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 /*
 26  * @test id=generational
 27  * @summary Test that growth of old-gen triggers old-gen marking
 28  * @requires vm.gc.Shenandoah
 29  * @library /test/lib
 30  * @run driver TestOldGrowthTriggers
 31  */
 32 
 33 import java.util.*;
 34 import java.math.BigInteger;
 35 
 36 import jdk.test.lib.Asserts;
 37 import jdk.test.lib.process.ProcessTools;
 38 import jdk.test.lib.process.OutputAnalyzer;
 39 
 40 public class TestOldGrowthTriggers {
 41 
 42     public static void makeOldAllocations() {
 43         // Expect most of the BigInteger entries placed into array to be promoted, and most will eventually become garbage within old
 44 
 45         final int ArraySize = 512 * 1024;   // 512K entries
 46         final int BitsInBigInteger = 128;
 47         final int RefillIterations = 64;
 48         BigInteger array[] = new BigInteger[ArraySize];
 49         Random r = new Random(46);
 50 
 51         for (int i = 0; i < ArraySize; i++) {
 52             array[i] = new BigInteger(BitsInBigInteger, r);
 53         }
 54 
 55         for (int refillCount = 0; refillCount < RefillIterations; refillCount++) {
 56             // Each refill repopulates ArraySize randomly selected elements within array
 57             for (int i = 0; i < ArraySize; i++) {
 58                 int replaceIndex = r.nextInt(ArraySize);
 59                 int deriveIndex = r.nextInt(ArraySize);
 60                 switch (i & 0x3) {
 61                     case 0:
 62                         // 50% chance of creating garbage
 63                         array[replaceIndex] = array[replaceIndex].max(array[deriveIndex]);
 64                         break;
 65                     case 1:
 66                         // 50% chance of creating garbage
 67                         array[replaceIndex] = array[replaceIndex].min(array[deriveIndex]);
 68                         break;
 69                     case 2:
 70                         // creates new old BigInteger, releases old BigInteger,
 71                         // may create ephemeral data while computing gcd
 72                         array[replaceIndex] = array[replaceIndex].gcd(array[deriveIndex]);
 73                         break;
 74                     case 3:
 75                         // creates new old BigInteger, releases old BigInteger
 76                         array[replaceIndex] = array[replaceIndex].multiply(array[deriveIndex]);
 77                         break;
 78                 }
 79             }
 80         }
 81     }
 82 
 83     public static void testOld(String... args) throws Exception {
 84         String[] cmds = Arrays.copyOf(args, args.length + 2);
 85         cmds[args.length] = TestOldGrowthTriggers.class.getName();
 86         cmds[args.length + 1] = "test";
 87         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(cmds);
 88         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 89         output.shouldHaveExitValue(0);
 90         output.shouldContain("Trigger (OLD): Old has overgrown");
 91     }
 92 
 93     public static void main(String[] args) throws Exception {
 94         if (args.length > 0 && args[0].equals("test")) {
 95             makeOldAllocations();
 96             return;
 97         }
 98 
 99         testOld("-Xlog:gc",
100                 "-Xms96m",
101                 "-Xmx96m",
102                 "-XX:+UnlockDiagnosticVMOptions",
103                 "-XX:+UnlockExperimentalVMOptions",
104                 "-XX:+UseShenandoahGC",
105                 "-XX:ShenandoahGCMode=generational",
106                 "-XX:ShenandoahGuaranteedYoungGCInterval=0",
107                 "-XX:ShenandoahGuaranteedOldGCInterval=0"
108         );
109     }
110 }