[go: up one dir, main page]

File: tosgatrace.cpp

package info (click to toggle)
tora 1.3.16-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,604 kB
  • ctags: 10,426
  • sloc: cpp: 100,338; sh: 5,034; perl: 1,482; makefile: 566; xml: 69
file content (395 lines) | stat: -rw-r--r-- 13,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*****
 *
 * TOra - An Oracle Toolkit for DBA's and developers
 * Copyright (C) 2003-2005 Quest Software, Inc
 * Portions Copyright (C) 2005 Other Contributors
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation;  only version 2 of
 * the License is valid for this program.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *      As a special exception, you have permission to link this program
 *      with the Oracle Client libraries and distribute executables, as long
 *      as you follow the requirements of the GNU GPL in regard to all of the
 *      software in the executable aside from Oracle client libraries.
 *
 *      Specifically you are not permitted to link this program with the
 *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 *      And you are not permitted to distribute binaries compiled against
 *      these libraries without written consent from Quest Software, Inc.
 *      Observe that this does not disallow linking to the Qt Free Edition.
 *
 *      You may link this product with any GPL'd Qt library such as Qt/Free
 *
 * All trademarks belong to their respective owners.
 *
 *****/

#include "tochangeconnection.h"
#include "toconf.h"
#include "toconnection.h"
#include "tomain.h"
#include "toresultcombo.h"
#include "toresultlong.h"
#include "toresultresources.h"
#include "toresultview.h"
#include "tosgastatement.h"
#include "tosgatrace.h"
#include "tosql.h"
#include "totool.h"

#ifdef TO_KDE
#  include <kmenubar.h>
#endif

#include <qcheckbox.h>
#include <qcombobox.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qmainwindow.h>
#include <qmenubar.h>
#include <qmessagebox.h>
#include <qpixmap.h>
#include <qpopupmenu.h>
#include <qsplitter.h>
#include <qtabwidget.h>
#include <qtimer.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qtooltip.h>
#include <qworkspace.h>

#include "tosgatrace.moc"

#include "icons/refresh.xpm"
#include "icons/tosgatrace.xpm"

#define CONF_AUTO_UPDATE    "AutoUpdate"

class toSGATracePrefs : public QGroupBox, public toSettingTab
{ 
  QCheckBox* AutoUpdate;
  toTool *Tool;

public:
  toSGATracePrefs(toTool *tool,QWidget* parent = 0,const char* name = 0)
    : QGroupBox(1,Horizontal,parent,name),toSettingTab("trace.html"),Tool(tool)
  {
    setTitle(qApp->translate("toSGATracePrefs","SGA Trace"));

    AutoUpdate = new QCheckBox(this,"AutoRefresh");
    AutoUpdate->setText(qApp->translate("toSGATracePrefs","&Auto update"));
    QToolTip::add(AutoUpdate,qApp->translate("toSGATracePrefs","Update automatically after change of schema."));

    if (!Tool->config(CONF_AUTO_UPDATE,"Yes").isEmpty())
      AutoUpdate->setChecked(true);
  }
  virtual void saveSetting(void)
  {
    if (AutoUpdate->isChecked())
      Tool->setConfig(CONF_AUTO_UPDATE,"Yes");
    else
      Tool->setConfig(CONF_AUTO_UPDATE,"");
  }
};

class toSGATraceTool : public toTool {
protected:
  virtual const char **pictureXPM(void)
  { return tosgatrace_xpm; }
public:
  toSGATraceTool()
    : toTool(230,"SGA Trace")
  { }
  virtual const char *menuItem()
  { return "SGA Trace"; }
  virtual QWidget *toolWindow(QWidget *parent,toConnection &connection)
  {
    return new toSGATrace(parent,connection);
  }
  virtual QWidget *configurationTab(QWidget *parent)
  { return new toSGATracePrefs(this,parent); }
};

static toSGATraceTool SGATraceTool;

