001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.ui.warmup;
043:
044: import java.lang.management.ClassLoadingMXBean;
045: import java.lang.management.CompilationMXBean;
046: import java.lang.management.GarbageCollectorMXBean;
047: import java.lang.management.ManagementFactory;
048: import java.lang.management.MemoryMXBean;
049: import java.lang.management.MemoryUsage;
050: import java.lang.management.OperatingSystemMXBean;
051: import java.lang.management.RuntimeMXBean;
052: import java.lang.reflect.InvocationTargetException;
053: import java.lang.reflect.Method;
054: import java.text.MessageFormat;
055: import java.util.logging.Level;
056: import java.util.logging.LogRecord;
057: import java.util.logging.Logger;
058: import org.openide.util.NbBundle;
059:
060: /** Task executed early after startup to log diagnostic data about
061: * JVM - memory settings, JIT type, some hardware information.
062: *
063: * @author Radim Kubacki
064: */
065: public final class DiagnosticTask implements Runnable {
066:
067: public DiagnosticTask() {
068: }
069:
070: /** Performs DnD pre-heat.
071: */
072: public void run() {
073: String diagInfo = logParams();
074: Logger.getLogger(DiagnosticTask.class.getName()).info(diagInfo);
075: logEnv();
076: }
077:
078: private void logEnv() {
079: try {
080: OperatingSystemMXBean osBean = ManagementFactory
081: .getOperatingSystemMXBean();
082: // w/o dependency on Sun's JDK
083: // long freeMem = ((com.sun.management.OperatingSystemMXBean)osBean).getTotalPhysicalMemorySize();
084: Method m = osBean.getClass().getMethod(
085: "getTotalPhysicalMemorySize");
086: m.setAccessible(true);
087: long freeMem = (Long) m.invoke(osBean);
088: Logger.getLogger(DiagnosticTask.class.getName()).log(
089: Level.INFO, "Total memory {0}", freeMem);
090:
091: LogRecord lr = new LogRecord(Level.INFO, "MEMORY");
092: lr.setResourceBundle(NbBundle
093: .getBundle(DiagnosticTask.class));
094: lr.setParameters(new Object[] { freeMem });
095: Logger.getLogger("org.netbeans.ui.performance").log(lr);
096: } catch (NoSuchMethodException ex) {
097: Logger.getLogger(DiagnosticTask.class.getName()).log(
098: Level.INFO, null, ex);
099: } catch (SecurityException ex) {
100: Logger.getLogger(DiagnosticTask.class.getName()).log(
101: Level.INFO, null, ex);
102: } catch (IllegalAccessException ex) {
103: Logger.getLogger(DiagnosticTask.class.getName()).log(
104: Level.INFO, null, ex);
105: } catch (IllegalArgumentException ex) {
106: Logger.getLogger(DiagnosticTask.class.getName()).log(
107: Level.INFO, null, ex);
108: } catch (InvocationTargetException ex) {
109: Logger.getLogger(DiagnosticTask.class.getName()).log(
110: Level.INFO, null, ex);
111: }
112: }
113:
114: private void logMemoryUsage(StringBuilder sb, MemoryUsage usage,
115: String label) {
116: // long used, commited;
117: long init, max;
118: init = usage.getInit();
119: // used = usage.getUsed();
120: // commited = usage.getCommitted();
121: max = usage.getMax();
122: // sb.append(label).append(" usage: used ").append(formatBytes(used)) // NOI18N
123: // .append(" of ").append(formatBytes(commited)); // NOI18N
124: sb.append(label).append(" usage: initial ").append(
125: formatBytes(init)) // NOI18N
126: .append(" maximum ").append(formatBytes(max)).append(
127: '\n'); // NOI18N
128: }
129:
130: /** Format the number to readable string using kB or MB.
131: */
132: private String formatBytes(long bytes) {
133: if (bytes > 1024L * 1024L) {
134: return MessageFormat.format("{0,number,0.0MB}",
135: bytes / 1024.0 / 1024.0); // NOI18N
136: } else if (bytes > 1024L) {
137: return MessageFormat.format("{0,number,0.0kB}",
138: bytes / 1024.0); // NOI18N
139: } else {
140: return MessageFormat.format("{0,number,0b}", bytes); // NOI18N
141: }
142: }
143:
144: private String formatTime(long time) {
145: StringBuilder sb = new StringBuilder();
146: if (time > 1000L * 60L * 60L * 24L) {
147: sb.append(MessageFormat.format("{0,number,0d}", time
148: / (1000L * 60L * 60L * 24L))); // NOI18N
149: time %= 1000L * 60L * 60L * 24L;
150: }
151: if (time > 1000L * 60L * 60L || sb.length() > 0) {
152: sb.append(MessageFormat.format("{0,number,0h}", time
153: / (1000L * 60L * 60L))); // NOI18N
154: time %= 1000L * 60L * 60L;
155: }
156: if (time > 1000L * 60L || sb.length() > 0) {
157: sb.append(MessageFormat.format("{0,number,0m}", time
158: / (1000L * 60L))); // NOI18N
159: time %= 1000L * 60L;
160: }
161: sb.append(MessageFormat.format("{0,number,0s}", time / 1000L)); // NOI18N
162: return sb.toString();
163: }
164:
165: private String logParams() {
166: StringBuilder sb = new StringBuilder(500);
167: sb.append("Diagnostic information\n");
168: try {
169: RuntimeMXBean rmBean = ManagementFactory.getRuntimeMXBean();
170: CompilationMXBean cmpMBean = ManagementFactory
171: .getCompilationMXBean();
172: // ThreadMXBean tmBean = ManagementFactory.getThreadMXBean();
173: MemoryMXBean memoryBean = ManagementFactory
174: .getMemoryMXBean();
175: ClassLoadingMXBean clMBean = ManagementFactory
176: .getClassLoadingMXBean();
177:
178: // MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
179: // ObjectName hsDiag = new ObjectName("com.sun.management:name=HotSpotDiagnostic");
180: // com.sun.management.OperatingSystemMXBean sunOSMBean =
181: // ManagementFactory.getSunOperatingSystemMXBean();
182:
183: // Map<String, String> props = new TreeMap<String, String>(rmBean.getSystemProperties());
184: // System.out.println("System properties");
185: // for (Map.Entry<String, String> entry: props.entrySet()) {
186: // System.out.println("Property: "+entry.getKey()+" Value: "+entry.getValue());
187: // }
188: sb.append("Input arguments:");
189: for (String s : rmBean.getInputArguments()) {
190: sb.append("\n\t").append(s);
191: }
192:
193: if (cmpMBean != null) {
194: sb.append("\nCompiler: " + cmpMBean.getName()).append(
195: '\n');
196: }
197:
198: // Memory
199: MemoryUsage usage = memoryBean.getHeapMemoryUsage();
200: logMemoryUsage(sb, usage, "Heap memory");
201: usage = memoryBean.getNonHeapMemoryUsage();
202: logMemoryUsage(sb, usage, "Non heap memory");
203: for (GarbageCollectorMXBean gcMBean : ManagementFactory
204: .getGarbageCollectorMXBeans()) {
205: sb
206: .append("Garbage collector: ")
207: .append(gcMBean.getName())
208: .append(" (Collections=")
209: .append(gcMBean.getCollectionCount())
210: .append(" Total time spent=")
211: .append(formatTime(gcMBean.getCollectionTime()))
212: .append(")\n");
213: }
214:
215: // classes
216: int clsLoaded;
217: long clsTotal, clsUnloaded;
218: clsLoaded = clMBean.getLoadedClassCount();
219: clsTotal = clMBean.getTotalLoadedClassCount();
220: clsUnloaded = clMBean.getUnloadedClassCount();
221: sb.append("Classes: loaded=").append(clsLoaded).append(
222: " total loaded=").append(clsTotal).append(
223: " unloaded ").append(clsUnloaded).append('\n');
224:
225: // } catch (MalformedObjectNameException ex) {
226: // Logger.getLogger("global").log(Level.WARNING, null, ex);
227: } catch (NullPointerException ex) {
228: Logger.getLogger(DiagnosticTask.class.getName()).log(
229: Level.WARNING, null, ex);
230: }
231: return sb.toString();
232: }
233:
234: }
|