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 Runtime.deepSizeOf with 32-bit compressed oops
  27  * @library /test/lib
  28  *
  29  * @run main/othervm -Xmx128m
  30  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  31  *                   -Xint
  32  *                   DeepSizeOf
  33  *
  34  * @run main/othervm -Xmx128m
  35  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  36  *                   -XX:TieredStopAtLevel=1
  37  *                   DeepSizeOf
  38  *
  39  * @run main/othervm -Xmx128m
  40  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  41  *                   -XX:TieredStopAtLevel=2
  42  *                   DeepSizeOf
  43  *
  44  * @run main/othervm -Xmx128m
  45  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  46  *                   -XX:TieredStopAtLevel=3
  47  *                   DeepSizeOf
  48  *
  49  * @run main/othervm -Xmx128m
  50  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  51  *                   -XX:TieredStopAtLevel=4
  52  *                   DeepSizeOf
  53  *
  54  * @run main/othervm -Xmx128m
  55  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  56  *                   -XX:-TieredCompilation
  57  *                   DeepSizeOf
  58  */
  59 
  60 /*
  61  * @test
  62  * @summary Test for Runtime.deepSizeOf with zero-based compressed oops
  63  * @library /test/lib
  64  * @requires vm.bits == 64
  65  *
  66  * @run main/othervm -Xmx4g
  67  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  68  *                   -Xint
  69  *                   DeepSizeOf
  70  *
  71  * @run main/othervm -Xmx4g
  72  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  73  *                   -XX:TieredStopAtLevel=1
  74  *                   DeepSizeOf
  75  *
  76  * @run main/othervm -Xmx4g
  77  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  78  *                   -XX:TieredStopAtLevel=2
  79  *                   DeepSizeOf
  80  *
  81  * @run main/othervm -Xmx4g
  82  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  83  *                   -XX:TieredStopAtLevel=3
  84  *                   DeepSizeOf
  85  *
  86  * @run main/othervm -Xmx4g
  87  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  88  *                   -XX:TieredStopAtLevel=4
  89  *                   DeepSizeOf
  90  *
  91  * @run main/othervm -Xmx4g
  92  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
  93  *                   -XX:-TieredCompilation
  94  *                   DeepSizeOf
  95  */
  96 
  97 /*
  98  * @test
  99  * @summary Test for Runtime.deepSizeOf without compressed oops
 100  * @library /test/lib
 101  * @requires vm.bits == 64
 102  *
 103  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 104  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 105  *                   -Xint
 106  *                   DeepSizeOf
 107  *
 108  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 109  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 110  *                   -XX:TieredStopAtLevel=1
 111  *                   DeepSizeOf
 112  *
 113  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 114  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 115  *                   -XX:-TieredCompilation
 116  *                   DeepSizeOf
 117  */
 118 
 119 public class DeepSizeOf {
 120 
 121     public static void main(String ... args) {
 122         testSame_newObject();
 123         testSimpleHierarchy();
 124         testPartialNulls();
 125 
 126         testNodeChain(0);
 127         testNodeChain(1);
 128         testNodeChain(10);
 129         testNodeChain(100);
 130 
 131         testNodeTree();
 132 
 133         testObjArray(0);
 134         testObjArray(1);
 135         testObjArray(10);
 136         testObjArray(100);
 137 
 138         testNulls();
 139 
 140         testIncludeCheck();
 141         testIncludeCheckDeep();
 142     }
 143 
 144     private static void testSame_newObject() {
 145         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 146             Object o = new Object();
 147             RuntimeOfUtil.assertEquals(Runtime.sizeOf(o), Runtime.deepSizeOf(o));
 148         }
 149     }
 150 
 151     private static void testNodeChain(int depth) {
 152         Node n = new Node(null);
 153         for (int d = 0; d < depth; d++) {
 154             n = new Node(n);
 155         }
 156 
 157         for (int c = 0; c < RuntimeOfUtil.SHORT_ITERS; c++) {
 158             RuntimeOfUtil.assertEquals(Runtime.sizeOf(n)*(depth + 1), Runtime.deepSizeOf(n));
 159         }
 160     }
 161 
 162     private static class Node {
 163        Node next;
 164        public Node(Node n) { next = n; }
 165     }
 166 
 167     private static void testNodeTree() {
 168         TreeNode r = new TreeNode(new TreeNode(new TreeNode(null, null), null), new TreeNode(null, null));
 169         for (int c = 0; c < RuntimeOfUtil.SHORT_ITERS; c++) {
 170             RuntimeOfUtil.assertEquals(Runtime.sizeOf(r)*4, Runtime.deepSizeOf(r));
 171         }
 172     }
 173 
 174     private static class TreeNode {
 175        TreeNode l, r;
 176        public TreeNode(TreeNode l, TreeNode r) { this.l = l; this.r = r; }
 177     }
 178 
 179     private static void testObjArray(int size) {
 180         Object o = new Object();
 181         Object[] arr = new Object[size];
 182         for (int d = 0; d < size; d++) {
 183             arr[d] = new Object();
 184         }
 185 
 186         for (int c = 0; c < RuntimeOfUtil.SHORT_ITERS; c++) {
 187             RuntimeOfUtil.assertEquals(Runtime.sizeOf(arr) + Runtime.sizeOf(o)*size, Runtime.deepSizeOf(arr));
 188         }
 189     }
 190 
 191     private static class A {
 192         Object o1;
 193     }
 194 
 195     private static class B extends A {
 196         Object o2;
 197     }
 198 
 199     private static void testSimpleHierarchy() {
 200         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 201             B b = new B();
 202             b.o1 = new Object();
 203             b.o2 = new Object();
 204             RuntimeOfUtil.assertEquals(Runtime.sizeOf(b) + Runtime.sizeOf(b.o1) + Runtime.sizeOf(b.o2), Runtime.deepSizeOf(b));
 205         }
 206     }
 207 
 208     private static class D {
 209         Object o1;
 210         Object o2;
 211     }
 212 
 213     private static void testPartialNulls() {
 214         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 215             D d = new D();
 216             d.o1 = null;
 217             d.o2 = new Object();
 218             RuntimeOfUtil.assertEquals(Runtime.sizeOf(d) + Runtime.sizeOf(d.o2), Runtime.deepSizeOf(d));
 219         }
 220     }
 221 
 222     private static void testNulls() {
 223         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 224             try {
 225                 Runtime.deepSizeOf(null);
 226                 RuntimeOfUtil.assertFail();
 227             } catch (NullPointerException e) {
 228                 // expected
 229             }
 230         }
 231     }
 232 
 233     private static void testIncludeCheck() {
 234         for (int i = 0; i < RuntimeOfUtil.ITERS; i++) {
 235             Object o = new Object();
 236             RuntimeOfUtil.assertEquals(42L, Runtime.deepSizeOf(o, (obj) -> -42L));
 237         }
 238     }
 239 
 240     private static void testIncludeCheckDeep() {
 241         for (int i = 0; i < RuntimeOfUtil.ITERS; i++) {
 242             DeepA a = new DeepA();
 243             DeepB b = new DeepB();
 244             DeepC c = new DeepC();
 245             a.b = b;
 246             b.c = c;
 247             c.x = new Object();
 248 
 249             long sA = Runtime.sizeOf(a);
 250             long sB = Runtime.sizeOf(b);
 251             long sC = Runtime.sizeOf(c);
 252             long sCX = Runtime.sizeOf(c.x);
 253 
 254             RuntimeOfUtil.assertEquals(sA,
 255                                        Runtime.deepSizeOf(a, (obj) -> {
 256                                            if (obj instanceof DeepB)
 257                                                return 0L; // don't consider DeepB (don't go deeper)
 258                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 259                                        }));
 260 
 261             RuntimeOfUtil.assertEquals(sA + sC + sCX,
 262                                        Runtime.deepSizeOf(a, (obj) -> {
 263                                            if (obj instanceof DeepB)
 264                                                return Runtime.DEEP_SIZE_OF_TRAVERSE; // don't consider DeepB (but its references!)
 265                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 266                                        }));
 267 
 268             RuntimeOfUtil.assertEquals(sA,
 269                                        Runtime.deepSizeOf(a, (obj) -> {
 270                                            if (obj instanceof DeepA)
 271                                                return Runtime.DEEP_SIZE_OF_SHALLOW; // consider DeepA, but don't go deeper
 272                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 273                                        }));
 274 
 275             RuntimeOfUtil.assertEquals(sA + sB,
 276                                        Runtime.deepSizeOf(a, (obj) -> {
 277                                            if (obj instanceof DeepB)
 278                                                return Runtime.DEEP_SIZE_OF_SHALLOW; // consider DeepB, but don't go deeper
 279                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 280                                        }));
 281         }
 282     }
 283 
 284     private static class DeepA {
 285         DeepB b;
 286     }
 287 
 288     private static class DeepB {
 289         DeepC c;
 290     }
 291 
 292     private static class DeepC {
 293         Object x;
 294     }
 295 
 296 }