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