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