Stack sampling profiler: add fire-and-forget interface
[chromium-blink-merge.git] / components / precache / core / precache_database.cc
bloba6580be463c83d290aab3f9269bede9209d5e994
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.
5 #include "components/precache/core/precache_database.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/location.h"
10 #include "base/metrics/histogram_macros.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h"
14 #include "sql/connection.h"
15 #include "sql/transaction.h"
16 #include "url/gurl.h"
18 namespace {
20 // The number of days old that an entry in the precache URL table can be before
21 // it is considered "old" and is removed from the table.
22 const int kPrecacheHistoryExpiryPeriodDays = 60;
24 } // namespace
26 namespace precache {
28 PrecacheDatabase::PrecacheDatabase() : is_flush_posted_(false) {
29 // A PrecacheDatabase can be constructed on any thread.
30 thread_checker_.DetachFromThread();
33 PrecacheDatabase::~PrecacheDatabase() {
34 // Since the PrecacheDatabase is refcounted, it will only be deleted if there
35 // are no references remaining to it, meaning that it is not in use. Thus, it
36 // is safe to delete it, regardless of what thread we are on.
37 thread_checker_.DetachFromThread();
40 bool PrecacheDatabase::Init(const base::FilePath& db_path) {
41 DCHECK(thread_checker_.CalledOnValidThread());
42 DCHECK(!db_); // Init must only be called once.
44 db_.reset(new sql::Connection());
45 db_->set_histogram_tag("Precache");
47 if (!db_->Open(db_path)) {
48 // Don't initialize the URL table if unable to access
49 // the database.
50 return false;
53 if (!precache_url_table_.Init(db_.get())) {
54 // Raze and close the database connection to indicate that it's not usable,
55 // and so that the database will be created anew next time, in case it's
56 // corrupted.
57 db_->RazeAndClose();
58 return false;
60 return true;
63 void PrecacheDatabase::DeleteExpiredPrecacheHistory(
64 const base::Time& current_time) {
65 if (!IsDatabaseAccessible()) {
66 // Do nothing if unable to access the database.
67 return;
70 // Delete old precache history that has expired.
71 base::Time delete_end = current_time - base::TimeDelta::FromDays(
72 kPrecacheHistoryExpiryPeriodDays);
73 buffered_writes_.push_back(
74 base::Bind(&PrecacheURLTable::DeleteAllPrecachedBefore,
75 base::Unretained(&precache_url_table_), delete_end));
76 Flush();
79 void PrecacheDatabase::ClearHistory() {
80 if (!IsDatabaseAccessible()) {
81 // Do nothing if unable to access the database.
82 return;
85 buffered_writes_.push_back(base::Bind(
86 &PrecacheURLTable::DeleteAll, base::Unretained(&precache_url_table_)));
87 Flush();
90 void PrecacheDatabase::RecordURLPrecached(const GURL& url,
91 const base::Time& fetch_time,
92 int64 size, bool was_cached) {
93 if (!IsDatabaseAccessible()) {
94 // Don't track anything if unable to access the database.
95 return;
98 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) {
99 // If the URL for this fetch is in the write buffer, then flush the write
100 // buffer.
101 Flush();
104 if (was_cached && !precache_url_table_.HasURL(url)) {
105 // Since the precache came from the cache, and there's no entry in the URL
106 // table for the URL, this means that the resource was already in the cache
107 // because of user browsing. Thus, this precache had no effect, so ignore
108 // it.
109 return;
112 if (!was_cached) {
113 // The precache only counts as overhead if it was downloaded over the
114 // network.
115 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated",
116 static_cast<base::HistogramBase::Sample>(size));
119 // Use the URL table to keep track of URLs that are in the cache thanks to
120 // precaching. If a row for the URL already exists, than update the timestamp
121 // to |fetch_time|.
122 buffered_writes_.push_back(
123 base::Bind(&PrecacheURLTable::AddURL,
124 base::Unretained(&precache_url_table_), url, fetch_time));
125 buffered_urls_.insert(url.spec());
126 MaybePostFlush();
129 void PrecacheDatabase::RecordURLFetched(const GURL& url,
130 const base::Time& fetch_time,
131 int64 size, bool was_cached,
132 bool is_connection_cellular) {
133 if (!IsDatabaseAccessible()) {
134 // Don't track anything if unable to access the database.
135 return;
138 if (buffered_urls_.find(url.spec()) != buffered_urls_.end()) {
139 // If the URL for this fetch is in the write buffer, then flush the write
140 // buffer.
141 Flush();
144 if (was_cached && !precache_url_table_.HasURL(url)) {
145 // Ignore cache hits that precache can't take credit for.
146 return;
149 base::HistogramBase::Sample size_sample =
150 static_cast<base::HistogramBase::Sample>(size);
151 if (!was_cached) {
152 // The fetch was served over the network during user browsing, so count it
153 // as downloaded non-precache bytes.
154 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", size_sample);
155 if (is_connection_cellular) {
156 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular",
157 size_sample);
159 } else {
160 // The fetch was served from the cache, and since there's an entry for this
161 // URL in the URL table, this means that the resource was served from the
162 // cache only because precaching put it there. Thus, precaching was helpful,
163 // so count the fetch as saved bytes.
164 UMA_HISTOGRAM_COUNTS("Precache.Saved", size_sample);
165 if (is_connection_cellular) {
166 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", size_sample);
170 // Since the resource has been fetched during user browsing, remove any record
171 // of that URL having been precached from the URL table, if any exists.
172 // The current fetch would have put this resource in the cache regardless of
173 // whether or not it was previously precached, so delete any record of that
174 // URL having been precached from the URL table.
175 buffered_writes_.push_back(
176 base::Bind(&PrecacheURLTable::DeleteURL,
177 base::Unretained(&precache_url_table_), url));
178 buffered_urls_.insert(url.spec());
179 MaybePostFlush();
182 bool PrecacheDatabase::IsDatabaseAccessible() const {
183 DCHECK(thread_checker_.CalledOnValidThread());
184 DCHECK(db_);
186 return db_->is_open();
189 void PrecacheDatabase::Flush() {
190 DCHECK(thread_checker_.CalledOnValidThread());
191 if (buffered_writes_.empty()) {
192 // Do nothing if there's nothing to flush.
193 DCHECK(buffered_urls_.empty());
194 return;
197 if (IsDatabaseAccessible()) {
198 sql::Transaction transaction(db_.get());
199 if (transaction.Begin()) {
200 for (std::vector<base::Closure>::const_iterator it =
201 buffered_writes_.begin();
202 it != buffered_writes_.end(); ++it) {
203 it->Run();
206 transaction.Commit();
210 // Clear the buffer, even if the database is inaccessible or unable to begin a
211 // transaction.
212 buffered_writes_.clear();
213 buffered_urls_.clear();
216 void PrecacheDatabase::PostedFlush() {
217 DCHECK(thread_checker_.CalledOnValidThread());
218 DCHECK(is_flush_posted_);
219 is_flush_posted_ = false;
220 Flush();
223 void PrecacheDatabase::MaybePostFlush() {
224 DCHECK(thread_checker_.CalledOnValidThread());
226 if (buffered_writes_.empty() || is_flush_posted_) {
227 // There's no point in posting a flush if there's nothing to be flushed or
228 // if a flush has already been posted.
229 return;
232 // Post a delayed task to flush the buffer in 1 second, so that multiple
233 // database writes can be buffered up and flushed together in the same
234 // transaction.
235 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
236 FROM_HERE, base::Bind(&PrecacheDatabase::PostedFlush,
237 scoped_refptr<PrecacheDatabase>(this)),
238 base::TimeDelta::FromSeconds(1));
239 is_flush_posted_ = true;
242 } // namespace precache