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