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