1 /*
   2  * Copyright (c) 2013, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.event.compiler;
  27 
  28 import static java.lang.Math.floor;
  29 import static jdk.testlibrary.Asserts.assertGreaterThanOrEqual;
  30 import static jdk.testlibrary.Asserts.assertLessThanOrEqual;
  31 
  32 import java.time.Duration;
  33 
  34 import jdk.jfr.Recording;
  35 import jdk.jfr.consumer.RecordedEvent;
  36 import jdk.testlibrary.jfr.EventNames;
  37 import jdk.testlibrary.jfr.Events;
  38 
  39 /*
  40  * @test
  41  * @summary Test that event is triggered when an object is allocated in a new TLAB.
  42  * @key jfr
  43  * @library /lib/testlibrary
  44  * @run main/othervm -XX:+UseTLAB -XX:TLABSize=100k -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=1 jdk.jfr.event.compiler.TestAllocInNewTLAB
  45  * @run main/othervm -XX:+UseTLAB -XX:TLABSize=100k -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=1 -XX:-FastTLABRefill jdk.jfr.event.compiler.TestAllocInNewTLAB
  46  * @run main/othervm -XX:+UseTLAB -XX:TLABSize=100k -XX:-ResizeTLAB -XX:TLABRefillWasteFraction=1 -Xint jdk.jfr.event.compiler.TestAllocInNewTLAB
  47  */
  48 
  49 /**
  50  * Test that when an object is allocated in a new Thread Local Allocation Buffer (TLAB)
  51  * an event will be triggered. The test is done for C1-compiler,
  52  * C2-compiler (-XX:-FastTLABRefill) and interpreted mode (-Xint).
  53  *
  54  * To force objects to be allocated in a new TLAB:
  55  *      the size of TLAB is set to 100k (-XX:TLABSize=100k);
  56  *      the size of allocated objects is set to 100k minus 16 bytes overhead;
  57  *      max TLAB waste at refill is set to minimum (-XX:TLABRefillWasteFraction=1),
  58  *          to provoke a new TLAB creation.
  59  */
  60 public class TestAllocInNewTLAB {
  61     private final static String EVENT_NAME = EventNames.ObjectAllocationInNewTLAB;
  62 
  63     private static final int BYTE_ARRAY_OVERHEAD = 16; // Extra bytes used by a byte array.
  64     private static final int OBJECT_SIZE  = 100 * 1024;
  65     private static final int OBJECT_SIZE_ALT = OBJECT_SIZE + 8; // Object size in case of disabled CompressedOops
  66     private static final int OBJECTS_TO_ALLOCATE = 100;
  67     private static final String BYTE_ARRAY_CLASS_NAME = new byte[0].getClass().getName();
  68     private static final int INITIAL_TLAB_SIZE = 100 * 1024;
  69 
  70     // make sure allocation isn't dead code eliminated
  71     public static byte[] tmp;
  72 
  73     public static void main(String[] args) throws Exception {
  74         Recording recording = new Recording();
  75         recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
  76 
  77         recording.start();
  78         System.gc();
  79         for (int i = 0; i < OBJECTS_TO_ALLOCATE; ++i) {
  80             tmp = new byte[OBJECT_SIZE - BYTE_ARRAY_OVERHEAD];
  81         }
  82         recording.stop();
  83 
  84         int countAllTlabs = 0;  // Count all matching tlab allocations.
  85         int countFullTlabs = 0; // Count matching tlab allocations with full tlab size.
  86         for (RecordedEvent event : Events.fromRecording(recording)) {
  87             if (!EVENT_NAME.equals(event.getEventType().getName())) {
  88                 continue;
  89             }
  90             System.out.println("Event:" + event);
  91 
  92             long allocationSize = Events.assertField(event, "allocationSize").atLeast(1L).getValue();
  93             long tlabSize = Events.assertField(event, "tlabSize").atLeast(allocationSize).getValue();
  94             String className = Events.assertField(event, "objectClass.name").notEmpty().getValue();
  95 
  96             boolean isMyEvent = Thread.currentThread().getId() == event.getThread().getJavaThreadId()
  97                  && className.equals(BYTE_ARRAY_CLASS_NAME)
  98                  && (allocationSize == OBJECT_SIZE || allocationSize == OBJECT_SIZE_ALT);
  99             if (isMyEvent) {
 100                 countAllTlabs++;
 101                 if (tlabSize == INITIAL_TLAB_SIZE + OBJECT_SIZE || tlabSize == INITIAL_TLAB_SIZE + OBJECT_SIZE_ALT) {
 102                     countFullTlabs++;
 103                 }
 104             }
 105         }
 106 
 107         int minCount = (int) floor(OBJECTS_TO_ALLOCATE * 0.80);
 108         assertGreaterThanOrEqual(countAllTlabs, minCount, "Too few tlab objects allocated");
 109         assertLessThanOrEqual(countAllTlabs, OBJECTS_TO_ALLOCATE, "Too many tlab objects allocated");
 110 
 111         // For most GCs we expect the size of each tlab to be
 112         // INITIAL_TLAB_SIZE + ALLOCATION_SIZE, but that is not always true for G1.
 113         // G1 may use a smaller tlab size if the full tlab does not fit in the
 114         // selected memory region.
 115         //
 116         // For example, if a G1 memory region has room for 4.7 tlabs,
 117         // then the first 4 tlabs will have the expected size,
 118         // but the fifth tlab would only have a size of 0.7*expected.
 119         //
 120         // It is only the last tlab in each region that has a smaller size.
 121         // This means that at least 50% of the allocated tlabs should
 122         // have the expected size (1 full tlab, and 1 fractional tlab).
 123         assertGreaterThanOrEqual(2*countFullTlabs, countAllTlabs, "Too many fractional tlabs.");
 124     }
 125 
 126 }