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 jfr.event.compiler;
  27 
  28 import java.util.List;
  29 
  30 import jdk.jfr.Recording;
  31 import jdk.jfr.consumer.RecordedEvent;
  32 import com.oracle.java.testlibrary.Asserts;
  33 import com.oracle.java.testlibrary.jfr.EventNames;
  34 import com.oracle.java.testlibrary.jfr.Events;
  35 import sun.hotspot.WhiteBox;
  36 import sun.hotspot.code.BlobType;
  37 
  38 /*
  39  * @test TestCodeCacheFull
  40  *
  41  * @library /testlibrary /testlibrary/whitebox
  42  * @modules jdk.jfr
  43  *          jdk.management.jfr
  44  * @build sun.hotspot.WhiteBox
  45  * @run main ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  46  *
  47  * @run main/othervm -Xbootclasspath/a:.
  48  *     -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  49  *     -XX:-UseLargePages -XX:+EnableJFR jfr.event.compiler.TestCodeCacheFull
  50  */
  51 public class TestCodeCacheFull {
  52 
  53     public static void main(String[] args) throws Exception {
  54         testWithBlobType(BlobType.All, BlobType.All.getSize());
  55     }
  56 
  57     private static void testWithBlobType(BlobType btype, long availableSize) throws Exception {
  58         Recording r = new Recording();
  59         r.enable(EventNames.CodeCacheFull);
  60         r.start();
  61         WhiteBox.getWhiteBox().allocateCodeBlob(availableSize, btype.id);
  62         r.stop();
  63 
  64         List<RecordedEvent> events = Events.fromRecording(r);
  65         Events.hasEvents(events);
  66         RecordedEvent event = events.get(0);
  67 
  68         String codeBlobType = Events.assertField(event, "codeBlobType").notNull().getValue();
  69         BlobType blobType = BlobType.All;
  70         Asserts.assertTrue(blobType.allowTypeWhenOverflow(blobType), "Unexpected overflow BlobType " + blobType.id);
  71         Events.assertField(event, "entryCount").atLeast(0);
  72         Events.assertField(event, "methodCount").atLeast(0);
  73         Events.assertEventThread(event);
  74         Events.assertField(event, "fullCount").atLeast(0);
  75         Events.assertField(event, "startAddress").notEqual(0L);
  76         Events.assertField(event, "commitedTopAddress").notEqual(0L);
  77         Events.assertField(event, "reservedTopAddress").notEqual(0L);
  78     }
  79 
  80     private static BlobType blobTypeFromName(String codeBlobTypeName) throws Exception {
  81         for (BlobType t : BlobType.getAvailable()) {
  82             if (t.beanName.equals(codeBlobTypeName)) {
  83                 return t;
  84             }
  85         }
  86         throw new Exception("Unexpected event " + codeBlobTypeName);
  87     }
  88 
  89     // Compute the available size for this BlobType by taking into account
  90     // that it may be stored in a different code heap in case it does not fit
  91     // into the current one.
  92     private static long calculateAvailableSize(BlobType btype) {
  93         long availableSize = btype.getSize();
  94         for (BlobType alternative : BlobType.getAvailable()) {
  95             if (btype.allowTypeWhenOverflow(alternative)) {
  96                 availableSize = Math.max(availableSize, alternative.getSize());
  97             }
  98         }
  99         return availableSize;
 100     }
 101 }