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
35 public class HeapWalkTests {
36
37 final static int JCK_STATUS_BASE = 95;
38 final static int PASSED = 0;
39 final static int FAILED = 2;
40
41 public static void main(String args[]) {
42 args = nsk.share.jvmti.JVMTITest.commonInit(args);
43
44 // produce JCK-like exit status.
45 System.exit(run(args, System.out) + JCK_STATUS_BASE);
46 }
47
48 public static int run(String args[], PrintStream out) {
49 test1();
50 test2();
51 test3();
52 return PASSED;
53 }
54
55 private static void test1() {
56 long tag;
57
58 // This test uses the heap_root_callback to tag all the roots
59
60 // It then examines a few known roots and checks that they
61 // are tagged.
62
63 // create a global reference
64 Object o = new Object();
65 Heap.newGlobalRef(o);
66
67 // make sure current thread isn't tagged
68 Heap.setTag(Thread.currentThread(), 0);
69
70 Heap.setHeapRootCallback();
71 Heap.iterateOverReachableObjects();
138 }
139
140 private static void test3() {
141 long tag;
142
143 // This test tags an object, and then calls IterateOverObjectsReachableFromObject
144 // The callback tags all objects with a tag value of 777.
145
146 Foo foo = new Foo();
147
148 Heap.setObjectRefCallback();
149 Heap.iterateOverObjectsReachableFromObject(foo);
150
151 check(Foo.class, 777);
152 check(Foo.static_field, 777);
153 check(foo.field(), 777);
154
155 if (failures > 0) {
156 throw new RuntimeException("IterateOverObjectsReachableFromObject test failed");
157 }
158 }
159 }
|
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();
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 }
|