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 TestOldObjectsOnNvdimm
 28  * @summary Check that objects in old generation reside in dram.
 29  * @requires vm.gc=="null" & os.family != "aix"
 30  * @library /test/lib
 31  * @build sun.hotspot.WhiteBox
 32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 33  * @run main gc.nvdimm.TestOldObjectsOnNvdimm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
 34  *                                  -XX:+WhiteBoxAPI
 35  */
 36 
 37 import jdk.test.lib.process.OutputAnalyzer;
 38 import jdk.test.lib.process.ProcessTools;
 39 import jdk.test.lib.Asserts;
 40 import sun.hotspot.WhiteBox;
 41 
 42 import java.util.ArrayList;
 43 import java.util.Collections;
 44 
 45 /*
 46  * Test spawns OldObjectTest in a separate VM and expects that it
 47  * completes without a RuntimeException.
 48  */
 49 public class TestOldObjectsOnNvdimm {
 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             "-Xms10M", "-Xmx10M",
 64             "-XX:MaxTenuringThreshold=1" // Promote 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(OldObjectTest.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 object is in Old generation after tenuring and resides in NVDIMM.
 97  * The necessary condition for this test is running in VM with the following flags:
 98  * -XX:AllocateOldGenAt=, -XX:MaxTenuringThreshold=1
 99  */
100 class OldObjectTest {
101     private static final WhiteBox WB = WhiteBox.getWhiteBox();
102 
103     private static void validateOldObject(Object o) {
104         Asserts.assertTrue(WB.isObjectInOldGen(o),
105                 "Object is supposed to be in OldGen");
106 
107         long oldObj_addr = WB.getObjectAddress(o);
108         long nvdimm_heap_start = WB.nvdimmReservedStart();
109         long nvdimm_heap_end = WB.nvdimmReservedEnd();
110 
111         Asserts.assertTrue(oldObj_addr >= nvdimm_heap_start && oldObj_addr <= nvdimm_heap_end,
112                 "Old object does not reside in NVDIMM");
113     }
114 
115     public static void main(String args[]) throws Exception {
116         // allocate an object and perform Young GCs to promote it to Old
117         byte[] oldObj = new byte[TestOldObjectsOnNvdimm.ALLOCATION_SIZE];
118         WB.youngGC();
119         WB.youngGC();
120         validateOldObject(oldObj);
121     }
122 }