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