toSGATrace::toSGATrace(QWidget *main,toConnection &connection)
  : toToolWidget(SGATraceTool,"trace.html",main,connection)
{
  QToolBar *toolbar=toAllocBar(this,tr("SGA trace"));

  new QToolButton(QPixmap((const char **)refresh_xpm),
		  tr("Fetch statements in SGA"),
		  tr("Fetch statements in SGA"),
		  this,SLOT(refresh(void)),
		  toolbar);
  toolbar->addSeparator();
  new QLabel(tr("Schema")+" ",toolbar,TO_KDE_TOOLBAR_WIDGET);
  Schema=new toResultCombo(toolbar);
  Schema->additionalItem(tr("Any"));
  Schema->setSelected(connection.user().upper());
  Schema->query(toSQL::sql(toSQL::TOSQL_USERLIST));

  connect(Schema,SIGNAL(activated(const QString &)),this,SLOT(changeSchema(const QString &)));

  toolbar->addSeparator();
  new QLabel(tr("Refresh")+" ",toolbar,TO_KDE_TOOLBAR_WIDGET);
  connect(Refresh=toRefreshCreate(toolbar,TO_KDE_TOOLBAR_WIDGET),
	  SIGNAL(activated(const QString &)),this,SLOT(changeRefresh(const QString &)));

  toolbar->addSeparator();
  new QLabel(tr("Type")+" ",toolbar,TO_KDE_TOOLBAR_WIDGET);
  Type=new QComboBox(toolbar,TO_KDE_TOOLBAR_WIDGET);
  Type->insertItem(tr("SGA"));
  Type->insertItem(tr("Long operations"));

  toolbar->addSeparator();
  new QLabel(tr("Selection")+" ",toolbar,TO_KDE_TOOLBAR_WIDGET);
  Limit=new QComboBox(toolbar,TO_KDE_TOOLBAR_WIDGET);
  Limit->insertItem(tr("All"));
  Limit->insertItem(tr("Unfinished"));
  Limit->insertItem(tr("1 execution, 1 parse"));
  Limit->insertItem(tr("Top executions"));
  Limit->insertItem(tr("Top sorts"));
  Limit->insertItem(tr("Top diskreads"));
  Limit->insertItem(tr("Top buffergets"));
  Limit->insertItem(tr("Top rows"));
  Limit->insertItem(tr("Top sorts/exec"));
  Limit->insertItem(tr("Top diskreads/exec"));
  Limit->insertItem(tr("Top buffergets/exec"));
  Limit->insertItem(tr("Top rows/exec"));
  Limit->insertItem(tr("Top buffers/row"));

  toolbar->setStretchableWidget(new QLabel(toolbar,TO_KDE_TOOLBAR_WIDGET));
  new toChangeConnection(toolbar,TO_KDE_TOOLBAR_WIDGET);

  QSplitter *splitter=new QSplitter(Vertical,this);

  Trace=new toResultLong(false,false,toQuery::Background,splitter);
  Trace->setReadAll(true);
  Statement=new toSGAStatement(splitter);

  Trace->setSelectionMode(QListView::Single);
  connect(Trace,SIGNAL(selectionChanged(QListViewItem *)),
	  this,SLOT(changeItem(QListViewItem *)));
  CurrentSchema=connection.user().upper();
  updateSchemas();


  ToolMenu=NULL;
  connect(toMainWidget()->workspace(),SIGNAL(windowActivated(QWidget *)),
	  this,SLOT(windowActivated(QWidget *)));

  try {
    connect(timer(),SIGNAL(timeout(void)),this,SLOT(refresh(void)));
    toRefreshParse(timer(),toTool::globalConfig(CONF_REFRESH,DEFAULT_REFRESH));
  } TOCATCH

  setFocusProxy(Trace);
}

void toSGATrace::windowActivated(QWidget *widget)
{
  if (widget==this) {
    if (!ToolMenu) {
      ToolMenu=new QPopupMenu(this);
      ToolMenu->insertItem(QPixmap((const char **)refresh_xpm),tr("&Refresh"),
			   this,SLOT(refresh(void)),
			   toKeySequence(tr("F5", "SGA Trace|Refresh")));
      ToolMenu->insertSeparator();
      ToolMenu->insertItem(tr("Change &schema"),
			   Schema,SLOT(setFocus()),
			   toKeySequence(tr("Alt+S", "SGA Trace|Change schema")));
      ToolMenu->insertItem(tr("Change &refresh"),
			   Refresh,SLOT(setFocus(void)),
			   toKeySequence(tr("Alt+R", "SGA Trace|Change refresh")));
      ToolMenu->insertItem(tr("Change t&ype"),
			   Type,SLOT(setFocus()),
			   toKeySequence(tr("Alt+Y", "SGA Trace|Change type")));
      toMainWidget()->menuBar()->insertItem(tr("&SGA Trace"),ToolMenu,-1,toToolMenuIndex());
    }
  } else {
    delete ToolMenu;
    ToolMenu=NULL;
  }
}

