am 90142c95: Adding a system property to log slow queries

Merge commit '90142c959e6de38eae1563cd8b3d2d448393e15f' into eclair-plus-aosp

* commit '90142c959e6de38eae1563cd8b3d2d448393e15f':
  Adding a system property to log slow queries
This commit is contained in:
Dmitri Plotnikov
2009-09-15 16:03:59 -07:00
committed by Android Git Automerger

View File

@@ -22,10 +22,11 @@ import android.database.DatabaseUtils;
import android.database.SQLException; import android.database.SQLException;
import android.os.Debug; import android.os.Debug;
import android.os.SystemClock; import android.os.SystemClock;
import android.os.SystemProperties;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Config; import android.util.Config;
import android.util.Log;
import android.util.EventLog; import android.util.EventLog;
import android.util.Log;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
@@ -221,6 +222,10 @@ public class SQLiteDatabase extends SQLiteClosable {
// that logging is not enabled. // that logging is not enabled.
/* package */ final boolean mLogStats; /* package */ final boolean mLogStats;
// System property that enables logging of slow queries. Specify the threshold in ms.
private static final String LOG_SLOW_QUERIES_PROPERTY = "db.log.slow_query_threshold";
private final int mSlowQueryThreshold;
/** /**
* @param closable * @param closable
*/ */
@@ -1202,27 +1207,38 @@ public class SQLiteDatabase extends SQLiteClosable {
String editTable) { String editTable) {
long timeStart = 0; long timeStart = 0;
if (Config.LOGV) { if (Config.LOGV || mSlowQueryThreshold != -1) {
timeStart = System.currentTimeMillis(); timeStart = System.currentTimeMillis();
} }
SQLiteCursorDriver driver = new SQLiteDirectCursorDriver(this, sql, editTable); SQLiteCursorDriver driver = new SQLiteDirectCursorDriver(this, sql, editTable);
Cursor cursor = null;
try { try {
return driver.query( cursor = driver.query(
cursorFactory != null ? cursorFactory : mFactory, cursorFactory != null ? cursorFactory : mFactory,
selectionArgs); selectionArgs);
} finally { } finally {
if (Config.LOGV) { if (Config.LOGV || mSlowQueryThreshold != -1) {
// Force query execution
if (cursor != null) {
cursor.moveToFirst();
cursor.moveToPosition(-1);
}
long duration = System.currentTimeMillis() - timeStart; long duration = System.currentTimeMillis() - timeStart;
Log.v(SQLiteCursor.TAG, if (Config.LOGV || duration >= mSlowQueryThreshold) {
"query (" + duration + " ms): " + driver.toString() + ", args are " Log.v(SQLiteCursor.TAG,
+ (selectionArgs != null "query (" + duration + " ms): " + driver.toString() + ", args are "
? TextUtils.join(",", selectionArgs) + (selectionArgs != null
: "<null>")); ? TextUtils.join(",", selectionArgs)
: "<null>"));
}
} }
} }
return cursor;
} }
/** /**
@@ -1671,6 +1687,7 @@ public class SQLiteDatabase extends SQLiteClosable {
mFlags = flags; mFlags = flags;
mPath = path; mPath = path;
mLogStats = "1".equals(android.os.SystemProperties.get("db.logstats")); mLogStats = "1".equals(android.os.SystemProperties.get("db.logstats"));
mSlowQueryThreshold = SystemProperties.getInt(LOG_SLOW_QUERIES_PROPERTY, -1);
mLeakedException = new IllegalStateException(path + mLeakedException = new IllegalStateException(path +
" SQLiteDatabase created and never closed"); " SQLiteDatabase created and never closed");