Bug 712427 - provide more information in SQL PRAGMA queries; r=mak
authorNathan Froyd <froydnj@mozilla.com>
Thu, 05 Jan 2012 18:46:35 -0500
changeset 85133 9136ddb5f8a14a680223281604ad138a7aea4171
parent 85132 e12b877ae637f5e2e8fa57ab6040ba25e10e3b8f
child 85134 b91361c61e7d287e2d267f73b8b836fd084c0db0
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs712427
milestone12.0a1
Bug 712427 - provide more information in SQL PRAGMA queries; r=mak
dom/src/storage/nsDOMStoragePersistentDB.cpp
netwerk/cookie/nsCookieService.cpp
storage/public/mozStorageHelper.h
storage/src/VacuumManager.cpp
storage/src/mozStorageConnection.cpp
toolkit/components/places/Database.cpp
toolkit/components/places/PlacesDBUtils.jsm
toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
--- a/dom/src/storage/nsDOMStoragePersistentDB.cpp
+++ b/dom/src/storage/nsDOMStoragePersistentDB.cpp
@@ -203,17 +203,17 @@ nsDOMStoragePersistentDB::Init(const nsS
     // delete the db and try opening again
     rv = storageFile->Remove(false);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = service->OpenDatabase(storageFile, getter_AddRefs(mConnection));
   }
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = mConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
-        "PRAGMA temp_store = MEMORY"));
+        MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA temp_store = MEMORY"));
   NS_ENSURE_SUCCESS(rv, rv);
 
   mozStorageTransaction transaction(mConnection, false);
 
   // Ensure Gecko 1.9.1 storage table
   rv = mConnection->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
          "CREATE TABLE IF NOT EXISTS webappsstore2 ("
          "scope TEXT, "
--- a/netwerk/cookie/nsCookieService.cpp
+++ b/netwerk/cookie/nsCookieService.cpp
@@ -1012,17 +1012,17 @@ nsCookieService::TryInitDB(bool aRecreat
 
   // make operations on the table asynchronous, for performance
   mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     "PRAGMA synchronous = OFF"));
 
   // Use write-ahead-logging for performance. We cap the autocheckpoint limit at
   // 16 pages (around 500KB).
   mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
-    "PRAGMA journal_mode = WAL"));
+    MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA journal_mode = WAL"));
   mDefaultDBState->dbConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
     "PRAGMA wal_autocheckpoint = 16"));
 
   // cache frequently used statements (for insertion, deletion, and updating)
   rv = mDefaultDBState->dbConn->CreateAsyncStatement(NS_LITERAL_CSTRING(
     "INSERT INTO moz_cookies ("
       "baseDomain, "
       "name, "
--- a/storage/public/mozStorageHelper.h
+++ b/storage/public/mozStorageHelper.h
@@ -187,9 +187,14 @@ public:
   {
     mStatement = nsnull;
   }
 
 protected:
   nsCOMPtr<mozIStorageStatement> mStatement;
 };
 
+// Use this to make queries uniquely identifiable in telemetry
+// statistics, especially PRAGMAs.  We don't include __LINE__ so that
+// queries are stable in the face of source code changes.
+#define MOZ_STORAGE_UNIQUIFY_QUERY_STR "/* " __FILE__ " */ "
+
 #endif /* MOZSTORAGEHELPER_H */
--- a/storage/src/VacuumManager.cpp
+++ b/storage/src/VacuumManager.cpp
@@ -47,16 +47,17 @@
 #include "nsThreadUtils.h"
 #include "prlog.h"
 
 #include "mozStorageConnection.h"
 #include "mozIStorageStatement.h"
 #include "mozIStorageAsyncStatement.h"
 #include "mozIStoragePendingStatement.h"
 #include "mozIStorageError.h"
+#include "mozStorageHelper.h"
 
 #define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
 #define OBSERVER_TOPIC_XPCOM_SHUTDOWN "xpcom-shutdown"
 
 // Used to notify begin and end of a heavy IO task.
 #define OBSERVER_TOPIC_HEAVY_IO "heavy-io-task"
 #define OBSERVER_DATA_VACUUM_BEGIN NS_LITERAL_STRING("vacuum-begin")
 #define OBSERVER_DATA_VACUUM_END NS_LITERAL_STRING("vacuum-end")
@@ -216,17 +217,17 @@ Vacuumer::execute()
     NS_WARNING(mDBFilename.get());
     expectedPageSize = mozIStorageConnection::DEFAULT_PAGE_SIZE;
   }
 
   bool canOptimizePageSize = false;
   {
     nsCOMPtr<mozIStorageStatement> stmt;
     rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
-      "PRAGMA page_size"
+      MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA page_size"
     ), getter_AddRefs(stmt));
     NS_ENSURE_SUCCESS(rv, false);
     bool hasResult;
     rv = stmt->ExecuteStep(&hasResult);
     NS_ENSURE_SUCCESS(rv, false);
     NS_ENSURE_TRUE(hasResult, false);
     PRInt32 currentPageSize;
     rv = stmt->GetInt32(0, &currentPageSize);
