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