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