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.util.ArrayList;
  29 import java.util.List;
  30 
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.consumer.RecordedClass;
  33 import jdk.jfr.consumer.RecordedEvent;
  34 import jdk.jfr.consumer.RecordedObject;
  35 import jdk.jfr.internal.test.WhiteBox;
  36 import jdk.testlibrary.jfr.EventNames;
  37 import jdk.testlibrary.jfr.Events;
  38 import jdk.testlibrary.jfr.OldObjects;
  39 
  40 /*
  41  * @test
  42  * @key jfr
  43  * @requires vm.gc == "null"
  44  * @library /lib/testlibrary
  45  * @modules jdk.jfr/jdk.jfr.internal.test
  46  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestArrayInformation
  47  */
  48 public class TestArrayInformation {
  49 
  50     private static class ArrayLeak {
  51     }
  52 
  53     private static final int CHAIN_DEPTH = 50;
  54     private static final int ARRAY_SIZE = 52;
  55     private static final int ARRAY_INDEX = 26;
  56 
  57     public static List<Object> leak = new ArrayList<>(25);
  58 
  59     public static void main(String[] args) throws Exception {
  60         WhiteBox.setWriteAllObjectSamples(true);
  61 
  62         try (Recording recording = new Recording()) {
  63             recording.enable(EventNames.OldObjectSample).withoutStackTrace().with("cutoff", "infinity");
  64             recording.start();
  65             for(int i = 0; i < 25; i++) {
  66               leak.add( buildNestedArray(CHAIN_DEPTH));
  67             }
  68             recording.stop();
  69             List<RecordedEvent> events = Events.fromRecording(recording);
  70             Events.hasEvents(events);
  71             verifyObjectArray(events);
  72         }
  73     }
  74 
  75     private static void verifyObjectArray(List<RecordedEvent> events) throws Exception {
  76         for (RecordedEvent e : events) {
  77             RecordedObject object = e.getValue("object");
  78             RecordedClass objectType = object.getValue("type");
  79             RecordedObject referrer = object.getValue("referrer");
  80             System.out.println(objectType.getName());
  81             if (objectType.getName().equals(ArrayLeak[].class.getName())) {
  82                 for (int i = 0; i < CHAIN_DEPTH; i++) {
  83                     object = referrer.getValue("object");
  84                     if (object == null) {
  85                         throw new Exception("Expected referrer object");
  86                     }
  87                     objectType = object.getValue("type");
  88                     if (!objectType.getName().equals(Object[].class.getName())) {
  89                         throw new Exception("Expect array class to be named " + Object[].class + " but found " + objectType.getName());
  90                     }
  91                     RecordedObject field = referrer.getValue("field");
  92                     if (field != null) {
  93                         throw new Exception("Didn't expect to find field");
  94                     }
  95                     RecordedObject array = referrer.getValue("array");
  96                     if (array == null) {
  97                         throw new Exception("Expected array object, but got null");
  98                     }
  99                     int index = referrer.getValue("array.index");
 100                     if (index != ARRAY_INDEX) {
 101                         throw new Exception("Expected array index: " + ARRAY_INDEX + ", but got " + index);
 102                     }
 103                     int size = referrer.getValue("array.size");
 104                     if (size != ARRAY_SIZE) {
 105                         throw new Exception("Expected array size: " + ARRAY_SIZE + ", but got " + size);
 106                     }
 107                     referrer = object.getValue("referrer");
 108                 }
 109                 return;
 110             }
 111         }
 112         throw new Exception("Could not find event with " + ArrayLeak[].class + " as (leak) object");
 113     }
 114 
 115     private static Object buildNestedArray(int depth) {
 116         if (depth > 0) {
 117             Object[] array = new Object[ARRAY_SIZE];
 118             array[ARRAY_INDEX] = buildNestedArray(depth - 1);
 119             return array;
 120         } else {
 121             return new ArrayLeak[OldObjects.MIN_SIZE];
 122         }
 123     }
 124 
 125 }