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