284 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest large
285 *
286 * @run main/othervm/timeout=240 -Xmx8g
287 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
288 * -Xbatch -XX:TieredStopAtLevel=1
289 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest large
290 *
291 * @run main/othervm/timeout=180 -Xmx8g
292 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
293 * -Xbatch -XX:-TieredCompilation
294 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest large
295 */
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().getIntVMFlag("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 }
354 testSize_newSmallIntArray();
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
413 static int[] smallArr = new int[SMALL_ARRAY_SIZE];
414
415 private void testSize_fieldSmallIntArray() {
416 long expected = roundUp(4L*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
417 for (int c = 0; c < ITERS; c++) {
418 assertEquals(expected, fInst.getObjectSize(smallArr));
419 }
420 }
421
422 private void testSize_newSmallObjArray() {
423 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
424 for (int c = 0; c < ITERS; c++) {
425 assertEquals(expected, fInst.getObjectSize(new Object[SMALL_ARRAY_SIZE]));
426 }
427 }
428
429 private void testSize_localSmallObjArray() {
430 Object[] arr = new Object[SMALL_ARRAY_SIZE];
431 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
432 for (int c = 0; c < ITERS; c++) {
433 assertEquals(expected, fInst.getObjectSize(arr));
434 }
435 }
436
437 static Object[] smallObjArr = new Object[SMALL_ARRAY_SIZE];
438
439 private void testSize_fieldSmallObjArray() {
440 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
441 for (int c = 0; c < ITERS; c++) {
442 assertEquals(expected, fInst.getObjectSize(smallObjArr));
443 }
444 }
445
446 private void testSize_localLargeIntArray() {
447 int[] arr = new int[LARGE_INT_ARRAY_SIZE];
448 long expected = roundUp(4L*LARGE_INT_ARRAY_SIZE + 16, OBJ_ALIGN);
449 for (int c = 0; c < ITERS; c++) {
450 assertEquals(expected, fInst.getObjectSize(arr));
451 }
452 }
453
454 private void testSize_localLargeObjArray() {
455 Object[] arr = new Object[LARGE_OBJ_ARRAY_SIZE];
456 long expected = roundUp(REF_SIZE*LARGE_OBJ_ARRAY_SIZE + 16, OBJ_ALIGN);
457 for (int c = 0; c < ITERS; c++) {
458 assertEquals(expected, fInst.getObjectSize(arr));
459 }
460 }
461
462 private void testNulls() {
463 for (int c = 0; c < ITERS; c++) {
464 try {
465 fInst.getObjectSize(null);
466 assertFail();
467 } catch (NullPointerException e) {
468 // expected
469 }
470 }
471 }
472
473 }
|
284 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest large
285 *
286 * @run main/othervm/timeout=240 -Xmx8g
287 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
288 * -Xbatch -XX:TieredStopAtLevel=1
289 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest large
290 *
291 * @run main/othervm/timeout=180 -Xmx8g
292 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
293 * -Xbatch -XX:-TieredCompilation
294 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest large
295 */
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 private static final boolean COMPACT_HEADERS = Platform.is64bit() && WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompactObjectHeaders");
305 static final Boolean COMPRESSED_OOPS = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");
306 static final long REF_SIZE = (COMPRESSED_OOPS == null || COMPRESSED_OOPS == true) ? 4 : 8;
307
308 static final Long align = WhiteBox.getWhiteBox().getIntVMFlag("ObjectAlignmentInBytes");
309 static final int OBJ_ALIGN = (align == null ? 8 : align.intValue());
310
311 static final int SMALL_ARRAY_SIZE = 1024;
312
313 // These should overflow 4G size boundary
314 static final int LARGE_INT_ARRAY_SIZE = 1024*1024*1024 + 1024;
315 static final int LARGE_OBJ_ARRAY_SIZE = (4096/(int)REF_SIZE)*1024*1024 + 1024;
316
317 static final boolean CCP = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedClassPointers");
318 static final int ARRAY_HEADER_SIZE = CCP ? 16 : (Platform.is64bit() ? 20 : 16);
319
320 final String mode;
321
322 public GetObjectSizeIntrinsicsTest(String name, String mode) {
323 super(name);
324 this.mode = mode;
325 }
326
327 public static void main(String[] args)throws Throwable {
328 new GetObjectSizeIntrinsicsTest(args[0], (args.length >= 2 ? args[1] : "")).runTest();
329 }
330
331 public static final int ITERS = 200_000;
332
333 public static void assertEquals(long expected, long actual) {
334 if (expected != actual) {
335 throw new IllegalStateException(
336 "Error: expected: " + expected + " (" + Long.toHexString(expected) +
337 "), actual: " + actual + " (" + Long.toHexString(actual) + ")");
338 }
339 }
358 testSize_newSmallIntArray();
359 testSize_localSmallIntArray();
360 testSize_fieldSmallIntArray();
361
362 testSize_newSmallObjArray();
363 testSize_localSmallObjArray();
364 testSize_fieldSmallObjArray();
365
366 if (mode.equals("large")) {
367 testSize_localLargeIntArray();
368 testSize_localLargeObjArray();
369 }
370
371 testNulls();
372 }
373
374 private static long roundUp(long v, long a) {
375 return (v + a - 1) / a * a;
376 }
377
378 private static long expectedSmallObjSize() {
379 long size;
380 if (!Platform.is64bit() || COMPACT_HEADERS) {
381 size = 8;
382 } else {
383 size = 16;
384 }
385 return roundUp(size, OBJ_ALIGN);
386 }
387
388 private void testSize_newObject() {
389 long expected = expectedSmallObjSize();
390 for (int c = 0; c < ITERS; c++) {
391 assertEquals(expected, fInst.getObjectSize(new Object()));
392 }
393 }
394
395 private void testSize_localObject() {
396 long expected = expectedSmallObjSize();
397 Object o = new Object();
398 for (int c = 0; c < ITERS; c++) {
399 assertEquals(expected, fInst.getObjectSize(o));
400 }
401 }
402
403 static Object staticO = new Object();
404
405 private void testSize_fieldObject() {
406 long expected = expectedSmallObjSize();
407 for (int c = 0; c < ITERS; c++) {
408 assertEquals(expected, fInst.getObjectSize(staticO));
409 }
410 }
411
412 private void testSize_newSmallIntArray() {
413 long expected = roundUp(4L*SMALL_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
414 for (int c = 0; c < ITERS; c++) {
415 assertEquals(expected, fInst.getObjectSize(new int[SMALL_ARRAY_SIZE]));
416 }
417 }
418
419 private void testSize_localSmallIntArray() {
420 int[] arr = new int[SMALL_ARRAY_SIZE];
421 long expected = roundUp(4L*SMALL_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
422 for (int c = 0; c < ITERS; c++) {
423 assertEquals(expected, fInst.getObjectSize(arr));
424 }
425 }
426
427 static int[] smallArr = new int[SMALL_ARRAY_SIZE];
428
429 private void testSize_fieldSmallIntArray() {
430 long expected = roundUp(4L*SMALL_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
431 for (int c = 0; c < ITERS; c++) {
432 assertEquals(expected, fInst.getObjectSize(smallArr));
433 }
434 }
435
436 private void testSize_newSmallObjArray() {
437 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
438 for (int c = 0; c < ITERS; c++) {
439 assertEquals(expected, fInst.getObjectSize(new Object[SMALL_ARRAY_SIZE]));
440 }
441 }
442
443 private void testSize_localSmallObjArray() {
444 Object[] arr = new Object[SMALL_ARRAY_SIZE];
445 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
446 for (int c = 0; c < ITERS; c++) {
447 assertEquals(expected, fInst.getObjectSize(arr));
448 }
449 }
450
451 static Object[] smallObjArr = new Object[SMALL_ARRAY_SIZE];
452
453 private void testSize_fieldSmallObjArray() {
454 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
455 for (int c = 0; c < ITERS; c++) {
456 assertEquals(expected, fInst.getObjectSize(smallObjArr));
457 }
458 }
459
460 private void testSize_localLargeIntArray() {
461 int[] arr = new int[LARGE_INT_ARRAY_SIZE];
462 long expected = roundUp(4L*LARGE_INT_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
463 for (int c = 0; c < ITERS; c++) {
464 assertEquals(expected, fInst.getObjectSize(arr));
465 }
466 }
467
468 private void testSize_localLargeObjArray() {
469 Object[] arr = new Object[LARGE_OBJ_ARRAY_SIZE];
470 long expected = roundUp(REF_SIZE*LARGE_OBJ_ARRAY_SIZE + ARRAY_HEADER_SIZE, OBJ_ALIGN);
471 for (int c = 0; c < ITERS; c++) {
472 assertEquals(expected, fInst.getObjectSize(arr));
473 }
474 }
475
476 private void testNulls() {
477 for (int c = 0; c < ITERS; c++) {
478 try {
479 fInst.getObjectSize(null);
480 assertFail();
481 } catch (NullPointerException e) {
482 // expected
483 }
484 }
485 }
486
487 }
|