OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sql/connection.h" | 5 #include "sql/connection.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 preload_size = file_size; | 459 preload_size = file_size; |
460 | 460 |
461 scoped_ptr<char[]> buf(new char[page_size]); | 461 scoped_ptr<char[]> buf(new char[page_size]); |
462 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) { | 462 for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) { |
463 rc = file->pMethods->xRead(file, buf.get(), page_size, pos); | 463 rc = file->pMethods->xRead(file, buf.get(), page_size, pos); |
464 if (rc != SQLITE_OK) | 464 if (rc != SQLITE_OK) |
465 return; | 465 return; |
466 } | 466 } |
467 } | 467 } |
468 | 468 |
469 // SQLite keeps unused pages associated with a connection in a cache. It asks | |
470 // the cache for pages by an id, and if the page is present and the database is | |
471 // unchanged, it considers the content of the page valid and doesn't read it | |
472 // from disk. When memory-mapped I/O is enabled, on read SQLite uses page | |
473 // structures created from the memory map data before consulting the cache. On | |
474 // write SQLite creates a new in-memory page structure, copies the data from the | |
475 // memory map, and later writes it, releasing the updated page back to the | |
476 // cache. | |
477 // | |
478 // This means that in memory-mapped mode, the contents of the cached pages are | |
479 // not re-used for reads, but they are re-used for writes if the re-written page | |
480 // is still in the cache. The implementation of sqlite3_db_release_memory() as | |
481 // of SQLite 3.8.7.4 frees all pages from pcaches associated with the | |
482 // connection, so it should free these pages. | |
483 // | |
484 // Unfortunately, the zero page is also freed. That page is never accessed | |
485 // using memory-mapped I/O, and the cached copy can be re-used after verifying | |
486 // the file change counter on disk. Also, fresh pages from cache receive some | |
487 // pager-level initialization before they can be used. Since the information | |
488 // involved will immediately be accessed in various ways, it is unclear if the | |
489 // additional overhead is material, or just moving processor cache effects | |
490 // around. | |
491 // | |
492 // TODO(shess): It would be better to release the pages immediately when they | |
493 // are no longer needed. This would basically happen after SQLite commits a | |
494 // transaction. I had implemented a pcache wrapper to do this, but it involved | |
495 // layering violations, and it had to be setup before any other sqlite call, | |
496 // which was brittle. Also, for large files it would actually make sense to | |
497 // maintain the existing pcache behavior for blocks past the memory-mapped | |
498 // segment. I think drh would accept a reasonable implementation of the overall | |
499 // concept for upstreaming to SQLite core. | |
500 // | |
501 // TODO(shess): Another possibility would be to set the cache size small, which | |
502 // would keep the zero page around, plus some pre-initialized pages, and SQLite | |
503 // can manage things. The downside is that updates larger than the cache would | |
504 // spill to the journal. That could be compensated by setting cache_spill to | |
505 // false. The downside then is that it allows open-ended use of memory for | |
506 // large transactions. | |
507 // | |
508 // TODO(shess): The TrimMemory() trick of bouncing the cache size would also | |
509 // work. There could be two prepared statements, one for cache_size=1 one for | |
510 // cache_size=goal. | |
511 void Connection::ReleaseCacheMemoryIfNeeded(bool implicit_change_performed) { | |
512 // If memory-mapping is not enabled, the page cache helps performance. | |
513 if (!mmap_enabled_) | |
514 return; | |
515 | |
516 // On caller request, force the change comparison to fail. Done before the | |
517 // transaction-nesting test so that the signal can carry to transaction | |
518 // commit. | |
519 if (implicit_change_performed) | |
520 --total_changes_at_last_release_; | |
521 | |
522 // Cached pages may be re-used within the same transaction. | |
523 if (transaction_nesting()) | |
524 return; | |
525 | |
526 // If no changes have been made, skip flushing. This allows the first page of | |
527 // the database to remain in cache across multiple reads. | |
528 const int total_changes = sqlite3_total_changes(db_); | |
529 if (total_changes == total_changes_at_last_release_) | |
530 return; | |
531 | |
532 total_changes_at_last_release_ = total_changes; | |
533 sqlite3_db_release_memory(db_); | |
534 } | |
535 | |
536 void Connection::TrimMemory(bool aggressively) { | 469 void Connection::TrimMemory(bool aggressively) { |
537 if (!db_) | 470 if (!db_) |
538 return; | 471 return; |
539 | 472 |
540 // TODO(shess): investigate using sqlite3_db_release_memory() when possible. | 473 // TODO(shess): investigate using sqlite3_db_release_memory() when possible. |
541 int original_cache_size; | 474 int original_cache_size; |
542 { | 475 { |
543 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size")); | 476 Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size")); |
544 if (!sql_get_original.Step()) { | 477 if (!sql_get_original.Step()) { |
545 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage(); | 478 DLOG(WARNING) << "Could not get cache size " << GetErrorMessage(); |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 | 769 |
837 // Collect the commit time manually, sql::Statement would register it as query | 770 // Collect the commit time manually, sql::Statement would register it as query |
838 // time only. | 771 // time only. |
839 const base::TimeTicks before = Now(); | 772 const base::TimeTicks before = Now(); |
840 bool ret = commit.RunWithoutTimers(); | 773 bool ret = commit.RunWithoutTimers(); |
841 const base::TimeDelta delta = Now() - before; | 774 const base::TimeDelta delta = Now() - before; |
842 | 775 |
843 RecordCommitTime(delta); | 776 RecordCommitTime(delta); |
844 RecordOneEvent(EVENT_COMMIT); | 777 RecordOneEvent(EVENT_COMMIT); |
845 | 778 |
846 // Release dirty cache pages after the transaction closes. | |
847 ReleaseCacheMemoryIfNeeded(false); | |
848 | |
849 return ret; | 779 return ret; |
850 } | 780 } |
851 | 781 |
852 void Connection::RollbackAllTransactions() { | 782 void Connection::RollbackAllTransactions() { |
853 if (transaction_nesting_ > 0) { | 783 if (transaction_nesting_ > 0) { |
854 transaction_nesting_ = 0; | 784 transaction_nesting_ = 0; |
855 DoRollback(); | 785 DoRollback(); |
856 } | 786 } |
857 } | 787 } |
858 | 788 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
929 // sqlite3_exec() does this, presumably to avoid spinning the parser for | 859 // sqlite3_exec() does this, presumably to avoid spinning the parser for |
930 // trailing whitespace. | 860 // trailing whitespace. |
931 // TODO(shess): Audit to see if this can become a DCHECK. | 861 // TODO(shess): Audit to see if this can become a DCHECK. |
932 while (base::IsAsciiWhitespace(*sql)) { | 862 while (base::IsAsciiWhitespace(*sql)) { |
933 sql++; | 863 sql++; |
934 } | 864 } |
935 | 865 |
936 const base::TimeDelta delta = Now() - before; | 866 const base::TimeDelta delta = Now() - before; |
937 RecordTimeAndChanges(delta, read_only); | 867 RecordTimeAndChanges(delta, read_only); |
938 } | 868 } |
939 | |
940 // Most calls to Execute() modify the database. The main exceptions would be | |
941 // calls such as CREATE TABLE IF NOT EXISTS which could modify the database | |
942 // but sometimes don't. | |
943 ReleaseCacheMemoryIfNeeded(true); | |
944 | |
945 return rc; | 869 return rc; |
946 } | 870 } |
947 | 871 |
948 bool Connection::Execute(const char* sql) { | 872 bool Connection::Execute(const char* sql) { |
949 if (!db_) { | 873 if (!db_) { |
950 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; | 874 DLOG_IF(FATAL, !poisoned_) << "Illegal use of connection without a db"; |
951 return false; | 875 return false; |
952 } | 876 } |
953 | 877 |
954 int error = ExecuteAndReturnErrorCode(sql); | 878 int error = ExecuteAndReturnErrorCode(sql); |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1299 | 1223 |
1300 // http://www.sqlite.org/pragma.html#pragma_journal_mode | 1224 // http://www.sqlite.org/pragma.html#pragma_journal_mode |
1301 // DELETE (default) - delete -journal file to commit. | 1225 // DELETE (default) - delete -journal file to commit. |
1302 // TRUNCATE - truncate -journal file to commit. | 1226 // TRUNCATE - truncate -journal file to commit. |
1303 // PERSIST - zero out header of -journal file to commit. | 1227 // PERSIST - zero out header of -journal file to commit. |
1304 // TRUNCATE should be faster than DELETE because it won't need directory | 1228 // TRUNCATE should be faster than DELETE because it won't need directory |
1305 // changes for each transaction. PERSIST may break the spirit of using | 1229 // changes for each transaction. PERSIST may break the spirit of using |
1306 // secure_delete. | 1230 // secure_delete. |
1307 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE")); | 1231 ignore_result(Execute("PRAGMA journal_mode = TRUNCATE")); |
1308 | 1232 |
1309 // Enable memory-mapped access. This value will be capped by | |
1310 // SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and 64-bit | |
1311 // platforms. | |
1312 mmap_enabled_ = false; | |
1313 if (!mmap_disabled_) | |
1314 ignore_result(Execute("PRAGMA mmap_size = 268435456")); // 256MB. | |
1315 { | |
1316 Statement s(GetUniqueStatement("PRAGMA mmap_size")); | |
1317 if (s.Step() && s.ColumnInt64(0) > 0) | |
1318 mmap_enabled_ = true; | |
1319 } | |
1320 | |
1321 const base::TimeDelta kBusyTimeout = | 1233 const base::TimeDelta kBusyTimeout = |
1322 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds); | 1234 base::TimeDelta::FromSeconds(kBusyTimeoutSeconds); |
1323 | 1235 |
1324 if (page_size_ != 0) { | 1236 if (page_size_ != 0) { |
1325 // Enforce SQLite restrictions on |page_size_|. | 1237 // Enforce SQLite restrictions on |page_size_|. |
1326 DCHECK(!(page_size_ & (page_size_ - 1))) | 1238 DCHECK(!(page_size_ & (page_size_ - 1))) |
1327 << " page_size_ " << page_size_ << " is not a power of two."; | 1239 << " page_size_ " << page_size_ << " is not a power of two."; |
1328 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h | 1240 const int kSqliteMaxPageSize = 32768; // from sqliteLimit.h |
1329 DCHECK_LE(page_size_, kSqliteMaxPageSize); | 1241 DCHECK_LE(page_size_, kSqliteMaxPageSize); |
1330 const std::string sql = | 1242 const std::string sql = |
(...skipping 23 matching lines...) Expand all Loading... |
1354 | 1266 |
1355 // Collect the rollback time manually, sql::Statement would register it as | 1267 // Collect the rollback time manually, sql::Statement would register it as |
1356 // query time only. | 1268 // query time only. |
1357 const base::TimeTicks before = Now(); | 1269 const base::TimeTicks before = Now(); |
1358 rollback.RunWithoutTimers(); | 1270 rollback.RunWithoutTimers(); |
1359 const base::TimeDelta delta = Now() - before; | 1271 const base::TimeDelta delta = Now() - before; |
1360 | 1272 |
1361 RecordUpdateTime(delta); | 1273 RecordUpdateTime(delta); |
1362 RecordOneEvent(EVENT_ROLLBACK); | 1274 RecordOneEvent(EVENT_ROLLBACK); |
1363 | 1275 |
1364 // The cache may have been accumulating dirty pages for commit. | |
1365 ReleaseCacheMemoryIfNeeded(false); | |
1366 | |
1367 needs_rollback_ = false; | 1276 needs_rollback_ = false; |
1368 } | 1277 } |
1369 | 1278 |
1370 void Connection::StatementRefCreated(StatementRef* ref) { | 1279 void Connection::StatementRefCreated(StatementRef* ref) { |
1371 DCHECK(open_statements_.find(ref) == open_statements_.end()); | 1280 DCHECK(open_statements_.find(ref) == open_statements_.end()); |
1372 open_statements_.insert(ref); | 1281 open_statements_.insert(ref); |
1373 } | 1282 } |
1374 | 1283 |
1375 void Connection::StatementRefDeleted(StatementRef* ref) { | 1284 void Connection::StatementRefDeleted(StatementRef* ref) { |
1376 StatementRefSet::iterator i = open_statements_.find(ref); | 1285 StatementRefSet::iterator i = open_statements_.find(ref); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1476 ignore_result(Execute(kNoWritableSchema)); | 1385 ignore_result(Execute(kNoWritableSchema)); |
1477 | 1386 |
1478 return ret; | 1387 return ret; |
1479 } | 1388 } |
1480 | 1389 |
1481 base::TimeTicks TimeSource::Now() { | 1390 base::TimeTicks TimeSource::Now() { |
1482 return base::TimeTicks::Now(); | 1391 return base::TimeTicks::Now(); |
1483 } | 1392 } |
1484 | 1393 |
1485 } // namespace sql | 1394 } // namespace sql |
OLD | NEW |