1 /*
  2  * Copyright (c) 2021, Datadog, Inc. 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 import java.util.Random;
 29 
 30 import jdk.jfr.Event;
 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  * @requires vm.flagless
 42  * @requires vm.hasJFR
 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.TestObjectSize
 46  */
 47 public class TestObjectSize {
 48 
 49     private interface Leak {
 50     }
 51     private static class Leak1 implements Leak {
 52         private long field1;
 53     }
 54     private static class Leak2 implements Leak {
 55         private long field1;
 56         private long field2;
 57     }
 58     private static class Leak3 implements Leak {
 59         private long field1;
 60         private long field2;
 61         private long field3;
 62     }
 63 
 64     public static List<Object> leak = new ArrayList<>(OldObjects.MIN_SIZE);
 65 
 66     public static void main(String[] args) throws Exception {
 67         WhiteBox.setWriteAllObjectSamples(true);
 68 
 69         final Random rand = new Random(1L);
 70 
 71         long sizeLeak1 = -1;
 72         long sizeLeak2 = -1;
 73         long sizeLeak3 = -1;
 74 
 75         do {
 76             try (Recording recording = new Recording()) {
 77                 leak.clear();
 78                 recording.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
 79                 recording.start();
 80 
 81                 for (int i = 0; i < 1000; i++) {
 82                     if (sizeLeak1 == -1) {
 83                         leak.add(new Leak1());
 84                         continue;
 85                     }
 86                     if (sizeLeak2 == -1) {
 87                         leak.add(new Leak2());
 88                         continue;
 89                     }
 90                     if (sizeLeak3 == -1) {
 91                         leak.add(new Leak3());
 92                     }
 93                 }
 94 
 95                 recording.stop();
 96 
 97                 List<RecordedEvent> events = Events.fromRecording(recording);
 98                 for (RecordedEvent e : events) {
 99                     RecordedObject object = e.getValue("object");
100                     RecordedClass type = object.getValue("type");
101                     long objectSize = e.getLong("objectSize");
102                     System.err.println("type = " + type.getName() + ", objectSize = " + e.getLong("objectSize"));
103                     if (objectSize <= 0) {
104                         throw new Exception("Object size for " + type.getName() + " is lower or equal to 0");
105                     }
106                     if (type.getName().equals(Leak1.class.getName()) && sizeLeak1 == -1) {
107                         sizeLeak1 = objectSize;
108                     }
109                     if (type.getName().equals(Leak2.class.getName()) && sizeLeak2 == -1) {
110                         sizeLeak2 = objectSize;
111                     }
112                     if (type.getName().equals(Leak3.class.getName()) && sizeLeak3 == -1) {
113                         sizeLeak3 = objectSize;
114                     }
115                 }
116             }
117         } while (sizeLeak1 == -1 || sizeLeak2 == -1 || sizeLeak3 == -1);
118 
119         if (sizeLeak3 <= sizeLeak2) {
120             throw new Exception("Object size for " + Leak3.class.getName() + " is lower or equal to size for" + Leak2.class.getName());
121         }
122         if (sizeLeak2 <= sizeLeak1) {
123             throw new Exception("Object size for " + Leak2.class.getName() + " is lower or equal to size for" + Leak1.class.getName());
124         }
125     }
126 }