001: package tijmp.ui;
002:
003: import java.awt.Dimension;
004: import java.awt.GridBagConstraints;
005: import java.awt.GridBagLayout;
006: import java.awt.Insets;
007: import java.lang.management.MemoryMXBean;
008: import java.lang.management.MemoryPoolMXBean;
009: import java.lang.management.MemoryType;
010: import java.lang.management.MemoryUsage;
011: import java.util.ArrayList;
012: import java.util.Arrays;
013: import java.util.List;
014: import javax.swing.BorderFactory;
015: import javax.swing.JLabel;
016: import javax.swing.JPanel;
017: import javax.swing.JScrollPane;
018: import javax.swing.JTable;
019: import javax.swing.ListSelectionModel;
020: import javax.swing.table.AbstractTableModel;
021: import javax.swing.table.TableColumn;
022: import tijmp.ProfilerHandler;
023:
024: /** A class to show current memory usage.
025: */
026: public class MemoryInfoPanel extends JPanel {
027:
028: public MemoryInfoPanel(ProfilerHandler ph) {
029: MemoryMXBean mbean = ph.getMemoryMXBean();
030: MemoryUsage heapUsage = mbean.getHeapMemoryUsage();
031: MemoryUsage nonHeapUsage = mbean.getNonHeapMemoryUsage();
032:
033: List<MemInfo> ls1 = Arrays.asList(new MemInfo("Heap",
034: MemoryType.HEAP, heapUsage), new MemInfo("Non heap",
035: MemoryType.NON_HEAP, nonHeapUsage));
036:
037: TM tm1 = new TM(ls1);
038: JTable t1 = new JTable(tm1);
039: fixTable(t1);
040: JScrollPane sp1 = new JScrollPane(t1);
041: sp1.setBorder(BorderFactory
042: .createTitledBorder("Memory Overview"));
043:
044: List<MemoryPoolMXBean> ls = ph.getMemoryPoolMXBeans();
045: List<MemInfo> ls2 = new ArrayList<MemInfo>(ls.size());
046: for (MemoryPoolMXBean mpb : ls)
047: ls2.add(new MemInfo(mpb.getName(), mpb.getType(), mpb
048: .getUsage()));
049: TM tm2 = new TM(ls2);
050: JTable t2 = new JTable(tm2);
051: fixTable(t2);
052: t2.getRowSorter().toggleSortOrder(TM.COL_COMMITED);
053: t2.getRowSorter().toggleSortOrder(TM.COL_COMMITED);
054: JScrollPane sp2 = new JScrollPane(t2);
055: sp2.setBorder(BorderFactory.createTitledBorder("Memory Pools"));
056:
057: int finilizationCount = mbean
058: .getObjectPendingFinalizationCount();
059: JLabel fc = new JLabel("Finilization count: "
060: + finilizationCount);
061:
062: GridBagLayout gb = new GridBagLayout();
063: GridBagConstraints c = new GridBagConstraints();
064: setLayout(gb);
065:
066: c.insets = new Insets(2, 2, 2, 2);
067: c.gridx = 0;
068: c.gridy = 0;
069: c.weightx = 1;
070: c.weighty = 3;
071: c.fill = GridBagConstraints.BOTH;
072: add(sp1, c);
073: c.gridy++;
074: c.weighty = 6;
075: add(sp2, c);
076: c.weighty = 0;
077: c.gridy++;
078: add(fc, c);
079: }
080:
081: private void fixTable(JTable table) {
082: table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
083: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
084: table.setAutoCreateRowSorter(true);
085: TableColumn column = null;
086: for (int i = 0; i < table.getModel().getColumnCount(); i++) {
087: column = table.getColumnModel().getColumn(i);
088: if (i == TM.COL_NAME)
089: column.setPreferredWidth(200);
090: else if (i == TM.COL_TYPE)
091: column.setPreferredWidth(25);
092: else
093: column.setPreferredWidth(75);
094: }
095: int rows = table.getModel().getRowCount() + 1; // header
096: table.setPreferredScrollableViewportSize(new Dimension(600,
097: 15 * rows));
098: table
099: .setDefaultRenderer(Long.class,
100: new ReadableSizeRenderer());
101: }
102: }
103:
104: class MemInfo {
105: private String name;
106: private MemoryType mt;
107: private MemoryUsage mu;
108:
109: public MemInfo(String name, MemoryType mt, MemoryUsage mu) {
110: this .name = name;
111: this .mt = mt;
112: this .mu = mu;
113: }
114:
115: public String getName() {
116: return name;
117: }
118:
119: public MemoryType getType() {
120: return mt;
121: }
122:
123: public MemoryUsage getUsage() {
124: return mu;
125: }
126: }
127:
128: class TM extends AbstractTableModel {
129: private List<MemInfo> ls;
130:
131: private static final String[] columnNames = { "Name", "Heap",
132: "Init", "Used", "Commited", "Max" };
133:
134: public static final int COL_NAME = 0;
135: public static final int COL_TYPE = COL_NAME + 1;
136: public static final int COL_INIT = COL_TYPE + 1;
137: public static final int COL_USED = COL_INIT + 1;
138: public static final int COL_COMMITED = COL_USED + 1;
139: public static final int COL_MAX = COL_COMMITED + 1;
140:
141: public TM(List<MemInfo> ls) {
142: this .ls = ls;
143: }
144:
145: public String getColumnName(int col) {
146: return columnNames[col];
147: }
148:
149: public int getRowCount() {
150: return ls.size();
151: }
152:
153: public int getColumnCount() {
154: return columnNames.length;
155: }
156:
157: public Object getValueAt(int row, int col) {
158: MemInfo mi = ls.get(row);
159: MemoryUsage m = mi.getUsage();
160: switch (col) {
161: case COL_NAME:
162: return mi.getName();
163: case COL_TYPE:
164: return mi.getType() == MemoryType.HEAP;
165: case COL_INIT:
166: return m.getInit();
167: case COL_USED:
168: return m.getUsed();
169: case COL_COMMITED:
170: return m.getCommitted();
171: case COL_MAX:
172: return m.getMax();
173: }
174: throw new IllegalStateException("unknow column");
175: }
176:
177: public Class<?> getColumnClass(int col) {
178: switch (col) {
179: case COL_NAME:
180: return String.class;
181: case COL_TYPE:
182: return Boolean.class;
183: default:
184: return Long.class;
185: }
186: }
187:
188: public boolean isCellEditable(int row, int col) {
189: return false;
190: }
191:
192: public void setValueAt(Object value, int row, int col) {
193: throw new IllegalStateException("non editable table");
194: }
195: }
|