Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/precache/core/precache_database.h" | |
| 6 | |
| 7 #include "base/metrics/histogram.h" | |
| 8 #include "base/time/time.h" | |
| 9 #include "components/precache/core/precache_statistics_table.h" | |
| 10 #include "components/precache/core/precache_url_table.h" | |
| 11 #include "sql/connection.h" | |
| 12 #include "sql/transaction.h" | |
| 13 #include "url/gurl.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // The number of days old that an entry in the precache URL table can be before | |
| 18 // it is considered "old" and is removed from the table. | |
| 19 const int64 kPrecacheHistoryExpiryPeriodDays = 60; | |
|
Scott Hess - ex-Googler
2013/11/27 01:32:33
Just |int| unless being 64-bit is somehow relevant
sclittle
2013/12/02 21:12:52
Done.
| |
| 20 | |
| 21 void RecordSingleDayPrecacheUMA( | |
| 22 const precache::PrecacheStatisticsTable::PrecacheStatistics& stats) { | |
| 23 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedPrecacheMotivatedKB", | |
| 24 stats.downloaded_precache_motivated_bytes / 1024); | |
| 25 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB", | |
| 26 stats.downloaded_non_precache_bytes / 1024); | |
| 27 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB.Cellular", | |
| 28 stats.downloaded_non_precache_bytes_cellular / 1024); | |
| 29 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB", stats.saved_bytes / 1024); | |
| 30 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB.Cellular", | |
| 31 stats.saved_bytes_cellular / 1024); | |
| 32 | |
| 33 if (stats.saved_bytes + stats.downloaded_non_precache_bytes > 0) { | |
| 34 UMA_HISTOGRAM_PERCENTAGE( | |
| 35 "Precache.DailySavingsPercentage", | |
| 36 stats.saved_bytes * 100.0 / | |
| 37 (stats.saved_bytes + stats.downloaded_non_precache_bytes)); | |
| 38 } | |
| 39 | |
| 40 if (stats.saved_bytes_cellular + | |
| 41 stats.downloaded_non_precache_bytes_cellular > 0) { | |
| 42 UMA_HISTOGRAM_PERCENTAGE( | |
| 43 "Precache.DailySavingsPercentage.Cellular", | |
| 44 stats.saved_bytes_cellular * 100.0 / | |
| 45 (stats.saved_bytes_cellular + | |
| 46 stats.downloaded_non_precache_bytes_cellular)); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 namespace precache { | |
| 53 | |
| 54 PrecacheDatabase::PrecacheDatabase() | |
| 55 : precache_url_table_(new PrecacheURLTable()), | |
| 56 precache_statistics_table_(new PrecacheStatisticsTable()) { | |
| 57 // A PrecacheDatabase can be constructed on any thread. | |
| 58 thread_checker_.DetachFromThread(); | |
| 59 } | |
| 60 | |
| 61 PrecacheDatabase::~PrecacheDatabase() { | |
| 62 // Since the PrecacheDatabase is refcounted, it will only be deleted if there | |
| 63 // are no references remaining to it, meaning that it is not in use. Thus, it | |
| 64 // is safe to delete it, regardless of what thread we are on. | |
| 65 thread_checker_.DetachFromThread(); | |
|
Scott Hess - ex-Googler
2013/11/27 01:32:33
Note that sql::Connection doesn't make thread-safe
sclittle
2013/12/02 21:12:52
Ok.
| |
| 66 } | |
| 67 | |
| 68 void PrecacheDatabase::Init(scoped_ptr<sql::Connection> db) { | |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 70 DCHECK(!db_); // Init must only be called once. | |
| 71 DCHECK(db); // |db| must not be NULL. | |
| 72 | |
| 73 db_ = db.Pass(); | |
| 74 db_->set_histogram_tag("Precache"); | |
| 75 | |
| 76 if (!IsDatabaseAccessible()) { | |
| 77 // Don't initialize the URL table or statistics table if unable to access | |
| 78 // the database. | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 bool initialization_was_successful = false; | |
| 83 | |
| 84 sql::Transaction transaction(db_.get()); | |
| 85 if (transaction.Begin()) { | |
| 86 precache_url_table_->Init(db_.get()); | |
| 87 precache_statistics_table_->Init(db_.get()); | |
| 88 initialization_was_successful = transaction.Commit(); | |
| 89 } | |
| 90 | |
| 91 if (!initialization_was_successful) { | |
| 92 // Raze and close the database connection to indicate that it's not usable, | |
| 93 // and so that the database will be created anew next time, in case it's | |
| 94 // corrupted. | |
| 95 db_->RazeAndClose(); | |
|
Scott Hess - ex-Googler
2013/11/27 01:32:33
Note that RazeAndClose() is a bit of a misnomer -
sclittle
2013/12/02 21:12:52
Ok
| |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void PrecacheDatabase::ReportAndDeleteOldStats(const base::Time& current_time) { | |
| 100 if (!IsDatabaseAccessible()) { | |
| 101 // Do nothing if unable to access the database. | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 sql::Transaction transaction(db_.get()); | |
| 106 if (!transaction.Begin()) { | |
| 107 // Do nothing if unable to begin a transaction. | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 // Delete old precache history that has expired. | |
| 112 precache_url_table_->DeleteAllPrecachedBefore( | |
| 113 current_time - | |
| 114 base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays)); | |
| 115 | |
| 116 PrecacheStatisticsTable::PrecacheStatisticsMap stats_map; | |
| 117 precache_statistics_table_->GetOldStats(current_time, &stats_map); | |
| 118 | |
| 119 precache_statistics_table_->DeleteOldStats(current_time); | |
| 120 | |
| 121 if (!transaction.Commit()) { | |
| 122 // If the database transaction was rolled back, then don't report any | |
| 123 // precache UMA, to avoid double-counting any stats. | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 // Report UMA for every row of old statistics in the statistics table. There | |
| 128 // won't be any rows in the statistics table for days when nothing was fetched | |
| 129 // or precached. | |
| 130 for (PrecacheStatisticsTable::PrecacheStatisticsMap::const_iterator it = | |
| 131 stats_map.begin(); | |
| 132 it != stats_map.end(); ++it) { | |
| 133 RecordSingleDayPrecacheUMA(it->second); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void PrecacheDatabase::RecordURLPrecached(const GURL& url, | |
| 138 const base::Time& fetch_time, | |
| 139 int64 size, bool was_cached) { | |
| 140 if (!IsDatabaseAccessible()) { | |
| 141 // Don't track anything if unable to access the database. | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 sql::Transaction transaction(db_.get()); | |
| 146 if (!transaction.Begin()) { | |
| 147 // Do nothing if unable to begin a transaction. | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 152 // Since the precache came from the cache, and there's no entry in the URL | |
| 153 // table for the URL, this means that the resource was already in the cache | |
| 154 // because of user browsing. Thus, this precache had no effect, so ignore | |
| 155 // it. | |
| 156 return; | |
| 157 } | |
| 158 | |
| 159 if (!was_cached) { | |
| 160 // The precache only counts as overhead if it was downloaded over the | |
| 161 // network. | |
| 162 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 163 stats.downloaded_precache_motivated_bytes = size; | |
| 164 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); | |
| 165 } | |
| 166 | |
| 167 // Use the URL table to keep track of URLs that are in the cache thanks to | |
| 168 // precaching. If a row for the URL already exists, than update the timestamp | |
| 169 // to |fetch_time|. | |
| 170 precache_url_table_->AddURL(url, fetch_time); | |
| 171 | |
| 172 transaction.Commit(); | |
| 173 } | |
| 174 | |
| 175 void PrecacheDatabase::RecordURLFetched(const GURL& url, | |
| 176 const base::Time& fetch_time, | |
| 177 int64 size, bool was_cached, | |
| 178 bool is_connection_cellular) { | |
| 179 if (!IsDatabaseAccessible()) { | |
| 180 // Don't track anything if unable to access the database. | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 sql::Transaction transaction(db_.get()); | |
| 185 if (!transaction.Begin()) { | |
| 186 // Do nothing if unable to begin a transaction. | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 191 // Ignore cache hits that precache can't take credit for. | |
| 192 return; | |
| 193 } | |
| 194 | |
| 195 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 196 | |
| 197 if (!was_cached) { | |
| 198 // The fetch was served over the network during user browsing, so count it | |
| 199 // as downloaded non-precache bytes. | |
| 200 stats.downloaded_non_precache_bytes = size; | |
| 201 if (is_connection_cellular) { | |
| 202 stats.downloaded_non_precache_bytes_cellular = size; | |
| 203 } | |
| 204 } else { | |
| 205 // The fetch was served from the cache, and since there's an entry for this | |
| 206 // URL in the URL table, this means that the resource was served from the | |
| 207 // cache only because precaching put it there. Thus, precaching was helpful, | |
| 208 // so count the fetch as saved bytes. | |
| 209 stats.saved_bytes = size; | |
| 210 if (is_connection_cellular) { | |
| 211 stats.saved_bytes_cellular = size; | |
| 212 } | |
| 213 } | |
| 214 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); | |
| 215 | |
| 216 // Since the resource has been fetched during user browsing, remove any record | |
| 217 // of that URL having been precached from the URL table, if any exists. | |
| 218 // The current fetch would have put this resource in the cache regardless of | |
| 219 // whether or not it was previously precached, so delete any record of that | |
| 220 // URL having been precached from the URL table. | |
| 221 precache_url_table_->DeleteURL(url); | |
| 222 | |
| 223 transaction.Commit(); | |
| 224 } | |
| 225 | |
| 226 bool PrecacheDatabase::IsDatabaseAccessible() const { | |
| 227 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 228 DCHECK(db_); | |
| 229 | |
| 230 return db_->is_open(); | |
| 231 } | |
| 232 | |
| 233 } // namespace precache | |
| OLD | NEW |