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
24 package jdk.jfr.startupargs;
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.jfr.internal.test.WhiteBox;
32 import jdk.test.lib.jfr.EventNames;
33 import jdk.test.lib.jfr.Events;
34
35 /**
36 * @test
37 * @requires vm.flagless
38 * @requires vm.hasJFR
39 * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
40 * @summary Test -XX:FlightRecorderOptions:old-object-queue-size
41 * @modules jdk.jfr/jdk.jfr.internal.test
42 * @library /test/lib
43 *
44 * @run main/othervm -XX:TLABSize=2k -XX:FlightRecorderOptions:old-object-queue-size=0 jdk.jfr.startupargs.TestOldObjectQueueSize off
45 * @run main/othervm -XX:TLABSize=2k -Xlog:gc+tlab=trace -XX:FlightRecorderOptions:old-object-queue-size=10000 jdk.jfr.startupargs.TestOldObjectQueueSize many
46 * @run main/othervm -XX:TLABSize=2k -Xlog:gc+tlab=trace -XX:FlightRecorderOptions:old-object-queue-size=1000000 jdk.jfr.startupargs.TestOldObjectQueueSize many
47 */
48 public class TestOldObjectQueueSize {
49
50 private static final int OBJECT_COUNT = 10_000;
51 private static final int DEFAULT_OLD_OBJECT_QUEUE_SIZE = 256;
52
53 public final static List<Object> leak = new ArrayList<>(OBJECT_COUNT);
54
55 public static void main(String[] args) throws Exception {
56 WhiteBox.setWriteAllObjectSamples(true);
57 System.out.println("TESTING: " + args[0]);
58
59 String amount = args[0];
60 try (Recording recording = new Recording()) {
61 recording.enable(EventNames.OldObjectSample).withoutStackTrace().with("cutoff", "infinity");
62 recording.start();
63 // It's hard to get a larger number of samples without running into OOM
64 // since the TLAB size increases when there is more allocation.
65 // Allocate 200 MB, which should give at least 400 samples if the TLAB
66 // size manages to stay below 512kb, which logging with -Xlog:gc+tlab=trace indicates.
67 for (int i = 0; i < OBJECT_COUNT; i++) {
68 leak.add(new byte[20_000]);
69 }
70 recording.stop();
71 List<RecordedEvent> events = Events.fromRecording(recording);
72 System.out.println("Found samples: " + events.size());
73 if (amount.equals("off")) {
74 if (!events.isEmpty()) {
75 throw new Exception("No objects shuld be emitted when queue size 0");
76 }
77 }
78 if (amount.equals("many")) {
79 // Sanity check that we at least have managed to set a value above the default
80 if (events.size() <= DEFAULT_OLD_OBJECT_QUEUE_SIZE) {
81 throw new Exception("Should be at least be " + DEFAULT_OLD_OBJECT_QUEUE_SIZE + ", but found " + events.size());
82 }
83 }
84 }
85 }
86 }