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