Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(717)

Side by Side Diff: chrome/browser/net/predictor.cc

Issue 15675002: Add the UMA recording recall of URLRequests preconnected. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move logic to predictor Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/net/predictor.h" 5 #include "chrome/browser/net/predictor.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <set> 9 #include <set>
10 #include <sstream> 10 #include <sstream>
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 url_request_context_getter_(NULL), 129 url_request_context_getter_(NULL),
130 predictor_enabled_(true), 130 predictor_enabled_(true),
131 peak_pending_lookups_(0), 131 peak_pending_lookups_(0),
132 shutdown_(false), 132 shutdown_(false),
133 max_concurrent_dns_lookups_(g_max_parallel_resolves), 133 max_concurrent_dns_lookups_(g_max_parallel_resolves),
134 max_dns_queue_delay_( 134 max_dns_queue_delay_(
135 TimeDelta::FromMilliseconds(g_max_queueing_delay_ms)), 135 TimeDelta::FromMilliseconds(g_max_queueing_delay_ms)),
136 host_resolver_(NULL), 136 host_resolver_(NULL),
137 preconnect_enabled_(preconnect_enabled), 137 preconnect_enabled_(preconnect_enabled),
138 consecutive_omnibox_preconnect_count_(0), 138 consecutive_omnibox_preconnect_count_(0),
139 recent_preconnects_(
140 TimeDelta::FromSeconds(kMaxUnusedSocketLifetimeSecondsWithoutAGet)),
139 next_trim_time_(base::TimeTicks::Now() + 141 next_trim_time_(base::TimeTicks::Now() +
140 TimeDelta::FromHours(kDurationBetweenTrimmingsHours)) { 142 TimeDelta::FromHours(kDurationBetweenTrimmingsHours)) {
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
142 } 144 }
143 145
144 Predictor::~Predictor() { 146 Predictor::~Predictor() {
145 // TODO(rlp): Add DCHECK for CurrentlyOn(BrowserThread::IO) when the 147 // TODO(rlp): Add DCHECK for CurrentlyOn(BrowserThread::IO) when the
146 // ProfileManagerTest has been updated with a mock profile. 148 // ProfileManagerTest has been updated with a mock profile.
147 DCHECK(shutdown_); 149 DCHECK(shutdown_);
148 } 150 }
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 // pool. Currently, we just do a connect, which MAY be reset if we 238 // pool. Currently, we just do a connect, which MAY be reset if we
237 // don't use it in 10 secondes!!! As a result, we may do more 239 // don't use it in 10 secondes!!! As a result, we may do more
238 // connections, and actually cost the server more than if we did a real 240 // connections, and actually cost the server more than if we did a real
239 // get with a fake request (/gen_204 might be the good path on Google). 241 // get with a fake request (/gen_204 might be the good path on Google).
240 const int kMaxSearchKeepaliveSeconds(10); 242 const int kMaxSearchKeepaliveSeconds(10);
241 if ((now - last_omnibox_preconnect_).InSeconds() < 243 if ((now - last_omnibox_preconnect_).InSeconds() <
242 kMaxSearchKeepaliveSeconds) 244 kMaxSearchKeepaliveSeconds)
243 return; // We've done a preconnect recently. 245 return; // We've done a preconnect recently.
244 last_omnibox_preconnect_ = now; 246 last_omnibox_preconnect_ = now;
245 const int kConnectionsNeeded = 1; 247 const int kConnectionsNeeded = 1;
246 PreconnectOnUIThread(CanonicalizeUrl(url), GURL(), motivation, 248 PreconnectUrl(CanonicalizeUrl(url), GURL(), motivation,
247 kConnectionsNeeded, 249 kConnectionsNeeded);
248 url_request_context_getter_);
249 return; // Skip pre-resolution, since we'll open a connection. 250 return; // Skip pre-resolution, since we'll open a connection.
250 } 251 }
251 } else { 252 } else {
252 consecutive_omnibox_preconnect_count_ = 0; 253 consecutive_omnibox_preconnect_count_ = 0;
253 } 254 }
254 } 255 }
255 256
256 // Fall through and consider pre-resolution. 257 // Fall through and consider pre-resolution.
257 258
258 // Omnibox tends to call in pairs (just a few milliseconds apart), and we 259 // Omnibox tends to call in pairs (just a few milliseconds apart), and we
(...skipping 18 matching lines...) Expand all
277 const GURL& first_party_for_cookies) { 278 const GURL& first_party_for_cookies) {
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
279 if (!predictor_enabled_) 280 if (!predictor_enabled_)
280 return; 281 return;
281 if (!url.is_valid() || !url.has_host()) 282 if (!url.is_valid() || !url.has_host())
282 return; 283 return;
283 if (preconnect_enabled()) { 284 if (preconnect_enabled()) {
284 std::string host = url.HostNoBrackets(); 285 std::string host = url.HostNoBrackets();
285 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED); 286 UrlInfo::ResolutionMotivation motivation(UrlInfo::EARLY_LOAD_MOTIVATED);
286 const int kConnectionsNeeded = 1; 287 const int kConnectionsNeeded = 1;
287 PreconnectOnUIThread(CanonicalizeUrl(url), first_party_for_cookies, 288 PreconnectUrl(CanonicalizeUrl(url), first_party_for_cookies,
288 motivation, kConnectionsNeeded, 289 motivation, kConnectionsNeeded);
289 url_request_context_getter_);
290 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies); 290 PredictFrameSubresources(url.GetWithEmptyPath(), first_party_for_cookies);
291 } 291 }
292 } 292 }
293 293
294 UrlList Predictor::GetPredictedUrlListAtStartup( 294 UrlList Predictor::GetPredictedUrlListAtStartup(
295 PrefService* user_prefs, 295 PrefService* user_prefs,
296 PrefService* local_state) { 296 PrefService* local_state) {
297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
298 UrlList urls; 298 UrlList urls;
299 // Recall list of URLs we learned about during last session. 299 // Recall list of URLs we learned about during last session.
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 base::Bind(&Predictor::EnablePredictorOnIOThread, 817 base::Bind(&Predictor::EnablePredictorOnIOThread,
818 base::Unretained(this), enable)); 818 base::Unretained(this), enable));
819 } 819 }
820 } 820 }
821 821
822 void Predictor::EnablePredictorOnIOThread(bool enable) { 822 void Predictor::EnablePredictorOnIOThread(bool enable) {
823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 823 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
824 predictor_enabled_ = enable; 824 predictor_enabled_ = enable;
825 } 825 }
826 826
827 void Predictor::PreconnectUrl(const GURL& url,
828 const GURL& first_party_for_cookies,
829 UrlInfo::ResolutionMotivation motivation,
830 int count) {
831 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
832 BrowserThread::CurrentlyOn(BrowserThread::IO));
833
834 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
835 PreconnectUrlOnIOThread(url, first_party_for_cookies, motivation, count);
836 } else {
837 BrowserThread::PostTask(
838 BrowserThread::IO,
839 FROM_HERE,
840 base::Bind(&Predictor::PreconnectUrlOnIOThread,
841 base::Unretained(this), url, first_party_for_cookies,
842 motivation, count));
mmenke 2013/05/23 15:28:36 I hadn't realized the Predictor lived on multiple
kouhei (in TOK) 2013/05/24 00:34:37 The Predictor instance is created in UIthread, but
mmenke 2013/05/24 18:28:55 Thanks for investigating this! Looks like you're
843 }
844 }
845
846 void Predictor::PreconnectUrlOnIOThread(
847 const GURL& url, const GURL& first_party_for_cookies,
848 UrlInfo::ResolutionMotivation motivation, int count) {
849 GURL canonical_url(CanonicalizeUrl(url));
850 recent_preconnects_.SetRecentlySeen(canonical_url);
851
852 PreconnectOnIOThread(url, first_party_for_cookies, motivation, count,
853 url_request_context_getter_);
854 }
855
856 void Predictor::RecordPreconnectNavigationStats(const GURL& url) {
857 UMA_HISTOGRAM_BOOLEAN(
858 "Net.PreconnectedNavigation",
859 recent_preconnects_.WasRecentlySeen(url));
860 }
861
827 void Predictor::PredictFrameSubresources(const GURL& url, 862 void Predictor::PredictFrameSubresources(const GURL& url,
828 const GURL& first_party_for_cookies) { 863 const GURL& first_party_for_cookies) {
829 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || 864 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
830 BrowserThread::CurrentlyOn(BrowserThread::IO)); 865 BrowserThread::CurrentlyOn(BrowserThread::IO));
831 if (!predictor_enabled_) 866 if (!predictor_enabled_)
832 return; 867 return;
833 DCHECK_EQ(url.GetWithEmptyPath(), url); 868 DCHECK_EQ(url.GetWithEmptyPath(), url);
834 // Add one pass through the message loop to allow current navigation to 869 // Add one pass through the message loop to allow current navigation to
835 // proceed. 870 // proceed.
836 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { 871 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) {
(...skipping 20 matching lines...) Expand all
857 DCHECK_EQ(url.GetWithEmptyPath(), url); 892 DCHECK_EQ(url.GetWithEmptyPath(), url);
858 Referrers::iterator it = referrers_.find(url); 893 Referrers::iterator it = referrers_.find(url);
859 if (referrers_.end() == it) { 894 if (referrers_.end() == it) {
860 // Only when we don't know anything about this url, make 2 connections 895 // Only when we don't know anything about this url, make 2 connections
861 // available. We could do this completely via learning (by prepopulating 896 // available. We could do this completely via learning (by prepopulating
862 // the referrer_ list with this expected value), but it would swell the 897 // the referrer_ list with this expected value), but it would swell the
863 // size of the list with all the "Leaf" nodes in the tree (nodes that don't 898 // size of the list with all the "Leaf" nodes in the tree (nodes that don't
864 // load any subresources). If we learn about this resource, we will instead 899 // load any subresources). If we learn about this resource, we will instead
865 // provide a more carefully estimated preconnection count. 900 // provide a more carefully estimated preconnection count.
866 if (preconnect_enabled_) { 901 if (preconnect_enabled_) {
867 PreconnectOnIOThread(url, first_party_for_cookies, 902 PreconnectUrlOnIOThread(url, first_party_for_cookies,
868 UrlInfo::SELF_REFERAL_MOTIVATED, 2, 903 UrlInfo::SELF_REFERAL_MOTIVATED, 2);
869 url_request_context_getter_);
870 } 904 }
871 return; 905 return;
872 } 906 }
873 907
874 Referrer* referrer = &(it->second); 908 Referrer* referrer = &(it->second);
875 referrer->IncrementUseCount(); 909 referrer->IncrementUseCount();
876 const UrlInfo::ResolutionMotivation motivation = 910 const UrlInfo::ResolutionMotivation motivation =
877 UrlInfo::LEARNED_REFERAL_MOTIVATED; 911 UrlInfo::LEARNED_REFERAL_MOTIVATED;
878 for (Referrer::iterator future_url = referrer->begin(); 912 for (Referrer::iterator future_url = referrer->begin();
879 future_url != referrer->end(); ++future_url) { 913 future_url != referrer->end(); ++future_url) {
880 SubresourceValue evalution(TOO_NEW); 914 SubresourceValue evalution(TOO_NEW);
881 double connection_expectation = future_url->second.subresource_use_rate(); 915 double connection_expectation = future_url->second.subresource_use_rate();
882 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation", 916 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.PreconnectSubresourceExpectation",
883 static_cast<int>(connection_expectation * 100), 917 static_cast<int>(connection_expectation * 100),
884 10, 5000, 50); 918 10, 5000, 50);
885 future_url->second.ReferrerWasObserved(); 919 future_url->second.ReferrerWasObserved();
886 if (preconnect_enabled_ && 920 if (preconnect_enabled_ &&
887 connection_expectation > kPreconnectWorthyExpectedValue) { 921 connection_expectation > kPreconnectWorthyExpectedValue) {
888 evalution = PRECONNECTION; 922 evalution = PRECONNECTION;
889 future_url->second.IncrementPreconnectionCount(); 923 future_url->second.IncrementPreconnectionCount();
890 int count = static_cast<int>(std::ceil(connection_expectation)); 924 int count = static_cast<int>(std::ceil(connection_expectation));
891 if (url.host() == future_url->first.host()) 925 if (url.host() == future_url->first.host())
892 ++count; 926 ++count;
893 PreconnectOnIOThread(future_url->first, first_party_for_cookies, 927 PreconnectUrlOnIOThread(future_url->first, first_party_for_cookies,
894 motivation, count, url_request_context_getter_); 928 motivation, count);
895 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) { 929 } else if (connection_expectation > kDNSPreresolutionWorthyExpectedValue) {
896 evalution = PRERESOLUTION; 930 evalution = PRERESOLUTION;
897 future_url->second.preresolution_increment(); 931 future_url->second.preresolution_increment();
898 UrlInfo* queued_info = AppendToResolutionQueue(future_url->first, 932 UrlInfo* queued_info = AppendToResolutionQueue(future_url->first,
899 motivation); 933 motivation);
900 if (queued_info) 934 if (queued_info)
901 queued_info->SetReferringHostname(url); 935 queued_info->SetReferringHostname(url);
902 } 936 }
903 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectSubresourceEval", evalution, 937 UMA_HISTOGRAM_ENUMERATION("Net.PreconnectSubresourceEval", evalution,
904 SUBRESOURCE_VALUE_MAX); 938 SUBRESOURCE_VALUE_MAX);
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 IOThread* io_thread, 1224 IOThread* io_thread,
1191 net::URLRequestContextGetter* getter) { 1225 net::URLRequestContextGetter* getter) {
1192 // Empty function for unittests. 1226 // Empty function for unittests.
1193 } 1227 }
1194 1228
1195 void SimplePredictor::ShutdownOnUIThread(PrefService* user_prefs) { 1229 void SimplePredictor::ShutdownOnUIThread(PrefService* user_prefs) {
1196 SetShutdown(true); 1230 SetShutdown(true);
1197 } 1231 }
1198 1232
1199 } // namespace chrome_browser_net 1233 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698