1 /*
 2  * Copyright (c) 2015, 2025, 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.
 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 package jdk.jfr.event.oldobject;
24 
25 import java.util.List;
26 
27 import jdk.jfr.Recording;
28 import jdk.jfr.consumer.RecordedEvent;
29 import jdk.jfr.internal.test.WhiteBox;
30 import jdk.test.lib.jfr.EventNames;
31 import jdk.test.lib.jfr.Events;
32 
33 /**
34  * @test
35  * @requires vm.flagless
36  * @requires vm.hasJFR

37  * @library /test/lib /test/jdk
38  * @modules jdk.jfr/jdk.jfr.internal.test
39  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestReferenceChainLimit
40  */
41 public class TestReferenceChainLimit {
42     private final static int TEST_CHAIN_LENGTH_FACTOR = 10; // scaling factor for the how long chain to be used in the test
43 
44     final static class ChainNode {
45         final ChainNode next;
46         byte[] leak;
47 
48         public ChainNode(ChainNode node) {
49             next = node;
50             leak = new byte[10_000];
51         }
52     }
53 
54     private static ChainNode createChain() {
55         ChainNode node = null;
56         final int testChainLength = OldObjects.MAX_CHAIN_LENGTH * TEST_CHAIN_LENGTH_FACTOR;
57         for (int i = 0; i < testChainLength; i++) {
58             node = new ChainNode(node);
59         }
60         return node;
61     }
62 
63     public static ChainNode leak;
64 
65     public static void main(String[] args) throws Exception {
66         WhiteBox.setWriteAllObjectSamples(true);
67 
68         try (Recording r = new Recording()) {
69             r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
70             r.start();
71             leak = createChain();
72             List<RecordedEvent> events = Events.fromRecording(r);
73             if (OldObjects.countMatchingEvents(events, byte[].class, null, null, -1, "createChain") == 0) {
74                 throw new Exception("Could not find ChainNode");
75             }
76             for (RecordedEvent e : events) {
77                 OldObjects.validateReferenceChainLimit(e, OldObjects.MAX_CHAIN_LENGTH);
78             }
79         }
80     }
81 }
--- EOF ---