rev 58143 : imported patch test_heap_walk

   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. 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  * Unit tests for JVMTI IterateOverReachableObjects and
  26  * IterateOverObjectsReachableFromObject functions.
  27  *
  28  */
  29 
  30 package nsk.jvmti.unit.heap;
  31 
  32 import nsk.share.jvmti.unit.*;
  33 import java.io.PrintStream;
  34 import java.util.*;
  35 
  36 public class HeapWalkTests {
  37 
  38     final static int JCK_STATUS_BASE = 95;
  39     final static int PASSED = 0;
  40     final static int FAILED = 2;
  41 
  42     public static void main(String args[]) {
  43         args = nsk.share.jvmti.JVMTITest.commonInit(args);
  44 
  45         // produce JCK-like exit status.
  46         System.exit(run(args, System.out) + JCK_STATUS_BASE);
  47     }
  48 
  49     public static int run(String args[], PrintStream out) {
  50 /*        test1();
  51         test2();
  52         test3();
  53 
  54  */
  55         test4();
  56         return PASSED;
  57     }
  58 
  59     private static void test1() {
  60         long tag;
  61 
  62         // This test uses the heap_root_callback to tag all the roots
  63 
  64         // It then examines a few known roots and checks that they
  65         // are tagged.
  66 
  67         // create a global reference
  68         Object o = new Object();
  69         Heap.newGlobalRef(o);
  70 
  71         // make sure current thread isn't tagged
  72         Heap.setTag(Thread.currentThread(), 0);
  73 
  74         Heap.setHeapRootCallback();
  75         Heap.iterateOverReachableObjects();
  76 
  77         // o must be JNI global - there's no other way it can be a root
  78 
  79         tag = Heap.getTag(o);
  80 
  81         if (tag != Heap.JVMTI_HEAP_ROOT_JNI_GLOBAL) {
  82             throw new RuntimeException("JNI global should have been tagged");
  83         }
  84 
  85         // Current thread is root but can't assume it will be
  86         // tagged with JVMTI_HEAP_ROOT_JNI_GLOBAL
  87 
  88         tag = Heap.getTag(Thread.currentThread());
  89         if (tag == 0) {
  90             throw new RuntimeException("Current thread isn't tagged");
  91         }
  92     }
  93 
  94     private static void test2() {
  95         long tag;
  96 
  97         // This test uses the stack_ref_callback to tag all references
  98         // from the thread stacks. The callback tags all objects with the tag
  99         // of the thread.
 100 
 101         Object o = new Object();
 102 
 103         Heap.setTag(Thread.currentThread(), 888);
 104 
 105         Heap.setStackRefCallback();
 106         Heap.iterateOverReachableObjects();
 107 
 108         tag = Heap.getTag(o);
 109         if (tag != 888) {
 110             throw new RuntimeException("stack local not tagged correctly");
 111         }
 112     }
 113 
 114 
 115     // used by test3
 116     static class Foo {
 117         private Object fld;
 118 
 119         Foo() {
 120             fld = new Object();
 121         }
 122 
 123         Object field() {
 124             return fld;
 125         }
 126 
 127         public static Object static_field = new Object();
 128     }
 129 
 130     private static int failures = 0;
 131 
 132     private static void check(Object o, long tag) {
 133         long actual_tag = Heap.getTag(o);
 134         if (actual_tag != tag) {
 135             if (actual_tag == 0) {
 136                 System.err.println(o + " is not tagged!");
 137             } else {
 138                 System.err.println(o + " is incorrectly tagged");
 139             }
 140             failures++;
 141         }
 142     }
 143 
 144     private static void test3() {
 145         long tag;
 146 
 147         // This test tags an object, and then calls IterateOverObjectsReachableFromObject
 148         // The callback tags all objects with a tag value of 777.
 149 
 150         Foo foo = new Foo();
 151 
 152         Heap.setObjectRefCallback();
 153         Heap.iterateOverObjectsReachableFromObject(foo);
 154 
 155         check(Foo.class, 777);
 156         check(Foo.static_field, 777);
 157         check(foo.field(), 777);
 158 
 159         if (failures > 0) {
 160             throw new RuntimeException("IterateOverObjectsReachableFromObject test failed");
 161         }
 162     }
 163 
 164     static final long TARGET_OBJECTS = 300_000;
 165 
 166     private static void test4() {
 167         ArrayList<ArrayList<Foo>> objects = new ArrayList<>();
 168         long cnt = 0;
 169         Random r = new Random();
 170         while (cnt < TARGET_OBJECTS) {
 171             int current = r.nextInt(2_000);
 172             ArrayList<Foo> list = new ArrayList<>(current);
 173             for (int index = 0; index < current; index ++) {
 174                 list.add(new Foo());
 175             }
 176             objects.add(list);
 177             cnt += current;
 178         }
 179 
 180         long start = System.nanoTime();
 181         Heap.iterateOverObjectsReachableFromObject(objects);
 182         long end = System.nanoTime();
 183 
 184         System.out.println("\nTime: " + (end - start) + " nsecs");
 185     }
 186 }
--- EOF ---