1 /*
   2  * Copyright (c) 2017, 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 import java.util.ArrayList;
  25 import java.util.List;
  26 
  27 import sun.jvm.hotspot.gc.g1.G1CollectedHeap;
  28 import sun.jvm.hotspot.gc.g1.HeapRegion;
  29 import sun.jvm.hotspot.HotSpotAgent;
  30 import sun.jvm.hotspot.runtime.VM;
  31 
  32 import jdk.test.lib.apps.LingeredApp;
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.Platform;
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import jdk.test.lib.process.ProcessTools;
  37 import jdk.test.lib.Utils;
  38 
  39 /*
  40  * @test
  41  * @library /test/lib
  42  * @requires os.family != "mac"
  43  * @modules jdk.hotspot.agent/sun.jvm.hotspot
  44  *          jdk.hotspot.agent/sun.jvm.hotspot.gc.g1
  45  *          jdk.hotspot.agent/sun.jvm.hotspot.memory
  46  *          jdk.hotspot.agent/sun.jvm.hotspot.runtime
  47  * @run main/othervm TestG1HeapRegion
  48  */
  49 
  50 public class TestG1HeapRegion {
  51 
  52     private static LingeredApp theApp = null;
  53 
  54     private static void checkHeapRegion(String pid) throws Exception {
  55         HotSpotAgent agent = new HotSpotAgent();
  56 
  57         try {
  58             agent.attach(Integer.parseInt(pid));
  59             G1CollectedHeap heap = (G1CollectedHeap)VM.getVM().getUniverse().heap();
  60             HeapRegion hr = heap.hrm().heapRegionIterator().next();
  61             HeapRegion hrTop = heap.hrm().getByAddress(hr.top());
  62 
  63             Asserts.assertEquals(hr.top(), hrTop.top(),
  64                                  "Address of HeapRegion does not match.");
  65         } finally {
  66             agent.detach();
  67         }
  68     }
  69 
  70     private static void createAnotherToAttach(long lingeredAppPid)
  71                                                          throws Exception {
  72         String[] toolArgs = {
  73             "--add-modules=jdk.hotspot.agent",
  74             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
  75             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.gc.g1=ALL-UNNAMED",
  76             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED",
  77             "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED",
  78             "TestG1HeapRegion",
  79             Long.toString(lingeredAppPid)
  80         };
  81 
  82         // Start a new process to attach to the lingered app
  83         ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(toolArgs);
  84         OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
  85         SAOutput.shouldHaveExitValue(0);
  86         System.out.println(SAOutput.getOutput());
  87     }
  88 
  89     public static void main (String... args) throws Exception {
  90         if (!Platform.shouldSAAttach()) {
  91             System.out.println(
  92                "SA attach not expected to work - test skipped.");
  93             return;
  94         }
  95 
  96         if (args == null || args.length == 0) {
  97             try {
  98                 List<String> vmArgs = new ArrayList<String>();
  99                 vmArgs.add("-XX:+UsePerfData");
 100                 vmArgs.add("-XX:+UseG1GC");
 101                 vmArgs.addAll(Utils.getVmOptions());
 102 
 103                 theApp = new LingeredApp();
 104                 LingeredApp.startApp(vmArgs, theApp);
 105                 createAnotherToAttach(theApp.getPid());
 106             } finally {
 107                 LingeredApp.stopApp(theApp);
 108             }
 109         } else {
 110             checkHeapRegion(args[0]);
 111         }
 112     }
 113 }