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:TieredStopAtLevel=2
 116  *                   DeepSizeOf
 117  *
 118  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 119  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 120  *                   -XX:TieredStopAtLevel=3
 121  *                   DeepSizeOf
 122  *
 123  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 124  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 125  *                   -XX:TieredStopAtLevel=4
 126  *                   DeepSizeOf
 127  *
 128  * @run main/othervm -Xmx128m -XX:-UseCompressedOops
 129  *                   -XX:+UnlockDiagnosticVMOptions -XX:+AbortVMOnCompilationFailure -Xcheck:jni
 130  *                   -XX:-TieredCompilation
 131  *                   DeepSizeOf
 132  */
 133 
 134 public class DeepSizeOf {
 135 
 136     public static void main(String ... args) {
 137         testSame_newObject();
 138         testSimpleHierarchy();
 139         testPartialNulls();
 140 
 141         testNodeChain(0);
 142         testNodeChain(1);
 143         testNodeChain(10);
 144         testNodeChain(100);
 145 
 146         testNodeTree();
 147 
 148         testObjArray(0);
 149         testObjArray(1);
 150         testObjArray(10);
 151         testObjArray(100);
 152 
 153         testNulls();
 154 
 155         testIncludeCheck();
 156         testIncludeCheckDeep();
 157     }
 158 
 159     private static void testSame_newObject() {
 160         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 161             Object o = new Object();
 162             RuntimeOfUtil.assertEquals(Runtime.sizeOf(o), Runtime.deepSizeOf(o));
 163         }
 164     }
 165 
 166     private static void testNodeChain(int depth) {
 167         Node n = new Node(null);
 168         for (int d = 0; d < depth; d++) {
 169             n = new Node(n);
 170         }
 171 
 172         for (int c = 0; c < RuntimeOfUtil.SHORT_ITERS; c++) {
 173             RuntimeOfUtil.assertEquals(Runtime.sizeOf(n)*(depth + 1), Runtime.deepSizeOf(n));
 174         }
 175     }
 176 
 177     private static class Node {
 178        Node next;
 179        public Node(Node n) { next = n; }
 180     }
 181 
 182     private static void testNodeTree() {
 183         TreeNode r = new TreeNode(new TreeNode(new TreeNode(null, null), null), new TreeNode(null, null));
 184         for (int c = 0; c < RuntimeOfUtil.SHORT_ITERS; c++) {
 185             RuntimeOfUtil.assertEquals(Runtime.sizeOf(r)*4, Runtime.deepSizeOf(r));
 186         }
 187     }
 188 
 189     private static class TreeNode {
 190        TreeNode l, r;
 191        public TreeNode(TreeNode l, TreeNode r) { this.l = l; this.r = r; }
 192     }
 193 
 194     private static void testObjArray(int size) {
 195         Object o = new Object();
 196         Object[] arr = new Object[size];
 197         for (int d = 0; d < size; d++) {
 198             arr[d] = new Object();
 199         }
 200 
 201         for (int c = 0; c < RuntimeOfUtil.SHORT_ITERS; c++) {
 202             RuntimeOfUtil.assertEquals(Runtime.sizeOf(arr) + Runtime.sizeOf(o)*size, Runtime.deepSizeOf(arr));
 203         }
 204     }
 205 
 206     private static class A {
 207         Object o1;
 208     }
 209 
 210     private static class B extends A {
 211         Object o2;
 212     }
 213 
 214     private static void testSimpleHierarchy() {
 215         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 216             B b = new B();
 217             b.o1 = new Object();
 218             b.o2 = new Object();
 219             RuntimeOfUtil.assertEquals(Runtime.sizeOf(b) + Runtime.sizeOf(b.o1) + Runtime.sizeOf(b.o2), Runtime.deepSizeOf(b));
 220         }
 221     }
 222 
 223     private static class D {
 224         Object o1;
 225         Object o2;
 226     }
 227 
 228     private static void testPartialNulls() {
 229         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 230             D d = new D();
 231             d.o1 = null;
 232             d.o2 = new Object();
 233             RuntimeOfUtil.assertEquals(Runtime.sizeOf(d) + Runtime.sizeOf(d.o2), Runtime.deepSizeOf(d));
 234         }
 235     }
 236 
 237     private static void testNulls() {
 238         for (int c = 0; c < RuntimeOfUtil.ITERS; c++) {
 239             try {
 240                 Runtime.deepSizeOf(null);
 241                 RuntimeOfUtil.assertFail();
 242             } catch (NullPointerException e) {
 243                 // expected
 244             }
 245         }
 246     }
 247 
 248     private static void testIncludeCheck() {
 249         for (int i = 0; i < RuntimeOfUtil.ITERS; i++) {
 250             Object o = new Object();
 251             RuntimeOfUtil.assertEquals(42L, Runtime.deepSizeOf(o, (obj) -> -42L));
 252         }
 253     }
 254 
 255     private static void testIncludeCheckDeep() {
 256         for (int i = 0; i < RuntimeOfUtil.ITERS; i++) {
 257             DeepA a = new DeepA();
 258             DeepB b = new DeepB();
 259             DeepC c = new DeepC();
 260             a.b = b;
 261             b.c = c;
 262             c.x = new Object();
 263 
 264             long sA = Runtime.sizeOf(a);
 265             long sB = Runtime.sizeOf(b);
 266             long sC = Runtime.sizeOf(c);
 267             long sCX = Runtime.sizeOf(c.x);
 268 
 269             RuntimeOfUtil.assertEquals(sA,
 270                                        Runtime.deepSizeOf(a, (obj) -> {
 271                                            if (obj instanceof DeepB)
 272                                                return 0L; // don't consider DeepB (don't go deeper)
 273                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 274                                        }));
 275 
 276             RuntimeOfUtil.assertEquals(sA + sC + sCX,
 277                                        Runtime.deepSizeOf(a, (obj) -> {
 278                                            if (obj instanceof DeepB)
 279                                                return Runtime.DEEP_SIZE_OF_TRAVERSE; // don't consider DeepB (but its references!)
 280                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 281                                        }));
 282 
 283             RuntimeOfUtil.assertEquals(sA,
 284                                        Runtime.deepSizeOf(a, (obj) -> {
 285                                            if (obj instanceof DeepA)
 286                                                return Runtime.DEEP_SIZE_OF_SHALLOW; // consider DeepA, but don't go deeper
 287                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 288                                        }));
 289 
 290             RuntimeOfUtil.assertEquals(sA + sB,
 291                                        Runtime.deepSizeOf(a, (obj) -> {
 292                                            if (obj instanceof DeepB)
 293                                                return Runtime.DEEP_SIZE_OF_SHALLOW; // consider DeepB, but don't go deeper
 294                                            return Runtime.DEEP_SIZE_OF_SHALLOW | Runtime.DEEP_SIZE_OF_TRAVERSE;
 295                                        }));
 296         }
 297     }
 298 
 299     private static class DeepA {
 300         DeepB b;
 301     }
 302 
 303     private static class DeepB {
 304         DeepC c;
 305     }
 306 
 307     private static class DeepC {
 308         Object x;
 309     }
 310 
 311 }