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