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 TestOldObjectsOnNvdimm
  26  * @summary Check that objects in old 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 TestOldObjectsOnNvdimm -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 OldObjectTest in a separate VM and expects that it
  46  * completes without a RuntimeException.
  47  */
  48 public class TestOldObjectsOnNvdimm {
  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             "-Xms10M", "-Xmx10M",
  63             "-XX:MaxTenuringThreshold=1" // Promote objects to Old Gen
  64         };
  65 
  66         String testVmOptsStr = System.getProperty("test.java.opts");
  67         if (!testVmOptsStr.isEmpty()) {
  68             String[] testVmOpts = testVmOptsStr.split(" ");
  69             Collections.addAll(testOpts, testVmOpts);
  70         }
  71         Collections.addAll(testOpts, common_options);
  72 
  73         // Test with G1 GC
  74         runTest("-XX:+UseG1GC");
  75         // Test with ParallelOld GC
  76         runTest("-XX:+UseParallelOldGC -XX:-UseAdaptiveGCBoundary");
  77         runTest("-XX:+UseParallelOldGC -XX:+UseAdaptiveGCBoundary");
  78     }
  79 
  80     private static void runTest(String... extraFlags) throws Exception {
  81         Collections.addAll(testOpts, extraFlags);
  82         testOpts.add(OldObjectTest.class.getName());
  83         System.out.println(testOpts);
  84 
  85         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(false,
  86                 testOpts.toArray(new String[testOpts.size()]));
  87 
  88         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  89         System.out.println(output.getStdout());
  90         output.shouldHaveExitValue(0);
  91     }
  92 }
  93 
  94 /*
  95  * This class tests that object is in Old generation after tenuring and resides in NVDIMM.
  96  * The necessary condition for this test is running in VM with the following flags:
  97  * -XX:AllocateOldGenAt=, -XX:MaxTenuringThreshold=1
  98  */
  99 class OldObjectTest {
 100     private static final WhiteBox WB = WhiteBox.getWhiteBox();
 101 
 102     private static void validateOldObject(Object o) {
 103         Asserts.assertTrue(WB.isObjectInOldGen(o),
 104                 "Object is supposed to be in OldGen");
 105 
 106         long oldObj_addr = WB.getObjectAddress(o);
 107         long nvdimm_heap_start = WB.nvdimmReservedStart();
 108         long nvdimm_heap_end = WB.nvdimmReservedEnd();
 109 
 110         Asserts.assertTrue(oldObj_addr >= nvdimm_heap_start && oldObj_addr <= nvdimm_heap_end,
 111                 "Old object does not reside in NVDIMM");
 112     }
 113 
 114     public static void main(String args[]) throws Exception {
 115         // allocate an object and perform Young GCs to promote it to Old
 116         byte[] oldObj = new byte[TestOldObjectsOnNvdimm.ALLOCATION_SIZE];
 117         WB.youngGC();
 118         WB.youngGC();
 119         validateOldObject(oldObj);
 120     }
 121 }