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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.core;
041:
042: import java.awt.AWTEvent;
043: import java.awt.EventQueue;
044: import java.awt.Toolkit;
045: import java.util.EmptyStackException;
046: import java.util.Map;
047: import java.util.ResourceBundle;
048: import java.util.logging.Level;
049: import java.util.logging.LogRecord;
050: import java.util.logging.Logger;
051: import org.netbeans.core.startup.Main;
052: import org.openide.util.Mutex;
053: import org.openide.util.RequestProcessor;
054:
055: /**
056: * Logging event queue that can report problems about too long execution times
057: *
058: *
059: * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
060: */
061: final class TimeableEventQueue extends EventQueue implements Runnable {
062: private static final Logger LOG = Logger
063: .getLogger(TimeableEventQueue.class.getName());
064: private static final RequestProcessor RP = new RequestProcessor(
065: "Timeable Event Queue Watch Dog", 1, true); // NOI18N
066: private static final int QUANTUM = Integer.getInteger(
067: "org.netbeans.core.TimeableEventQueue.quantum", 5000); // NOI18N
068: private static final int PAUSE = Integer.getInteger(
069: "org.netbeans.core.TimeableEventQueue.pause", 60000); // NOI18N
070:
071: private final RequestProcessor.Task TIMEOUT;
072: private volatile Map<Thread, StackTraceElement[]> stack;
073: private volatile long ignoreTill;
074: private volatile long start;
075:
076: public TimeableEventQueue() {
077: TIMEOUT = RP.create(this );
078: TIMEOUT.setPriority(Thread.MIN_PRIORITY);
079: }
080:
081: static void initialize() {
082: // #28536: make sure a JRE bug does not prevent the event queue from having
083: // the right context class loader
084: // and #35470: do it early, before any module-loaded AWT code might run
085: // and #36820: even that isn't always early enough, so we need to push
086: // a new EQ to enforce the context loader
087: // XXX this is a hack!
088: try {
089: Mutex.EVENT.writeAccess(new Mutex.Action<Void>() {
090: public Void run() {
091: Thread.currentThread().setContextClassLoader(
092: Main.getModuleSystem().getManager()
093: .getClassLoader());
094: Toolkit.getDefaultToolkit().getSystemEventQueue()
095: .push(new TimeableEventQueue());
096: LOG.fine("Initialization done");
097: return null;
098: }
099: });
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103: }
104:
105: @Override
106: protected void dispatchEvent(AWTEvent event) {
107: try {
108: tick();
109: super .dispatchEvent(event);
110: } finally {
111: done();
112: }
113: }
114:
115: @Override
116: public AWTEvent getNextEvent() throws InterruptedException {
117: try {
118: tick();
119: return super .getNextEvent();
120: } finally {
121: done();
122: }
123: }
124:
125: @Override
126: public synchronized AWTEvent peekEvent() {
127: try {
128: tick();
129: return super .peekEvent();
130: } finally {
131: done();
132: }
133: }
134:
135: @Override
136: public synchronized AWTEvent peekEvent(int id) {
137: try {
138: tick();
139: return super .peekEvent(id);
140: } finally {
141: done();
142: }
143: }
144:
145: @Override
146: protected void pop() throws EmptyStackException {
147: try {
148: tick();
149: super .pop();
150: } finally {
151: done();
152: }
153: }
154:
155: @Override
156: public void postEvent(AWTEvent theEvent) {
157: try {
158: tick();
159: super .postEvent(theEvent);
160: } finally {
161: done();
162: }
163: }
164:
165: @Override
166: public synchronized void push(EventQueue newEventQueue) {
167: try {
168: tick();
169: super .push(newEventQueue);
170: } finally {
171: done();
172: }
173: }
174:
175: private void done() {
176: stack = null;
177: TIMEOUT.cancel();
178: long time = System.currentTimeMillis() - start;
179: if (time > 50) {
180: LOG.log(Level.FINE, "done, timer stopped, took {0}", time);
181: } else {
182: LOG
183: .log(Level.FINEST, "done, timer stopped, took {0}",
184: time);
185: }
186: }
187:
188: private void tick() {
189: stack = null;
190: start = System.currentTimeMillis();
191: if (start >= ignoreTill) {
192: LOG.log(Level.FINEST, "tick, schedule a timer at {0}",
193: start);
194: TIMEOUT.schedule(QUANTUM);
195: }
196: }
197:
198: public void run() {
199: stack = Thread.getAllStackTraces();
200: LOG.log(Level.FINER, "timer running");
201: for (int i = 0; i < 10; i++) {
202: if (Thread.interrupted()) {
203: LOG.log(Level.FINER, "timer cancelled");
204: return;
205: }
206: Thread.yield();
207: System.gc();
208: System.runFinalization();
209: }
210: final Map<Thread, StackTraceElement[]> myStack = stack;
211: if (myStack == null) {
212: LOG.log(Level.FINER, "timer cancelled");
213: return;
214: }
215:
216: long now = System.currentTimeMillis();
217: ignoreTill = now + PAUSE;
218: long howLong = now - start;
219:
220: // Logger UI_LOG = Logger.getLogger("org.netbeans.ui.performance"); // NOI18N
221: LogRecord rec = new LogRecord(Level.INFO,
222: "LOG_EventQueueBlocked"); // NOI18N
223: rec.setParameters(new Object[] { howLong });
224: EQException eq = new EQException(myStack);
225: rec.setThrown(eq);
226: rec.setResourceBundleName("org.netbeans.core.Bundle"); // NOI18N
227: rec.setResourceBundle(ResourceBundle
228: .getBundle("org.netbeans.core.Bundle")); // NOI18N
229: // UI_LOG.log(rec);
230: LOG.log(rec);
231: }
232:
233: private static final class EQException extends Exception {
234: private volatile Map<Thread, StackTraceElement[]> stack;
235:
236: public EQException(Map<Thread, StackTraceElement[]> stack) {
237: this .stack = stack;
238: for (Map.Entry<Thread, StackTraceElement[]> en : stack
239: .entrySet()) {
240: if (en.getKey().getName().indexOf("AWT-EventQueue") >= 0) {
241: setStackTrace(en.getValue());
242: break;
243: }
244: }
245: }
246:
247: @Override
248: public String getMessage() {
249: return threadDump("AWT Event Queue Thread Blocked", stack); // NOI18N
250: }
251:
252: private static void appendThread(StringBuilder sb,
253: String indent, Thread t,
254: Map<Thread, StackTraceElement[]> data) {
255: sb.append(indent).append("Thread ").append(t.getName())
256: .append('\n');
257: indent = indent.concat(" ");
258: StackTraceElement[] arr = data.get(t);
259: if (arr != null) {
260: for (StackTraceElement e : arr) {
261: sb.append(indent).append(e.getClassName()).append(
262: '.').append(e.getMethodName()).append(':')
263: .append(e.getLineNumber()).append('\n');
264: }
265: } else {
266: sb.append(indent).append("no stacktrace info"); // NOI18N
267: }
268: }
269:
270: private static void appendGroup(StringBuilder sb,
271: String indent, ThreadGroup tg,
272: Map<Thread, StackTraceElement[]> data) {
273: sb.append(indent).append("Group ").append(tg.getName())
274: .append('\n');
275: indent = indent.concat(" ");
276:
277: int groups = tg.activeGroupCount();
278: ThreadGroup[] chg = new ThreadGroup[groups];
279: tg.enumerate(chg, false);
280: for (ThreadGroup inner : chg) {
281: if (inner != null)
282: appendGroup(sb, indent, inner, data);
283: }
284:
285: int threads = tg.activeCount();
286: Thread[] cht = new Thread[threads];
287: tg.enumerate(cht, false);
288: for (Thread t : cht) {
289: if (t != null)
290: appendThread(sb, indent, t, data);
291: }
292: }
293:
294: private static String threadDump(String msg,
295: Map<Thread, StackTraceElement[]> all) {
296: ThreadGroup root = Thread.currentThread().getThreadGroup();
297: while (root.getParent() != null)
298: root = root.getParent();
299:
300: StringBuilder sb = new StringBuilder();
301: sb.append(msg).append('\n');
302: appendGroup(sb, "", root, all);
303: sb.append('\n').append("---");
304: return sb.toString();
305: }
306:
307: }
308: }
|