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 "url/gurl.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // The number of days old that an entry in the precache URL table can be before | |
| 17 // it is considered "old" and is removed from the table. | |
| 18 const int64 kPrecacheHistoryExpiryPeriodDays = 60; | |
| 19 | |
| 20 void RecordSingleDayPrecacheUMA( | |
| 21 const precache::PrecacheStatisticsTable::PrecacheStatistics& stats) { | |
| 22 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedPrecacheMotivatedKB", | |
| 23 stats.downloaded_precache_motivated_bytes / 1024); | |
| 24 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB", | |
| 25 stats.downloaded_non_precache_bytes / 1024); | |
| 26 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB.Cellular", | |
| 27 stats.downloaded_non_precache_bytes_cellular / 1024); | |
| 28 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB", stats.saved_bytes / 1024); | |
| 29 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB.Cellular", | |
| 30 stats.saved_bytes_cellular / 1024); | |
| 31 | |
| 32 if (stats.saved_bytes + stats.downloaded_non_precache_bytes > 0) { | |
| 33 UMA_HISTOGRAM_PERCENTAGE( | |
| 34 "Precache.DailySavingsPercentage", | |
| 35 stats.saved_bytes * 100.0 / | |
| 36 (stats.saved_bytes + stats.downloaded_non_precache_bytes)); | |
| 37 } | |
| 38 | |
| 39 if (stats.saved_bytes_cellular + | |
| 40 stats.downloaded_non_precache_bytes_cellular > 0) { | |
| 41 UMA_HISTOGRAM_PERCENTAGE( | |
| 42 "Precache.DailySavingsPercentage.Cellular", | |
| 43 stats.saved_bytes_cellular * 100.0 / | |
| 44 (stats.saved_bytes_cellular + | |
| 45 stats.downloaded_non_precache_bytes_cellular)); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 namespace precache { | |
| 52 | |
| 53 PrecacheDatabase::PrecacheDatabase() | |
| 54 : precache_url_table_(new PrecacheURLTable()), | |
| 55 precache_statistics_table_(new PrecacheStatisticsTable()) { | |
| 56 // A PrecacheDatabase can be constructed on any thread. | |
| 57 DetachFromThread(); | |
| 58 } | |
| 59 | |
| 60 PrecacheDatabase::~PrecacheDatabase() { | |
| 61 // Since the PrecacheDatabase is refcounted, it will only be deleted if there | |
| 62 // are no references remaining to it, meaning that it is not in use. Thus, it | |
| 63 // is safe to delete it, regardless of what thread we are on. | |
| 64 DetachFromThread(); | |
| 65 } | |
| 66 | |
| 67 void PrecacheDatabase::Init(scoped_ptr<sql::Connection> db) { | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
I'm not seeing where this database is coming from
sclittle
2013/11/19 23:27:10
There will be a PrecacheManager BrowserContextKeye
Scott Hess - ex-Googler
2013/11/27 01:32:33
But why does the caller create the database, as op
sclittle
2013/12/02 21:12:52
Good point. Changed this to take in a path instead
| |
| 68 DCHECK(CalledOnValidThread()); | |
| 69 DCHECK(!db_); // Init must only be called once. | |
| 70 DCHECK(db); // |db| must not be NULL. | |
| 71 | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
sql::Connection has a set_histogram_tag() which in
sclittle
2013/11/19 23:27:10
Done.
| |
| 72 db_ = db.Pass(); | |
| 73 if (!IsDatabaseAccessible()) { | |
| 74 // Don't initialize the URL table or statistics table if unable to access | |
| 75 // the database. | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 precache_url_table_->Init(db_.get()); | |
| 80 precache_statistics_table_->Init(db_.get()); | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These should probably be in a transaction.
sclittle
2013/11/19 23:27:10
Done.
| |
| 81 } | |
| 82 | |
| 83 void PrecacheDatabase::ReportAndDeleteOldStats(const base::Time& current_time) { | |
| 84 DCHECK(CalledOnValidThread()); | |
| 85 DCHECK(db_); | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These are implicit in IsDatabaseAccessible().
sclittle
2013/11/19 23:27:10
Done.
| |
| 86 | |
| 87 if (!IsDatabaseAccessible()) { | |
| 88 // Do nothing if unable to access the database. | |
| 89 return; | |
| 90 } | |
| 91 | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
You should probably start a transaction here to en
sclittle
2013/11/19 23:27:10
Done.
| |
| 92 // Delete old precache history that has expired. | |
| 93 precache_url_table_->DeleteAllPrecachedBefore( | |
| 94 current_time - | |
| 95 base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays)); | |
| 96 | |
| 97 PrecacheStatisticsTable::PrecacheStatisticsMap stats_map; | |
| 98 precache_statistics_table_->GetOldStats(current_time, &stats_map); | |
| 99 | |
| 100 // Report UMA for every row of old statistics in the statistics table. There | |
| 101 // won't be any rows in the statistics table for days when nothing was fetched | |
| 102 // or precached. | |
| 103 for (PrecacheStatisticsTable::PrecacheStatisticsMap::const_iterator it = | |
| 104 stats_map.begin(); | |
| 105 it != stats_map.end(); ++it) { | |
| 106 RecordSingleDayPrecacheUMA(it->second); | |
| 107 } | |
| 108 | |
| 109 precache_statistics_table_->DeleteOldStats(current_time); | |
| 110 } | |
| 111 | |
| 112 void PrecacheDatabase::RecordURLPrecached(const GURL& url, | |
| 113 const base::Time& fetch_time, | |
| 114 int64 size, bool was_cached) { | |
| 115 DCHECK(CalledOnValidThread()); | |
| 116 DCHECK(db_); | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These are implicit in IsDatabaseAccessible().
sclittle
2013/11/19 23:27:10
Done.
| |
| 117 | |
| 118 if (!IsDatabaseAccessible()) { | |
| 119 // Don't track anything if unable to access the database. | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 124 // Since the precache came from the cache, and there's no entry in the URL | |
| 125 // table for the URL, this means that the resource was already in the cache | |
| 126 // because of user browsing. Thus, this precache had no effect, so ignore | |
| 127 // it. | |
| 128 return; | |
| 129 } | |
| 130 | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
Probably should start a transaction here.
sclittle
2013/11/19 23:27:10
Done.
| |
| 131 if (!was_cached) { | |
| 132 // The precache only counts as overhead if it was downloaded over the | |
| 133 // network. | |
| 134 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 135 stats.downloaded_precache_motivated_bytes = size; | |
| 136 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); | |
| 137 } | |
| 138 | |
| 139 // Use the URL table to keep track of URLs that are in the cache thanks to | |
| 140 // precaching. If a row for the URL already exists, than update the timestamp | |
| 141 // to |fetch_time|. | |
| 142 precache_url_table_->AddURL(url, fetch_time); | |
| 143 } | |
| 144 | |
| 145 void PrecacheDatabase::RecordURLFetched(const GURL& url, | |
| 146 const base::Time& fetch_time, | |
| 147 int64 size, bool was_cached, | |
| 148 bool is_connection_cellular) { | |
| 149 DCHECK(CalledOnValidThread()); | |
| 150 DCHECK(db_); | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
These are implicit in IsDatabaseAccessible().
sclittle
2013/11/19 23:27:10
Done.
| |
| 151 | |
| 152 if (!IsDatabaseAccessible()) { | |
| 153 // Don't track anything if unable to access the database. | |
| 154 return; | |
| 155 } | |
| 156 | |
|
Scott Hess - ex-Googler
2013/11/19 18:47:48
Probably should open a transaction here.
sclittle
2013/11/19 23:27:10
Done.
| |
| 157 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 158 // Ignore cache hits that precache can't take credit for. | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 163 | |
| 164 if (!was_cached) { | |
| 165 // The fetch was served over the network during user browsing, so count it | |
| 166 // as downloaded non-precache bytes. | |
| 167 stats.downloaded_non_precache_bytes = size; | |
| 168 if (is_connection_cellular) { | |
| 169 stats.downloaded_non_precache_bytes_cellular = size; | |
| 170 } | |
| 171 } else { | |
| 172 // The fetch was served from the cache, and since there's an entry for this | |
| 173 // URL in the URL table, this means that the resource was served from the | |
| 174 // cache only because precaching put it there. Thus, precaching was helpful, | |
| 175 // so count the fetch as saved bytes. | |
| 176 stats.saved_bytes = size; | |
| 177 if (is_connection_cellular) { | |
| 178 stats.saved_bytes_cellular = size; | |
| 179 } | |
| 180 } | |
| 181 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); | |
| 182 | |
| 183 // Since the resource has been fetched during user browsing, remove any record | |
| 184 // of that URL having been precached from the URL table, if any exists. | |
| 185 // The current fetch would have put this resource in the cache regardless of | |
| 186 // whether or not it was previously precached, so delete any record of that | |
| 187 // URL having been precached from the URL table. | |
| 188 precache_url_table_->DeleteURL(url); | |
| 189 } | |
| 190 | |
| 191 bool PrecacheDatabase::IsDatabaseAccessible() const { | |
| 192 DCHECK(CalledOnValidThread()); | |
| 193 DCHECK(db_); | |
| 194 | |
| 195 return db_->is_open(); | |
| 196 } | |
| 197 | |
| 198 } // namespace precache | |
| OLD | NEW |