1 /*
   2  * Copyright (c) 2014, 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 sun.hotspot.code;
  25 
  26 import java.lang.management.ManagementFactory;
  27 import java.lang.management.MemoryPoolMXBean;
  28 import java.util.EnumSet;
  29 
  30 import sun.hotspot.WhiteBox;
  31 
  32 public enum BlobType {
  33     // Execution level 1 and 4 (non-profiled) nmethods (including native nmethods)
  34     MethodNonProfiled(0, "CodeHeap 'non-profiled nmethods'", "NonProfiledCodeHeapSize") {
  35         @Override
  36         public boolean allowTypeWhenOverflow(BlobType type) {
  37             return super.allowTypeWhenOverflow(type)
  38                     || type == BlobType.MethodProfiled;
  39         }
  40     },
  41     // Execution level 2 and 3 (profiled) nmethods
  42     MethodProfiled(1, "CodeHeap 'profiled nmethods'", "ProfiledCodeHeapSize") {
  43         @Override
  44         public boolean allowTypeWhenOverflow(BlobType type) {
  45             return super.allowTypeWhenOverflow(type)
  46                     || type == BlobType.MethodNonProfiled;
  47         }
  48     },
  49     // Non-nmethods like Buffers, Adapters and Runtime Stubs
  50     NonNMethod(2, "CodeHeap 'non-nmethods'", "NonNMethodCodeHeapSize") {
  51         @Override
  52         public boolean allowTypeWhenOverflow(BlobType type) {
  53             return super.allowTypeWhenOverflow(type)
  54                     || type == BlobType.MethodNonProfiled
  55                     || type == BlobType.MethodProfiled;
  56         }
  57     },
  58     // All types (No code cache segmentation)
  59     All(3, "CodeCache", "ReservedCodeCacheSize");
  60 
  61     public final int id;
  62     public final String sizeOptionName;
  63     public final String beanName;
  64 
  65     private BlobType(int id, String beanName, String sizeOptionName) {
  66         this.id = id;
  67         this.beanName = beanName;
  68         this.sizeOptionName = sizeOptionName;
  69     }
  70 
  71     public MemoryPoolMXBean getMemoryPool() {
  72         for (MemoryPoolMXBean bean : ManagementFactory.getMemoryPoolMXBeans()) {
  73             String name = bean.getName();
  74             if (beanName.equals(name)) {
  75                 return bean;
  76             }
  77         }
  78         return null;
  79     }
  80 
  81     public boolean allowTypeWhenOverflow(BlobType type) {
  82         return type == this;
  83     }
  84 
  85     public static EnumSet<BlobType> getAvailable() {
  86         WhiteBox whiteBox = WhiteBox.getWhiteBox();
  87         if (!whiteBox.getBooleanVMFlag("SegmentedCodeCache")) {
  88             // only All for non segmented world
  89             return EnumSet.of(All);
  90         }
  91         if (System.getProperty("java.vm.info").startsWith("interpreted ")) {
  92             // only NonNMethod for -Xint
  93             return EnumSet.of(NonNMethod);
  94         }
  95 
  96         EnumSet<BlobType> result = EnumSet.complementOf(EnumSet.of(All));
  97         if (!whiteBox.getBooleanVMFlag("TieredCompilation")
  98                 || whiteBox.getIntxVMFlag("TieredStopAtLevel") <= 1) {
  99             // there is no MethodProfiled in non tiered world or pure C1
 100             result.remove(MethodProfiled);
 101         }
 102         return result;
 103     }
 104 
 105     public long getSize() {
 106         return WhiteBox.getWhiteBox().getUintxVMFlag(sizeOptionName);
 107     }
 108 }