Source for file adodb-perf.inc.php
Documentation is available at adodb-perf.inc.php
V4.97 22 Jan 2008 (c) 2000-2008 John Lim (jlim#natsoft.com.my). All rights reserved.
Released under both BSD license and Lesser GPL library license.
Whenever there is any discrepancy between the two licenses,
the BSD license will take precedence. See License.txt.
Set tabs to 4 for best viewing.
Latest version is available at http://adodb.sourceforge.net
Library for basic performance monitoring and tuning.
My apologies if you see code mixed with presentation. The presentation suits
my needs. If you want to separate code from presentation, be my guest. Patches
if (!defined('ADODB_DIR')) include_once(dirname(__FILE__ ). '/adodb.inc.php');
include_once(ADODB_DIR. '/tohtml.inc.php');
$ADODB_PERF_MIN = 0.05; // log only if >= minimum number of secs to run
// returns in K the memory of current process, or 0 if not known
exec('tasklist /FI "PID eq ' . $pid. '" /FO LIST', $output);
exec("ps --pid $pid --no-headers -o%mem,size", $output);
if (sizeof($output) == 0) return 0;
if (sizeof($memarr)>= 2) return (integer) $memarr[1];
// avoids localization problems where , is used instead of .
/* return microtime value as a float */
return (float) $t[1]+ (float) $t[0];
$connx->fnExecute = false;
$rs = & $connx->Execute($sql,$inputarr);
if (!empty($connx->_logsql) && (empty($connx->_logsqlErrors) || !$rs)) {
if (!empty($ADODB_LOG_CONN)) {
$conn = &$ADODB_LOG_CONN;
if ($conn->databaseType != $connx->databaseType)
$prefix = '/*dbx='. $connx->databaseType . '*/ ';
$conn->_logsql = false; // disable logsql error simulation
$dbT = $conn->databaseType;
$a0 = (float) $a0[1]+(float) $a0[0];
$a1 = (float) $a1[1]+(float) $a1[0];
$errM = $connx->ErrorMsg();
$errN = $connx->ErrorNo();
if (!is_object($rs) || $rs->dataProvider == 'empty')
$conn->_affected = $conn->affected_rows(true);
$conn->lastInsID = @$conn->Insert_ID();
if (isset ($_SERVER['HTTP_HOST'])) {
$tracer .= '<br>'. $_SERVER['HTTP_HOST'];
if (isset ($_SERVER['PHP_SELF'])) $tracer .= $_SERVER['PHP_SELF'];
if (isset ($_SERVER['PHP_SELF'])) $tracer .= '<br>'. $_SERVER['PHP_SELF'];
//$tracer .= (string) adodb_backtrace(false);
$tracer = (string) substr($tracer,0,500);
// Quote string parameters so we can see them in the
// performance stats. This helps spot disabled indexes.
foreach ($xar_params as $xar_param_key => $xar_param) {
if (gettype($xar_param) == 'string')
$xar_params[$xar_param_key] = '"' . $xar_param . '"';
$params = implode(', ', $xar_params);
if (strlen($params) >= 3000) $params = substr($params, 0, 3000);
if ($prefix) $sql = $prefix. $sql;
$d = $conn->sysTimeStamp;
if (empty($d)) $d = date("'Y-m-d H:i:s'");
if ($conn->dataProvider == 'oci8' && $dbT != 'oci8po') {
$isql = "insert into $perf_table values($d,:b,:c,:d,:e,:f)";
} else if ($dbT == 'odbc_mssql' || $dbT == 'informix' || strncmp($dbT,'odbtp',4)== 0) {
if ($dbT == 'informix') $sql2 = substr($sql2,0,230);
$sql1 = $conn->qstr($arr['b']);
$sql2 = $conn->qstr($arr['c']);
$params = $conn->qstr($arr['d']);
$tracer = $conn->qstr($arr['e']);
$isql = "insert into $perf_table (created,sql0,sql1,params,tracer,timer) values($d,$sql1,$sql2,$params,$tracer,$timer)";
if ($dbT == 'db2') $arr['f'] = (float) $arr['f'];
$isql = "insert into $perf_table (created,sql0,sql1,params,tracer,timer) values( $d,?,?,?,?,?)";
if ($errN != 0 || $time >= $ADODB_PERF_MIN) {
$ok = $conn->Execute($isql,$arr);
$err2 = $conn->ErrorMsg();
$conn->_logsql = true; // enable logsql error simulation
if ($perf->CreateLogTable()) $ok = $conn->Execute($isql,$arr);
$ok = $conn->Execute("create table $perf_table (
$connx->_errorMsg = $errM;
$connx->_errorCode = $errN;
$connx->fnExecute = 'adodb_log_sql';
The settings data structure is an associative array that database parameter per element.
Each database parameter element in the array is itself an array consisting of:
0: category code, used to group related db parameters
a. sql string to retrieve value, eg. "select value from v\$parameter where name='db_block_size'",
b. array holding sql string and field to look for, e.g. array('show variables','table_cache'),
c. a string prefixed by =, then a PHP method of the class is invoked,
e.g. to invoke $this->GetIndexValue(), set this array element to '=GetIndexValue',
2: description of the database parameter
var $table = '<table border=1 bgcolor=white>';
var $titles = '<tr><td><b>Parameter</b></td><td><b>Value</b></td><td><b>Description</b></td></tr>';
var $sql1 = 'sql1'; // used for casting sql1 to text for mssql
var $helpurl = "<a href=http://phplens.com/adodb/reference.functions.fnexecute.and.fncacheexecute.properties.html#logsql>LogSQL help</a>";
// Sets the tablename to be used
function table($newtable = false)
if (!empty($newtable)) $_table = $newtable;
if (empty($_table)) $_table = 'adodb_logsql';
// returns array with info to calculate CPU Load
cpu 524152 2662 2515228 336057010
cpu0 264339 1408 1257951 168025827
cpu1 259813 1254 1257277 168031181
intr 890153570 868093576 6 0 4 4 0 6 1 2 0 0 0 124 0 8098760 2 13961053 0 0 0 0 0 0 0 0 0 0 0 0 0 16 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
disk_io: (3,0):(3144904,54369,610378,3090535,50936192) (3,1):(3630212,54097,633016,3576115,50951320)
// Algorithm is taken from
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__obtaining_raw_performance_data.asp
if (PHP_VERSION == '5.0.0') return false;
if (PHP_VERSION == '5.0.1') return false;
if (PHP_VERSION == '5.0.2') return false;
if (PHP_VERSION == '5.0.3') return false;
if (PHP_VERSION == '4.3.10') return false; # see http://bugs.php.net/bug.php?id=31737
@$c = new COM("WinMgmts:{impersonationLevel=impersonate}!Win32_PerfRawData_PerfOS_Processor.Name='_Total'");
$info[0] = $c->PercentProcessorTime;
$info[3] = $c->TimeStamp_Sys100NS;
// Algorithm - Steve Blinch (BlitzAffe Online, http://www.blitzaffe.com)
$statfile = '/proc/stat';
$fd = fopen($statfile,"r");
foreach($statinfo as $line) {
if(!$info[0]) array_shift($info); // pop off blank space (if any)
total: used: free: shared: buffers: cached:
Mem: 1055289344 917299200 137990144 0 165437440 599773184
Swap: 2146775040 11055104 2135719936
Remember that this is client load, not db server load!
if (!$info) return false;
$d_user = $info[0] - $last[0];
$d_nice = $info[1] - $last[1];
$d_system = $info[2] - $last[2];
$d_idle = $info[3] - $last[3];
//printf("Delta - User: %f Nice: %f System: %f Idle: %f<br>",$d_user,$d_nice,$d_system,$d_idle);
if ($d_idle < 1) $d_idle = 1;
return 100* (1- $d_user/ $d_idle);
$total= $d_user+ $d_nice+ $d_system+ $d_idle;
return 100* ($d_user+ $d_nice+ $d_system)/ $total;
$saveE = $this->conn->fnExecute;
$this->conn->fnExecute = false;
global $ADODB_FETCH_MODE;
$save = $ADODB_FETCH_MODE;
if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
$sqlq = $this->conn->qstr($sql);
$arr = $this->conn->GetArray(
from $perf_table where sql1=$sqlq
$s .= '<h3>Scripts Affected</h3>';
if (isset ($savem)) $this->conn->SetFetchMode($savem);
$ADODB_CACHE_MODE = $save;
$this->conn->fnExecute = $saveE;
If only a snippet of the $sql is passed in, then $partial will hold the crc32 of the
function Explain($sql,$partial= false)
if (isset ($_GET['sql'])) return;
$s = '<h3>Invalid SQL</h3>';
$saveE = $this->conn->fnExecute;
$this->conn->fnExecute = false;
$rs = & $this->conn->SelectLimit("select distinct count(*),sql1,tracer as error_msg from $perf_table where tracer like 'ERROR:%' group by sql1,tracer order by 1 desc",$numsql);//,$numsql);
$this->conn->fnExecute = $saveE;
$s .= rs2html($rs,false,false,false,false);
return "<p>$this->helpurl. ". $this->conn->ErrorMsg(). "</p>";
This script identifies the longest running SQL
global $ADODB_FETCH_MODE;
$saveE = $this->conn->fnExecute;
$this->conn->fnExecute = false;
if (isset ($_GET['exps']) && isset ($_GET['sql'])) {
$partial = !empty($_GET['part']);
echo "<a name=explain></a>". $this->Explain($_GET['sql'],$partial). "\n";
if (isset ($_GET['sql'])) return;
$save = $ADODB_FETCH_MODE;
if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
$rs = & $this->conn->SelectLimit(
"select avg(timer) as avg_timer,$sql1,count(*),max(timer) as max_timer,min(timer) as min_timer
where {$this->conn->upperCase}({ $this->conn->substr}(sql0,1,5)) not in ('DROP ','INSER','COMMI','CREAT')
and (tracer is null or tracer not like 'ERROR:%')
order by 1 desc",$numsql);
if (isset ($savem)) $this->conn->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save;
$this->conn->fnExecute = $saveE;
if (!$rs) return "<p>$this->helpurl. ". $this->conn->ErrorMsg(). "</p>";
$s = "<h3>Suspicious SQL</h3>
<font size=1>The following SQL have high average execution times</font><br>
<table border=1 bgcolor=white><tr><td><b>Avg Time</b><td><b>Count</b><td><b>SQL</b><td><b>Max</b><td><b>Min</b></tr>\n";
$sql2 = substr($sql,0,$max- 500);
$prefix = "<a target=sql". rand(). " href=\"?hidem=1&exps=1&sql=". $raw. "&x#explain\">";
$suffix = ' ... <i>String too long for GET parameter: '. strlen($prefix). '</i>';
$s .= "<tr><td>". adodb_round($rs->fields[0],6). "<td align=right>". $rs->fields[2]. "<td><font size=-1>". $prefix. htmlspecialchars($sql). $suffix. "</font>".
"<td>". $rs->fields[3]. "<td>". $rs->fields[4]. "</tr>";
This reports the percentage of load on the instance due to the most
expensive few SQL statements. Tuning these statements can often
make huge improvements in overall system performance.
global $ADODB_FETCH_MODE;
$saveE = $this->conn->fnExecute;
$this->conn->fnExecute = false;
if (isset ($_GET['expe']) && isset ($_GET['sql'])) {
$partial = !empty($_GET['part']);
echo "<a name=explain></a>". $this->Explain($_GET['sql'],$partial). "\n";
if (isset ($_GET['sql'])) return;
$save = $ADODB_FETCH_MODE;
if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
$rs = & $this->conn->SelectLimit(
"select sum(timer) as total,$sql1,count(*),max(timer) as max_timer,min(timer) as min_timer
where {$this->conn->upperCase}({ $this->conn->substr}(sql0,1,5)) not in ('DROP ','INSER','COMMI','CREAT')
and (tracer is null or tracer not like 'ERROR:%')
order by 1 desc",$numsql);
if (isset ($savem)) $this->conn->SetFetchMode($savem);
$this->conn->fnExecute = $saveE;
$ADODB_FETCH_MODE = $save;
if (!$rs) return "<p>$this->helpurl. ". $this->conn->ErrorMsg(). "</p>";
$s = "<h3>Expensive SQL</h3>
<font size=1>Tuning the following SQL could reduce the server load substantially</font><br>
<table border=1 bgcolor=white><tr><td><b>Load</b><td><b>Count</b><td><b>SQL</b><td><b>Max</b><td><b>Min</b></tr>\n";
$sql2 = substr($sql,0,$max- 500);
$prefix = "<a target=sqle". rand(). " href=\"?hidem=1&expe=1&sql=". $raw. "&x#explain\">";
$s .= "<tr><td>". adodb_round($rs->fields[0],6). "<td align=right>". $rs->fields[2]. "<td><font size=-1>". $prefix. htmlspecialchars($sql). $suffix. "</font>".
"<td>". $rs->fields[3]. "<td>". $rs->fields[4]. "</tr>";
Raw function to return parameter value from $settings.
if (empty($this->settings[$param])) return false;
$sql = $this->settings[$param][1];
Raw function returning array of poll paramters
$arr[0] = (float) $this->DBParameter('data cache hit ratio');
$arr[3] = (integer) $this->DBParameter('current connections');
Low-level Get Database Parameter
$savelog = $this->conn->LogSQL(false);
global $ADODB_FETCH_MODE;
if (sizeof($sql)> 2) $pos = $sql[2];
if (sizeof($sql)> 3) $coef = $sql[3];
$save = $ADODB_FETCH_MODE;
if ($this->conn->fetchMode !== false) $savem = $this->conn->SetFetchMode(false);
$rs = $this->conn->Execute($sql1);
if (isset ($savem)) $this->conn->SetFetchMode($savem);
$ADODB_FETCH_MODE = $save;
$keyf = reset($rs->fields);
if (trim($keyf) == $key) {
$ret = $rs->fields[$pos];
if ($coef) $ret *= $coef;
$this->conn->LogSQL($savelog);
$ret = $this->conn->GetOne($sql);
$this->conn->LogSQL($savelog);
Warn if cache ratio falls below threshold. Displayed in "Description" column.
return '<font color=red><b>Cache ratio should be at least '. $this->warnRatio. '%</b></font>';
$this->conn->Execute("delete from $perf_table where created<". $this->conn->sysTimeStamp);
/***********************************************************************************************/
// HIGH LEVEL UI FUNCTIONS
/***********************************************************************************************/
if ($conn->host && $conn->database) $app .= ', db=';
$savelog = $this->conn->LogSQL(false);
$info = $conn->ServerInfo();
if (isset ($_GET['clearsql'])) {
$this->conn->LogSQL($savelog);
$_GET['sql'] = $_GET['sql'] = str_replace(array("\\'",'\"'),array("'",'"'),$_GET['sql']);
if (!isset ($_SESSION['ADODB_PERF_SQL'])) $nsql = $_SESSION['ADODB_PERF_SQL'] = 10;
else $nsql = $_SESSION['ADODB_PERF_SQL'];
$app .= $info['description'];
if (isset ($_GET['do'])) $do = $_GET['do'];
else if (isset ($_POST['do'])) $do = $_POST['do'];
else if (isset ($_GET['sql'])) $do = 'viewsql';
if (isset ($_GET['nsql'])) {
if ($_GET['nsql'] > 0) $nsql = $_SESSION['ADODB_PERF_SQL'] = (integer) $_GET['nsql'];
echo "<title>ADOdb Performance Monitor on $app</title><body bgcolor=white>";
if ($do == 'viewsql') $form = "<td><form># SQL:<input type=hidden value=viewsql name=do> <input type=text size=4 name=nsql value=$nsql><input type=submit value=Go></td></form>";
else $form = "<td> </td>";
$allowsql = !defined('ADODB_PERF_NO_RUN_SQL');
$app .= " (Min sql timing \$ADODB_PERF_MIN=$ADODB_PERF_MIN secs)";
if (empty($_GET['hidem']))
echo "<table border=1 width=100% bgcolor=lightyellow><tr><td colspan=2>
<b><a href=http://adodb.sourceforge.net/?perf=1>ADOdb</a> Performance Monitor</b> <font size=1>for $app</font></tr><tr><td>
<a href=?do=stats><b>Performance Stats</b></a> <a href=?do=viewsql><b>View SQL</b></a>
<a href=?do=tables><b>View Tables</b></a> <a href=?do=poll><b>Poll Stats</b></a>",
$allowsql ? ' <a href=?do=dosql><b>Run SQL</b></a>' : '',
if (empty($ADODB_LOG_CONN))
echo "<p> <a href=\"?do=viewsql&clearsql=1\">Clear SQL Log</a><br>";
echo "<iframe width=720 height=80%
src=\"{$_SERVER['PHP_SELF']}?do=poll2&hidem=1\"></iframe>";
if (empty($_GET['hidem']))
echo " <a href=\"?do=viewsql&clearsql=1\">Clear SQL Log</a><br>";
echo "<p><div align=center><font size=1>$ADODB_vers Sponsored by <a href=http://phplens.com/>phpLens</a></font></div>";
Runs in infinite loop, returning real-time statistics
$this->conn->fnExecute = false;
if ($secs <= 1) $secs = 1;
echo "Accumulating statistics, every $secs seconds...\n";flush();
$reads = sprintf('%12.4f',($arr[1]- $arro[1])/ $secs);
$writes = sprintf('%12.4f',($arr[2]- $arro[2])/ $secs);
$osval = sprintf(" %2.1f ",(float) $load);
if ($cnt % 10 == 0) echo " Time ". $oslabel. " Hit% Sess Reads/s Writes/s\n";
echo date('H:i:s'). ' '. $osval. "$hits $sess $reads $writes\n";
Returns basic health check in a command line interface
Returns basic health check as HTML
$saveE = $this->conn->fnExecute;
$this->conn->fnExecute = false;
else $html = $this->table. '<tr><td colspan=3><h3>'. $this->conn->databaseType. '</h3></td></tr>'. $this->titles;
foreach($this->settings as $name => $arr) {
if ($arr === false) break;
if ($cli) $html .= " -- $arr -- \n";
else $html .= "<tr bgcolor=$this->color><td colspan=3><i>$arr</i> </td></tr>";
if (sizeof($arr)> 2) $desc = $arr[2];
if ($category == 'HIDE') continue;
if ($desc && strncmp($desc,"=",1) === 0) {
$desc = $this->$fn($val);
$m = $this->conn->ErrorMsg();
if ($val % (1024* 1024) == 0) {
} else if ($val % 1024 == 0) {
//$val = htmlspecialchars($val);
if ($category != $oldc) {
//$bgc = ($bgc == ' bgcolor='.$this->color) ? ' bgcolor=white' : ' bgcolor='.$this->color;
if (strlen($desc)== 0) $desc = ' ';
if (strlen($val)== 0) $val = ' ';
$html .= "<tr$bgc><td>". $name. '</td><td>'. $val. '</td><td>'. $desc. "</td></tr>\n";
if (!$cli) $html .= "</table>\n";
$this->conn->fnExecute = $saveE;
$savelog = $this->conn->LogSQL(false);
$rs = $this->conn->Execute($this->tablesSQL. ' order by '. $orderby);
$this->conn->LogSQL($savelog);
$html = rs2html($rs,false,false,false,false);
$savelog = $this->conn->LogSQL(false);
$ok = $this->conn->Execute($sql);
$this->conn->LogSQL($savelog);
return ($ok) ? true : false;
$PHP_SELF = $_SERVER['PHP_SELF'];
$sql = isset ($_REQUEST['sql']) ? $_REQUEST['sql'] : '';
if (isset ($_SESSION['phplens_sqlrows'])) $rows = $_SESSION['phplens_sqlrows'];
if (isset ($_REQUEST['SMALLER'])) {
if ($rows < 3) $rows = 3;
$_SESSION['phplens_sqlrows'] = $rows;
if (isset ($_REQUEST['BIGGER'])) {
$_SESSION['phplens_sqlrows'] = $rows;
<form method="POST" action=" <?php echo $PHP_SELF ?>">
<td> Form size: <input type="submit" value=" < " name="SMALLER"><input type="submit" value=" > > " name="BIGGER">
<input type="submit" value=" Run SQL Below " name="RUN"><input type=hidden name=do value=dosql>
<td colspan=2><textarea rows= <?php print $rows; ?> name="sql" cols="80"> <?php print htmlspecialchars($sql) ?></textarea>
if (!isset ($_REQUEST['sql'])) return;
foreach($sqla as $sqls) {
$savelog = $this->conn->LogSQL(false);
$rs = $this->conn->Execute($sqls);
$this->conn->LogSQL($savelog);
while ($rs->NextRecordSet()) {
print "<table width=98% bgcolor=#C0C0FF><tr><td> </td></tr></table>";
$e1 = (integer) $this->conn->ErrorNo();
$e2 = $this->conn->ErrorMsg();
if (empty($e1)) $e1 = '-1'; // postgresql fix
print ' '. $e1. ': '. $e2;
print "<p>No Recordset returned<br></p>";
/************************************************************************/
* Reorganise multiple table-indices/statistics/..
* OptimizeMode could be given by last Parameter
* optimizeTables( 'tableA');
* optimizeTables( 'tableA', 'tableB', 'tableC');
* optimizeTables( 'tableA', 'tableB', ADODB_OPT_LOW);
* @param string table name of the table to optimize
* @param int mode optimization-mode
* <code>ADODB_OPT_HIGH</code> for full optimization
* <code>ADODB_OPT_LOW</code> for CPU-less optimization
* Default is LOW <code>ADODB_OPT_LOW</code>
* @return Returns <code>true</code> on success and <code>false</code> on error
if ( $numArgs == 0) return false;
$lastArg = $args[ $numArgs - 1];
unset ( $args[ $numArgs - 1]);
foreach( $args as $table) {
$this->optimizeTable( $table, $mode);
* Reorganise the table-indices/statistics/.. depending on the given mode.
* Default Implementation throws an error.
* @param string table name of the table to optimize
* @param int mode optimization-mode
* <code>ADODB_OPT_HIGH</code> for full optimization
* <code>ADODB_OPT_LOW</code> for CPU-less optimization
* Default is LOW <code>ADODB_OPT_LOW</code>
* @return Returns <code>true</code> on success and <code>false</code> on error
* Reorganise current database.
* Default implementation loops over all <code>MetaTables()</code> and
* optimize each using <code>optmizeTable()</code>
* @return Returns <code>true</code> on success and <code>false</code> on error
if ( !$conn) return false;
$tables = $conn->MetaTables( 'TABLES');
if ( !$tables ) return false;
foreach( $tables as $table) {
if ( !$this->optimizeTable( $table)) {
|