void toSGATrace::changeRefresh(const QString &str)
{
  try {
    toRefreshParse(timer(),str);
  } TOCATCH
}

#define LARGE_BUFFER 4096

void toSGATrace::changeSchema(const QString &str)
{
  if (str!=tr("Any"))
    CurrentSchema=str;
  else
    CurrentSchema=QString::null;
  if (!SGATraceTool.config(CONF_AUTO_UPDATE,"Yes").isEmpty())
    refresh();
}

static toSQL SQLSGATrace("toSGATrace:SGATrace",
			 "SELECT a.SQL_Text \"SQL Text\",\n"
			 "       a.First_Load_Time \"First Load Time\",\n"
			 "       b.username \"Parsing Schema\",\n"
			 "       a.Parse_Calls \"Parse Calls\",\n"
			 "       a.Executions \"Executions\",\n"
			 "       a.Sorts \"Sorts\",\n"
			 "       a.Disk_Reads \"Disk Reads\",\n"
			 "       a.Buffer_Gets \"Buffer Gets\",\n"
			 "       a.Rows_Processed \"Rows\",\n"
			 "       DECODE(a.Executions,0,'N/A',ROUND(a.Sorts/a.Executions,3)) \"Sorts/Exec\",\n"
			 "       DECODE(a.Executions,0,'N/A',ROUND(a.Disk_Reads/a.Executions,3)) \"Disk/Exec\",\n"
			 "       DECODE(a.Executions,0,'N/A',ROUND(a.Buffer_Gets/a.Executions,3)) \"Buffers/Exec\",\n"
			 "       DECODE(a.Executions,0,'N/A',ROUND(a.Rows_Processed/a.Executions,3)) \"Rows/Exec\",\n"
			 "       DECODE(a.Rows_Processed,0,'N/A',ROUND(a.Sorts/a.Rows_Processed,3)) \"Sorts/Rows\",\n"
			 "       DECODE(a.Rows_Processed,0,'N/A',ROUND(a.Disk_Reads/a.Rows_Processed,3)) \"Disk/Rows\",\n"
			 "       DECODE(a.Rows_Processed,0,'N/A',ROUND(a.Buffer_Gets/a.Rows_Processed,3)) \"Buffers/Rows\",\n"
			 "       a.Address||':'||a.Hash_Value \" \"\n"
			 "  from v$sqlarea a,\n"
			 "       sys.all_users b\n"
			 " where a.parsing_user_id = b.user_id",
			 "Display the contents of the SGA stack. Must have one hidden column "
			 "with SGA address at the end and a table name 'b' with a column username and must accept \"and ...\" clauses at end.");

