1 /*
2 * Copyright (c) 2018, 2025, 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.G1HeapRegion;
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.SA.SATestUtils;
38 import jdk.test.lib.Utils;
39
40 /**
41 * @test
42 * @library /test/lib
43 * @requires vm.hasSA
44 * @requires (os.arch != "riscv64" | !(vm.cpu.features ~= ".*qemu.*"))
45 * @requires vm.gc.G1
46 * @modules jdk.hotspot.agent/sun.jvm.hotspot
47 * jdk.hotspot.agent/sun.jvm.hotspot.gc.g1
48 * jdk.hotspot.agent/sun.jvm.hotspot.memory
49 * jdk.hotspot.agent/sun.jvm.hotspot.runtime
50 * @run driver TestG1HeapRegion
51 */
52
53 public class TestG1HeapRegion {
54
55 private static LingeredApp theApp = null;
56
57 private static void checkHeapRegion(String pid) throws Exception {
58 HotSpotAgent agent = new HotSpotAgent();
59
60 try {
61 agent.attach(Integer.parseInt(pid));
62 G1CollectedHeap heap = (G1CollectedHeap)VM.getVM().getUniverse().heap();
63 G1HeapRegion hr = heap.hrm().heapRegionIterator().next();
64 G1HeapRegion hrTop = heap.hrm().getByAddress(hr.top());
65
66 Asserts.assertEquals(hr.top(), hrTop.top(),
67 "Address of G1HeapRegion does not match.");
68 } finally {
69 agent.detach();
70 }
71 }
72
73 private static void createAnotherToAttach(long lingeredAppPid)
74 throws Exception {
75 // Start a new process to attach to the lingered app
76 ProcessBuilder processBuilder = ProcessTools.createLimitedTestJavaProcessBuilder(
77 "--add-modules=jdk.hotspot.agent",
78 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
79 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.gc.g1=ALL-UNNAMED",
80 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED",
81 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED",
82 "TestG1HeapRegion",
83 Long.toString(lingeredAppPid));
84 SATestUtils.addPrivilegesIfNeeded(processBuilder);
85 OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
86 SAOutput.shouldHaveExitValue(0);
87 System.out.println(SAOutput.getOutput());
88 }
89
90 public static void main (String... args) throws Exception {
91 SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
92 if (args == null || args.length == 0) {
93 try {
94 theApp = new LingeredApp();
95 LingeredApp.startApp(theApp, "-XX:+UsePerfData", "-XX:+UseG1GC");
96 createAnotherToAttach(theApp.getPid());
97 } finally {
98 LingeredApp.stopApp(theApp);
|
1 /*
2 * Copyright (c) 2018, 2026, 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.Iterator;
26 import java.util.List;
27
28 import sun.jvm.hotspot.debugger.Address;
29 import sun.jvm.hotspot.gc.g1.G1CollectedHeap;
30 import sun.jvm.hotspot.gc.g1.G1HeapRegion;
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.SA.SATestUtils;
40 import jdk.test.lib.Utils;
41
42 /**
43 * @test
44 * @bug 8194249
45 * @library /test/lib
46 * @requires vm.hasSA
47 * @requires (os.arch != "riscv64" | !(vm.cpu.features ~= ".*qemu.*"))
48 * @requires vm.gc.G1
49 * @modules jdk.hotspot.agent/sun.jvm.hotspot
50 * jdk.hotspot.agent/sun.jvm.hotspot.debugger
51 * jdk.hotspot.agent/sun.jvm.hotspot.gc.g1
52 * jdk.hotspot.agent/sun.jvm.hotspot.memory
53 * jdk.hotspot.agent/sun.jvm.hotspot.runtime
54 * @run driver TestG1HeapRegion
55 */
56
57 public class TestG1HeapRegion {
58
59 private static LingeredApp theApp = null;
60
61 private static void checkHeapRegion(String pid) throws Exception {
62 HotSpotAgent agent = new HotSpotAgent();
63
64 try {
65 agent.attach(Integer.parseInt(pid));
66
67 G1CollectedHeap heap = (G1CollectedHeap)VM.getVM().getUniverse().heap();
68 heap.printOn(System.out);
69
70 // Print each region first.
71 System.out.println();
72 Iterator<G1HeapRegion> hri = heap.hrm().heapRegionIterator();
73 G1HeapRegion hr = hri.next();
74 while (hr != null) {
75 hr.printOn(System.out);
76 hr = hri.next();
77 }
78 System.out.println();
79
80 // Iterate over each region and confirm that getByAddress(top) returns
81 // the same address as the region being looked at.
82 hri = heap.hrm().heapRegionIterator();
83 hr = hri.next();
84 while (hr != null) {
85 hr.printOn(System.out);
86 Address top = hr.top();
87 if (top.equals(hr.end())) {
88 // The end of the region is actually the first address after
89 // the end, so it points to the start of the next region. We need to
90 // subtract to avoid getByAddress(top) returning the next region.
91 top = top.addOffsetTo(-1);
92 }
93 G1HeapRegion hrTop = heap.hrm().getByAddress(top);
94 System.out.format("hr.top():0x%x <--> hrTop.top():0x%x\n",
95 hr.top().asLongValue(), hrTop.top().asLongValue());
96 Asserts.assertEquals(hr.top(), hrTop.top(),
97 "Address of G1HeapRegion does not match.");
98 hr = hri.next();
99 }
100 } finally {
101 agent.detach();
102 }
103 }
104
105 private static void createAnotherToAttach(long lingeredAppPid)
106 throws Exception {
107 // Start a new process to attach to the lingered app
108 ProcessBuilder processBuilder = ProcessTools.createLimitedTestJavaProcessBuilder(
109 "--add-modules=jdk.hotspot.agent",
110 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
111 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",
112 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.gc.g1=ALL-UNNAMED",
113 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED",
114 "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED",
115 "TestG1HeapRegion",
116 Long.toString(lingeredAppPid));
117 SATestUtils.addPrivilegesIfNeeded(processBuilder);
118 OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
119 SAOutput.shouldHaveExitValue(0);
120 System.out.println(SAOutput.getOutput());
121 }
122
123 public static void main (String... args) throws Exception {
124 SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
125 if (args == null || args.length == 0) {
126 try {
127 theApp = new LingeredApp();
128 LingeredApp.startApp(theApp, "-XX:+UsePerfData", "-XX:+UseG1GC");
129 createAnotherToAttach(theApp.getPid());
130 } finally {
131 LingeredApp.stopApp(theApp);
|