OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/safe_browsing/safe_browsing_database_bloom.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_database_bloom.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 using base::TimeDelta; | 23 using base::TimeDelta; |
24 | 24 |
25 // Database version. If this is different than what's stored on disk, the | 25 // Database version. If this is different than what's stored on disk, the |
26 // database is reset. | 26 // database is reset. |
27 static const int kDatabaseVersion = 6; | 27 static const int kDatabaseVersion = 6; |
28 | 28 |
29 // Don't want to create too small of a bloom filter initially while we're | 29 // Don't want to create too small of a bloom filter initially while we're |
30 // downloading the data and then keep having to rebuild it. | 30 // downloading the data and then keep having to rebuild it. |
31 static const int kBloomFilterMinSize = 250000; | 31 static const int kBloomFilterMinSize = 250000; |
32 | 32 |
33 // How many bits to use per item. See the design doc for more information. | |
34 static const int kBloomFilterSizeRatio = 13; | |
35 | |
36 // When we awake from a low power state, we try to avoid doing expensive disk | 33 // When we awake from a low power state, we try to avoid doing expensive disk |
37 // operations for a few minutes to let the system page itself in and settle | 34 // operations for a few minutes to let the system page itself in and settle |
38 // down. | 35 // down. |
39 static const int kOnResumeHoldupMs = 5 * 60 * 1000; // 5 minutes. | 36 static const int kOnResumeHoldupMs = 5 * 60 * 1000; // 5 minutes. |
40 | 37 |
41 // The maximum staleness for a cached entry. | 38 // The maximum staleness for a cached entry. |
42 static const int kMaxStalenessMinutes = 45; | 39 static const int kMaxStalenessMinutes = 45; |
43 | 40 |
44 // The bloom filter based file name suffix. | 41 // The bloom filter based file name suffix. |
45 static const FilePath::CharType kBloomFilterFileSuffix[] = | 42 static const FilePath::CharType kBloomFilterFileSuffix[] = |
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 *filter = NULL; | 1055 *filter = NULL; |
1059 *new_add_count = 0; | 1056 *new_add_count = 0; |
1060 | 1057 |
1061 SQLITE_UNIQUE_STATEMENT(insert, *statement_cache_, | 1058 SQLITE_UNIQUE_STATEMENT(insert, *statement_cache_, |
1062 "INSERT INTO add_prefix VALUES (?,?)"); | 1059 "INSERT INTO add_prefix VALUES (?,?)"); |
1063 if (!insert.is_valid()) { | 1060 if (!insert.is_valid()) { |
1064 NOTREACHED(); | 1061 NOTREACHED(); |
1065 return false; | 1062 return false; |
1066 } | 1063 } |
1067 | 1064 |
| 1065 // Determine the size of the new bloom filter. We will cap the maximum size at |
| 1066 // 2 MB to prevent an error from consuming large amounts of memory. |
1068 int number_of_keys = std::max(add_count_, kBloomFilterMinSize); | 1067 int number_of_keys = std::max(add_count_, kBloomFilterMinSize); |
1069 int filter_size = number_of_keys * kBloomFilterSizeRatio; | 1068 int filter_size = std::min(number_of_keys * kBloomFilterSizeRatio, |
| 1069 2 * 1024 * 1024 * 8); |
1070 BloomFilter* new_filter = new BloomFilter(filter_size); | 1070 BloomFilter* new_filter = new BloomFilter(filter_size); |
1071 SBPair* add = adds; | 1071 SBPair* add = adds; |
1072 int new_count = 0; | 1072 int new_count = 0; |
1073 | 1073 |
1074 while (add - adds < add_count_) { | 1074 while (add - adds < add_count_) { |
1075 if (!adds_removed[add - adds]) { | 1075 if (!adds_removed[add - adds]) { |
1076 // Check to see if we have an AddDel for this chunk and skip writing it | 1076 // Check to see if we have an AddDel for this chunk and skip writing it |
1077 // if there is. | 1077 // if there is. |
1078 if (add_del_cache_.find(add->chunk_id) != add_del_cache_.end()) { | 1078 if (add_del_cache_.find(add->chunk_id) != add_del_cache_.end()) { |
1079 add++; | 1079 add++; |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1350 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", | 1350 UMA_HISTOGRAM_COUNTS("SB2.BuildWriteOperations", |
1351 static_cast<int>(io_after.WriteOperationCount - | 1351 static_cast<int>(io_after.WriteOperationCount - |
1352 io_before.WriteOperationCount)); | 1352 io_before.WriteOperationCount)); |
1353 #endif | 1353 #endif |
1354 SB_DLOG(INFO) << "SafeBrowsingDatabaseImpl built bloom filter in " | 1354 SB_DLOG(INFO) << "SafeBrowsingDatabaseImpl built bloom filter in " |
1355 << bloom_gen.InMilliseconds() | 1355 << bloom_gen.InMilliseconds() |
1356 << " ms total. prefix count: "<< add_count_; | 1356 << " ms total. prefix count: "<< add_count_; |
1357 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", bloom_gen); | 1357 UMA_HISTOGRAM_LONG_TIMES("SB2.BuildFilter", bloom_gen); |
1358 UMA_HISTOGRAM_COUNTS("SB2.AddPrefixes", add_count_); | 1358 UMA_HISTOGRAM_COUNTS("SB2.AddPrefixes", add_count_); |
1359 UMA_HISTOGRAM_COUNTS("SB2.SubPrefixes", subs); | 1359 UMA_HISTOGRAM_COUNTS("SB2.SubPrefixes", subs); |
| 1360 UMA_HISTOGRAM_COUNTS("SB2.FilterSize", filter->size()); |
1360 int64 size_64; | 1361 int64 size_64; |
1361 if (file_util::GetFileSize(filename_, &size_64)) | 1362 if (file_util::GetFileSize(filename_, &size_64)) |
1362 UMA_HISTOGRAM_COUNTS("SB2.DatabaseBytes", static_cast<int>(size_64)); | 1363 UMA_HISTOGRAM_COUNTS("SB2.DatabaseBytes", static_cast<int>(size_64)); |
1363 } | 1364 } |
1364 | 1365 |
1365 void SafeBrowsingDatabaseBloom::GetCachedFullHashes( | 1366 void SafeBrowsingDatabaseBloom::GetCachedFullHashes( |
1366 const std::vector<SBPrefix>* prefix_hits, | 1367 const std::vector<SBPrefix>* prefix_hits, |
1367 std::vector<SBFullHashResult>* full_hits, | 1368 std::vector<SBFullHashResult>* full_hits, |
1368 Time last_update) { | 1369 Time last_update) { |
1369 DCHECK(prefix_hits && full_hits); | 1370 DCHECK(prefix_hits && full_hits); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1488 if (did_resume_) { | 1489 if (did_resume_) { |
1489 PlatformThread::Sleep(kOnResumeHoldupMs); | 1490 PlatformThread::Sleep(kOnResumeHoldupMs); |
1490 did_resume_ = false; | 1491 did_resume_ = false; |
1491 } | 1492 } |
1492 } | 1493 } |
1493 | 1494 |
1494 // This database is always synchronous since we don't need to worry about | 1495 // This database is always synchronous since we don't need to worry about |
1495 // blocking any incoming reads. | 1496 // blocking any incoming reads. |
1496 void SafeBrowsingDatabaseBloom::SetSynchronous() { | 1497 void SafeBrowsingDatabaseBloom::SetSynchronous() { |
1497 } | 1498 } |
OLD | NEW |