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