001: /*
002: * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.management;
027:
028: import java.util.regex.*;
029: import java.util.List;
030: import java.util.ListIterator;
031: import java.util.Iterator;
032: import java.util.ArrayList;
033: import java.util.Map;
034: import java.util.TreeMap;
035: import sun.management.counter.*;
036:
037: /**
038: * Implementation class of HotspotCompilationMBean interface.
039: *
040: * Internal, uncommitted management interface for Hotspot compilation
041: * system.
042: *
043: */
044: class HotspotCompilation implements HotspotCompilationMBean {
045:
046: private VMManagement jvm;
047:
048: /**
049: * Constructor of HotspotRuntime class.
050: */
051: HotspotCompilation(VMManagement vm) {
052: jvm = vm;
053: initCompilerCounters();
054: }
055:
056: // Performance counter support
057: private static final String JAVA_CI = "java.ci.";
058: private static final String COM_SUN_CI = "com.sun.ci.";
059: private static final String SUN_CI = "sun.ci.";
060: private static final String CI_COUNTER_NAME_PATTERN = JAVA_CI + "|"
061: + COM_SUN_CI + "|" + SUN_CI;
062:
063: private LongCounter compilerThreads;
064: private LongCounter totalCompiles;
065: private LongCounter totalBailouts;
066: private LongCounter totalInvalidates;
067: private LongCounter nmethodCodeSize;
068: private LongCounter nmethodSize;
069: private StringCounter lastMethod;
070: private LongCounter lastSize;
071: private LongCounter lastType;
072: private StringCounter lastFailedMethod;
073: private LongCounter lastFailedType;
074: private StringCounter lastInvalidatedMethod;
075: private LongCounter lastInvalidatedType;
076:
077: private class CompilerThreadInfo {
078: int index;
079: String name;
080: StringCounter method;
081: LongCounter type;
082: LongCounter compiles;
083: LongCounter time;
084:
085: CompilerThreadInfo(String bname, int index) {
086: String basename = bname + "." + index + ".";
087: this .name = bname + "-" + index;
088: this .method = (StringCounter) lookup(basename + "method");
089: this .type = (LongCounter) lookup(basename + "type");
090: this .compiles = (LongCounter) lookup(basename + "compiles");
091: this .time = (LongCounter) lookup(basename + "time");
092: }
093:
094: CompilerThreadInfo(String bname) {
095: String basename = bname + ".";
096: this .name = bname;
097: this .method = (StringCounter) lookup(basename + "method");
098: this .type = (LongCounter) lookup(basename + "type");
099: this .compiles = (LongCounter) lookup(basename + "compiles");
100: this .time = (LongCounter) lookup(basename + "time");
101: }
102:
103: CompilerThreadStat getCompilerThreadStat() {
104: MethodInfo minfo = new MethodInfo(method.stringValue(),
105: (int) type.longValue(), -1);
106: return new CompilerThreadStat(name, compiles.longValue(),
107: time.longValue(), minfo);
108: }
109: }
110:
111: private CompilerThreadInfo[] threads;
112: private int numActiveThreads; // number of active compiler threads
113:
114: private Map<String, Counter> counters;
115:
116: private Counter lookup(String name) {
117: Counter c = null;
118:
119: // Only one counter exists with the specified name in the
120: // current implementation. We first look up in the SUN_CI namespace
121: // since most counters are in SUN_CI namespace.
122:
123: if ((c = (Counter) counters.get(SUN_CI + name)) != null) {
124: return c;
125: }
126: if ((c = (Counter) counters.get(COM_SUN_CI + name)) != null) {
127: return c;
128: }
129: if ((c = (Counter) counters.get(JAVA_CI + name)) != null) {
130: return c;
131: }
132:
133: // FIXME: should tolerate if counter doesn't exist
134: throw new InternalError("Counter " + name + " does not exist");
135: }
136:
137: private void initCompilerCounters() {
138: // Build a tree map of the current list of performance counters
139: ListIterator iter = getInternalCompilerCounters()
140: .listIterator();
141: counters = new TreeMap<String, Counter>();
142: while (iter.hasNext()) {
143: Counter c = (Counter) iter.next();
144: counters.put(c.getName(), c);
145: }
146:
147: compilerThreads = (LongCounter) lookup("threads");
148: totalCompiles = (LongCounter) lookup("totalCompiles");
149: totalBailouts = (LongCounter) lookup("totalBailouts");
150: totalInvalidates = (LongCounter) lookup("totalInvalidates");
151: nmethodCodeSize = (LongCounter) lookup("nmethodCodeSize");
152: nmethodSize = (LongCounter) lookup("nmethodSize");
153: lastMethod = (StringCounter) lookup("lastMethod");
154: lastSize = (LongCounter) lookup("lastSize");
155: lastType = (LongCounter) lookup("lastType");
156: lastFailedMethod = (StringCounter) lookup("lastFailedMethod");
157: lastFailedType = (LongCounter) lookup("lastFailedType");
158: lastInvalidatedMethod = (StringCounter) lookup("lastInvalidatedMethod");
159: lastInvalidatedType = (LongCounter) lookup("lastInvalidatedType");
160:
161: numActiveThreads = (int) compilerThreads.longValue();
162:
163: // Allocate CompilerThreadInfo for compilerThread and adaptorThread
164: threads = new CompilerThreadInfo[numActiveThreads + 1];
165:
166: // AdaptorThread has index 0
167: if (counters.containsKey(SUN_CI + "adapterThread.compiles")) {
168: threads[0] = new CompilerThreadInfo("adapterThread", 0);
169: numActiveThreads++;
170: } else {
171: threads[0] = null;
172: }
173:
174: for (int i = 1; i < threads.length; i++) {
175: threads[i] = new CompilerThreadInfo("compilerThread", i - 1);
176: }
177: }
178:
179: public int getCompilerThreadCount() {
180: return numActiveThreads;
181: }
182:
183: public long getTotalCompileCount() {
184: return totalCompiles.longValue();
185: }
186:
187: public long getBailoutCompileCount() {
188: return totalBailouts.longValue();
189: }
190:
191: public long getInvalidatedCompileCount() {
192: return totalInvalidates.longValue();
193: }
194:
195: public long getCompiledMethodCodeSize() {
196: return nmethodCodeSize.longValue();
197: }
198:
199: public long getCompiledMethodSize() {
200: return nmethodSize.longValue();
201: }
202:
203: public java.util.List<CompilerThreadStat> getCompilerThreadStats() {
204: List<CompilerThreadStat> list = new ArrayList<CompilerThreadStat>(
205: threads.length);
206: int i = 0;
207: if (threads[0] == null) {
208: // no adaptor thread
209: i = 1;
210: }
211: for (; i < threads.length; i++) {
212: list.add(threads[i].getCompilerThreadStat());
213: }
214: return list;
215: }
216:
217: public MethodInfo getLastCompile() {
218: return new MethodInfo(lastMethod.stringValue(), (int) lastType
219: .longValue(), (int) lastSize.longValue());
220: }
221:
222: public MethodInfo getFailedCompile() {
223: return new MethodInfo(lastFailedMethod.stringValue(),
224: (int) lastFailedType.longValue(), -1);
225: }
226:
227: public MethodInfo getInvalidatedCompile() {
228: return new MethodInfo(lastInvalidatedMethod.stringValue(),
229: (int) lastInvalidatedType.longValue(), -1);
230: }
231:
232: public java.util.List<Counter> getInternalCompilerCounters() {
233: return jvm.getInternalCounters(CI_COUNTER_NAME_PATTERN);
234: }
235: }
|