|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 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.HotSpotAgent; |
| 28 | +import sun.jvm.hotspot.runtime.VM; |
| 29 | + |
| 30 | +import jdk.test.lib.apps.LingeredApp; |
| 31 | +import jdk.test.lib.Asserts; |
| 32 | +import jdk.test.lib.Platform; |
| 33 | +import jdk.test.lib.process.OutputAnalyzer; |
| 34 | +import jdk.test.lib.process.ProcessTools; |
| 35 | +import jdk.test.lib.SA.SATestUtils; |
| 36 | +import jdk.test.lib.Utils; |
| 37 | + |
| 38 | +/** |
| 39 | + * @test |
| 40 | + * @library /test/lib |
| 41 | + * @requires vm.hasSA |
| 42 | + * @modules jdk.hotspot.agent/sun.jvm.hotspot |
| 43 | + * jdk.hotspot.agent/sun.jvm.hotspot.runtime |
| 44 | + * @run driver TestObjectAlignment |
| 45 | + */ |
| 46 | + |
| 47 | +public class TestObjectAlignment { |
| 48 | + |
| 49 | + private static LingeredApp theApp = null; |
| 50 | + |
| 51 | + private static void checkAlignment(String pid, int expectedAlign) throws Exception { |
| 52 | + HotSpotAgent agent = new HotSpotAgent(); |
| 53 | + |
| 54 | + try { |
| 55 | + agent.attach(Integer.parseInt(pid)); |
| 56 | + int actualAlign = VM.getVM().getObjectAlignmentInBytes(); |
| 57 | + Asserts.assertEquals(expectedAlign, actualAlign, |
| 58 | + "Address of HeapRegion does not match."); |
| 59 | + } finally { |
| 60 | + agent.detach(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static void createAnotherToAttach(long lingeredAppPid, int expectedAlign) |
| 65 | + throws Exception { |
| 66 | + // Start a new process to attach to the lingered app |
| 67 | + ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder( |
| 68 | + "--add-modules=jdk.hotspot.agent", |
| 69 | + "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED", |
| 70 | + "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED", |
| 71 | + "TestObjectAlignment", |
| 72 | + Long.toString(lingeredAppPid), |
| 73 | + Integer.toString(expectedAlign) |
| 74 | + ); |
| 75 | + SATestUtils.addPrivilegesIfNeeded(processBuilder); |
| 76 | + OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder); |
| 77 | + SAOutput.shouldHaveExitValue(0); |
| 78 | + System.out.println(SAOutput.getOutput()); |
| 79 | + } |
| 80 | + |
| 81 | + public static void main (String... args) throws Exception { |
| 82 | + SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work. |
| 83 | + if (args == null || args.length == 0) { |
| 84 | + for (int align = 8; align <= 256; align *= 2) { |
| 85 | + try { |
| 86 | + theApp = new LingeredApp(); |
| 87 | + LingeredApp.startApp(theApp, "-XX:ObjectAlignmentInBytes=" + align); |
| 88 | + createAnotherToAttach(theApp.getPid(), align); |
| 89 | + } finally { |
| 90 | + LingeredApp.stopApp(theApp); |
| 91 | + } |
| 92 | + } |
| 93 | + } else { |
| 94 | + checkAlignment(args[0], Integer.parseInt(args[1])); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments