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