1 /*
   2  * Copyright (c) 2018, 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 /*
  25  * @test TestYoungObjectsOnDram
  26  * @summary Check that objects in young generation reside in dram.
  27  * @requires vm.gc=="null" & os.family != "aix"
  28  * @library /test/lib
  29  * @build sun.hotspot.WhiteBox
  30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  31  * @run main TestYoungObjectsOnDram -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
  32  *                                  -XX:+WhiteBoxAPI
  33  */
  34 
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.process.ProcessTools;
  37 import jdk.test.lib.Asserts;
  38 import sun.hotspot.WhiteBox;
  39 
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import java.util.Collections;
  43 
  44 /**
  45  * Test spawns YoungObjectTest in a separate VM and expects that it
  46  * completes without a RuntimeException.
  47  */
  48 public class TestYoungObjectsOnDram {
  49 
  50     public static final int ALLOCATION_SIZE = 100;
  51     private static ArrayList<String> testOpts;
  52 
  53     public static void main(String args[]) throws Exception {
  54         testOpts = new ArrayList();
  55 
  56         String[] common_options = new String[] {
  57             "-Xbootclasspath/a:.",
  58             "-XX:+UnlockExperimentalVMOptions",
  59             "-XX:+UnlockDiagnosticVMOptions",
  60             "-XX:+WhiteBoxAPI",
  61             "-XX:AllocateOldGenAt="+System.getProperty("test.dir", "."),
  62             "-XX:SurvivorRatio=1", // Survivor-to-eden ratio is 1:1
  63             "-Xms10M", "-Xmx10M",
  64             "-XX:InitialTenuringThreshold=15" // avoid promotion of objects to Old Gen
  65         };
  66 
  67         String testVmOptsStr = System.getProperty("test.java.opts");
  68         if (!testVmOptsStr.isEmpty()) {
  69             String[] testVmOpts = testVmOptsStr.split(" ");
  70             Collections.addAll(testOpts, testVmOpts);
  71         }
  72         Collections.addAll(testOpts, common_options);
  73 
  74         // Test with G1 GC
  75         runTest("-XX:+UseG1GC");
  76         // Test with ParallelOld GC
  77         runTest("-XX:+UseParallelOldGC -XX:-UseAdaptiveGCBoundary");
  78         runTest("-XX:+UseParallelOldGC -XX:+UseAdaptiveGCBoundary");
  79     }
  80 
  81     private static void runTest(String... extraFlags) throws Exception {
  82         Collections.addAll(testOpts, extraFlags);
  83         testOpts.add(YoungObjectTest.class.getName());
  84         System.out.println(testOpts);
  85 
  86         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(false,
  87                 testOpts.toArray(new String[testOpts.size()]));
  88 
  89         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  90         System.out.println(output.getStdout());
  91         output.shouldHaveExitValue(0);
  92     }
  93 }
  94 
  95 /**
  96  * This class tests that newly created object is in Young generation and resides in DRAM.
  97  * The necessary condition for this test is running in VM with the following flags:
  98  * -XX:AllocateOldGenAt=, -XX:InitialTenuringThreshold=15, -XX:SurvivorRatio=1
  99  */
 100 class YoungObjectTest {
 101     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 102 
 103     private static void validateYoungObject(Object o) {
 104         Asserts.assertTrue(!WB.isObjectInOldGen(o),
 105                 "Object is supposed to be in YoungGen");
 106 
 107         long youngObj_addr = WB.getObjectAddress(o);
 108         long dram_heap_start = WB.dramReservedStart();
 109         long dram_heap_end = WB.dramReservedEnd();
 110 
 111         Asserts.assertTrue(youngObj_addr >= dram_heap_start && youngObj_addr <= dram_heap_end,
 112                 "Young object does not reside in DRAM");
 113     }
 114 
 115     public static void main(String args[]) throws Exception {
 116         // allocate an object
 117         byte[] youngObj = new byte[TestYoungObjectsOnDram.ALLOCATION_SIZE];
 118         validateYoungObject(youngObj);
 119 
 120         // Start a Young GC and check that object is still in DRAM.
 121         // We have used -XX:InitialTenuringThreshold=15 to invoke this test
 122         WB.youngGC();
 123         validateYoungObject(youngObj);
 124     }
 125 }