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().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 }
336
337 public static void assertNotEquals(long notExpected, long actual) {
338 if (notExpected == actual) {
339 throw new IllegalStateException(
340 "Error: not expected: " + notExpected + " (" + Long.toHexString(notExpected) +
341 "), actual: " + actual + " (" + Long.toHexString(actual) + ")");
342 }
343 }
344
345 public static void assertFail() {
346 throw new IllegalStateException("Should not be here");
347 }
348
349 protected final void doRunTest() throws Throwable {
350 testSize_newObject();
351 testSize_localObject();
352 testSize_fieldObject();
353
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 }