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