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.lang.reflect.Modifier;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import jdk.jfr.Recording;
32 import jdk.jfr.consumer.RecordedEvent;
33 import jdk.jfr.consumer.RecordedObject;
34 import jdk.jfr.internal.test.WhiteBox;
35 import jdk.test.lib.Asserts;
36 import jdk.test.lib.jfr.EventNames;
37 import jdk.test.lib.jfr.Events;
38
39 /**
40 * @test
41 * @requires vm.flagless
42 * @requires vm.hasJFR
43 * @library /test/lib /test/jdk
44 * @modules jdk.jfr/jdk.jfr.internal.test
45 * @run main/othervm -XX:TLABSize=2k -Xlog:gc+tlab=trace jdk.jfr.event.oldobject.TestFieldInformation
46 */
47 public class TestFieldInformation {
48
49 public static final Object[] testField = new Object[50];
50
51 public static void main(String[] args) throws Exception {
52 WhiteBox.setWriteAllObjectSamples(true);
53
54 try (Recording recording = new Recording()) {
55 recording.enable(EventNames.OldObjectSample).withoutStackTrace().with("cutoff", "infinity");
56 recording.start();
57
58 addToTestField();
59
60 recording.stop();
61
62 List<RecordedEvent> events = Events.fromRecording(recording);
63 Events.hasEvents(events);
64 for (RecordedEvent e : events) {
65 if (hasValidField(e)) {
66 return;
67 }
68 }
69 System.out.println(events);
70 Asserts.fail("Could not find old object with field 'testField'");
71 }
72 }
73
74 private static boolean hasValidField(RecordedEvent e) throws Exception {
75 RecordedObject object = e.getValue("object");
76 Set<Long> visited = new HashSet<>();
77 while (object != null) {
78 Long address = object.getValue("address");
79 if (visited.contains(address)) {
80 return false;
81 }
82 visited.add(address);
83 RecordedObject referrer = object.getValue("referrer");
84 RecordedObject fieldObject = referrer != null ? referrer.getValue("field") : null;
85 if (fieldObject != null) {
86 String name = fieldObject.getValue("name");
87 if (name != null && name.equals("testField")) {
88 int modifiers = (short) fieldObject.getValue("modifiers");
89 if (!Modifier.isStatic(modifiers)) {
90 throw new Exception("Field should be static");
91 }
92 if (!Modifier.isPublic(modifiers)) {
93 throw new Exception("Field should be private");
94 }
95 if (!Modifier.isFinal(modifiers)) {
96 throw new Exception("Field should be final");
97 }
98 if (Modifier.isTransient(modifiers)) {
99 throw new Exception("Field should not be transient");
100 }
101 if (Modifier.isVolatile(modifiers)) {
102 throw new Exception("Field should not be volatile");
103 }
104 return true;
105 }
106 }
107 object = referrer != null ? referrer.getValue("object") : null;
108 }
109 return false;
110 }
111
112 private static void addToTestField() {
113 for (int i = 0; i < testField.length; i++) {
114 // Allocate array to trigger sampling code path for interpreter / c1
115 testField[i] = new Object[1_000_000];
116 }
117 }
118 }