1 /*
2 * Copyright (c) 2020, 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.allocation;
25
26 import static java.lang.Math.floor;
27
28 import java.util.List;
29
30 import jdk.jfr.Recording;
31 import jdk.jfr.consumer.RecordedEvent;
32 import jdk.test.lib.jfr.EventNames;
33 import jdk.test.lib.jfr.Events;
34 import jdk.test.lib.Asserts;
35 import jdk.test.lib.Platform;
36 import jdk.test.whitebox.WhiteBox;
37
38 /**
39 * @test
40 * @summary Test that when an object is allocated outside a TLAB an event will be triggered.
41 * @requires vm.flagless
42 * @requires vm.hasJFR
43 * @library /test/lib
44 * @build jdk.test.whitebox.WhiteBox
45 *
46 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
47 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
48 * -XX:+UseTLAB -XX:TLABSize=2k -XX:-ResizeTLAB
49 * jdk.jfr.event.allocation.TestObjectAllocationSampleEventThrottling
50 */
51
52 public class TestObjectAllocationSampleEventThrottling {
53 private static final String EVENT_NAME = EventNames.ObjectAllocationSample;
54
55 private static final Boolean COMPRESSED_CLASS_PTRS = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompressedClassPointers");
56
57 private static final int BYTE_ARRAY_OVERHEAD = (Platform.is64bit() && !COMPRESSED_CLASS_PTRS) ? 24 : 16;
58 private static final int OBJECT_SIZE = 128 * 1024;
59
60 private static final int OBJECTS_TO_ALLOCATE = 100;
61 private static final String BYTE_ARRAY_CLASS_NAME = new byte[0].getClass().getName();
62 private static int eventCount;
63
64 // Make sure allocation isn't dead code eliminated.
65 public static byte[] tmp;
66
67 public static void main(String[] args) throws Exception {
68 testZeroPerSecond();
69 testThrottleSettings();
70 }
71
72 private static void testZeroPerSecond() throws Exception {
73 Recording r1 = new Recording();
74 setThrottle(r1, "0/s");
75 r1.start();
76 allocate();
77 r1.stop();
78 List<RecordedEvent> events = Events.fromRecording(r1);
79 Asserts.assertTrue(events.isEmpty(), "throttle rate 0/s should not emit any events");
80 }
81
82 private static void testThrottleSettings() throws Exception {
83 Recording r1 = new Recording();
84 // 0/s will not emit any events
85 setThrottle(r1, "0/s");
86 r1.start();
87 Recording r2 = new Recording();
88 // 1/ns is a *very* high emit rate, it should trump the previous 0/s value
89 // to allow the allocation sample events to be recorded.
90 setThrottle(r2, "1/ns");
91 r2.start();
92 allocate();
93 r2.stop();
94 r1.stop();
95 verifyRecording(r2);
96 int minCount = (int) floor(OBJECTS_TO_ALLOCATE * 0.80);
97 Asserts.assertGreaterThanOrEqual(eventCount, minCount, "Too few object samples allocated");
98 List<RecordedEvent> events = Events.fromRecording(r1);
99 Asserts.assertFalse(events.isEmpty(), "r1 should also have events");
100 }
101
102 private static void setThrottle(Recording recording, String rate) {
103 recording.enable(EVENT_NAME).with("throttle", rate);
104 }
105
106 private static void allocate() {
107 for (int i = 0; i < OBJECTS_TO_ALLOCATE; ++i) {
108 tmp = new byte[OBJECT_SIZE - BYTE_ARRAY_OVERHEAD];
109 }
110 }
111
112 private static void verifyRecording(Recording recording) throws Exception {
113 for (RecordedEvent event : Events.fromRecording(recording)) {
114 verify(event);
115 }
116 }
117
118 private static void verify(RecordedEvent event) {
119 if (Thread.currentThread().getId() != event.getThread().getJavaThreadId()) {
120 return;
121 }
122 if (Events.assertField(event, "objectClass.name").notEmpty().getValue().equals(BYTE_ARRAY_CLASS_NAME)) {
123 Events.assertField(event, "weight").atLeast(1L);
124 ++eventCount;
125 }
126 }
127 }