001: package net.sourceforge.squirrel_sql.plugins.oracle.SGAtrace;
002:
003: /*
004: * Copyright (C) 2004 Jason Height
005: * jmheight@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: import java.awt.BorderLayout;
023: import java.sql.PreparedStatement;
024: import java.sql.ResultSet;
025: import java.sql.SQLException;
026: import java.util.Timer;
027: import java.util.TimerTask;
028:
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.JTable;
032: import javax.swing.table.DefaultTableModel;
033:
034: import net.sourceforge.squirrel_sql.client.IApplication;
035: import net.sourceforge.squirrel_sql.client.session.ISession;
036: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
037: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
038: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
039: import net.sourceforge.squirrel_sql.fw.util.StringManager;
040: import net.sourceforge.squirrel_sql.plugins.oracle.OraclePlugin;
041: import net.sourceforge.squirrel_sql.plugins.oracle.common.AutoWidthResizeTable;
042:
043: public class SGATracePanel extends JPanel {
044: private static final StringManager s_stringMgr = StringManagerFactory
045: .getStringManager(SGATracePanel.class);
046:
047: /**
048: * Logger for this class.
049: */
050: private static final ILogger s_log = LoggerController
051: .createLogger(SGATracePanel.class);
052:
053: private static final String sgaTraceSQL = " SELECT a.SQL_Text, "
054: + " a.First_Load_Time, "
055: + " b.username, "
056: + " a.Parse_Calls, "
057: + " a.Executions, "
058: + " a.Sorts, "
059: + " a.Disk_Reads, "
060: + " a.Buffer_Gets, "
061: + " a.Rows_Processed, "
062: + " DECODE ( a.Executions, "
063: + " 0, "
064: + " 'N/A', "
065: + " ROUND ( a.Sorts / a.Executions, "
066: + " 3 ) ), "
067: + " DECODE ( a.Executions, "
068: + " 0, "
069: + " 'N/A', "
070: + " ROUND ( a.Disk_Reads / a.Executions, "
071: + " 3 ) ), "
072: + " DECODE ( a.Executions, "
073: + " 0, "
074: + " 'N/A', "
075: + " ROUND ( a.Buffer_Gets / a.Executions, "
076: + " 3 ) ), "
077: + " DECODE ( a.Executions, "
078: + " 0, "
079: + " 'N/A', "
080: + " ROUND ( a.Rows_Processed / a.Executions, "
081: + " 3 ) ), "
082: + " DECODE ( a.Rows_Processed, "
083: + " 0, "
084: + " 'N/A', "
085: + " ROUND ( a.Sorts / a.Rows_Processed, "
086: + " 3 ) ), "
087: + " DECODE ( a.Rows_Processed, "
088: + " 0, "
089: + " 'N/A', "
090: + " ROUND ( a.Disk_Reads / a.Rows_Processed, "
091: + " 3 ) ) , "
092: + " DECODE ( a.Rows_Processed, "
093: + " 0, "
094: + " 'N/A', "
095: + " ROUND ( a.Buffer_Gets / a.Rows_Processed, "
096: + " 3 ) ), "
097: + " a.Address || ':' || a.Hash_Value "
098: + " FROM v$sqlarea a, " + " sys.all_users b "
099: + " WHERE a.parsing_user_id = b.user_id ";
100:
101: /**
102: * Current session.
103: */
104: private ISession _session;
105:
106: private AutoWidthResizeTable _sgaTrace;
107: private boolean hasResized = false;
108: private Timer _refreshTimer = new Timer(true);
109:
110: private boolean _autoRefresh = false;
111: private int _refreshPeriod = 10;
112:
113: public class RefreshTimerTask extends TimerTask {
114: public void run() {
115: populateSGATrace();
116: }
117: }
118:
119: /**
120: * Ctor.
121: *
122: * @param autoRefeshPeriod
123: * @param session Current session.
124: * @throws IllegalArgumentException Thrown if a <TT>null</TT> <TT>ISession</TT> passed.
125: */
126: public SGATracePanel(ISession session, int autoRefeshPeriod) {
127: super ();
128: _session = session;
129: _refreshPeriod = autoRefeshPeriod;
130: createGUI();
131: }
132:
133: /**
134: * Current session.
135: */
136: public ISession getSession() {
137: return _session;
138: }
139:
140: private void resetTimer() {
141: if (_refreshTimer != null) {
142: _refreshTimer.cancel();
143: //Nil out the timer so that it can be gc'd
144: _refreshTimer = null;
145: }
146: if (_autoRefresh && (_refreshPeriod > 0)) {
147: _refreshTimer = new Timer(true);
148: _refreshTimer.scheduleAtFixedRate(new RefreshTimerTask(),
149: _refreshPeriod * 1000, _refreshPeriod * 1000);
150: }
151: }
152:
153: public void setAutoRefresh(boolean enable) {
154: if (enable != _autoRefresh) {
155: _autoRefresh = enable;
156: resetTimer();
157: }
158: }
159:
160: public boolean getAutoRefesh() {
161: return _autoRefresh;
162: }
163:
164: public void setAutoRefreshPeriod(int seconds) {
165: if (_refreshPeriod != seconds) {
166: _refreshPeriod = seconds;
167: resetTimer();
168: }
169: }
170:
171: public int getAutoRefreshPeriod() {
172: return _refreshPeriod;
173: }
174:
175: protected DefaultTableModel createTableModel() {
176: DefaultTableModel tm = new DefaultTableModel();
177: // i18n[oracle.sqlText=SQL Text]
178: tm.addColumn(s_stringMgr.getString("oracle.sqlText"));
179: // i18n[oracle.firstLoadTime=First Load Time]
180: tm.addColumn(s_stringMgr.getString("oracle.firstLoadTime"));
181: // i18n[oracle.parseSchema=Parse Schema]
182: tm.addColumn(s_stringMgr.getString("oracle.parseSchema"));
183: // i18n[oracle.parseCalla=Parse Calls]
184: tm.addColumn(s_stringMgr.getString("oracle.parseCalla"));
185: // i18n[oracle.execution=Executions]
186: tm.addColumn(s_stringMgr.getString("oracle.execution"));
187: // i18n[oracle.sorts=Sorts]
188: tm.addColumn(s_stringMgr.getString("oracle.sorts"));
189: // i18n[oracle.diskReads=Disk Reads]
190: tm.addColumn(s_stringMgr.getString("oracle.diskReads"));
191: // i18n[oracle.bufferGets=Buffer Gets]
192: tm.addColumn(s_stringMgr.getString("oracle.bufferGets"));
193: // i18n[oracle.rows=Rows]
194: tm.addColumn(s_stringMgr.getString("oracle.rows"));
195: // i18n[oracle.sortsPerExec=Sorts per Exec]
196: tm.addColumn(s_stringMgr.getString("oracle.sortsPerExec"));
197: // i18n[oracle.diskReadsPerExec=Disk Reads per Exec]
198: tm.addColumn(s_stringMgr.getString("oracle.diskReadsPerExec"));
199: // i18n[oracle.bufferPerExec=Buffer Gets per Exec]
200: tm.addColumn(s_stringMgr.getString("oracle.bufferPerExec"));
201: // i18n[oracle.rowsPerExec=Rows per Exec]
202: tm.addColumn(s_stringMgr.getString("oracle.rowsPerExec"));
203: // i18n[oracle.sortsPerExec=Sorts per Row]
204: tm.addColumn(s_stringMgr.getString("oracle.sortsPerExec"));
205: // i18n[oracle.diskReadsPerRow=Disk Reads per Row]
206: tm.addColumn(s_stringMgr.getString("oracle.diskReadsPerRow"));
207: // i18n[oracle.buffer.getsPerRow=Buffer Gets per Row]
208: tm.addColumn(s_stringMgr.getString("oracle.buffer.getsPerRow"));
209: return tm;
210: }
211:
212: public synchronized void populateSGATrace() {
213: if (!OraclePlugin.checkObjectAccessible(_session, sgaTraceSQL)) {
214: return;
215: }
216: try {
217: PreparedStatement s = _session.getSQLConnection()
218: .getConnection().prepareStatement(sgaTraceSQL);
219: if (s.execute()) {
220: ResultSet rs = s.getResultSet();
221: DefaultTableModel tm = createTableModel();
222: while (rs.next()) {
223: String sqlText = rs.getString(1);
224: String flt = rs.getString(2);
225: String schema = rs.getString(3);
226: String calls = rs.getString(4);
227: String executions = rs.getString(5);
228: String sorts = rs.getString(6);
229: String diskReads = rs.getString(7);
230: String bufGets = rs.getString(8);
231: String rows = rs.getString(9);
232: String sortsExec = rs.getString(10);
233: String diskReadsExec = rs.getString(11);
234: String rowsExec = rs.getString(12);
235: String sortsRows = rs.getString(13);
236: String diskReadsRow = rs.getString(14);
237: String bufGetsRow = rs.getString(15);
238:
239: //Should probably create my own table model but i am being a bit slack.
240: tm.addRow(new Object[] { sqlText, flt, schema,
241: calls, executions, sorts, diskReads,
242: bufGets, rows, sortsExec, diskReadsExec,
243: rowsExec, sortsRows, diskReadsRow,
244: bufGetsRow });
245: }
246: _sgaTrace.setModel(tm);
247: if (!hasResized) {
248: hasResized = true;
249: _sgaTrace.resizeColumnWidth(300);
250: }
251: }
252: } catch (SQLException ex) {
253: _session.showErrorMessage(ex);
254: }
255: }
256:
257: private void createGUI() {
258: final IApplication app = _session.getApplication();
259: setLayout(new BorderLayout());
260: _sgaTrace = new AutoWidthResizeTable(new DefaultTableModel());
261: _sgaTrace.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
262: add(new JScrollPane(_sgaTrace));
263:
264: populateSGATrace();
265: }
266:
267: }
|