@@ -235,17 +236,17 @@ Vacuumer::execute()
     if (currentPageSize != expectedPageSize) {
       // Check journal mode.  WAL journaling does not allow vacuum to change
       // the page size.
       // TODO Bug 634374: figure out a strategy to fix page size with WAL.
       nsCAutoString journalMode;
       {
         nsCOMPtr<mozIStorageStatement> stmt;
         rv = mDBConn->CreateStatement(NS_LITERAL_CSTRING(
-          "PRAGMA journal_mode"
+          MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA journal_mode"
         ), getter_AddRefs(stmt));
         NS_ENSURE_SUCCESS(rv, false);
         bool hasResult;
         rv = stmt->ExecuteStep(&hasResult);
         NS_ENSURE_SUCCESS(rv, false);
         NS_ENSURE_TRUE(hasResult, false);
         rv = stmt->GetUTF8String(0, journalMode);
         NS_ENSURE_SUCCESS(rv, false);
@@ -295,17 +296,17 @@ Vacuumer::execute()
   if (os) {
     (void)os->NotifyObservers(nsnull, OBSERVER_TOPIC_HEAVY_IO,
                               OBSERVER_DATA_VACUUM_BEGIN.get());
   }
 
   if (canOptimizePageSize) {
     nsCOMPtr<mozIStorageAsyncStatement> pageSizeStmt;
     rv = mDBConn->CreateAsyncStatement(nsPrintfCString(
-      "PRAGMA page_size = %ld", expectedPageSize
+      MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA page_size = %ld", expectedPageSize
     ), getter_AddRefs(pageSizeStmt));
     NS_ENSURE_SUCCESS(rv, false);
     nsCOMPtr<BaseCallback> callback = new BaseCallback();
     NS_ENSURE_TRUE(callback, false);
     nsCOMPtr<mozIStoragePendingStatement> ps;
     rv = pageSizeStmt->ExecuteAsync(callback, getter_AddRefs(ps));
     NS_ENSURE_SUCCESS(rv, false);
   }
--- a/storage/src/mozStorageConnection.cpp
+++ b/storage/src/mozStorageConnection.cpp
@@ -65,16 +65,17 @@
 #include "mozStorageStatement.h"
 #include "mozStorageAsyncStatement.h"
 #include "mozStorageArgValueArray.h"
 #include "mozStoragePrivateHelpers.h"
 #include "mozStorageStatementData.h"
 #include "StorageBaseStatementInternal.h"
 #include "SQLCollations.h"
 #include "FileSystemModule.h"
+#include "mozStorageHelper.h"
 
 #include "prlog.h"
 #include "prprf.h"
 
 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
 
 // Maximum size of the pages cache per connection.  If the default cache_size
 // value evaluates to a larger size, it will be reduced to save memory.
@@ -645,35 +646,39 @@ Connection::initialize(nsIFile *aDatabas
   PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Opening connection to '%s' (%p)",
                                       leafName.get(), this));
 #endif
 
   // Set page_size to the preferred default value.  This is effective only if
   // the database has just been created, otherwise, if the database does not
   // use WAL journal mode, a VACUUM operation will updated its page_size.
   PRInt64 pageSize = DEFAULT_PAGE_SIZE;
-  nsCAutoString pageSizeQuery("PRAGMA page_size = ");
+  nsCAutoString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
+                              "PRAGMA page_size = ");
   pageSizeQuery.AppendInt(pageSize);
   rv = ExecuteSimpleSQL(pageSizeQuery);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Get the current page_size, since it may differ from the specified value.
   sqlite3_stmt *stmt;
