1 | /* |
2 | * $Id: SystemStatsDialog.java,v 1.3 2005/12/03 07:52:34 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | |
13 | package com.moesol.util; |
14 | |
15 | import java.awt.Container; |
16 | import java.awt.Dimension; |
17 | import java.awt.GridBagConstraints; |
18 | import java.awt.GridBagLayout; |
19 | import java.awt.event.ActionEvent; |
20 | import java.awt.event.ActionListener; |
21 | import java.awt.event.ComponentAdapter; |
22 | import java.awt.event.ComponentEvent; |
23 | import java.awt.event.WindowAdapter; |
24 | import java.awt.event.WindowEvent; |
25 | |
26 | import javax.swing.JButton; |
27 | import javax.swing.JComponent; |
28 | import javax.swing.JFrame; |
29 | import javax.swing.JLabel; |
30 | |
31 | import com.moesol.bindings.platform_sdk.component_services.COM; |
32 | |
33 | |
34 | /** |
35 | * @author robert |
36 | */ |
37 | public class SystemStatsDialog extends JFrame { |
38 | public SystemStatsDialog() { |
39 | setTitle("System Stats"); |
40 | connectUi(); |
41 | pack(); |
42 | m_min_size = getSize(); |
43 | } |
44 | public void connectUi() { |
45 | connectUi(getContentPane()); |
46 | addComponentListener(m_component_listener); |
47 | addWindowListener(m_window_listener); |
48 | m_gc_button.addActionListener(m_gc_action); |
49 | } |
50 | private void connectUi(Container pane) { |
51 | setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
52 | pane.setLayout(new GridBagLayout()); |
53 | |
54 | connectUiPair(pane, m_coheap_label, m_coheap_value); |
55 | connectUiPair(pane, m_iunks_label, m_iunks_value); |
56 | connectUiPair(pane, m_heap_label, m_heap_value); |
57 | connectUiPair(pane, m_total_label, m_total_value); |
58 | connectUiPair(pane, m_free_label, m_free_value); |
59 | connectGcButton(pane); |
60 | } |
61 | private void connectUiPair(Container pane, JComponent label, JComponent value) { |
62 | GridBagConstraints c = new GridBagConstraints(); |
63 | |
64 | c.weightx = 1.0; |
65 | c.anchor = GridBagConstraints.LINE_END; |
66 | pane.add(label, c); |
67 | |
68 | c.gridwidth = GridBagConstraints.REMAINDER; |
69 | c.anchor = GridBagConstraints.LINE_START; |
70 | value.setMinimumSize(new Dimension(100,100)); |
71 | pane.add(value, c); |
72 | |
73 | } |
74 | private void connectGcButton(Container pane) { |
75 | GridBagConstraints c = new GridBagConstraints(); |
76 | c.weightx = 1.0; |
77 | c.gridx = 1; |
78 | c.gridwidth = GridBagConstraints.REMAINDER; |
79 | c.anchor = GridBagConstraints.LINE_START; |
80 | |
81 | pane.add(m_gc_button, c); |
82 | } |
83 | public void onUpdateUi() { |
84 | System.out.println("onUpdateUi"); |
85 | long total_memory = Runtime.getRuntime().totalMemory(); |
86 | long free_memory = Runtime.getRuntime().freeMemory(); |
87 | long heap_memory = total_memory - free_memory; |
88 | |
89 | m_coheap_value.setText( |
90 | com.moesol.bindings.platform_sdk.component_services.MallocSpy.getAllocatedBytes() + " bytes"); |
91 | m_iunks_value.setText( |
92 | COM.getUnreleasedCount() + " inst"); |
93 | m_heap_value.setText((heap_memory / (1024 * 1024)) + " M"); |
94 | m_total_value.setText((total_memory / (1024 * 1024)) + "M"); |
95 | m_free_value.setText((free_memory / (1024 * 1024)) + "M"); |
96 | } |
97 | public void onHidden() { |
98 | System.out.println("onHidden"); |
99 | m_update_ui.stop(); |
100 | } |
101 | public void onShown() { |
102 | System.out.println("onShown"); |
103 | onUpdateUi(); |
104 | m_update_ui.start(); |
105 | } |
106 | public void onResized() { |
107 | boolean need_set_size = false; |
108 | Dimension size = getSize(); |
109 | if (size.width < m_min_size.width) { |
110 | size.width = m_min_size.width; |
111 | need_set_size = true; |
112 | } |
113 | if (size.height < m_min_size.height) { |
114 | size.height = m_min_size.height; |
115 | need_set_size = true; |
116 | } |
117 | if (need_set_size) { |
118 | setSize(size); |
119 | } |
120 | } |
121 | public void onGcPushed() { |
122 | System.out.println("gc"); |
123 | System.gc(); |
124 | } |
125 | |
126 | public static void main(String[] args) { |
127 | SystemStatsDialog frame = new SystemStatsDialog(); |
128 | frame.setVisible(true); |
129 | } |
130 | |
131 | private JLabel m_coheap_label = new JLabel("coheap: "); |
132 | private JLabel m_coheap_value = new JLabel("000000"); |
133 | private JLabel m_iunks_label = new JLabel("iunks: "); |
134 | private JLabel m_iunks_value = new JLabel("000000"); |
135 | private JLabel m_heap_label = new JLabel("heap: "); |
136 | private JLabel m_heap_value = new JLabel("000000"); |
137 | private JLabel m_total_label = new JLabel("total: "); |
138 | private JLabel m_total_value = new JLabel("000000"); |
139 | private JLabel m_free_label = new JLabel("free: "); |
140 | private JLabel m_free_value = new JLabel("000000"); |
141 | private JButton m_gc_button = new JButton("GC"); |
142 | private Dimension m_min_size; |
143 | |
144 | private javax.swing.Timer m_update_ui = |
145 | new javax.swing.Timer(1000, new ActionListener() { |
146 | public void actionPerformed(ActionEvent e) { |
147 | onUpdateUi(); |
148 | } |
149 | }); |
150 | private ComponentAdapter m_component_listener = new ComponentAdapter() { |
151 | public void componentResized(ComponentEvent e) { |
152 | onResized(); |
153 | } |
154 | public void componentShown(ComponentEvent e) { |
155 | onShown(); |
156 | } |
157 | public void componentHidden(ComponentEvent e) { |
158 | onHidden(); |
159 | } |
160 | }; |
161 | private WindowAdapter m_window_listener = new WindowAdapter() { |
162 | public void windowDeiconified(WindowEvent e) { |
163 | onShown(); |
164 | } |
165 | public void windowIconified(WindowEvent e) { |
166 | onHidden(); |
167 | } |
168 | }; |
169 | private ActionListener m_gc_action = new ActionListener() { |
170 | public void actionPerformed(ActionEvent e) { |
171 | onGcPushed(); |
172 | } |
173 | }; |
174 | } |