296
297 import java.util.*;
298
299 import jdk.test.lib.Platform;
300 import jdk.test.whitebox.WhiteBox;
301
302 public class GetObjectSizeIntrinsicsTest extends ASimpleInstrumentationTestCase {
303
304 static final Boolean COMPRESSED_OOPS = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");
305 static final long REF_SIZE = (COMPRESSED_OOPS == null || COMPRESSED_OOPS == true) ? 4 : 8;
306
307 static final Long align = WhiteBox.getWhiteBox().getIntxVMFlag("ObjectAlignmentInBytes");
308 static final int OBJ_ALIGN = (align == null ? 8 : align.intValue());
309
310 static final int SMALL_ARRAY_SIZE = 1024;
311
312 // These should overflow 4G size boundary
313 static final int LARGE_INT_ARRAY_SIZE = 1024*1024*1024 + 1024;
314 static final int LARGE_OBJ_ARRAY_SIZE = (4096/(int)REF_SIZE)*1024*1024 + 1024;
315
316 final String mode;
317
318 public GetObjectSizeIntrinsicsTest(String name, String mode) {
319 super(name);
320 this.mode = mode;
321 }
322
323 public static void main(String[] args)throws Throwable {
324 new GetObjectSizeIntrinsicsTest(args[0], (args.length >= 2 ? args[1] : "")).runTest();
325 }
326
327 public static final int ITERS = 200_000;
328
329 public static void assertEquals(long expected, long actual) {
330 if (expected != actual) {
331 throw new IllegalStateException(
332 "Error: expected: " + expected + " (" + Long.toHexString(expected) +
333 "), actual: " + actual + " (" + Long.toHexString(actual) + ")");
334 }
335 }
355 testSize_localSmallIntArray();
356 testSize_fieldSmallIntArray();
357
358 testSize_newSmallObjArray();
359 testSize_localSmallObjArray();
360 testSize_fieldSmallObjArray();
361
362 if (mode.equals("large")) {
363 testSize_localLargeIntArray();
364 testSize_localLargeObjArray();
365 }
366
367 testNulls();
368 }
369
370 private static long roundUp(long v, long a) {
371 return (v + a - 1) / a * a;
372 }
373
374 private void testSize_newObject() {
375 long expected = roundUp(Platform.is64bit() ? 16 : 8, OBJ_ALIGN);
376 for (int c = 0; c < ITERS; c++) {
377 assertEquals(expected, fInst.getObjectSize(new Object()));
378 }
379 }
380
381 private void testSize_localObject() {
382 long expected = roundUp(Platform.is64bit() ? 16 : 8, OBJ_ALIGN);
383 Object o = new Object();
384 for (int c = 0; c < ITERS; c++) {
385 assertEquals(expected, fInst.getObjectSize(o));
386 }
387 }
388
389 static Object staticO = new Object();
390
391 private void testSize_fieldObject() {
392 long expected = roundUp(Platform.is64bit() ? 16 : 8, OBJ_ALIGN);
393 for (int c = 0; c < ITERS; c++) {
394 assertEquals(expected, fInst.getObjectSize(staticO));
395 }
396 }
397
398 private void testSize_newSmallIntArray() {
399 long expected = roundUp(4L*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
400 for (int c = 0; c < ITERS; c++) {
401 assertEquals(expected, fInst.getObjectSize(new int[SMALL_ARRAY_SIZE]));
402 }
403 }
404
405 private void testSize_localSmallIntArray() {
406 int[] arr = new int[SMALL_ARRAY_SIZE];
407 long expected = roundUp(4L*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
408 for (int c = 0; c < ITERS; c++) {
409 assertEquals(expected, fInst.getObjectSize(arr));
410 }
411 }
412
|
296
297 import java.util.*;
298
299 import jdk.test.lib.Platform;
300 import jdk.test.whitebox.WhiteBox;
301
302 public class GetObjectSizeIntrinsicsTest extends ASimpleInstrumentationTestCase {
303
304 static final Boolean COMPRESSED_OOPS = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");
305 static final long REF_SIZE = (COMPRESSED_OOPS == null || COMPRESSED_OOPS == true) ? 4 : 8;
306
307 static final Long align = WhiteBox.getWhiteBox().getIntxVMFlag("ObjectAlignmentInBytes");
308 static final int OBJ_ALIGN = (align == null ? 8 : align.intValue());
309
310 static final int SMALL_ARRAY_SIZE = 1024;
311
312 // These should overflow 4G size boundary
313 static final int LARGE_INT_ARRAY_SIZE = 1024*1024*1024 + 1024;
314 static final int LARGE_OBJ_ARRAY_SIZE = (4096/(int)REF_SIZE)*1024*1024 + 1024;
315
316 static final boolean COMPACT_HEADERS = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompactObjectHeaders");
317 static final int HEADER_SIZE = COMPACT_HEADERS ? 8 : (Platform.is64bit() ? 16 : 8);
318
319 final String mode;
320
321 public GetObjectSizeIntrinsicsTest(String name, String mode) {
322 super(name);
323 this.mode = mode;
324 }
325
326 public static void main(String[] args)throws Throwable {
327 new GetObjectSizeIntrinsicsTest(args[0], (args.length >= 2 ? args[1] : "")).runTest();
328 }
329
330 public static final int ITERS = 200_000;
331
332 public static void assertEquals(long expected, long actual) {
333 if (expected != actual) {
334 throw new IllegalStateException(
335 "Error: expected: " + expected + " (" + Long.toHexString(expected) +
336 "), actual: " + actual + " (" + Long.toHexString(actual) + ")");
337 }
338 }
358 testSize_localSmallIntArray();
359 testSize_fieldSmallIntArray();
360
361 testSize_newSmallObjArray();
362 testSize_localSmallObjArray();
363 testSize_fieldSmallObjArray();
364
365 if (mode.equals("large")) {
366 testSize_localLargeIntArray();
367 testSize_localLargeObjArray();
368 }
369
370 testNulls();
371 }
372
373 private static long roundUp(long v, long a) {
374 return (v + a - 1) / a * a;
375 }
376
377 private void testSize_newObject() {
378 long expected = roundUp(HEADER_SIZE, OBJ_ALIGN);
379 for (int c = 0; c < ITERS; c++) {
380 assertEquals(expected, fInst.getObjectSize(new Object()));
381 }
382 }
383
384 private void testSize_localObject() {
385 long expected = roundUp(HEADER_SIZE, OBJ_ALIGN);
386 Object o = new Object();
387 for (int c = 0; c < ITERS; c++) {
388 assertEquals(expected, fInst.getObjectSize(o));
389 }
390 }
391
392 static Object staticO = new Object();
393
394 private void testSize_fieldObject() {
395 long expected = roundUp(HEADER_SIZE, OBJ_ALIGN);
396 for (int c = 0; c < ITERS; c++) {
397 assertEquals(expected, fInst.getObjectSize(staticO));
398 }
399 }
400
401 private void testSize_newSmallIntArray() {
402 long expected = roundUp(4L*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
403 for (int c = 0; c < ITERS; c++) {
404 assertEquals(expected, fInst.getObjectSize(new int[SMALL_ARRAY_SIZE]));
405 }
406 }
407
408 private void testSize_localSmallIntArray() {
409 int[] arr = new int[SMALL_ARRAY_SIZE];
410 long expected = roundUp(4L*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
411 for (int c = 0; c < ITERS; c++) {
412 assertEquals(expected, fInst.getObjectSize(arr));
413 }
414 }
415
|