1 /*
2 * Copyright (c) 2018, 2025, 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 package jdk.jfr.event.oldobject;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import jdk.jfr.Recording;
30 import jdk.jfr.consumer.RecordedClass;
31 import jdk.jfr.consumer.RecordedEvent;
32 import jdk.jfr.consumer.RecordedObject;
33 import jdk.jfr.internal.test.WhiteBox;
34 import jdk.test.lib.jfr.EventNames;
35 import jdk.test.lib.jfr.Events;
36
37 /**
38 * @test
39 * @requires vm.flagless
40 * @requires vm.hasJFR
41 * @library /test/lib /test/jdk
42 * @modules jdk.jfr/jdk.jfr.internal.test
43 * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestArrayInformation
44 */
45 public class TestArrayInformation {
46
47 private static class ArrayLeak {
48 }
49
50 private static final int CHAIN_DEPTH = 50;
51 private static final int ARRAY_SIZE = 52;
52 private static final int ARRAY_INDEX = 26;
53
54 public static List<Object> leak = new ArrayList<>(25);
55
56 public static void main(String[] args) throws Exception {
57 WhiteBox.setWriteAllObjectSamples(true);
58
59 while (true) {
60 try (Recording recording = new Recording()) {
61 recording.enable(EventNames.OldObjectSample).withoutStackTrace().with("cutoff", "infinity");
62 recording.start();
63 for(int i = 0; i < 25; i++) {
64 leak.add(buildNestedArray(CHAIN_DEPTH));
65 }
66 recording.stop();
67 List<RecordedEvent> events = Events.fromRecording(recording);
68 Events.hasEvents(events);
69 if (verifyObjectArray(events)) {
70 return;
71 }
72 }
73 leak.clear();
74 System.out.println("Retrying...");
75 }
76 }
77
78 private static boolean verifyObjectArray(List<RecordedEvent> events) throws Exception {
79 for (RecordedEvent e : events) {
80 RecordedObject object = e.getValue("object");
81 RecordedClass objectType = object.getValue("type");
82 RecordedObject referrer = object.getValue("referrer");
83 System.out.println(objectType.getName());
84 if (objectType.getName().equals(ArrayLeak[].class.getName())) {
85 for (int i = 0; i < CHAIN_DEPTH; i++) {
86 object = referrer.getValue("object");
87 if (object == null) {
88 throw new Exception("Expected referrer object");
89 }
90 objectType = object.getValue("type");
91 if (!objectType.getName().equals(Object[].class.getName())) {
92 throw new Exception("Expect array class to be named " + Object[].class + " but found " + objectType.getName());
93 }
94 RecordedObject field = referrer.getValue("field");
95 if (field != null) {
96 throw new Exception("Didn't expect to find field");
97 }
98 RecordedObject array = referrer.getValue("array");
99 if (array == null) {
100 throw new Exception("Expected array object, but got null");
101 }
102 int index = referrer.getValue("array.index");
103 if (index != ARRAY_INDEX) {
104 throw new Exception("Expected array index: " + ARRAY_INDEX + ", but got " + index);
105 }
106 int size = referrer.getValue("array.size");
107 if (size != ARRAY_SIZE) {
108 throw new Exception("Expected array size: " + ARRAY_SIZE + ", but got " + size);
109 }
110 referrer = object.getValue("referrer");
111 }
112 return true;
113 }
114 }
115 System.out.println("Could not find event with " + ArrayLeak[].class + " as (leak) object");
116 return false;
117 }
118
119 private static Object buildNestedArray(int depth) {
120 if (depth > 0) {
121 Object[] array = new Object[ARRAY_SIZE];
122 array[ARRAY_INDEX] = buildNestedArray(depth - 1);
123 return array;
124 } else {
125 return new ArrayLeak[OldObjects.MIN_SIZE];
126 }
127 }
128
129 }