1 /*
   2  * Copyright (c) 2014, 2020, 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 package gc.g1;
  25 
  26 /*
  27  * @test TestEagerReclaimHumongousRegionsClearMarkBits
  28  * @bug 8051973
  29  * @summary Test to make sure that eager reclaim of humongous objects correctly clears
  30  * mark bitmaps at reclaim.
  31  * @key randomness
  32  * @requires vm.gc.G1
  33  * @library /test/lib
  34  * @modules java.base/jdk.internal.misc
  35  *          java.management
  36  * @run driver gc.g1.TestEagerReclaimHumongousRegionsClearMarkBits
  37  */
  38 
  39 import java.util.ArrayList;
  40 import java.util.LinkedList;
  41 import java.util.Random;
  42 
  43 import jdk.test.lib.process.OutputAnalyzer;
  44 import jdk.test.lib.process.ProcessTools;
  45 import jdk.test.lib.Utils;
  46 
  47 // An object that has a few references to other instances to slow down marking.
  48 class ObjectWithSomeRefs {
  49     public ObjectWithSomeRefs other1;
  50     public ObjectWithSomeRefs other2;
  51     public ObjectWithSomeRefs other3;
  52     public ObjectWithSomeRefs other4;
  53 }
  54 
  55 class TestEagerReclaimHumongousRegionsClearMarkBitsReclaimRegionFast {
  56     public static final long MAX_MILLIS_FOR_RUN = 50 * 1000; // The maximum runtime for the actual test.
  57 
  58     public static final int M = 1024*1024;
  59 
  60     public static LinkedList<Object> garbageList = new LinkedList<Object>();
  61 
  62     public static void genGarbage(Object large) {
  63         for (int i = 0; i < 64*1024; i++) {
  64             Object[] garbage = new Object[50];
  65             garbage[0] = large;
  66             garbageList.add(garbage);
  67         }
  68         garbageList.clear();
  69     }
  70 
  71     public static ArrayList<ObjectWithSomeRefs> longList = new ArrayList<ObjectWithSomeRefs>();
  72 
  73     public static void main(String[] args) {
  74 
  75         for (int i = 0; i < 16*1024; i++) {
  76              longList.add(new ObjectWithSomeRefs());
  77         }
  78 
  79         Random rnd = Utils.getRandomInstance();
  80         for (int i = 0; i < longList.size(); i++) {
  81              int len = longList.size();
  82              longList.get(i).other1 = longList.get(rnd.nextInt(len));
  83              longList.get(i).other2 = longList.get(rnd.nextInt(len));
  84              longList.get(i).other3 = longList.get(rnd.nextInt(len));
  85              longList.get(i).other4 = longList.get(rnd.nextInt(len));
  86         }
  87 
  88         int[] large1 = new int[M];
  89         int[] large2 = null;
  90         int[] large3 = null;
  91         int[] large4 = null;
  92 
  93         Object ref_from_stack = large1;
  94 
  95         long start_millis = System.currentTimeMillis();
  96 
  97         for (int i = 0; i < 20; i++) {
  98             long current_millis = System.currentTimeMillis();
  99             if ((current_millis - start_millis) > MAX_MILLIS_FOR_RUN) {
 100               System.out.println("Finishing test because maximum runtime exceeded");
 101               break;
 102             }
 103             // A set of large objects that will be reclaimed eagerly - and hopefully marked.
 104             large1 = new int[M - 20];
 105             large2 = new int[M - 20];
 106             large3 = new int[M - 20];
 107             large4 = new int[M - 20];
 108             genGarbage(large1);
 109             // Make sure that the compiler cannot completely remove
 110             // the allocation of the large object until here.
 111             System.out.println(large1 + " " + large2 + " " + large3 + " " + large4);
 112         }
 113 
 114         // Keep the reference to the first object alive.
 115         System.out.println(ref_from_stack);
 116     }
 117 }
 118 
 119 public class TestEagerReclaimHumongousRegionsClearMarkBits {
 120     public static void main(String[] args) throws Exception {
 121         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 122             "-XX:+UseG1GC",
 123             "-Xms128M",
 124             "-Xmx128M",
 125             "-Xmn2M",
 126             "-XX:G1HeapRegionSize=1M",
 127             "-XX:InitiatingHeapOccupancyPercent=0", // Want to have as much as possible initial marks.
 128             "-Xlog:gc",
 129             "-XX:+UnlockDiagnosticVMOptions",
 130             "-XX:+VerifyAfterGC",
 131             "-XX:ConcGCThreads=1", // Want to make marking as slow as possible.
 132             "-XX:+IgnoreUnrecognizedVMOptions", // G1VerifyBitmaps is develop only.
 133             "-XX:+G1VerifyBitmaps",
 134             TestEagerReclaimHumongousRegionsClearMarkBitsReclaimRegionFast.class.getName());
 135         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 136         output.shouldHaveExitValue(0);
 137     }
 138 }
 139