Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestG1HeapRegion.java
41149 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.List;2526import sun.jvm.hotspot.gc.g1.G1CollectedHeap;27import sun.jvm.hotspot.gc.g1.HeapRegion;28import sun.jvm.hotspot.HotSpotAgent;29import sun.jvm.hotspot.runtime.VM;3031import jdk.test.lib.apps.LingeredApp;32import jdk.test.lib.Asserts;33import jdk.test.lib.Platform;34import jdk.test.lib.process.OutputAnalyzer;35import jdk.test.lib.process.ProcessTools;36import jdk.test.lib.SA.SATestUtils;37import jdk.test.lib.Utils;3839/**40* @test41* @library /test/lib42* @requires vm.hasSA43* @requires vm.gc.G144* @modules jdk.hotspot.agent/sun.jvm.hotspot45* jdk.hotspot.agent/sun.jvm.hotspot.gc.g146* jdk.hotspot.agent/sun.jvm.hotspot.memory47* jdk.hotspot.agent/sun.jvm.hotspot.runtime48* @run driver TestG1HeapRegion49*/5051public class TestG1HeapRegion {5253private static LingeredApp theApp = null;5455private static void checkHeapRegion(String pid) throws Exception {56HotSpotAgent agent = new HotSpotAgent();5758try {59agent.attach(Integer.parseInt(pid));60G1CollectedHeap heap = (G1CollectedHeap)VM.getVM().getUniverse().heap();61HeapRegion hr = heap.hrm().heapRegionIterator().next();62HeapRegion hrTop = heap.hrm().getByAddress(hr.top());6364Asserts.assertEquals(hr.top(), hrTop.top(),65"Address of HeapRegion does not match.");66} finally {67agent.detach();68}69}7071private static void createAnotherToAttach(long lingeredAppPid)72throws Exception {73// Start a new process to attach to the lingered app74ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(75"--add-modules=jdk.hotspot.agent",76"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",77"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.gc.g1=ALL-UNNAMED",78"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED",79"--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED",80"TestG1HeapRegion",81Long.toString(lingeredAppPid));82SATestUtils.addPrivilegesIfNeeded(processBuilder);83OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);84SAOutput.shouldHaveExitValue(0);85System.out.println(SAOutput.getOutput());86}8788public static void main (String... args) throws Exception {89SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.90if (args == null || args.length == 0) {91try {92theApp = new LingeredApp();93LingeredApp.startApp(theApp, "-XX:+UsePerfData", "-XX:+UseG1GC");94createAnotherToAttach(theApp.getPid());95} finally {96LingeredApp.stopApp(theApp);97}98} else {99checkHeapRegion(args[0]);100}101}102}103104105