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-2006 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 threaddemo;
043:
044: import java.awt.Font;
045: import java.awt.GridLayout;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import java.text.DecimalFormat;
049: import java.text.NumberFormat;
050: import java.util.TimerTask;
051: import javax.swing.JButton;
052: import javax.swing.JCheckBox;
053: import javax.swing.JLabel;
054: import javax.swing.JPanel;
055: import javax.swing.JTextField;
056: import javax.swing.Timer;
057:
058: /**
059: * Displays information about any long pauses in the AWT thread.
060: * @author Jesse Glick
061: */
062: final class Monitor extends JPanel implements ActionListener {
063:
064: private static final int DELAY_CPU = 100; // msec between AWT pings
065: private static final long DELAY_HEAP = 1000; // msec between heap recalcs
066: private static final long GRACE = 500; // msec permitted without access
067:
068: private long last;
069: private final Timer t1;
070: private final java.util.Timer t2;
071: private final TimerTask task;
072: private final JTextField blockageField;
073: private final JTextField heapField;
074: private static final NumberFormat format = new DecimalFormat("0.00");
075: private JCheckBox heapLiveCheckBox;
076: private boolean heapLive;
077:
078: public Monitor() {
079: setLayout(new GridLayout(3, 1));
080: t1 = new Timer(DELAY_CPU, this );
081: t1.setRepeats(true);
082: t2 = new java.util.Timer(true);
083: task = new TimerTask() {
084: public void run() {
085: updateHeap();
086: }
087: };
088: last = System.currentTimeMillis();
089: JPanel sub = new JPanel();
090: sub.add(new JLabel("Last blockage above " + GRACE + "msec:"));
091: blockageField = new JTextField(10);
092: Font font = new Font("Monospaced", Font.PLAIN, blockageField
093: .getFont().getSize());
094: blockageField.setFont(font);
095: blockageField.setEditable(false);
096: sub.add(blockageField);
097: add(sub);
098: sub = new JPanel();
099: sub.add(new JLabel("Heap:"));
100: heapField = new JTextField(10);
101: heapField.setFont(font);
102: heapField.setEditable(false);
103: sub.add(heapField);
104: heapLiveCheckBox = new JCheckBox("Immediate Heap Updates");
105: heapLiveCheckBox.setMnemonic('h');
106: sub.add(heapLiveCheckBox);
107: add(sub);
108: sub = new JPanel();
109: JButton gc = new JButton("GC");
110: gc.setMnemonic('g');
111: gc.addActionListener(new ActionListener() {
112: public void actionPerformed(ActionEvent ev) {
113: System.gc();
114: System.runFinalization();
115: System.gc();
116: }
117: });
118: sub.add(gc);
119: add(sub);
120: }
121:
122: public void addNotify() {
123: super .addNotify();
124: t1.start();
125: t2.schedule(task, 0L, DELAY_HEAP);
126: }
127:
128: public void removeNotify() {
129: t1.stop();
130: t2.cancel();
131: }
132:
133: public void actionPerformed(ActionEvent e) {
134: long now = System.currentTimeMillis();
135: long block = now - last;
136: if (block > GRACE) {
137: System.err.println("AWT blocked for " + block + "msec");
138: blockageField.setText(Long.toString(block) + "msec");
139: blockageField.paintImmediately(0, 0, blockageField
140: .getWidth(), blockageField.getHeight());
141: }
142: last = now;
143: // Also update heapLive since we are in AWT:
144: heapLive = heapLiveCheckBox.isSelected();
145: }
146:
147: private void updateHeap() {
148: // setText is thread-safe:
149: heapField.setText(format.format(heapUsedMega()) + "Mb");
150: if (heapLive) {
151: // Show live updates even when AWT is blocked.
152: // Note that this can sometimes cause weird painting problems
153: // in other windows, so off by default.
154: heapField.paintImmediately(0, 0, heapField.getWidth(),
155: heapField.getHeight());
156: }
157: }
158:
159: private static float heapUsedMega() {
160: Runtime r = Runtime.getRuntime();
161: return (r.totalMemory() - r.freeMemory()) / 1024.0f / 1024.0f;
162: }
163:
164: }
|