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
 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 array_size = 512 * 1024;   // 512K entries
 46     BigInteger array[] = new BigInteger[array_size];
 47     Random r = new Random(46);
 48 
 49     for (int i = 0; i < array_size; i++) {
 50       array[i] = new BigInteger(128, r);
 51     }
 52 
 53     for (int refill_count = 0; refill_count < 192; refill_count++) {
 54       // Each refill repopulates array_size randomly selected elements within array
 55       for (int i = 0; i < array_size; i++) {
 56         int replace_index = r.nextInt(array_size);
 57         int derive_index = r.nextInt(array_size);
 58         switch (i & 0x3) {
 59           case 0:
 60             // 50% chance of creating garbage
 61             array[replace_index] = array[replace_index].max(array[derive_index]);
 62             break;
 63           case 1:
 64             // 50% chance of creating garbage
 65             array[replace_index] = array[replace_index].min(array[derive_index]);
 66             break;
 67           case 2:
 68             // creates new old BigInteger, releases old BigInteger,
 69             // may create ephemeral data while computing gcd
 70             array[replace_index] = array[replace_index].gcd(array[derive_index]);
 71             break;
 72           case 3:
 73             // creates new old BigInteger, releases old BigInteger
 74             array[replace_index] = array[replace_index].multiply(array[derive_index]);
 75             break;
 76         }
 77         if ((i & 0x3) == 0x3) {
 78         } else {
 79         }
 80       }
 81     }
 82   }
 83 
 84   public static void testOld(String... args) throws Exception {
 85     String[] cmds = Arrays.copyOf(args, args.length + 2);
 86     cmds[args.length] = TestOldGrowthTriggers.class.getName();
 87     cmds[args.length + 1] = "test";
 88     ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(cmds);
 89     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 90     output.shouldHaveExitValue(0);
 91     if (!output.getOutput().contains("Trigger (OLD): Old has overgrown")) {
 92       throw new AssertionError("Generational mode: Should experience OLD triggers due to growth");
 93     }
 94   }
 95 
 96   public static void main(String[] args) throws Exception {
 97     if (args.length > 0 && args[0].equals("test")) {
 98       makeOldAllocations();
 99       return;
100     }
101 
102     testOld("-Xlog:gc",
103             "-Xms256m",
104             "-Xmx256m",
105             "-XX:+UnlockDiagnosticVMOptions",
106             "-XX:+UnlockExperimentalVMOptions",
107             "-XX:+UseShenandoahGC",
108             "-XX:ShenandoahGCMode=generational",
109             "-XX:ShenandoahGuaranteedYoungGCInterval=0",
110             "-XX:ShenandoahGuaranteedOldGCInterval=0"
111             );
112   }
113 }