1 /*
   2  * Copyright (c) 2020, Red Hat, Inc. 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  * @summary Test for Runtime.SizeOf with 32-bit compressed oops
  27  * @library /test/lib
  28  *
  29  * @build sun.hotspot.WhiteBox
  30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  31  *
  32  * @run main/othervm -Xmx128m
  33  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  34  *                   -Xint
  35  *                   SizeOf
  36  *
  37  * @run main/othervm -Xmx128m
  38  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  39  *                   -XX:TieredStopAtLevel=1
  40  *                   SizeOf
  41  *
  42  * @run main/othervm -Xmx128m
  43  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  44  *                   -XX:-TieredCompilation
  45  *                   SizeOf
  46  */
  47 
  48 /*
  49  * @test
  50  * @summary Test for Runtime.SizeOf with zero-based compressed oops
  51  * @library /test/lib
  52  * @requires vm.bits == 64
  53  *
  54  * @build sun.hotspot.WhiteBox
  55  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  56  *
  57  * @run main/othervm -Xmx4g
  58  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  59  *                   -Xint
  60  *                   SizeOf
  61  *
  62  * @run main/othervm -Xmx4g
  63  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  64  *                   -XX:TieredStopAtLevel=1
  65  *                   SizeOf
  66  *
  67  * @run main/othervm -Xmx4g
  68  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  69  *                   -XX:-TieredCompilation
  70  *                   SizeOf
  71  */
  72 
  73 /*
  74  * @test
  75  * @summary Test for Runtime.SizeOf without compressed oops
  76  * @library /test/lib
  77  * @requires vm.bits == 64
  78  *
  79  * @build sun.hotspot.WhiteBox
  80  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  81  *
  82  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
  83  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  84  *                   -Xint
  85  *                   SizeOf
  86  *
  87  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
  88  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  89  *                   -XX:TieredStopAtLevel=1
  90  *                   SizeOf
  91  *
  92  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
  93  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  94  *                   -XX:-TieredCompilation
  95  *                   SizeOf
  96  */
  97 
  98 /*
  99  * @test
 100  * @summary Test for Runtime.SizeOf with 32-bit compressed oops
 101  * @library /test/lib
 102  * @requires vm.debug
 103  *
 104  * @build sun.hotspot.WhiteBox
 105  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 106  *
 107  * @run main/othervm -Xmx128m
 108  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 109  *                   -XX:FastAllocateSizeLimit=0
 110  *                   -Xint
 111  *                   SizeOf
 112  *
 113  * @run main/othervm -Xmx128m
 114  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 115  *                   -XX:FastAllocateSizeLimit=0
 116  *                   -XX:TieredStopAtLevel=1
 117  *                   SizeOf
 118  *
 119  * @run main/othervm -Xmx128m
 120  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 121  *                   -XX:FastAllocateSizeLimit=0
 122  *                   -XX:-TieredCompilation
 123  *                   SizeOf
 124  */
 125 
 126 /*
 127  * @test
 128  * @summary Test for Runtime.SizeOf with zero-based compressed oops
 129  * @library /test/lib
 130  * @requires vm.bits == 64
 131  * @requires vm.debug
 132  *
 133  * @build sun.hotspot.WhiteBox
 134  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 135  *
 136  * @run main/othervm -Xmx4g
 137  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 138  *                   -XX:FastAllocateSizeLimit=0
 139  *                   -Xint
 140  *                   SizeOf
 141  *
 142  * @run main/othervm -Xmx4g
 143  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 144  *                   -XX:FastAllocateSizeLimit=0
 145  *                   -XX:TieredStopAtLevel=1
 146  *                   SizeOf
 147  *
 148  * @run main/othervm -Xmx4g
 149  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 150  *                   -XX:FastAllocateSizeLimit=0
 151  *                   -XX:-TieredCompilation
 152  *                   SizeOf
 153  */
 154 
 155 /*
 156  * @test
 157  * @summary Test for Runtime.SizeOf without compressed oops
 158  * @library /test/lib
 159  * @requires vm.bits == 64
 160  * @requires vm.debug
 161  *
 162  * @build sun.hotspot.WhiteBox
 163  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 164  *
 165  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 166  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 167  *                   -XX:FastAllocateSizeLimit=0
 168  *                   -Xint
 169  *                   SizeOf
 170  *
 171  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 172  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 173  *                   -XX:FastAllocateSizeLimit=0
 174  *                   -XX:TieredStopAtLevel=1
 175  *                   SizeOf
 176  *
 177  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 178  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 179  *                   -XX:FastAllocateSizeLimit=0
 180  *                   -XX:-TieredCompilation
 181  *                   SizeOf
 182  */
 183 
 184 /*
 185  * @test
 186  * @summary Test for Runtime.SizeOf with 32-bit compressed oops
 187  * @library /test/lib
 188  * @requires vm.bits == 64
 189  * @requires vm.debug
 190  *
 191  * @build sun.hotspot.WhiteBox
 192  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 193  *
 194  * @run main/othervm -Xmx128m
 195  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 196  *                   -XX:ObjectAlignmentInBytes=32
 197  *                   -Xint
 198  *                   SizeOf
 199  *
 200  * @run main/othervm -Xmx128m
 201  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 202  *                   -XX:ObjectAlignmentInBytes=32
 203  *                   -XX:TieredStopAtLevel=1
 204  *                   SizeOf
 205  *
 206  * @run main/othervm -Xmx128m
 207  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 208  *                   -XX:ObjectAlignmentInBytes=32
 209  *                   -XX:-TieredCompilation
 210  *                   SizeOf
 211  */
 212 
 213 /*
 214  * @test
 215  * @summary Test for Runtime.SizeOf with zero-based compressed oops
 216  * @library /test/lib
 217  * @requires vm.bits == 64
 218  * @requires vm.debug
 219  *
 220  * @build sun.hotspot.WhiteBox
 221  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
 222  *
 223  * @run main/othervm -Xmx4g
 224  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 225  *                   -XX:ObjectAlignmentInBytes=32
 226  *                   -Xint
 227  *                   SizeOf
 228  *
 229  * @run main/othervm -Xmx4g
 230  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 231  *                   -XX:ObjectAlignmentInBytes=32
 232  *                   -XX:TieredStopAtLevel=1
 233  *                   SizeOf
 234  *
 235  * @run main/othervm -Xmx4g
 236  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 237  *                   -XX:ObjectAlignmentInBytes=32
 238  *                   -XX:-TieredCompilation
 239  *                   SizeOf
 240  */
 241 
 242 import jdk.test.lib.Platform;
 243 import sun.hotspot.WhiteBox;
 244 
 245 public class SizeOf {
 246 
 247     static final Boolean compressedOops = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");
 248     static final int R = ((compressedOops == null) || (compressedOops == true)) ?  4 : 8;
 249 
 250     static final Long align = WhiteBox.getWhiteBox().getIntxVMFlag("ObjectAlignmentInBytes");
 251     static final int A = (align == null ? 8 : align.intValue());
 252 
 253     public static void main(String ... args) {
 254         testSize_newObject();
 255         testSize_localObject();
 256         testSize_fieldObject();
 257 
 258         testSize_newSmallByteArray();
 259         testSize_localSmallByteArray();
 260         testSize_fieldSmallByteArray();
 261 
 262         testSize_newSmallObjArray();
 263         testSize_localSmallObjArray();
 264         testSize_fieldSmallObjArray();
 265 
 266         testNulls();
 267     }
 268 
 269     private static int roundUp(int v, int a) {
 270         return (v + a - 1) / a * a;
 271     }
 272 
 273     private static void testSize_newObject() {
 274         int expected = roundUp(Platform.is64bit() ? 16 : 8, A);
 275         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 276             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(new Object()));
 277         }
 278     }
 279 
 280     private static void testSize_localObject() {
 281         int expected = roundUp(Platform.is64bit() ? 16 : 8, A);
 282         Object o = new Object();
 283         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 284             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(o));
 285         }
 286     }
 287 
 288     static Object staticO = new Object();
 289 
 290     private static void testSize_fieldObject() {
 291         int expected = roundUp(Platform.is64bit() ? 16 : 8, A);
 292         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 293             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(staticO));
 294         }
 295     }
 296 
 297     private static void testSize_newSmallByteArray() {
 298         int expected = roundUp(1024 + 16, A);
 299         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 300             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(new byte[1024]));
 301         }
 302     }
 303 
 304     private static void testSize_localSmallByteArray() {
 305         byte[] arr = new byte[1024];
 306         int expected = roundUp(arr.length + 16, A);
 307         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 308             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(arr));
 309         }
 310     }
 311 
 312     static byte[] smallArr = new byte[1024];
 313 
 314     private static void testSize_fieldSmallByteArray() {
 315         int expected = roundUp(smallArr.length + 16, A);
 316         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 317             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(smallArr));
 318         }
 319     }
 320 
 321     private static void testSize_newSmallObjArray() {
 322         int expected = roundUp(1024*R + 16, A);
 323         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 324             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(new Object[1024]));
 325         }
 326     }
 327 
 328     private static void testSize_localSmallObjArray() {
 329         Object[] arr = new Object[1024];
 330         int expected = roundUp(arr.length*R + 16, A);
 331         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 332             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(arr));
 333         }
 334     }
 335 
 336     static Object[] smallObjArr = new Object[1024];
 337 
 338     private static void testSize_fieldSmallObjArray() {
 339         int expected = roundUp(smallArr.length*R + 16, A);
 340         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 341             RuntimeOfUtil.assertEquals(expected, Runtime.sizeOf(smallObjArr));
 342         }
 343     }
 344 
 345     private static void testNulls() {
 346         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 347             try {
 348                 Runtime.sizeOf(null);
 349                 RuntimeOfUtil.assertFail();
 350             } catch (NullPointerException e) {
 351                 // expected
 352             }
 353         }
 354     }
 355 
 356 }