1 /*
2  * Copyright (c) 2013, 2020, 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 
34 /**
35  * @test
36  * @summary Test that event is triggered when an object is allocated in a new TLAB.
37  * @key jfr
38  * @requires vm.hasJFR
39  * @library /test/lib
40  * @run main/othervm -XX:+UseTLAB -XX:TLABSize=100k -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=1 jdk.jfr.event.allocation.TestObjectAllocationInNewTLABEvent
41  * @run main/othervm -XX:+UseTLAB -XX:TLABSize=100k -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=1 -Xint jdk.jfr.event.allocation.TestObjectAllocationInNewTLABEvent
42  */
43 
44 /**
45  * Test that when an object is allocated in a new Thread Local Allocation Buffer (TLAB)
46  * an event will be triggered. The test is done for default and interpreted mode (-Xint).
47  *
48  * To force objects to be allocated in a new TLAB:
49  *      the size of TLAB is set to 100k (-XX:TLABSize=100k);
50  *      the size of allocated objects is set to 100k minus 16 bytes overhead;
51  *      max TLAB waste at refill is set to minimum (-XX:TLABRefillWasteFraction=1),
52  *          to provoke a new TLAB creation.
53  */
54 public class TestObjectAllocationInNewTLABEvent {
55     private final static String EVENT_NAME = EventNames.ObjectAllocationInNewTLAB;
56 
57     private static final int BYTE_ARRAY_OVERHEAD = 16; // Extra bytes used by a byte array.
58     private static final int OBJECT_SIZE  = 100 * 1024;
59     private static final int OBJECT_SIZE_ALT = OBJECT_SIZE + 8; // Object size in case of disabled CompressedOops.
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 final int INITIAL_TLAB_SIZE = 100 * 1024;
63     private static int countAllTlabs; // Count all matching tlab allocations.
64     private static int countFullTlabs; // Count matching tlab allocations with full tlab size.
65 
66     // Make sure allocation isn't dead code eliminated.
67     public static byte[] tmp;
68 
main(String[] args)69     public static void main(String[] args) throws Exception {
70         Recording recording = new Recording();
71         recording.enable(EVENT_NAME);
72         recording.start();
73         System.gc();
74         allocate();
75         recording.stop();
76         verifyRecording(recording);
77         int minCount = (int) floor(OBJECTS_TO_ALLOCATE * 0.80);
78         Asserts.assertGreaterThanOrEqual(countAllTlabs, minCount, "Too few tlab objects allocated");
79         Asserts.assertLessThanOrEqual(countAllTlabs, OBJECTS_TO_ALLOCATE, "Too many tlab objects allocated");
80 
81         // For most GCs we expect the size of each tlab to be
82         // INITIAL_TLAB_SIZE + ALLOCATION_SIZE, but that is not always true for G1.
83         // G1 may use a smaller tlab size if the full tlab does not fit in the
84         // selected memory region.
85         //
86         // For example, if a G1 memory region has room for 4.7 tlabs,
87         // then the first 4 tlabs will have the expected size,
88         // but the fifth tlab would only have a size of 0.7*expected.
89         //
90         // It is only the last tlab in each region that has a smaller size.
91         // This means that at least 50% of the allocated tlabs should
92         // have the expected size (1 full tlab, and 1 fractional tlab).
93         Asserts.assertGreaterThanOrEqual(2*countFullTlabs, countAllTlabs, "Too many fractional tlabs.");
94     }
95 
allocate()96     private static void allocate() {
97         for (int i = 0; i < OBJECTS_TO_ALLOCATE; ++i) {
98             tmp = new byte[OBJECT_SIZE - BYTE_ARRAY_OVERHEAD];
99         }
100     }
101 
verifyRecording(Recording recording)102     private static void verifyRecording(Recording recording) throws Exception {
103         for (RecordedEvent event : Events.fromRecording(recording)) {
104             verify(event);
105         }
106     }
107 
verify(RecordedEvent event)108     private static void verify(RecordedEvent event) {
109         if (Thread.currentThread().getId() != event.getThread().getJavaThreadId()) {
110             return;
111         }
112         long allocationSize = Events.assertField(event, "allocationSize").atLeast(1L).getValue();
113         long tlabSize = Events.assertField(event, "tlabSize").atLeast(allocationSize).getValue();
114         String className = Events.assertField(event, "objectClass.name").notEmpty().getValue();
115         if (className.equals(BYTE_ARRAY_CLASS_NAME) && (allocationSize == OBJECT_SIZE || allocationSize == OBJECT_SIZE_ALT)) {
116             countAllTlabs++;
117             if (tlabSize == INITIAL_TLAB_SIZE + OBJECT_SIZE || tlabSize == INITIAL_TLAB_SIZE + OBJECT_SIZE_ALT) {
118                 countFullTlabs++;
119             }
120         }
121     }
122 }
123