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);
99 }
100 } else {
101 checkHeapRegion(args[0]);
102 }
103 }
104 }