1 /*
   2  * Copyright (c) 2014, 2019, 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.survivorAlignment;
  25 
  26 /**
  27  * @test
  28  * @bug 8031323
  29  * @summary Verify that objects promoted from survivor space to tenured space
  30  *          when their age exceeded tenuring threshold are not aligned to
  31  *          SurvivorAlignmentInBytes value.
  32  * @requires vm.gc != "Z" & vm.gc != "Shenandoah"
  33  * @library /test/lib
  34  * @modules java.base/jdk.internal.misc
  35  *          java.management
  36  * @build sun.hotspot.WhiteBox
  37  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  38  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  39  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  40  *                   -XX:+WhiteBoxAPI -XX:NewSize=128m -XX:MaxNewSize=128m
  41  *                   -XX:OldSize=32M -XX:MaxHeapSize=160m -XX:SurvivorRatio=1 -XX:-ResizePLAB
  42  *                   -XX:-ExplicitGCInvokesConcurrent
  43  *                   -XX:+UnlockExperimentalVMOptions
  44  *                   -XX:SurvivorAlignmentInBytes=32
  45  *                   gc.survivorAlignment.TestPromotionFromSurvivorToTenuredAfterMinorGC 10m 9
  46  *                   TENURED
  47  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  48  *                   -XX:+WhiteBoxAPI -XX:NewSize=128m -XX:MaxNewSize=128m
  49  *                   -XX:OldSize=32M -XX:MaxHeapSize=160m -XX:SurvivorRatio=1 -XX:-ResizePLAB
  50  *                   -XX:-ExplicitGCInvokesConcurrent
  51  *                   -XX:+UnlockExperimentalVMOptions
  52  *                   -XX:SurvivorAlignmentInBytes=32
  53  *                   gc.survivorAlignment.TestPromotionFromSurvivorToTenuredAfterMinorGC 20m 47
  54  *                   TENURED
  55  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  56  *                   -XX:+WhiteBoxAPI -XX:NewSize=200m -XX:MaxNewSize=200m
  57  *                   -XX:OldSize=32M -XX:MaxHeapSize=232m -XX:SurvivorRatio=1 -XX:-ResizePLAB
  58  *                   -XX:-ExplicitGCInvokesConcurrent
  59  *                   -XX:+UnlockExperimentalVMOptions
  60  *                   -XX:SurvivorAlignmentInBytes=64
  61  *                   gc.survivorAlignment.TestPromotionFromSurvivorToTenuredAfterMinorGC 10m 9
  62  *                   TENURED
  63  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  64  *                   -XX:+WhiteBoxAPI -XX:NewSize=128m -XX:MaxNewSize=128m
  65  *                   -XX:OldSize=32M -XX:MaxHeapSize=160m -XX:SurvivorRatio=1 -XX:-ResizePLAB
  66  *                   -XX:-ExplicitGCInvokesConcurrent
  67  *                   -XX:+UnlockExperimentalVMOptions
  68  *                   -XX:SurvivorAlignmentInBytes=64
  69  *                   gc.survivorAlignment.TestPromotionFromSurvivorToTenuredAfterMinorGC 20m 87
  70  *                   TENURED
  71  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  72  *                   -XX:+WhiteBoxAPI -XX:NewSize=256m -XX:MaxNewSize=256m
  73  *                   -XX:OldSize=32M -XX:MaxHeapSize=288m -XX:SurvivorRatio=1 -XX:-ResizePLAB
  74  *                   -XX:-ExplicitGCInvokesConcurrent
  75  *                   -XX:+UnlockExperimentalVMOptions
  76  *                   -XX:SurvivorAlignmentInBytes=128
  77  *                    gc.survivorAlignment.TestPromotionFromSurvivorToTenuredAfterMinorGC 10m 9
  78  *                    TENURED
  79  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  80  *                   -XX:+WhiteBoxAPI -XX:NewSize=128m -XX:MaxNewSize=128m
  81  *                   -XX:OldSize=32M -XX:MaxHeapSize=160m -XX:SurvivorRatio=1 -XX:-ResizePLAB
  82  *                   -XX:-ExplicitGCInvokesConcurrent
  83  *                   -XX:+UnlockExperimentalVMOptions
  84  *                   -XX:SurvivorAlignmentInBytes=128
  85  *                   gc.survivorAlignment.TestPromotionFromSurvivorToTenuredAfterMinorGC 20m 147
  86  *                   TENURED
  87  */
  88 public class TestPromotionFromSurvivorToTenuredAfterMinorGC {
  89     public static void main(String args[]) throws Exception {
  90         SurvivorAlignmentTestMain test
  91                 = SurvivorAlignmentTestMain.fromArgs(args);
  92         System.out.println(test);
  93 
  94         long expectedMemoryUsage = test.getExpectedMemoryUsage();
  95         test.baselineMemoryAllocation();
  96         SurvivorAlignmentTestMain.WHITE_BOX.fullGC();
  97         // increase expected usage by current old gen usage
  98         expectedMemoryUsage += SurvivorAlignmentTestMain.getAlignmentHelper(
  99                 SurvivorAlignmentTestMain.HeapSpace.TENURED)
 100                 .getActualMemoryUsage();
 101 
 102         test.allocate();
 103         for (int i = 0; i <= SurvivorAlignmentTestMain.MAX_TENURING_THRESHOLD; i++) {
 104             SurvivorAlignmentTestMain.WHITE_BOX.youngGC();
 105         }
 106 
 107         // Sometimes we see that data unrelated to the test has been allocated during
 108         // the loop. This data is included in the expectedMemoryUsage since we look
 109         // through all threads to see what they allocated. If this data is still in
 110         // the survivor area however, it should not be included in expectedMemoryUsage
 111         // since the verification below only look at what's in tenured space.
 112         expectedMemoryUsage -= SurvivorAlignmentTestMain.getAlignmentHelper(
 113                                    SurvivorAlignmentTestMain.HeapSpace.SURVIVOR)
 114                                    .getActualMemoryUsage();
 115         test.verifyMemoryUsage(expectedMemoryUsage);
 116     }
 117 }