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