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