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