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