1 /*
 2  * Copyright (c) 2018, 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 
24 package jdk.jfr.event.oldobject;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 import jdk.jfr.Recording;
30 import jdk.jfr.consumer.RecordedEvent;
31 import jdk.test.lib.jfr.EventNames;
32 import jdk.test.lib.jfr.Events;
33 
34 /**
35  * @test
36  * @requires vm.hasJFR
37  * @requires vm.flagless
38  * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
39  * @library /test/lib /test/jdk
40  * @summary Purpose of this test is to run leak profiler without command line tweaks or WhiteBox hacks until we succeed
41  * @run main/othervm -Xmx1G jdk.jfr.event.oldobject.TestSanityDefault
42  */
43 public class TestSanityDefault {
44 
45     static private class FindMe {
46     }
47 
48     public static List<FindMe> list = new ArrayList<>(OldObjects.MIN_SIZE);
49 
50     public static void main(String[] args) throws Exception {
51         // Should not use WhiteBox API, we want to execute actual code paths
52 
53         // Trigger c2 compilation, so we get sample
54         for (long i = 0; i < 100_000_000; i++) {
55             allocateFindMe(true);
56         }
57 
58 
59         // It's hard to get samples with interpreter / C1 so loop until we do
60         while (true) {
61             try (Recording r = new Recording()) {
62                 r.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "infinity");
63                 r.start();
64                 allocateFindMe(false);
65                 System.gc();
66                 r.stop();
67                 List<RecordedEvent> events = Events.fromRecording(r);
68                 if (OldObjects.countMatchingEvents(events, FindMe.class, null, null, -1, "allocateFindMe") > 0) {
69                     return;
70                 }
71                 // no events found, retry
72             }
73         }
74     }
75 
76     public static void allocateFindMe(boolean doNothing) {
77         if (doNothing) {
78             return;
79         }
80         for (int i = 0; i < OldObjects.MIN_SIZE; i++) {
81             // Purposely don't allocate array, so we at least
82             // in one old-object test check an ordinary object.
83             list.add(new FindMe());
84         }
85     }
86 }