static toSQL SQLLongOps(TOSQL_LONGOPS,
			"SELECT b.opname \"Type\",\n"
			"       a.SQL_Text \"SQL Text\",\n"
			"       b.start_time \"Start Time\",\n"
			"       b.sofar||'/'||b.totalwork \"Completed/Total\",\n"
			"       b.elapsed_seconds||'/'||(b.elapsed_seconds+b.time_remaining) \"Completed/Total Time\",\n"
			"       b.username \"Parsing Schema\",\n"
			"       a.Parse_Calls \"Parse Calls\",\n"
			"       a.Executions \"Executions\",\n"
			"       a.Sorts \"Sorts\",\n"
			"       a.Disk_Reads \"Disk Reads\",\n"
			"       a.Buffer_Gets \"Buffer Gets\",\n"
			"       a.Rows_Processed \"Rows\",\n"
			"       DECODE(a.Executions,0,'N/A',ROUND(a.Sorts/a.Executions,3)) \"Sorts/Exec\",\n"
			"       DECODE(a.Executions,0,'N/A',ROUND(a.Disk_Reads/a.Executions,3)) \"Disk/Exec\",\n"
			"       DECODE(a.Executions,0,'N/A',ROUND(a.Buffer_Gets/a.Executions,3)) \"Buffers/Exec\",\n"
			"       DECODE(a.Executions,0,'N/A',ROUND(a.Rows_Processed/a.Executions,3)) \"Rows/Exec\",\n"
			"       DECODE(a.Rows_Processed,0,'N/A',ROUND(a.Sorts/a.Rows_Processed,3)) \"Sorts/Rows\",\n"
			"       DECODE(a.Rows_Processed,0,'N/A',ROUND(a.Disk_Reads/a.Rows_Processed,3)) \"Disk/Rows\",\n"
			"       DECODE(a.Rows_Processed,0,'N/A',ROUND(a.Buffer_Gets/a.Rows_Processed,3)) \"Buffers/Rows\",\n"
			"       b.SQL_Address||':'||b.SQL_Hash_Value \" \"\n"
			"  from v$sqlarea a,\n"
			"       v$session_longops b\n"
			" where b.sql_address = a.address(+)\n"
			"   and b.sql_hash_value = a.hash_value(+)\n"
			"   and b.opname is not null",
			"Display the contents of long the long operations list. Must have a hidden column "
			"with SGA address at the end and a table name 'b' with a column username and sid "
			"and must accept \"and ...\" clauses at end.");

void toSGATrace::refresh(void)
{
  try {
    updateSchemas();

    QString select;
    switch(Type->currentItem()) {
    case 0:
      select=toSQL::string(SQLSGATrace,connection());
      break;
    case 1:
      select=toSQL::string(SQLLongOps,connection());
      break;
    default:
      toStatusMessage(tr("Unknown type of trace"));
      return;
    }
    if (!CurrentSchema.isEmpty())
      select.append(QString::fromLatin1("\n   and b.username = :f1<char[101]>"));

    QString order;
    switch (Limit->currentItem()) {
    case 0:
      break;
    case 1:
      if (Type->currentItem()==1)
	select+=QString::fromLatin1("\n   and b.sofar != b.totalwork");
      else
	toStatusMessage(tr("Unfinished is only available for long operations"));
      break;
    case 2:
      select+=QString::fromLatin1("\n   and a.executions = 1 and a.parse = 1");
      break;
    case 3:
      order=QString::fromLatin1("a.Executions");
      break;
    case 4:
      order=QString::fromLatin1("a.Sorts");
      break;
    case 5:
      order=QString::fromLatin1("a.Disk_Reads");
      break;
    case 6:
      order=QString::fromLatin1("a.Buffer_Gets");
      break;
    case 7:
      order=QString::fromLatin1("a.Rows_Processed");
      break;
    case 8:
      order=QString::fromLatin1("DECODE(a.Executions,0,0,a.Sorts/a.Executions)");
      break;
    case 9:
      order=QString::fromLatin1("DECODE(a.Executions,0,0,a.Disk_Reads/a.Executions)");
      break;
    case 10:
      order=QString::fromLatin1("DECODE(a.Executions,0,0,a.Buffer_Gets/a.Executions)");
      break;
    case 11:
      order=QString::fromLatin1("DECODE(a.Executions,0,0,a.Rows_Processed/a.Executions)");
      break;
    case 12:
      order=QString::fromLatin1("DECODE(a.Rows_Processed,0,0,a.Buffer_Gets/a.Rows_Processed)");
      break;
    default:
      toStatusMessage(tr("Unknown selection"));
      break;
    }

    if (!order.isEmpty())
      select=QString::fromLatin1("SELECT * FROM (\n")+select+
				QString::fromLatin1("\n ORDER BY "+order+" DESC)\n WHERE ROWNUM < 20");

    Trace->setSQL(QString::null);
    if (!CurrentSchema.isEmpty()) {
      toQList p;
      p.insert(p.end(),CurrentSchema);
      Trace->query(select,p);
    } else
      Trace->query(select);

    Statement->refresh();
  } TOCATCH
}

void toSGATrace::updateSchemas(void)
{
  try {
    Schema->refresh();
  } TOCATCH
}

void toSGATrace::changeItem(QListViewItem *item)
{
  if (item)
    Statement->changeAddress(item->text(Trace->columns()));
}