-  srv = prepareStatement(NS_LITERAL_CSTRING("PRAGMA page_size"), &stmt);
+  NS_NAMED_LITERAL_CSTRING(pragma_page_size,
+                           MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA page_size");
+  srv = prepareStatement(pragma_page_size, &stmt);
   if (srv == SQLITE_OK) {
     if (SQLITE_ROW == stepStatement(stmt)) {
       pageSize = ::sqlite3_column_int64(stmt, 0);
     }
     (void)::sqlite3_finalize(stmt);
   }
 
   // Setting the cache_size forces the database open, verifying if it is valid
   // or corrupt.  So this is executed regardless it being actually needed.
   // The cache_size is calculated from the actual page_size, to save memory.
-  nsCAutoString cacheSizeQuery("PRAGMA cache_size = ");
+  nsCAutoString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
+                               "PRAGMA cache_size = ");
   cacheSizeQuery.AppendInt(NS_MIN(DEFAULT_CACHE_SIZE_PAGES,
                                   PRInt32(MAX_CACHE_SIZE_BYTES / pageSize)));
   srv = ::sqlite3_exec(mDBConn, cacheSizeQuery.get(), NULL, NULL, NULL);
   if (srv != SQLITE_OK) {
     ::sqlite3_close(mDBConn);
     mDBConn = nsnull;
     return convertResultCode(srv);
   }
--- a/toolkit/components/places/Database.cpp
+++ b/toolkit/components/places/Database.cpp
@@ -228,17 +228,18 @@ SetJournalMode(nsCOMPtr<mozIStorageConne
       journalMode.AssignLiteral("memory");
       break;
     case JOURNAL_WAL:
       journalMode.AssignLiteral("wal");
       break;
   }
 
   nsCOMPtr<mozIStorageStatement> statement;
