1/*
2 * Copyright (c) 2014, 2016, 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/*
25 * @test
26 * @bug 8022865
27 * @summary Tests for the -XX:ObjectAlignmentInBytes command line option
28 * @library /test/lib
29 * @modules java.base/jdk.internal.misc
30 *          java.management
31 * @run main ObjectAlignment
32 */
33
34import jdk.test.lib.Platform;
35import jdk.test.lib.process.ProcessTools;
36import jdk.test.lib.process.OutputAnalyzer;
37
38public class ObjectAlignment {
39
40    public static void main(String[] args) throws Exception {
41
42        if (Platform.is64bit()) {
43            // Minimum alignment should be 8
44            testObjectAlignment(4)
45                .shouldContain("outside the allowed range")
46                .shouldHaveExitValue(1);
47
48            // Alignment has to be a power of 2
49            testObjectAlignment(9)
50                .shouldContain("must be power of 2")
51                .shouldHaveExitValue(1);
52
53            testObjectAlignment(-1)
54                .shouldContain("outside the allowed range")
55                .shouldHaveExitValue(1);
56
57            // Maximum alignment allowed is 256
58            testObjectAlignment(512)
59                .shouldContain("outside the allowed range")
60                .shouldHaveExitValue(1);
61
62            // Valid alignments should work
63            testObjectAlignment(8).shouldHaveExitValue(0);
64            testObjectAlignment(16).shouldHaveExitValue(0);
65            testObjectAlignment(256).shouldHaveExitValue(0);
66
67        } else {
68            // For a 32bit JVM the option isn't there, make sure it's not silently ignored
69            testObjectAlignment(8)
70                .shouldContain("Unrecognized VM option 'ObjectAlignmentInBytes=8'")
71                .shouldHaveExitValue(1);
72        }
73    }
74
75    private static OutputAnalyzer testObjectAlignment(int alignment) throws Exception {
76        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:ObjectAlignmentInBytes=" + alignment,
77                                                                  "-version");
78        return new OutputAnalyzer(pb.start());
79    }
80}
81