1 /* 2 * Copyright (c) 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.jfr.event.oldobject; 27 28 import java.lang.reflect.Modifier; 29 import java.util.HashSet; 30 import java.util.List; 31 import java.util.Set; 32 33 import jdk.jfr.Recording; 34 import jdk.jfr.consumer.RecordedEvent; 35 import jdk.jfr.consumer.RecordedObject; 36 import jdk.jfr.internal.test.WhiteBox; 37 import jdk.test.lib.Asserts; 38 import jdk.test.lib.jfr.EventNames; 39 import jdk.test.lib.jfr.Events; 40 41 /** 42 * @test 43 * @key jfr 44 * 45 * 46 * @library /lib / 47 * 48 * @run main/othervm -XX:TLABSize=2k -XX:+PrintGCDetails -XX:+PrintGC -verbose:class jdk.jfr.event.oldobject.TestFieldInformation 49 */ 50 public class TestFieldInformation { 51 52 public static final Object[] testField = new Object[50]; 53 54 public static void main(String[] args) throws Exception { 55 WhiteBox.setWriteAllObjectSamples(true); 56 57 try (Recording recording = new Recording()) { 58 recording.enable(EventNames.OldObjectSample).withoutStackTrace().with("cutoff", "infinity"); 59 recording.start(); 60 61 addToTestField(); 62 63 recording.stop(); 64 65 List<RecordedEvent> events = Events.fromRecording(recording); 66 Events.hasEvents(events); 67 for (RecordedEvent e : events) { 68 if (hasValidField(e)) { 69 return; 70 } 71 } 72 System.out.println(events); 73 Asserts.fail("Could not find old object with field 'testField'"); 74 } 75 } 76 77 private static boolean hasValidField(RecordedEvent e) throws Exception { 78 RecordedObject object = e.getValue("object"); 79 Set<Long> visited = new HashSet<>(); 80 while (object != null) { 81 Long address = object.getValue("address"); 82 if (visited.contains(address)) { 83 return false; 84 } 85 visited.add(address); 86 RecordedObject referrer = object.getValue("referrer"); 87 RecordedObject fieldObject = referrer != null ? referrer.getValue("field") : null; 88 if (fieldObject != null) { 89 String name = fieldObject.getValue("name"); 90 if (name != null && name.equals("testField")) { 91 int modifiers = (short) fieldObject.getValue("modifiers"); 92 if (!Modifier.isStatic(modifiers)) { 93 throw new Exception("Field should be static"); 94 } 95 if (!Modifier.isPublic(modifiers)) { 96 throw new Exception("Field should be private"); 97 } 98 if (!Modifier.isFinal(modifiers)) { 99 throw new Exception("Field should be final"); 100 } 101 if (Modifier.isTransient(modifiers)) { 102 throw new Exception("Field should not be transient"); 103 } 104 if (Modifier.isVolatile(modifiers)) { 105 throw new Exception("Field should not be volatile"); 106 } 107 return true; 108 } 109 } 110 object = referrer != null ? referrer.getValue("object") : null; 111 } 112 return false; 113 } 114 115 private static void addToTestField() { 116 for (int i = 0; i < testField.length; i++) { 117 // Allocate array to trigger sampling code path for interpreter / c1 118 testField[i] = new Object[1_000_000]; 119 } 120 } 121 }