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 private static final Boolean COMPACT_HEADERS = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompactObjectHeaders");
57
58 private static final int BYTE_ARRAY_OVERHEAD = COMPACT_HEADERS ? 12 : ((Platform.is64bit() && !COMPRESSED_CLASS_PTRS) ? 24 : 16);
59 private static final int OBJECT_SIZE = 128 * 1024;
60
61 private static final int OBJECTS_TO_ALLOCATE = 100;
62 private static final String BYTE_ARRAY_CLASS_NAME = new byte[0].getClass().getName();
63 private static int eventCount;
64
65 // Make sure allocation isn't dead code eliminated.
66 public static byte[] tmp;
67
68 public static void main(String[] args) throws Exception {
69 testZeroPerSecond();
70 testThrottleSettings();
71 }
72
73 private static void testZeroPerSecond() throws Exception {
74 Recording r1 = new Recording();
75 setThrottle(r1, "0/s");
76 r1.start();
77 allocate();
78 r1.stop();
79 List<RecordedEvent> events = Events.fromRecording(r1);
80 Asserts.assertTrue(events.isEmpty(), "throttle rate 0/s should not emit any events");
81 }
82
83 private static void testThrottleSettings() throws Exception {
84 Recording r1 = new Recording();
85 // 0/s will not emit any events
86 setThrottle(r1, "0/s");
87 r1.start();
88 Recording r2 = new Recording();
89 // 1/ns is a *very* high emit rate, it should trump the previous 0/s value
90 // to allow the allocation sample events to be recorded.
91 setThrottle(r2, "1/ns");
92 r2.start();
93 allocate();
94 r2.stop();
95 r1.stop();
96 verifyRecording(r2);
97 int minCount = (int) floor(OBJECTS_TO_ALLOCATE * 0.80);
98 Asserts.assertGreaterThanOrEqual(eventCount, minCount, "Too few object samples allocated");
99 List<RecordedEvent> events = Events.fromRecording(r1);
100 Asserts.assertFalse(events.isEmpty(), "r1 should also have events");
101 }
102
103 private static void setThrottle(Recording recording, String rate) {
104 recording.enable(EVENT_NAME).with("throttle", rate);
105 }
106
107 private static void allocate() {
108 for (int i = 0; i < OBJECTS_TO_ALLOCATE; ++i) {
109 tmp = new byte[OBJECT_SIZE - BYTE_ARRAY_OVERHEAD];
110 }
111 }
112
113 private static void verifyRecording(Recording recording) throws Exception {
114 for (RecordedEvent event : Events.fromRecording(recording)) {
115 verify(event);
116 }
117 }
118
119 private static void verify(RecordedEvent event) {
120 if (Thread.currentThread().getId() != event.getThread().getJavaThreadId()) {
121 return;
122 }
123 if (Events.assertField(event, "objectClass.name").notEmpty().getValue().equals(BYTE_ARRAY_CLASS_NAME)) {
124 Events.assertField(event, "weight").atLeast(1L);
125 ++eventCount;
126 }
127 }
128 }