Path: blob/master/test/jdk/java/lang/instrument/GetObjectSizeIntrinsicsTest.java
41149 views
/*1* Copyright (c) 2020, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 825352526* @summary Test for fInst.getObjectSize with 32-bit compressed oops27* @library /test/lib28*29* @build sun.hotspot.WhiteBox30* @run build GetObjectSizeIntrinsicsTest31* @run shell MakeJAR.sh basicAgent32*33* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox34*35* @run main/othervm -Xmx128m36* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.37* -Xint38* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest39*40* @run main/othervm -Xmx128m41* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.42* -Xbatch -XX:TieredStopAtLevel=143* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest44*45* @run main/othervm -Xmx128m46* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.47* -Xbatch -XX:-TieredCompilation48* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest49*/5051/*52* @test53* @summary Test for fInst.getObjectSize with zero-based compressed oops54* @library /test/lib55* @requires vm.bits == 6456*57* @build sun.hotspot.WhiteBox58* @run build GetObjectSizeIntrinsicsTest59* @run shell MakeJAR.sh basicAgent60*61* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox62*63* @run main/othervm -Xmx4g64* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.65* -Xint66* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest67*68* @run main/othervm -Xmx4g69* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.70* -Xbatch -XX:TieredStopAtLevel=171* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest72*73* @run main/othervm -Xmx4g74* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.75* -Xbatch -XX:-TieredCompilation76* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest77*/7879/*80* @test81* @summary Test for fInst.getObjectSize without compressed oops82* @library /test/lib83* @requires vm.bits == 6484*85* @build sun.hotspot.WhiteBox86* @run build GetObjectSizeIntrinsicsTest87* @run shell MakeJAR.sh basicAgent88*89* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox90*91* @run main/othervm -Xmx128m -XX:-UseCompressedOops92* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.93* -Xint94* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest95*96* @run main/othervm -Xmx128m -XX:-UseCompressedOops97* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.98* -Xbatch -XX:TieredStopAtLevel=199* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest100*101* @run main/othervm -Xmx128m -XX:-UseCompressedOops102* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.103* -Xbatch -XX:-TieredCompilation104* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest105*/106107/*108* @test109* @summary Test for fInst.getObjectSize with 32-bit compressed oops110* @library /test/lib111* @requires vm.debug112*113* @build sun.hotspot.WhiteBox114* @run build GetObjectSizeIntrinsicsTest115* @run shell MakeJAR.sh basicAgent116*117* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox118*119* @run main/othervm -Xmx128m120* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.121* -XX:FastAllocateSizeLimit=0122* -Xint123* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest124*125* @run main/othervm -Xmx128m126* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.127* -XX:FastAllocateSizeLimit=0128* -Xbatch -XX:TieredStopAtLevel=1129* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest130*131* @run main/othervm -Xmx128m132* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.133* -XX:FastAllocateSizeLimit=0134* -Xbatch -XX:-TieredCompilation135* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest136*/137138/*139* @test140* @summary Test for fInst.getObjectSize with zero-based compressed oops141* @library /test/lib142* @requires vm.bits == 64143* @requires vm.debug144*145* @build sun.hotspot.WhiteBox146* @run build GetObjectSizeIntrinsicsTest147* @run shell MakeJAR.sh basicAgent148*149* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox150*151* @run main/othervm -Xmx4g152* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.153* -XX:FastAllocateSizeLimit=0154* -Xint155* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest156*157* @run main/othervm -Xmx4g158* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.159* -XX:FastAllocateSizeLimit=0160* -Xbatch -XX:TieredStopAtLevel=1161* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest162*163* @run main/othervm -Xmx4g164* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.165* -XX:FastAllocateSizeLimit=0166* -Xbatch -XX:-TieredCompilation167* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest168*/169170/*171* @test172* @summary Test for fInst.getObjectSize without compressed oops173* @library /test/lib174* @requires vm.bits == 64175* @requires vm.debug176*177* @build sun.hotspot.WhiteBox178* @run build GetObjectSizeIntrinsicsTest179* @run shell MakeJAR.sh basicAgent180*181* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox182*183* @run main/othervm -Xmx128m -XX:-UseCompressedOops184* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.185* -XX:FastAllocateSizeLimit=0186* -Xint187* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest188*189* @run main/othervm -Xmx128m -XX:-UseCompressedOops190* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.191* -XX:FastAllocateSizeLimit=0192* -Xbatch -XX:TieredStopAtLevel=1193* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest194*195* @run main/othervm -Xmx128m -XX:-UseCompressedOops196* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.197* -XX:FastAllocateSizeLimit=0198* -Xbatch -XX:-TieredCompilation199* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest200*/201202/*203* @test204* @summary Test for fInst.getObjectSize with 32-bit compressed oops205* @library /test/lib206* @requires vm.bits == 64207* @requires vm.debug208*209* @build sun.hotspot.WhiteBox210* @run build GetObjectSizeIntrinsicsTest211* @run shell MakeJAR.sh basicAgent212*213* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox214*215* @run main/othervm -Xmx128m216* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.217* -XX:ObjectAlignmentInBytes=32218* -Xint219* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest220*221* @run main/othervm -Xmx128m222* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.223* -XX:ObjectAlignmentInBytes=32224* -Xbatch -XX:TieredStopAtLevel=1225* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest226*227* @run main/othervm -Xmx128m228* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.229* -XX:ObjectAlignmentInBytes=32230* -Xbatch -XX:-TieredCompilation231* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest232*/233234/*235* @test236* @summary Test for fInst.getObjectSize with zero-based compressed oops237* @library /test/lib238* @requires vm.bits == 64239* @requires vm.debug240*241* @build sun.hotspot.WhiteBox242* @run build GetObjectSizeIntrinsicsTest243* @run shell MakeJAR.sh basicAgent244*245* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox246*247* @run main/othervm -Xmx4g248* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.249* -XX:ObjectAlignmentInBytes=32250* -Xint251* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest252*253* @run main/othervm -Xmx4g254* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.255* -XX:ObjectAlignmentInBytes=32256* -Xbatch -XX:TieredStopAtLevel=1257* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest258*259* @run main/othervm -Xmx4g260* -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -XX:+WhiteBoxAPI -Xbootclasspath/a:.261* -XX:ObjectAlignmentInBytes=32262* -Xbatch -XX:-TieredCompilation263* -javaagent:basicAgent.jar GetObjectSizeIntrinsicsTest GetObjectSizeIntrinsicsTest264*/265266import java.util.*;267268import jdk.test.lib.Platform;269import sun.hotspot.WhiteBox;270271public class GetObjectSizeIntrinsicsTest extends ASimpleInstrumentationTestCase {272273static final Boolean compressedOops = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedOops");274static final int REF_SIZE = (compressedOops == null || compressedOops == true) ? 4 : 8;275276static final Long align = WhiteBox.getWhiteBox().getIntxVMFlag("ObjectAlignmentInBytes");277static final int OBJ_ALIGN = (align == null ? 8 : align.intValue());278279public GetObjectSizeIntrinsicsTest(String name) {280super(name);281}282283public static void main(String[] args)throws Throwable {284new GetObjectSizeIntrinsicsTest(args[0]).runTest();285}286287public static final int ITERS = 200_000;288289public static void assertEquals(long expected, long actual) {290if (expected != actual) {291throw new IllegalStateException(292"Error: expected: " + expected + " (" + Long.toHexString(expected) +293"), actual: " + actual + " (" + Long.toHexString(actual) + ")");294}295}296297public static void assertNotEquals(long notExpected, long actual) {298if (notExpected == actual) {299throw new IllegalStateException(300"Error: not expected: " + notExpected + " (" + Long.toHexString(notExpected) +301"), actual: " + actual + " (" + Long.toHexString(actual) + ")");302}303}304305public static void assertFail() {306throw new IllegalStateException("Should not be here");307}308309protected final void doRunTest() throws Throwable {310testSize_newObject();311testSize_localObject();312testSize_fieldObject();313314testSize_newSmallByteArray();315testSize_localSmallByteArray();316testSize_fieldSmallByteArray();317318testSize_newSmallObjArray();319testSize_localSmallObjArray();320testSize_fieldSmallObjArray();321322testNulls();323}324325private static int roundUp(int v, int a) {326return (v + a - 1) / a * a;327}328329private void testSize_newObject() {330int expected = roundUp(Platform.is64bit() ? 16 : 8, OBJ_ALIGN);331for (int c = 0; c < ITERS; c++) {332assertEquals(expected, fInst.getObjectSize(new Object()));333}334}335336private void testSize_localObject() {337int expected = roundUp(Platform.is64bit() ? 16 : 8, OBJ_ALIGN);338Object o = new Object();339for (int c = 0; c < ITERS; c++) {340assertEquals(expected, fInst.getObjectSize(o));341}342}343344static Object staticO = new Object();345346private void testSize_fieldObject() {347int expected = roundUp(Platform.is64bit() ? 16 : 8, OBJ_ALIGN);348for (int c = 0; c < ITERS; c++) {349assertEquals(expected, fInst.getObjectSize(staticO));350}351}352353private void testSize_newSmallByteArray() {354int expected = roundUp(1024 + 16, OBJ_ALIGN);355for (int c = 0; c < ITERS; c++) {356assertEquals(expected, fInst.getObjectSize(new byte[1024]));357}358}359360private void testSize_localSmallByteArray() {361byte[] arr = new byte[1024];362int expected = roundUp(arr.length + 16, OBJ_ALIGN);363for (int c = 0; c < ITERS; c++) {364assertEquals(expected, fInst.getObjectSize(arr));365}366}367368static byte[] smallArr = new byte[1024];369370private void testSize_fieldSmallByteArray() {371int expected = roundUp(smallArr.length + 16, OBJ_ALIGN);372for (int c = 0; c < ITERS; c++) {373assertEquals(expected, fInst.getObjectSize(smallArr));374}375}376377private void testSize_newSmallObjArray() {378int expected = roundUp(1024*REF_SIZE + 16, OBJ_ALIGN);379for (int c = 0; c < ITERS; c++) {380assertEquals(expected, fInst.getObjectSize(new Object[1024]));381}382}383384private void testSize_localSmallObjArray() {385Object[] arr = new Object[1024];386int expected = roundUp(arr.length*REF_SIZE + 16, OBJ_ALIGN);387for (int c = 0; c < ITERS; c++) {388assertEquals(expected, fInst.getObjectSize(arr));389}390}391392static Object[] smallObjArr = new Object[1024];393394private void testSize_fieldSmallObjArray() {395int expected = roundUp(smallArr.length*REF_SIZE + 16, OBJ_ALIGN);396for (int c = 0; c < ITERS; c++) {397assertEquals(expected, fInst.getObjectSize(smallObjArr));398}399}400401private void testNulls() {402for (int c = 0; c < ITERS; c++) {403try {404fInst.getObjectSize(null);405assertFail();406} catch (NullPointerException e) {407// expected408}409}410}411412}413414415