1 /*
   2  * Copyright (c) 2003, 2015, 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 sun.management;
  27 
  28 import java.util.regex.*;
  29 import java.util.List;
  30 import java.util.ListIterator;
  31 import java.util.Iterator;
  32 import java.util.ArrayList;
  33 import java.util.Map;
  34 import java.util.TreeMap;
  35 import sun.management.counter.*;
  36 
  37 /**
  38  * Implementation class of HotspotCompilationMBean interface.
  39  *
  40  * Internal, uncommitted management interface for Hotspot compilation
  41  * system.
  42  *
  43  */
  44 class HotspotCompilation
  45     implements HotspotCompilationMBean {
  46 
  47     private VMManagement jvm;
  48 
  49     /**
  50      * Constructor of HotspotRuntime class.
  51      */
  52     HotspotCompilation(VMManagement vm) {
  53         jvm = vm;
  54         initCompilerCounters();
  55     }
  56 
  57     // Performance counter support
  58     private static final String JAVA_CI    = "java.ci.";
  59     private static final String COM_SUN_CI = "com.sun.ci.";
  60     private static final String SUN_CI     = "sun.ci.";
  61     private static final String CI_COUNTER_NAME_PATTERN =
  62         JAVA_CI + "|" + COM_SUN_CI + "|" + SUN_CI;
  63 
  64     private LongCounter compilerThreads;
  65     private LongCounter totalCompiles;
  66     private LongCounter totalBailouts;
  67     private LongCounter totalInvalidates;
  68     private LongCounter nmethodCodeSize;
  69     private LongCounter nmethodSize;
  70     private StringCounter lastMethod;
  71     private LongCounter lastSize;
  72     private LongCounter lastType;
  73     private StringCounter lastFailedMethod;
  74     private LongCounter lastFailedType;
  75     private StringCounter lastInvalidatedMethod;
  76     private LongCounter lastInvalidatedType;
  77 
  78     private class CompilerThreadInfo {
  79         String name;
  80         StringCounter method;
  81         LongCounter type;
  82         LongCounter compiles;
  83         LongCounter time;
  84         CompilerThreadInfo(String bname, int index) {
  85             String basename = bname + "." + index + ".";
  86             this.name = bname + "-" + index;
  87             this.method = (StringCounter) lookup(basename + "method");
  88             this.type = (LongCounter) lookup(basename + "type");
  89             this.compiles = (LongCounter) lookup(basename + "compiles");
  90             this.time = (LongCounter) lookup(basename + "time");
  91         }
  92 
  93         @SuppressWarnings("deprecation")
  94         CompilerThreadStat getCompilerThreadStat() {
  95             MethodInfo minfo = new MethodInfo(method.stringValue(),
  96                                               (int) type.longValue(),
  97                                               -1);
  98             return new CompilerThreadStat(name,
  99                                           compiles.longValue(),
 100                                           time.longValue(),
 101                                           minfo);
 102         }
 103     }
 104     private List<CompilerThreadInfo> threads;
 105     private int numActiveThreads; // number of active compiler threads
 106 
 107     private Map<String, Counter> counters;
 108     private Counter lookup(String name) {
 109         Counter c = null;
 110 
 111         // Only one counter exists with the specified name in the
 112         // current implementation.  We first look up in the SUN_CI namespace
 113         // since most counters are in SUN_CI namespace.
 114 
 115         if ((c = counters.get(SUN_CI + name)) != null) {
 116             return c;
 117         }
 118         if ((c = counters.get(COM_SUN_CI + name)) != null) {
 119             return c;
 120         }
 121         if ((c = counters.get(JAVA_CI + name)) != null) {
 122             return c;
 123         }
 124 
 125         // FIXME: should tolerate if counter doesn't exist
 126         throw new AssertionError("Counter " + name + " does not exist");
 127     }
 128 
 129     private void initCompilerCounters() {
 130         // Build a tree map of the current list of performance counters
 131         counters = new TreeMap<>();
 132         for (Counter c: getInternalCompilerCounters()) {
 133             counters.put(c.getName(), c);
 134         }
 135 
 136         compilerThreads = (LongCounter) lookup("threads");
 137         totalCompiles = (LongCounter) lookup("totalCompiles");
 138         totalBailouts = (LongCounter) lookup("totalBailouts");
 139         totalInvalidates = (LongCounter) lookup("totalInvalidates");
 140         nmethodCodeSize = (LongCounter) lookup("nmethodCodeSize");
 141         nmethodSize = (LongCounter) lookup("nmethodSize");
 142         lastMethod = (StringCounter) lookup("lastMethod");
 143         lastSize = (LongCounter) lookup("lastSize");
 144         lastType = (LongCounter) lookup("lastType");
 145         lastFailedMethod = (StringCounter) lookup("lastFailedMethod");
 146         lastFailedType = (LongCounter) lookup("lastFailedType");
 147         lastInvalidatedMethod = (StringCounter) lookup("lastInvalidatedMethod");
 148         lastInvalidatedType = (LongCounter) lookup("lastInvalidatedType");
 149 
 150         numActiveThreads = (int) compilerThreads.longValue();
 151 
 152         // Allocate CompilerThreadInfo for compilerThread and adaptorThread
 153         threads = new ArrayList<CompilerThreadInfo>();
 154 
 155         for (int i = 0; i < numActiveThreads; i++) {
 156             if (counters.containsKey(SUN_CI + "compilerThread." + i + ".method")) {
 157                 threads.add(new CompilerThreadInfo("compilerThread", i));
 158             }
 159         }
 160     }
 161 
 162     public int getCompilerThreadCount() {
 163         return numActiveThreads;
 164     }
 165 
 166     public long getTotalCompileCount() {
 167         return totalCompiles.longValue();
 168     }
 169 
 170     public long getBailoutCompileCount() {
 171         return totalBailouts.longValue();
 172     }
 173 
 174     public long getInvalidatedCompileCount() {
 175         return totalInvalidates.longValue();
 176     }
 177 
 178     public long getCompiledMethodCodeSize() {
 179         return nmethodCodeSize.longValue();
 180     }
 181 
 182     public long getCompiledMethodSize() {
 183         return nmethodSize.longValue();
 184     }
 185 
 186     @Deprecated
 187     public List<CompilerThreadStat> getCompilerThreadStats() {
 188         List<CompilerThreadStat> list = new ArrayList<>(threads.size());
 189         for (CompilerThreadInfo info : threads) {
 190             list.add(info.getCompilerThreadStat());
 191         }
 192         return list;
 193     }
 194 
 195     public MethodInfo getLastCompile() {
 196         return new MethodInfo(lastMethod.stringValue(),
 197                               (int) lastType.longValue(),
 198                               (int) lastSize.longValue());
 199     }
 200 
 201     public MethodInfo getFailedCompile() {
 202         return new MethodInfo(lastFailedMethod.stringValue(),
 203                               (int) lastFailedType.longValue(),
 204                               -1);
 205     }
 206 
 207     public MethodInfo getInvalidatedCompile() {
 208         return new MethodInfo(lastInvalidatedMethod.stringValue(),
 209                               (int) lastInvalidatedType.longValue(),
 210                               -1);
 211     }
 212 
 213     public java.util.List<Counter> getInternalCompilerCounters() {
 214         return jvm.getInternalCounters(CI_COUNTER_NAME_PATTERN);
 215     }
 216 }