-  nsCAutoString query("PRAGMA journal_mode = ");
+  nsCAutoString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
+		      "PRAGMA journal_mode = ");
   query.Append(journalMode);
   aDBConn->CreateStatement(query, getter_AddRefs(statement));
   NS_ENSURE_TRUE(statement, JOURNAL_DELETE);
 
   bool hasResult = false;
   if (NS_SUCCEEDED(statement->ExecuteStep(&hasResult)) && hasResult &&
       NS_SUCCEEDED(statement->GetUTF8String(0, journalMode))) {
     if (journalMode.EqualsLiteral("delete")) {
@@ -546,29 +547,29 @@ Database::InitSchema(bool* aDatabaseMigr
   // finalized, since SQLite doesn't allow changing the journal mode if there
   // is any outstanding statement.
 
   {
     // Get the page size.  This may be different than the default if the
     // database file already existed with a different page size.
     nsCOMPtr<mozIStorageStatement> statement;
     nsresult rv = mMainConn->CreateStatement(NS_LITERAL_CSTRING(
-      "PRAGMA page_size"
+      MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA page_size"
     ), getter_AddRefs(statement));
     NS_ENSURE_SUCCESS(rv, rv);
     bool hasResult = false;
     rv = statement->ExecuteStep(&hasResult);
     NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasResult, NS_ERROR_FAILURE);
     rv = statement->GetInt32(0, &mDBPageSize);
     NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && mDBPageSize > 0, NS_ERROR_UNEXPECTED);
   }
 
   // Ensure that temp tables are held in memory, not on disk.
   nsresult rv = mMainConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
-      "PRAGMA temp_store = MEMORY"));
+      MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA temp_store = MEMORY"));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Get the current database size. Due to chunked growth we have to use
   // page_count to evaluate it.
   PRUint64 databaseSizeBytes = 0;
   {
     nsCOMPtr<mozIStorageStatement> statement;
     nsresult rv = mMainConn->CreateStatement(NS_LITERAL_CSTRING(
@@ -588,17 +589,18 @@ Database::InitSchema(bool* aDatabaseMigr
   // meaningful limits.
   PRInt64 cacheSize = clamped(databaseSizeBytes *  DATABASE_CACHE_TO_DATABASE_PERC / 100,
                               DATABASE_CACHE_MIN_BYTES,
                               DATABASE_CACHE_MAX_BYTES);
 
   // Set the number of cached pages.
   // We don't use PRAGMA default_cache_size, since the database could be moved
   // among different devices and the value would adapt accordingly.
-  nsCAutoString cacheSizePragma("PRAGMA cache_size = ");
+  nsCAutoString cacheSizePragma(MOZ_STORAGE_UNIQUIFY_QUERY_STR
+				"PRAGMA cache_size = ");
   cacheSizePragma.AppendInt(cacheSize / mDBPageSize);
   rv = mMainConn->ExecuteSimpleSQL(cacheSizePragma);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Be sure to set journal mode after page_size.  WAL would prevent the change
   // otherwise.
   if (NS_SUCCEEDED(SetJournalMode(mMainConn, JOURNAL_WAL))) {
     // Set the WAL journal size limit.  We want it to be small, since in
--- a/toolkit/components/places/PlacesDBUtils.jsm
+++ b/toolkit/components/places/PlacesDBUtils.jsm
@@ -903,21 +903,21 @@ let PlacesDBUtils = {
         DBFile.append("places.sqlite-wal");
         try {
           return parseInt(DBFile.fileSize / BYTES_PER_MEBIBYTE);
         } catch (ex) {
           return 0;
         }
       },
 
-      PLACES_DATABASE_PAGESIZE_B: "PRAGMA page_size",
+      PLACES_DATABASE_PAGESIZE_B: "PRAGMA page_size /* PlacesDBUtils.jsm PAGESIZE_B */",
 
       PLACES_DATABASE_SIZE_PER_PAGE_B: function() {
         // Cannot use the filesize here, due to chunked growth.
-        let stmt = DBConn.createStatement("PRAGMA page_size");
+        let stmt = DBConn.createStatement("PRAGMA page_size /* PlacesDBUtils.jsm SIZE_PER_PAGE_B */");
         stmt.executeStep();
         let pageSize = stmt.row.page_size;
         stmt.finalize();
         stmt = DBConn.createStatement("PRAGMA page_count");
         stmt.executeStep();
         let pageCount = stmt.row.page_count;
         stmt.finalize();
         stmt = DBConn.createStatement("SELECT count(*) AS c FROM moz_places");
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -3121,17 +3121,18 @@ nsUrlClassifierDBServiceWorker::SetCache
   mozStorageStatementScoper scoper(mGetPageSizeStatement);
   bool hasResult;
   nsresult rv = mGetPageSizeStatement->ExecuteStep(&hasResult);
   NS_ENSURE_SUCCESS(rv, rv);
 
   NS_ASSERTION(hasResult, "Should always be able to get page size from sqlite");
   PRUint32 pageSize = mGetPageSizeStatement->AsInt32(0);
   PRUint32 cachePages = aCacheSize / pageSize;
-  nsCAutoString cacheSizePragma("PRAGMA cache_size=");
+  nsCAutoString cacheSizePragma(MOZ_STORAGE_UNIQUIFY_QUERY_STR
+                                "PRAGMA cache_size=");
   cacheSizePragma.AppendInt(cachePages);
   rv = aConnection->ExecuteSimpleSQL(cacheSizePragma);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 nsresult
@@ -3408,17 +3409,17 @@ nsUrlClassifierDBServiceWorker::OpenDb()
     }
   }
 
   connection->SetGrowthIncrement(5 * 1024 * 1024, EmptyCString());
   rv = connection->ExecuteSimpleSQL(NS_LITERAL_CSTRING("PRAGMA synchronous=OFF"));
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = connection->CreateStatement
-    (NS_LITERAL_CSTRING("PRAGMA page_size"),
+    (NS_LITERAL_CSTRING(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA page_size"),
      getter_AddRefs(mGetPageSizeStatement));
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = SetCacheSize(connection, gLookupCacheSize);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (newDB) {
     rv = connection->SetSchemaVersion(IMPLEMENTATION_VERSION);