1 /*
2 * Copyright (c) 2020, 2022, 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 * @bug 8253525
27 * @summary Test for fInst.getObjectSize with 32-bit compressed oops
28 * @library /test/lib
29 *
30 * @build jdk.test.whitebox.WhiteBox
31 * @run build GetObjectSizeIntrinsicsTest
32 * @run shell MakeJAR.sh basicAgent
33 *
34 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
35 *
36 * @run main/othervm -Xmx128m
37 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
38 * -Xint
39 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
40 *
41 * @run main/othervm -Xmx128m
42 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
43 * -Xbatch -XX:TieredStopAtLevel=1
44 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
45 *
46 * @run main/othervm -Xmx128m
47 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
48 * -Xbatch -XX:-TieredCompilation
49 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
50 */
51
52 /*
53 * @test
54 * @summary Test for fInst.getObjectSize with zero-based compressed oops
55 * @library /test/lib
56 * @requires vm.bits == 64
57 *
58 * @build jdk.test.whitebox.WhiteBox
59 * @run build GetObjectSizeIntrinsicsTest
60 * @run shell MakeJAR.sh basicAgent
61 *
62 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
63 *
64 * @run main/othervm -Xmx4g
65 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
66 * -Xint
67 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
68 *
69 * @run main/othervm -Xmx4g
70 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
71 * -Xbatch -XX:TieredStopAtLevel=1
72 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
73 *
74 * @run main/othervm -Xmx4g
75 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
76 * -Xbatch -XX:-TieredCompilation
77 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
78 */
79
80 /*
81 * @test
82 * @summary Test for fInst.getObjectSize without compressed oops
83 * @library /test/lib
84 * @requires vm.bits == 64
85 *
86 * @build jdk.test.whitebox.WhiteBox
87 * @run build GetObjectSizeIntrinsicsTest
88 * @run shell MakeJAR.sh basicAgent
89 *
90 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
91 *
92 * @run main/othervm -Xmx128m -XX:-UseCompressedOops
93 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
94 * -Xint
95 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
96 *
97 * @run main/othervm -Xmx128m -XX:-UseCompressedOops
98 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
99 * -Xbatch -XX:TieredStopAtLevel=1
100 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
101 *
102 * @run main/othervm -Xmx128m -XX:-UseCompressedOops
103 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
104 * -Xbatch -XX:-TieredCompilation
105 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
106 */
107
108 /*
109 * @test
110 * @summary Test for fInst.getObjectSize with 32-bit compressed oops
111 * @library /test/lib
112 * @requires vm.debug
113 *
114 * @build jdk.test.whitebox.WhiteBox
115 * @run build GetObjectSizeIntrinsicsTest
116 * @run shell MakeJAR.sh basicAgent
117 *
118 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
119 *
120 * @run main/othervm -Xmx128m
121 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
122 * -XX:FastAllocateSizeLimit=0
123 * -Xint
124 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
125 *
126 * @run main/othervm -Xmx128m
127 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
128 * -XX:FastAllocateSizeLimit=0
129 * -Xbatch -XX:TieredStopAtLevel=1
130 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
131 *
132 * @run main/othervm -Xmx128m
133 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
134 * -XX:FastAllocateSizeLimit=0
135 * -Xbatch -XX:-TieredCompilation
136 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
137 */
138
139 /*
140 * @test
141 * @summary Test for fInst.getObjectSize with zero-based compressed oops
142 * @library /test/lib
143 * @requires vm.bits == 64
144 * @requires vm.debug
145 *
146 * @build jdk.test.whitebox.WhiteBox
147 * @run build GetObjectSizeIntrinsicsTest
148 * @run shell MakeJAR.sh basicAgent
149 *
150 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
151 *
152 * @run main/othervm -Xmx4g
153 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
154 * -XX:FastAllocateSizeLimit=0
155 * -Xint
156 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
157 *
158 * @run main/othervm -Xmx4g
159 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
160 * -XX:FastAllocateSizeLimit=0
161 * -Xbatch -XX:TieredStopAtLevel=1
162 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
163 *
164 * @run main/othervm -Xmx4g
165 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
166 * -XX:FastAllocateSizeLimit=0
167 * -Xbatch -XX:-TieredCompilation
168 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
169 */
170
171 /*
172 * @test
173 * @summary Test for fInst.getObjectSize without compressed oops
174 * @library /test/lib
175 * @requires vm.bits == 64
176 * @requires vm.debug
177 *
178 * @build jdk.test.whitebox.WhiteBox
179 * @run build GetObjectSizeIntrinsicsTest
180 * @run shell MakeJAR.sh basicAgent
181 *
182 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
183 *
184 * @run main/othervm -Xmx128m -XX:-UseCompressedOops
185 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
186 * -XX:FastAllocateSizeLimit=0
187 * -Xint
188 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
189 *
190 * @run main/othervm -Xmx128m -XX:-UseCompressedOops
191 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
192 * -XX:FastAllocateSizeLimit=0
193 * -Xbatch -XX:TieredStopAtLevel=1
194 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
195 *
196 * @run main/othervm -Xmx128m -XX:-UseCompressedOops
197 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
198 * -XX:FastAllocateSizeLimit=0
199 * -Xbatch -XX:-TieredCompilation
200 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
201 */
202
203 /*
204 * @test
205 * @summary Test for fInst.getObjectSize with 32-bit compressed oops
206 * @library /test/lib
207 * @requires vm.bits == 64
208 * @requires vm.debug
209 *
210 * @build jdk.test.whitebox.WhiteBox
211 * @run build GetObjectSizeIntrinsicsTest
212 * @run shell MakeJAR.sh basicAgent
213 *
214 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
215 *
216 * @run main/othervm -Xmx128m
217 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
218 * -XX:ObjectAlignmentInBytes=32
219 * -Xint
220 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
221 *
222 * @run main/othervm -Xmx128m
223 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
224 * -XX:ObjectAlignmentInBytes=32
225 * -Xbatch -XX:TieredStopAtLevel=1
226 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
227 *
228 * @run main/othervm -Xmx128m
229 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
230 * -XX:ObjectAlignmentInBytes=32
231 * -Xbatch -XX:-TieredCompilation
232 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
233 */
234
235 /*
236 * @test
237 * @summary Test for fInst.getObjectSize with zero-based compressed oops
238 * @library /test/lib
239 * @requires vm.bits == 64
240 * @requires vm.debug
241 *
242 * @build jdk.test.whitebox.WhiteBox
243 * @run build GetObjectSizeIntrinsicsTest
244 * @run shell MakeJAR.sh basicAgent
245 *
246 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
247 *
248 * @run main/othervm -Xmx4g
249 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
250 * -XX:ObjectAlignmentInBytes=32
251 * -Xint
252 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
253 *
254 * @run main/othervm -Xmx4g
255 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
256 * -XX:ObjectAlignmentInBytes=32
257 * -Xbatch -XX:TieredStopAtLevel=1
258 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
259 *
260 * @run main/othervm -Xmx4g
261 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
262 * -XX:ObjectAlignmentInBytes=32
263 * -Xbatch -XX:-TieredCompilation
264 * -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest
265 */
266
267 /*
268 * @test
269 * @summary Test for fInst.getObjectSize with large arrays
270 * @library /test/lib
271 * @requires vm.bits == 64
272 * @requires vm.debug
273 * @requires os.maxMemory >= 10G
274 *
275 * @build jdk.test.whitebox.WhiteBox
276 * @run build GetObjectSizeIntrinsicsTest
277 * @run shell MakeJAR.sh basicAgent
278 *
279 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
280 *
281 * @run main/othervm/timeout=300 -Xmx8g
282 * -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.
283 * -Xint
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().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 }
339
340 public static void assertNotEquals(long notExpected, long actual) {
341 if (notExpected == actual) {
342 throw new IllegalStateException(
343 "Error: not expected: " + notExpected + " (" + Long.toHexString(notExpected) +
344 "), actual: " + actual + " (" + Long.toHexString(actual) + ")");
345 }
346 }
347
348 public static void assertFail() {
349 throw new IllegalStateException("Should not be here");
350 }
351
352 protected final void doRunTest() throws Throwable {
353 testSize_newObject();
354 testSize_localObject();
355 testSize_fieldObject();
356
357 testSize_newSmallIntArray();
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
416 static int[] smallArr = new int[SMALL_ARRAY_SIZE];
417
418 private void testSize_fieldSmallIntArray() {
419 long expected = roundUp(4L*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
420 for (int c = 0; c < ITERS; c++) {
421 assertEquals(expected, fInst.getObjectSize(smallArr));
422 }
423 }
424
425 private void testSize_newSmallObjArray() {
426 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
427 for (int c = 0; c < ITERS; c++) {
428 assertEquals(expected, fInst.getObjectSize(new Object[SMALL_ARRAY_SIZE]));
429 }
430 }
431
432 private void testSize_localSmallObjArray() {
433 Object[] arr = new Object[SMALL_ARRAY_SIZE];
434 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
435 for (int c = 0; c < ITERS; c++) {
436 assertEquals(expected, fInst.getObjectSize(arr));
437 }
438 }
439
440 static Object[] smallObjArr = new Object[SMALL_ARRAY_SIZE];
441
442 private void testSize_fieldSmallObjArray() {
443 long expected = roundUp(REF_SIZE*SMALL_ARRAY_SIZE + 16, OBJ_ALIGN);
444 for (int c = 0; c < ITERS; c++) {
445 assertEquals(expected, fInst.getObjectSize(smallObjArr));
446 }
447 }
448
449 private void testSize_localLargeIntArray() {
450 int[] arr = new int[LARGE_INT_ARRAY_SIZE];
451 long expected = roundUp(4L*LARGE_INT_ARRAY_SIZE + 16, OBJ_ALIGN);
452 for (int c = 0; c < ITERS; c++) {
453 assertEquals(expected, fInst.getObjectSize(arr));
454 }
455 }
456
457 private void testSize_localLargeObjArray() {
458 Object[] arr = new Object[LARGE_OBJ_ARRAY_SIZE];
459 long expected = roundUp(REF_SIZE*LARGE_OBJ_ARRAY_SIZE + 16, OBJ_ALIGN);
460 for (int c = 0; c < ITERS; c++) {
461 assertEquals(expected, fInst.getObjectSize(arr));
462 }
463 }
464
465 private void testNulls() {
466 for (int c = 0; c < ITERS; c++) {
467 try {
468 fInst.getObjectSize(null);
469 assertFail();
470 } catch (NullPointerException e) {
471 // expected
472 }
473 }
474 }
475
476 }