1 /*
  2  * Copyright (c) 2013, 2026, 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 jdk.jfr.Recording;
 29 import jdk.jfr.consumer.RecordedEvent;
 30 import jdk.test.lib.jfr.EventNames;
 31 import jdk.test.lib.jfr.Events;
 32 import jdk.test.lib.Asserts;
 33 import jdk.test.lib.Platform;
 34 import jdk.test.whitebox.WhiteBox;
 35 
 36 /**
 37  * @test
 38  * @summary Test that when an object is allocated outside a TLAB an event will be triggered.
 39  * @requires vm.flagless
 40  * @requires vm.hasJFR
 41  * @library /test/lib
 42  * @build jdk.test.whitebox.WhiteBox
 43  *
 44  * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 45  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 46  *                   -XX:+UseTLAB -XX:TLABSize=90k -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=256
 47  *                   jdk.jfr.event.allocation.TestObjectAllocationOutsideTLABEvent
 48  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
 49  *                   -XX:+UseTLAB -XX:TLABSize=90k -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=256
 50  *                   -Xint
 51  *                   jdk.jfr.event.allocation.TestObjectAllocationOutsideTLABEvent
 52  */
 53 
 54 /**
 55  * Test that an event is triggered when an object is allocated outside a
 56  * Thread Local Allocation Buffer (TLAB). The test is done for default interpreted mode (-Xint).
 57  *
 58  * To force objects to be allocated outside TLAB:
 59  *      the initial size of TLAB is set to 90k (-XX:TLABSize=90k);
 60  *      the size of allocated objects is set to 128k;
 61  *      max TLAB waste at refill is set to 256 (-XX:TLABRefillWasteFraction=256),
 62  *          to prevent a new TLAB creation.
 63 */
 64 public class TestObjectAllocationOutsideTLABEvent {
 65     private static final String EVENT_NAME = EventNames.ObjectAllocationOutsideTLAB;
 66 
 67     // 64-bit  COH: MW8 +      L4 + End Alignment = 16
 68     // 64-bit -COH: MW8 + K4 + L4                 = 16
 69     // 32-bit     : MW4 + K4 + L4 + End Alignment = 16
 70     private static final Boolean COMPACT_HEADERS = WhiteBox.getWhiteBox().getBooleanVMFlag("UseCompactObjectHeaders");
 71 
 72     private static final int BYTE_ARRAY_OVERHEAD = COMPACT_HEADERS ? 12 : 16;
 73     private static final int OBJECT_SIZE = 128 * 1024;
 74 
 75     private static final int OBJECTS_TO_ALLOCATE = 100;
 76     private static final String BYTE_ARRAY_CLASS_NAME = new byte[0].getClass().getName();
 77     private static int eventCount;
 78 
 79     // Make sure allocation isn't dead code eliminated.
 80     public static byte[] tmp;
 81 
 82     public static void main(String[] args) throws Exception {
 83         Recording recording = new Recording();
 84         recording.enable(EVENT_NAME);
 85         recording.start();
 86         allocate();
 87         recording.stop();
 88         verifyRecording(recording);
 89         int minCount = (int) floor(OBJECTS_TO_ALLOCATE * 0.80);
 90         Asserts.assertGreaterThanOrEqual(eventCount, minCount, "Too few objects allocated");
 91         Asserts.assertLessThanOrEqual(eventCount, OBJECTS_TO_ALLOCATE, "Too many objects allocated");
 92     }
 93 
 94     private static void allocate() {
 95         for (int i = 0; i < OBJECTS_TO_ALLOCATE; ++i) {
 96             tmp = new byte[OBJECT_SIZE - BYTE_ARRAY_OVERHEAD];
 97         }
 98     }
 99 
100     private static void verifyRecording(Recording recording) throws Exception {
101         for (RecordedEvent event : Events.fromRecording(recording)) {
102             verify(event);
103         }
104     }
105 
106     private static void verify(RecordedEvent event) {
107         if (Thread.currentThread().getId() != event.getThread().getJavaThreadId()) {
108             return;
109         }
110         long allocationSize = Events.assertField(event, "allocationSize").atLeast(1L).getValue();
111         String className = Events.assertField(event, "objectClass.name").notEmpty().getValue();
112         if (className.equals(BYTE_ARRAY_CLASS_NAME) && (allocationSize == OBJECT_SIZE)) {
113             ++eventCount;
114         }
115     }
116 }