Commit bd5535a9 authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Move query counting inside adodb to count queries that were being missed


Signed-off-by: default avatarRichard Mansfield <richard.mansfield@catalyst.net.nz>
parent 0e7a938a
...@@ -296,7 +296,7 @@ function init_performance_info() { ...@@ -296,7 +296,7 @@ function init_performance_info() {
global $PERF; global $PERF;
$PERF = new StdClass; $PERF = new StdClass;
$PERF->dbreads = $PERF->dbwrites = 0; $PERF->dbreads = $PERF->dbwrites = $PERF->dbcached = 0;
$PERF->logwrites = 0; $PERF->logwrites = 0;
if (function_exists('microtime')) { if (function_exists('microtime')) {
$PERF->starttime = microtime(); $PERF->starttime = microtime();
......
...@@ -34,6 +34,7 @@ $string['included'] = 'Included files'; ...@@ -34,6 +34,7 @@ $string['included'] = 'Included files';
$string['dbqueries'] = 'DB queries'; $string['dbqueries'] = 'DB queries';
$string['reads'] = 'reads'; $string['reads'] = 'reads';
$string['writes'] = 'writes'; $string['writes'] = 'writes';
$string['cached'] = 'cached';
$string['ticks'] = 'ticks'; $string['ticks'] = 'ticks';
$string['sys'] = 'sys'; $string['sys'] = 'sys';
$string['user'] = 'user'; $string['user'] = 'user';
......
...@@ -154,13 +154,6 @@ function execute_sql($command, $values=null) { ...@@ -154,13 +154,6 @@ function execute_sql($command, $values=null) {
else { else {
$result = $db->Execute($command); $result = $db->Execute($command);
} }
// searching for these rather than just select as subqueries may have select in them.
if (preg_match('/(update|insert|delete|alter|create)/i', $command)) {
increment_perf_db_writes();
}
else {
increment_perf_db_reads();
}
} }
catch (ADODB_Exception $e) { catch (ADODB_Exception $e) {
log_debug($e->getMessage() . "Command was: $command"); log_debug($e->getMessage() . "Command was: $command");
...@@ -477,10 +470,8 @@ function get_recordset_sql($sql, $values=null, $limitfrom=null, $limitnum=null) ...@@ -477,10 +470,8 @@ function get_recordset_sql($sql, $values=null, $limitfrom=null, $limitnum=null)
if (!empty($values) && is_array($values) && count($values) > 0) { if (!empty($values) && is_array($values) && count($values) > 0) {
$stmt = $db->Prepare($sql); $stmt = $db->Prepare($sql);
$rs = $db->Execute($stmt, $values); $rs = $db->Execute($stmt, $values);
increment_perf_db_reads();
} else { } else {
$rs = $db->Execute($sql); $rs = $db->Execute($sql);
increment_perf_db_reads();
} }
} }
} }
...@@ -895,7 +886,6 @@ function set_field_select($table, $newfield, $newvalue, $select, $values) { ...@@ -895,7 +886,6 @@ function set_field_select($table, $newfield, $newvalue, $select, $values) {
$sql = 'UPDATE '. db_table_name($table) .' SET '. db_quote_identifier($newfield) .' = ? ' . $select; $sql = 'UPDATE '. db_table_name($table) .' SET '. db_quote_identifier($newfield) .' = ? ' . $select;
try { try {
$stmt = $db->Prepare($sql); $stmt = $db->Prepare($sql);
increment_perf_db_writes();
return $db->Execute($stmt, $values); return $db->Execute($stmt, $values);
} }
catch (ADODB_Exception $e) { catch (ADODB_Exception $e) {
...@@ -931,7 +921,6 @@ function delete_records($table, $field1=null, $value1=null, $field2=null, $value ...@@ -931,7 +921,6 @@ function delete_records($table, $field1=null, $value1=null, $field2=null, $value
$sql = 'DELETE FROM '. db_table_name($table) . ' ' . $select; $sql = 'DELETE FROM '. db_table_name($table) . ' ' . $select;
try { try {
$stmt = $db->Prepare($sql); $stmt = $db->Prepare($sql);
increment_perf_db_writes();
return $db->Execute($stmt,$values); return $db->Execute($stmt,$values);
} }
catch (ADODB_Exception $e) { catch (ADODB_Exception $e) {
...@@ -970,10 +959,8 @@ function delete_records_sql($sql, $values=null) { ...@@ -970,10 +959,8 @@ function delete_records_sql($sql, $values=null) {
$result = false; $result = false;
if (!empty($values) && is_array($values) && count($values) > 0) { if (!empty($values) && is_array($values) && count($values) > 0) {
$stmt = $db->Prepare($sql); $stmt = $db->Prepare($sql);
increment_perf_db_writes();
$result = $db->Execute($stmt, $values); $result = $db->Execute($stmt, $values);
} else { } else {
increment_perf_db_writes();
$result = $db->Execute($sql); $result = $db->Execute($sql);
} }
} }
...@@ -1062,7 +1049,6 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false) ...@@ -1062,7 +1049,6 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false)
// Run the SQL statement // Run the SQL statement
try { try {
$stmt = $db->Prepare($insertSQL); $stmt = $db->Prepare($insertSQL);
increment_perf_db_writes();
$rs = $db->Execute($stmt,$ddd); $rs = $db->Execute($stmt,$ddd);
} }
catch (ADODB_Exception $e) { catch (ADODB_Exception $e) {
...@@ -1089,7 +1075,6 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false) ...@@ -1089,7 +1075,6 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false)
// try to get the primary key based on id // try to get the primary key based on id
try { try {
$oidsql = 'SELECT ' . $primarykey . ' FROM '. db_table_name($table) . ' WHERE oid = ' . $id; $oidsql = 'SELECT ' . $primarykey . ' FROM '. db_table_name($table) . ' WHERE oid = ' . $id;
increment_perf_db_reads();
$rs = $db->Execute($oidsql); $rs = $db->Execute($oidsql);
if ($rs->RecordCount() == 1) { if ($rs->RecordCount() == 1) {
return (integer)$rs->fields[0]; return (integer)$rs->fields[0];
...@@ -1276,7 +1261,6 @@ function update_record($table, $dataobject, $where=null) { ...@@ -1276,7 +1261,6 @@ function update_record($table, $dataobject, $where=null) {
$sql = 'UPDATE '. db_table_name($table) .' SET '. $update .' WHERE ' . $whereclause; $sql = 'UPDATE '. db_table_name($table) .' SET '. $update .' WHERE ' . $whereclause;
try { try {
$stmt = $db->Prepare($sql); $stmt = $db->Prepare($sql);
increment_perf_db_writes();
$rs = $db->Execute($stmt,array_merge($values, $wherevalues)); $rs = $db->Execute($stmt,array_merge($values, $wherevalues));
return true; return true;
} }
...@@ -1373,7 +1357,6 @@ function where_values_prepared($value1=null, $value2=null, $value3=null, $value4 ...@@ -1373,7 +1357,6 @@ function where_values_prepared($value1=null, $value2=null, $value3=null, $value4
function column_type($table, $column) { function column_type($table, $column) {
global $db; global $db;
increment_perf_db_reads();
if(!$rs = $db->Execute('SELECT ' . $column.' FROM ' . db_table_name($table) . ' WHERE 1=2')) { if(!$rs = $db->Execute('SELECT ' . $column.' FROM ' . db_table_name($table) . ' WHERE 1=2')) {
return false; return false;
} }
...@@ -1475,7 +1458,12 @@ function db_format_tsfield($field, $as = null) { ...@@ -1475,7 +1458,12 @@ function db_format_tsfield($field, $as = null) {
* and some other variables. * and some other variables.
*/ */
function configure_dbconnection() { function configure_dbconnection() {
global $db; global $db, $CFG;
if (!empty($CFG->perftolog) || !empty($CFG->perftofoot)) {
$db->fnExecute = 'increment_perf_db';
$db->fnCacheExecute = 'increment_perf_db_cached';
}
$db->Execute("SET NAMES 'utf8'"); $db->Execute("SET NAMES 'utf8'");
...@@ -1575,20 +1563,24 @@ function create_sql_exception_message($e, $sql, $values) { ...@@ -1575,20 +1563,24 @@ function create_sql_exception_message($e, $sql, $values) {
return $message; return $message;
} }
function increment_perf_db_reads() { function &increment_perf_db($db, $sql, $inputarray) {
if (!get_config('perftolog') && !get_config('perftofoot')) {
return true;
}
global $PERF; global $PERF;
// searching for these rather than just select as subqueries may have select in them.
if (preg_match('/^(update|insert|delete|alter|create)/i', trim($sql))) {
$PERF->dbwrites++;
}
else {
$PERF->dbreads++; $PERF->dbreads++;
}
$null = null;
return $null;
} }
function increment_perf_db_writes() { function increment_perf_db_cached($db, $secs2cache, $sql, $inputarray) {
if (!get_config('perftolog') && !get_config('perftofoot')) {
return true;
}
global $PERF; global $PERF;
$PERF->dbwrites++; $PERF->dbcached++;
} }
/** /**
......
...@@ -1962,6 +1962,7 @@ function get_performance_info() { ...@@ -1962,6 +1962,7 @@ function get_performance_info() {
$info['dbreads'] = $PERF->dbreads; $info['dbreads'] = $PERF->dbreads;
$info['dbwrites'] = $PERF->dbwrites; $info['dbwrites'] = $PERF->dbwrites;
$info['dbcached'] = $PERF->dbcached;
if (function_exists('posix_times')) { if (function_exists('posix_times')) {
$ptimes = posix_times(); $ptimes = posix_times();
...@@ -2008,7 +2009,7 @@ function perf_to_log($info=null) { ...@@ -2008,7 +2009,7 @@ function perf_to_log($info=null) {
$logstring .= ' memory_total: '.$info['memory_total'].'B (' . display_size($info['memory_total']).') memory_growth: '.$info['memory_growth'].'B ('.display_size($info['memory_growth']).')'; $logstring .= ' memory_total: '.$info['memory_total'].'B (' . display_size($info['memory_total']).') memory_growth: '.$info['memory_growth'].'B ('.display_size($info['memory_growth']).')';
$logstring .= ' time: '.$info['realtime'].'s'; $logstring .= ' time: '.$info['realtime'].'s';
$logstring .= ' includecount: '.$info['includecount']; $logstring .= ' includecount: '.$info['includecount'];
$logstring .= ' dbqueries: '.$info['dbreads'] . ' reads, ' . $info['dbwrites'] . ' writes'; $logstring .= ' dbqueries: '.$info['dbreads'] . ' reads, ' . $info['dbwrites'] . ' writes, ' . $info['dbcached'] . ' cached';
$logstring .= ' ticks: ' . $info['ticks'] . ' user: ' . $info['utime'] . ' sys: ' . $info['stime'] .' cuser: ' . $info['cutime'] . ' csys: ' . $info['cstime']; $logstring .= ' ticks: ' . $info['ticks'] . ' user: ' . $info['utime'] . ' sys: ' . $info['stime'] .' cuser: ' . $info['cutime'] . ' csys: ' . $info['cstime'];
$logstring .= ' serverload: ' . $info['serverload']; $logstring .= ' serverload: ' . $info['serverload'];
log_debug($logstring); log_debug($logstring);
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
{if $perf_includecount} {if $perf_includecount}
<span id="included">{str tag="included" section="performance"}: {$perf_includecount}</span> | <span id="included">{str tag="included" section="performance"}: {$perf_includecount}</span> |
{/if} {/if}
{if $perf_dbreads || $perf_dbwrites} {if $perf_dbreads || $perf_dbwrites || $perf_dbcached}
<span id="dbqueries">{str tag="dbqueries" section="performance"}: {$perf_dbreads} {str tag='reads' section='performance'}, {$perf_dbwrites} {str tag='writes' section='performance'}</span> | <span id="dbqueries">{str tag="dbqueries" section="performance"}: {$perf_dbreads} {str tag='reads' section='performance'}, {$perf_dbwrites} {str tag='writes' section='performance'}, {$perf_dbcached} {str tag='cached' section='performance'}</span> |
{/if} {/if}
{if $perf_ticks} {if $perf_ticks}
<span id="posixtimes">{str tag="ticks" section="performance"}: {$perf_ticks} {str tag="user" section="performance"}: {$perf_utime} <span id="posixtimes">{str tag="ticks" section="performance"}: {$perf_ticks} {str tag="user" section="performance"}: {$perf_utime}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment