|
| 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 | + * @key jfr |
| 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, sizeLeak2, sizeLeak3; |
| 72 | + |
| 73 | + do { |
| 74 | + sizeLeak1 = -1; |
| 75 | + sizeLeak2 = -1; |
| 76 | + sizeLeak3 = -1; |
| 77 | + |
| 78 | + try (Recording recording = new Recording()) { |
| 79 | + leak.clear(); |
| 80 | + recording.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity"); |
| 81 | + recording.start(); |
| 82 | + |
| 83 | + for (int i = 0; i < 1000; i++) { |
| 84 | + switch (rand.nextInt(3)) { |
| 85 | + case 0: leak.add(new Leak1()); break; |
| 86 | + case 1: leak.add(new Leak2()); break; |
| 87 | + case 2: leak.add(new Leak3()); break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + recording.stop(); |
| 92 | + |
| 93 | + List<RecordedEvent> events = Events.fromRecording(recording); |
| 94 | + Events.hasEvents(events); |
| 95 | + for (RecordedEvent e : events) { |
| 96 | + RecordedObject object = e.getValue("object"); |
| 97 | + RecordedClass type = object.getValue("type"); |
| 98 | + long objectSize = e.getLong("objectSize"); |
| 99 | + System.err.println("type = " + type.getName() + ", objectSize = " + e.getLong("objectSize")); |
| 100 | + if (objectSize <= 0) { |
| 101 | + throw new Exception("Object size for " + type.getName() + " is lower or equal to 0"); |
| 102 | + } |
| 103 | + if (type.getName().equals(Leak1.class.getName())) { |
| 104 | + sizeLeak1 = objectSize; |
| 105 | + } |
| 106 | + if (type.getName().equals(Leak2.class.getName())) { |
| 107 | + sizeLeak2 = objectSize; |
| 108 | + } |
| 109 | + if (type.getName().equals(Leak3.class.getName())) { |
| 110 | + sizeLeak3 = objectSize; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } while (sizeLeak1 == -1 || sizeLeak2 == -1 || sizeLeak3 == -1); |
| 115 | + |
| 116 | + if (sizeLeak3 <= sizeLeak2) { |
| 117 | + throw new Exception("Object size for " + Leak3.class.getName() + " is lower or equal to size for" + Leak2.class.getName()); |
| 118 | + } |
| 119 | + if (sizeLeak2 <= sizeLeak1) { |
| 120 | + throw new Exception("Object size for " + Leak2.class.getName() + " is lower or equal to size for" + Leak1.class.getName()); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments