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