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.util.ArrayList; 29 import java.util.List; 30 31 import jdk.jfr.Event; 32 import jdk.jfr.Recording; 33 import jdk.jfr.consumer.RecordedClass; 34 import jdk.jfr.consumer.RecordedEvent; 35 import jdk.jfr.consumer.RecordedObject; 36 import jdk.jfr.internal.test.WhiteBox; 37 import jdk.test.lib.jfr.EventNames; 38 import jdk.test.lib.jfr.Events; 39 40 /* 41 * @test 42 * @key jfr 43 * @requires vm.gc == "null" 44 * @library /test/lib /test/jdk 45 * @modules jdk.jfr/jdk.jfr.internal.test 46 * @run main/othervm -XX:TLABSize=2k -XX:-FastTLABRefill jdk.jfr.event.oldobject.TestAllocationTime 47 */ 48 public class TestAllocationTime { 49 50 private static class BeforeLeakEvent extends Event { 51 } 52 private static class AfterLeakEvent extends Event { 53 } 54 private static class Leak { 55 } 56 57 public static List<Leak[]> leak = new ArrayList<>(OldObjects.MIN_SIZE); 58 59 public static void main(String[] args) throws Exception { 60 WhiteBox.setWriteAllObjectSamples(true); 61 62 while(true) { 63 try (Recording recording = new Recording()) { 64 leak.clear(); 65 recording.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "1 h"); 66 recording.start(); 67 68 BeforeLeakEvent be = new BeforeLeakEvent(); 69 be.commit(); 70 71 // Allocate array to trigger sampling code path for interpreter / c1 72 for (int i = 0; i < OldObjects.MIN_SIZE; i++) { 73 leak.add(new Leak[0]); 74 } 75 76 AfterLeakEvent ae = new AfterLeakEvent(); 77 ae.commit(); 78 79 recording.stop(); 80 81 List<RecordedEvent> events = Events.fromRecording(recording); 82 Events.hasEvents(events); 83 RecordedObject sample = findLeak(events, BeforeLeakEvent.class.getName()); 84 if (sample != null) { 85 long beforeTime = find(events, BeforeLeakEvent.class.getName()).getValue("startTime"); 86 long allocationTime = sample.getValue("allocationTime"); 87 long afterTime = find(events, AfterLeakEvent.class.getName()).getValue("startTime"); 88 System.out.println("Before time : " + beforeTime); 89 System.out.println("Allocation time : " + allocationTime); 90 System.out.println("After time : " + afterTime); 91 92 if (allocationTime < beforeTime) { 93 throw new Exception("Allocation should not happen this early"); 94 } 95 if (allocationTime > afterTime) { 96 throw new Exception("Allocation should not happen this late"); 97 } 98 return; // sample ok 99 } 100 } 101 } 102 } 103 104 private static RecordedObject findLeak(List<RecordedEvent> events, String name) throws Exception { 105 for (RecordedEvent e : events) { 106 if (e.getEventType().getName().equals(EventNames.OldObjectSample)) { 107 RecordedObject object = e.getValue("object"); 108 RecordedClass rc = object.getValue("type"); 109 if (rc.getName().equals(Leak[].class.getName())) { 110 return e; 111 } 112 } 113 } 114 return null; 115 } 116 117 private static RecordedEvent find(List<RecordedEvent> events, String name) throws Exception { 118 for (RecordedEvent e : events) { 119 if (e.getEventType().getName().equals(name)) { 120 return e; 121 } 122 } 123 throw new Exception("Could not find event with name " + name); 124 } 125 }