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.ArrayList;
 26 import java.util.HashMap;
 27 import java.util.List;
 28 import java.util.Random;
 29 import java.util.Vector;
 30 import java.util.concurrent.CountDownLatch;
 31 import java.util.concurrent.atomic.AtomicInteger;
 32 
 33 import jdk.jfr.Recording;
 34 import jdk.jfr.consumer.RecordedClass;
 35 import jdk.jfr.consumer.RecordedEvent;
 36 import jdk.jfr.consumer.RecordedFrame;
 37 import jdk.jfr.consumer.RecordedMethod;
 38 import jdk.jfr.consumer.RecordedObject;
 39 import jdk.jfr.consumer.RecordedStackTrace;
 40 import jdk.jfr.internal.test.WhiteBox;
 41 import jdk.test.lib.jfr.EventNames;
 42 import jdk.test.lib.jfr.Events;
 43 
 44 /**
 45  * @test
 46  * @requires vm.flagless
 47  * @requires vm.hasJFR
 48  * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
 49  * @library /test/lib /test/jdk
 50  * @modules jdk.jfr/jdk.jfr.internal.test
 51  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestLargeRootSet
 52  */
 53 public class TestLargeRootSet {
 54     static class Node {
 55         Node left;
 56         Node right;
 57         Object value;
 58     }
 59 
 60     static class Leak {
 61         // Leaking object has to be of some size,
 62         // otherwise Node object wins most of the
 63         // slots in the object queue.
 64         // In a normal application, objects would
 65         // be of various size and allocated over a
 66         // longer period of time. This would create
 67         // randomness not present in the test.
 68         public long value1;
 69         public Object value2;
 70         float value3;
 71         int value4;
 72         double value5;
 73     }
 74 
 75     public static void main(String[] args) throws Exception {
 76         WhiteBox.setWriteAllObjectSamples(true);
 77         WhiteBox.setSkipBFS(true);
 78         HashMap<Object, Node> leaks = new HashMap<>();
 79         try (Recording r = new Recording()) {
 80             r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
 81             r.start();
 82             for (int i = 0; i < 1_000_000; i++) {
 83                 Node node = new Node();
 84                 node.left = new Node();
 85                 node.right = new Node();
 86                 node.right.value = new Leak();
 87                 leaks.put(i, node);
 88             }
 89             r.stop();
 90             List<RecordedEvent> events = Events.fromRecording(r);
 91             Events.hasEvents(events);
 92             for (RecordedEvent e : events) {
 93                 RecordedClass type = e.getValue("object.type");
 94                 if (type.getName().equals(Leak.class.getName())) {
 95                     return;
 96                 }
 97             }
 98         }
 99     }
100 }