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 <map> | |
| 8 | |
| 9 #include "base/metrics/histogram.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "components/precache/core/precache_statistics_table.h" | |
| 12 #include "components/precache/core/precache_url_table.h" | |
| 13 #include "sql/connection.h" | |
| 14 #include "url/gurl.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // The number of days old that an entry in the precache URL table can be before | |
| 19 // it is considered "old" and is removed from the table. | |
| 20 const int64 kPrecacheHistoryExpiryPeriodDays = 60; | |
| 21 | |
| 22 // The default max range of UMA_HISTOGRAM_COUNTS is 1 million, which is too | |
| 23 // small for tracking precache byte counts, since counts can easily be greater | |
| 24 // than 1 MB. | |
| 25 #define UMA_HISTOGRAM_BYTES(name, sample) \ | |
| 26 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 1, 1000000000, 50); | |
|
mmenke
2013/10/29 18:23:34
Samples are integers, aren't they? If that's the
mmenke
2013/10/29 18:23:34
I think we really should change the name. One cou
sclittle
2013/10/29 23:40:45
Done.
sclittle
2013/10/29 23:40:45
Changed to report values in KB using UMA_HISTOGRAM
| |
| 27 | |
| 28 void RecordSingleDayPrecacheUMA( | |
| 29 const precache::PrecacheStatisticsTable::PrecacheStatistics& stats) { | |
| 30 UMA_HISTOGRAM_BYTES("Precache.DailyPrecachedBytes", stats.precached_bytes); | |
| 31 UMA_HISTOGRAM_BYTES("Precache.DailyDownloadedBytes", stats.downloaded_bytes); | |
| 32 UMA_HISTOGRAM_BYTES("Precache.DailyDownloadedBytes.Cellular", | |
| 33 stats.downloaded_bytes_cellular); | |
| 34 UMA_HISTOGRAM_BYTES("Precache.DailySavedBytes", stats.saved_bytes); | |
| 35 UMA_HISTOGRAM_BYTES("Precache.DailySavedBytes.Cellular", | |
| 36 stats.saved_bytes_cellular); | |
| 37 | |
| 38 if (stats.saved_bytes + stats.downloaded_bytes > 0) { | |
| 39 UMA_HISTOGRAM_PERCENTAGE("Precache.DailySavingsPercentage", | |
| 40 stats.saved_bytes * 100.0 / | |
| 41 (stats.saved_bytes + stats.downloaded_bytes)); | |
| 42 } | |
| 43 | |
| 44 if (stats.saved_bytes_cellular + stats.downloaded_bytes_cellular > 0) { | |
| 45 UMA_HISTOGRAM_PERCENTAGE( | |
| 46 "Precache.DailySavingsPercentage.Cellular", | |
| 47 stats.saved_bytes_cellular * 100.0 / | |
| 48 (stats.saved_bytes_cellular + stats.downloaded_bytes_cellular)); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 #undef UMA_HISTOGRAM_BYTES | |
|
mmenke
2013/10/29 18:23:34
Don't think this is needed, and #undef seems prett
sclittle
2013/10/29 23:40:45
Removed.
| |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 namespace precache { | |
| 57 | |
| 58 PrecacheDatabase::PrecacheDatabase() | |
| 59 : precache_url_table_(new PrecacheURLTable()), | |
| 60 precache_statistics_table_(new PrecacheStatisticsTable()) { | |
| 61 // A PrecacheDatabase can be constructed on any thread. | |
| 62 DetachFromThread(); | |
|
mmenke
2013/10/29 18:23:34
Does this work? Looks like the parent's destructo
sclittle
2013/10/29 23:40:45
If the thread ID is not set, CalledOnValidThread()
| |
| 63 } | |
| 64 | |
| 65 PrecacheDatabase::~PrecacheDatabase() { | |
| 66 // Since the PrecacheDatabase is refcounted, it will only be deleted if there | |
| 67 // are no references remaining to it, meaning that it is not in use. Thus, it | |
| 68 // is safe to delete it, regardless of what thread we are on. | |
| 69 DetachFromThread(); | |
| 70 } | |
| 71 | |
| 72 void PrecacheDatabase::Init(sql::Connection* db) { | |
| 73 DCHECK(CalledOnValidThread()); | |
| 74 DCHECK(!db_); // Init must only be called once. | |
| 75 DCHECK(db); // |db| must not be NULL. | |
| 76 | |
| 77 db_.reset(db); | |
| 78 if (CantAccessDatabase()) { | |
| 79 // Don't initialize the URL table or statistics table if unable to access | |
| 80 // the database. | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 precache_url_table_->Init(db_.get()); | |
| 85 precache_statistics_table_->Init(db_.get()); | |
| 86 } | |
| 87 | |
| 88 void PrecacheDatabase::ReportAndDeleteOldStats(base::Time end_date) { | |
| 89 DCHECK(CalledOnValidThread()); | |
| 90 DCHECK(db_); | |
| 91 | |
| 92 if (CantAccessDatabase()) { | |
| 93 // Do nothing if unable to access the database. | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 // Delete old precache history that has expired. | |
| 98 precache_url_table_->DeleteAllPrecachedBetween( | |
| 99 base::Time::FromInternalValue(0), | |
|
mmenke
2013/10/29 18:23:34
Can't this just be base::Time()?
sclittle
2013/10/29 23:40:45
Done.
| |
| 100 end_date - base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays)); | |
| 101 | |
| 102 std::map<base::Time, PrecacheStatisticsTable::PrecacheStatistics> stats_map; | |
|
mmenke
2013/10/29 18:23:34
This is really crying out for a public typedef in
sclittle
2013/10/29 23:40:45
Done.
| |
| 103 precache_statistics_table_->GetAllStatsBetween( | |
| 104 base::Time::FromInternalValue(0), end_date, &stats_map); | |
|
mmenke
2013/10/29 18:23:34
base::Time(0)?
sclittle
2013/10/29 23:40:45
Changed to base::Time().
| |
| 105 | |
| 106 // Report UMA for every row of old statistics in the statistics table. There | |
| 107 // won't be any rows in the statistics table for days when nothing was fetched | |
| 108 // or precached. | |
| 109 for (std::map<base::Time, | |
| 110 PrecacheStatisticsTable::PrecacheStatistics>::const_iterator | |
| 111 it = stats_map.begin(); | |
| 112 it != stats_map.end(); ++it) { | |
| 113 RecordSingleDayPrecacheUMA(it->second); | |
| 114 } | |
| 115 | |
| 116 precache_statistics_table_->DeleteAllStatsBetween( | |
| 117 base::Time::FromInternalValue(0), end_date); | |
| 118 } | |
| 119 | |
| 120 void PrecacheDatabase::RecordURLFetched(GURL url, base::Time fetch_time, | |
| 121 int64 size, bool was_cached, | |
| 122 bool is_precaching, bool is_cellular) { | |
|
mmenke
2013/10/29 18:23:34
Should we separate out those resources we had to r
sclittle
2013/10/29 23:40:45
That's true, but for individual fetches we're just
| |
| 123 DCHECK(CalledOnValidThread()); | |
| 124 DCHECK(db_); | |
| 125 | |
| 126 if (CantAccessDatabase()) { | |
|
mmenke
2013/10/29 18:23:34
Should we just DCHECK on this instead, or is it an
sclittle
2013/10/29 23:40:45
If the user ran out of disk space or something and
| |
| 127 // Don't track anything if unable to access the database. | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 if (is_precaching) { | |
| 132 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 133 // If a precache fetch came from the cache, and the user browsed the | |
| 134 // resource recently, then the precache fetch did nothing, so ignore it. | |
|
mmenke
2013/10/29 18:23:34
I'm not following...The table should have the URL
sclittle
2013/10/29 23:40:45
The URL table only keeps track of URLs that are in
mmenke
2013/10/30 18:29:08
You seem to add everything we download to the tabl
sclittle
2013/10/30 20:29:48
The purpose of the URL table is to allow us to ide
| |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 if (!was_cached) { | |
| 139 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 140 stats.precached_bytes = size; | |
| 141 precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats); | |
|
mmenke
2013/10/29 18:23:34
Why are we only setting one of the fields? I'm co
sclittle
2013/10/29 23:40:45
All fields in the PrecacheStatistics default to 0.
| |
| 142 } | |
| 143 | |
| 144 precache_url_table_->AddURL(url, fetch_time); | |
| 145 return; | |
| 146 } | |
| 147 | |
| 148 if (!was_cached) { | |
| 149 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 150 stats.downloaded_bytes = size; | |
| 151 if (is_cellular) { | |
| 152 stats.downloaded_bytes_cellular = size; | |
| 153 } | |
| 154 precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats); | |
| 155 | |
| 156 // Since the fetch was over the network, delete any record of it having been | |
| 157 // prefetched from the URL table. If it had been precached, the resource | |
| 158 // must have expired. | |
| 159 precache_url_table_->DeleteURL(url); | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 if (was_cached && precache_url_table_->HasURL(url)) { | |
| 164 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 165 stats.saved_bytes = size; | |
| 166 if (is_cellular) { | |
| 167 stats.saved_bytes_cellular = size; | |
| 168 } | |
| 169 precache_statistics_table_->IncreaseStatsForFetch(fetch_time, stats); | |
|
mmenke
2013/10/29 18:23:34
When not precaching, we record stats if it wasn't
sclittle
2013/10/29 23:40:45
Added comment.
| |
| 170 | |
| 171 precache_url_table_->DeleteURL(url); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 bool PrecacheDatabase::CantAccessDatabase() { | |
|
mmenke
2013/10/29 18:23:34
nit: I think it's easy to miss the "T" when readi
mmenke
2013/10/29 18:23:34
Can this be const?
sclittle
2013/10/29 23:40:45
Done.
sclittle
2013/10/29 23:40:45
Done.
| |
| 176 DCHECK(CalledOnValidThread()); | |
| 177 DCHECK(db_); | |
| 178 | |
| 179 return !db_->is_open(); | |
| 180 } | |
| 181 | |
| 182 } // namespace precache | |
| OLD | NEW |