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

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor.h

Issue 2796783004: predictors: Add origin learning. (Closed)
Patch Set: , Created 3 years, 8 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 5 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 6 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <map> 10 #include <map>
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 // 97 //
98 // TODO(zhenw): Currently only main frame requests/redirects/responses are 98 // TODO(zhenw): Currently only main frame requests/redirects/responses are
99 // recorded. Consider recording sub-frame responses independently or together 99 // recorded. Consider recording sub-frame responses independently or together
100 // with main frame. 100 // with main frame.
101 class ResourcePrefetchPredictor 101 class ResourcePrefetchPredictor
102 : public KeyedService, 102 : public KeyedService,
103 public history::HistoryServiceObserver, 103 public history::HistoryServiceObserver,
104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>, 104 public base::SupportsWeakPtr<ResourcePrefetchPredictor>,
105 public precache::PrecacheManager::Delegate { 105 public precache::PrecacheManager::Delegate {
106 public: 106 public:
107 // Data collected for origin-based prediction, for a single origin during a
108 // page load (see PageRequestSummary).
109 struct OriginRequestSummary {
110 OriginRequestSummary();
111 OriginRequestSummary(const OriginRequestSummary& other);
112 ~OriginRequestSummary();
113
114 GURL origin;
115 bool always_access_network;
116 bool accessed_network;
117 int first_occurrence;
118 };
119
107 // Stores the data that we need to get from the URLRequest. 120 // Stores the data that we need to get from the URLRequest.
108 struct URLRequestSummary { 121 struct URLRequestSummary {
109 URLRequestSummary(); 122 URLRequestSummary();
110 URLRequestSummary(const URLRequestSummary& other); 123 URLRequestSummary(const URLRequestSummary& other);
111 ~URLRequestSummary(); 124 ~URLRequestSummary();
112 125
113 NavigationID navigation_id; 126 NavigationID navigation_id;
114 GURL resource_url; 127 GURL resource_url;
128 GURL request_url; // URL after all redirects.
115 content::ResourceType resource_type; 129 content::ResourceType resource_type;
116 net::RequestPriority priority; 130 net::RequestPriority priority;
117 131
118 // Only for responses. 132 // Only for responses.
119 std::string mime_type; 133 std::string mime_type;
120 bool was_cached; 134 bool was_cached;
121 GURL redirect_url; // Empty unless request was redirected to a valid url. 135 GURL redirect_url; // Empty unless request was redirected to a valid url.
122 136
123 bool has_validators; 137 bool has_validators;
124 bool always_revalidate; 138 bool always_revalidate;
139 bool is_no_store;
140 bool network_accessed;
125 141
126 // Initializes a |URLRequestSummary| from a |URLRequest| response. 142 // Initializes a |URLRequestSummary| from a |URLRequest| response.
127 // Returns true for success. Note: NavigationID is NOT initialized 143 // Returns true for success. Note: NavigationID is NOT initialized
128 // by this function. 144 // by this function.
129 static bool SummarizeResponse(const net::URLRequest& request, 145 static bool SummarizeResponse(const net::URLRequest& request,
130 URLRequestSummary* summary); 146 URLRequestSummary* summary);
131 }; 147 };
132 148
133 // Stores the data learned from a single navigation. 149 // Stores the data learned from a single navigation.
134 struct PageRequestSummary { 150 struct PageRequestSummary {
135 explicit PageRequestSummary(const GURL& main_frame_url); 151 explicit PageRequestSummary(const GURL& main_frame_url);
136 PageRequestSummary(const PageRequestSummary& other); 152 PageRequestSummary(const PageRequestSummary& other);
137 ~PageRequestSummary(); 153 ~PageRequestSummary();
138 154
139 GURL main_frame_url; 155 GURL main_frame_url;
140 GURL initial_url; 156 GURL initial_url;
141 157
142 // Stores all subresource requests within a single navigation, from initial 158 // Stores all subresource requests within a single navigation, from initial
143 // main frame request to navigation completion. 159 // main frame request to navigation completion.
144 std::vector<URLRequestSummary> subresource_requests; 160 std::vector<URLRequestSummary> subresource_requests;
161 // Map of origin -> OriginRequestSummary. Only one instance of each origin
162 // is kept per navigation, but the summary is updated several times.
163 std::map<GURL, OriginRequestSummary> origins;
145 }; 164 };
146 165
147 // Stores a result of prediction. Essentially, |subresource_urls| is main 166 // Stores a result of prediction. Essentially, |subresource_urls| is main
148 // result and other fields are used for diagnosis and histograms reporting. 167 // result and other fields are used for diagnosis and histograms reporting.
149 struct Prediction { 168 struct Prediction {
150 Prediction(); 169 Prediction();
151 Prediction(const Prediction& other); 170 Prediction(const Prediction& other);
152 ~Prediction(); 171 ~Prediction();
153 172
154 bool is_host; 173 bool is_host;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 TestPrefetchingDurationHistogram); 292 TestPrefetchingDurationHistogram);
274 293
275 enum InitializationState { 294 enum InitializationState {
276 NOT_INITIALIZED = 0, 295 NOT_INITIALIZED = 0,
277 INITIALIZING = 1, 296 INITIALIZING = 1,
278 INITIALIZED = 2 297 INITIALIZED = 2
279 }; 298 };
280 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap; 299 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
281 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap; 300 typedef ResourcePrefetchPredictorTables::RedirectDataMap RedirectDataMap;
282 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap; 301 typedef ResourcePrefetchPredictorTables::ManifestDataMap ManifestDataMap;
302 typedef ResourcePrefetchPredictorTables::OriginDataMap OriginDataMap;
283 303
284 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>> 304 typedef std::map<NavigationID, std::unique_ptr<PageRequestSummary>>
285 NavigationMap; 305 NavigationMap;
286 306
287 // Returns true if the main page request is supported for prediction. 307 // Returns true if the main page request is supported for prediction.
288 static bool IsHandledMainPage(net::URLRequest* request); 308 static bool IsHandledMainPage(net::URLRequest* request);
289 309
290 // Returns true if the subresource request is supported for prediction. 310 // Returns true if the subresource request is supported for prediction.
291 static bool IsHandledSubresource(net::URLRequest* request, 311 static bool IsHandledSubresource(net::URLRequest* request,
292 content::ResourceType resource_type); 312 content::ResourceType resource_type);
293 313
294 // Returns true if the subresource has a supported type. 314 // Returns true if the subresource has a supported type.
295 static bool IsHandledResourceType(content::ResourceType resource_type, 315 static bool IsHandledResourceType(content::ResourceType resource_type,
296 const std::string& mime_type); 316 const std::string& mime_type);
297 317
298 // Returns true if the request (should have a response in it) is "no-store". 318 // Returns true if the request (should have a response in it) is "no-store".
299 static bool IsNoStore(const net::URLRequest* request); 319 static bool IsNoStore(const net::URLRequest& request);
300 320
301 // Returns true iff |redirect_data_map| contains confident redirect endpoint 321 // Returns true iff |redirect_data_map| contains confident redirect endpoint
302 // for |entry_point| and assigns it to the |redirect_endpoint|. 322 // for |entry_point| and assigns it to the |redirect_endpoint|.
303 static bool GetRedirectEndpoint(const std::string& entry_point, 323 static bool GetRedirectEndpoint(const std::string& entry_point,
304 const RedirectDataMap& redirect_data_map, 324 const RedirectDataMap& redirect_data_map,
305 std::string* redirect_endpoint); 325 std::string* redirect_endpoint);
306 326
307 static void SetAllowPortInUrlsForTesting(bool state); 327 static void SetAllowPortInUrlsForTesting(bool state);
308 328
309 // KeyedService methods override. 329 // KeyedService methods override.
310 void Shutdown() override; 330 void Shutdown() override;
311 331
312 // Functions called on different network events pertaining to the loading of 332 // Functions called on different network events pertaining to the loading of
313 // main frame resource or sub resources. 333 // main frame resource or sub resources.
314 void OnMainFrameRequest(const URLRequestSummary& request); 334 void OnMainFrameRequest(const URLRequestSummary& request);
315 void OnMainFrameResponse(const URLRequestSummary& response); 335 void OnMainFrameResponse(const URLRequestSummary& response);
316 void OnMainFrameRedirect(const URLRequestSummary& response); 336 void OnMainFrameRedirect(const URLRequestSummary& response);
317 void OnSubresourceResponse(const URLRequestSummary& response); 337 void OnSubresourceResponse(const URLRequestSummary& response);
338 void OnSubresourceRedirect(const URLRequestSummary& response);
318 339
319 // Called when onload completes for a navigation. We treat this point as the 340 // Called when onload completes for a navigation. We treat this point as the
320 // "completion" of the navigation. The resources requested by the page up to 341 // "completion" of the navigation. The resources requested by the page up to
321 // this point are the only ones considered for prefetching. 342 // this point are the only ones considered for prefetching.
322 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info); 343 void OnNavigationComplete(const NavigationID& nav_id_without_timing_info);
323 344
324 // Returns true iff there is PrefetchData that can be used for a 345 // Returns true iff there is PrefetchData that can be used for a
325 // |main_frame_url| and fills |prediction| with resources that need to be 346 // |main_frame_url| and fills |prediction| with resources that need to be
326 // prefetched. |prediction| pointer may be equal nullptr to get return value 347 // prefetched. |prediction| pointer may be equal nullptr to get return value
327 // only. 348 // only.
328 bool GetPrefetchData(const GURL& main_frame_url, 349 bool GetPrefetchData(const GURL& main_frame_url,
329 Prediction* prediction) const; 350 Prediction* prediction) const;
330 351
331 // Returns true iff the |data_map| contains PrefetchData that can be used 352 // Returns true iff the |data_map| contains PrefetchData that can be used
332 // for a |main_frame_key| and fills |urls| with resources that need to be 353 // for a |main_frame_key| and fills |urls| with resources that need to be
333 // prefetched. |urls| pointer may be equal nullptr to get return value only. 354 // prefetched. |urls| pointer may be equal nullptr to get return value only.
334 bool PopulatePrefetcherRequest(const std::string& main_frame_key, 355 bool PopulatePrefetcherRequest(const std::string& main_frame_key,
335 const PrefetchDataMap& data_map, 356 const PrefetchDataMap& data_map,
336 std::vector<GURL>* urls) const; 357 std::vector<GURL>* urls) const;
337 358
338 // Callback for task to read predictor database. Takes ownership of 359 // Callback for task to read predictor database. Takes ownership of
339 // all arguments. 360 // all arguments.
340 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map, 361 void CreateCaches(std::unique_ptr<PrefetchDataMap> url_data_map,
341 std::unique_ptr<PrefetchDataMap> host_data_map, 362 std::unique_ptr<PrefetchDataMap> host_data_map,
342 std::unique_ptr<RedirectDataMap> url_redirect_data_map, 363 std::unique_ptr<RedirectDataMap> url_redirect_data_map,
343 std::unique_ptr<RedirectDataMap> host_redirect_data_map, 364 std::unique_ptr<RedirectDataMap> host_redirect_data_map,
344 std::unique_ptr<ManifestDataMap> manifest_data_map); 365 std::unique_ptr<ManifestDataMap> manifest_data_map,
366 std::unique_ptr<OriginDataMap> origin_data_map);
345 367
346 // Called during initialization when history is read and the predictor 368 // Called during initialization when history is read and the predictor
347 // database has been read. 369 // database has been read.
348 void OnHistoryAndCacheLoaded(); 370 void OnHistoryAndCacheLoaded();
349 371
350 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_. 372 // Cleanup inflight_navigations_, inflight_prefetches_, and prefetcher_stats_.
351 void CleanupAbandonedNavigations(const NavigationID& navigation_id); 373 void CleanupAbandonedNavigations(const NavigationID& navigation_id);
352 374
353 // Deletes all URLs from the predictor database, the caches and removes all 375 // Deletes all URLs from the predictor database, the caches and removes all
354 // inflight navigations. 376 // inflight navigations.
355 void DeleteAllUrls(); 377 void DeleteAllUrls();
356 378
357 // Deletes data for the input |urls| and their corresponding hosts from the 379 // Deletes data for the input |urls| and their corresponding hosts from the
358 // predictor database and caches. 380 // predictor database and caches.
359 void DeleteUrls(const history::URLRows& urls); 381 void DeleteUrls(const history::URLRows& urls);
360 382
361 // Callback for GetUrlVisitCountTask. 383 // Callback for GetUrlVisitCountTask.
362 void OnVisitCountLookup(size_t url_visit_count, 384 void OnVisitCountLookup(size_t url_visit_count,
363 const PageRequestSummary& summary); 385 const PageRequestSummary& summary);
364 386
365 // Removes the oldest entry in the input |data_map|, also deleting it from the 387 // Removes the oldest entry in the input |data_map|, also deleting it from the
366 // predictor database. 388 // predictor database.
367 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type, 389 void RemoveOldestEntryInPrefetchDataMap(PrefetchKeyType key_type,
368 PrefetchDataMap* data_map); 390 PrefetchDataMap* data_map);
369 391
370 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type, 392 void RemoveOldestEntryInRedirectDataMap(PrefetchKeyType key_type,
371 RedirectDataMap* data_map); 393 RedirectDataMap* data_map);
372 394
373 void RemoveOldestEntryInManifestDataMap(ManifestDataMap* data_map); 395 void RemoveOldestEntryInManifestDataMap(ManifestDataMap* data_map);
396 void RemoveOldestEntryInOriginDataMap(OriginDataMap* data_map);
374 397
375 // Merges resources in |new_resources| into the |data_map| and correspondingly 398 // Merges resources in |new_resources| into the |data_map| and correspondingly
376 // updates the predictor database. Also calls LearnRedirect if relevant. 399 // updates the predictor database. Also calls LearnRedirect if relevant.
377 void LearnNavigation(const std::string& key, 400 void LearnNavigation(const std::string& key,
378 PrefetchKeyType key_type, 401 PrefetchKeyType key_type,
379 const std::vector<URLRequestSummary>& new_resources, 402 const std::vector<URLRequestSummary>& new_resources,
380 size_t max_data_map_size, 403 size_t max_data_map_size,
381 PrefetchDataMap* data_map, 404 PrefetchDataMap* data_map,
382 const std::string& key_before_redirects, 405 const std::string& key_before_redirects,
383 RedirectDataMap* redirect_map); 406 RedirectDataMap* redirect_map);
384 407
385 // Updates information about final redirect destination for |key| in 408 // Updates information about final redirect destination for |key| in
386 // |redirect_map| and correspondingly updates the predictor database. 409 // |redirect_map| and correspondingly updates the predictor database.
387 void LearnRedirect(const std::string& key, 410 void LearnRedirect(const std::string& key,
388 PrefetchKeyType key_type, 411 PrefetchKeyType key_type,
389 const std::string& final_redirect, 412 const std::string& final_redirect,
390 size_t max_redirect_map_size, 413 size_t max_redirect_map_size,
391 RedirectDataMap* redirect_map); 414 RedirectDataMap* redirect_map);
392 415
416 void LearnOrigins(const std::string& host,
417 const std::map<GURL, OriginRequestSummary>& summaries,
418 size_t max_data_map_size,
419 OriginDataMap* data_map);
420
393 // Reports database readiness metric defined as percentage of navigated hosts 421 // Reports database readiness metric defined as percentage of navigated hosts
394 // found in DB for last X entries in history. 422 // found in DB for last X entries in history.
395 void ReportDatabaseReadiness(const history::TopHostsList& top_hosts) const; 423 void ReportDatabaseReadiness(const history::TopHostsList& top_hosts) const;
396 424
397 // history::HistoryServiceObserver: 425 // history::HistoryServiceObserver:
398 void OnURLsDeleted(history::HistoryService* history_service, 426 void OnURLsDeleted(history::HistoryService* history_service,
399 bool all_history, 427 bool all_history,
400 bool expired, 428 bool expired,
401 const history::URLRows& deleted_rows, 429 const history::URLRows& deleted_rows,
402 const std::set<GURL>& favicon_urls) override; 430 const std::set<GURL>& favicon_urls) override;
(...skipping 16 matching lines...) Expand all
419 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 447 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
420 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_; 448 scoped_refptr<ResourcePrefetcherManager> prefetch_manager_;
421 base::CancelableTaskTracker history_lookup_consumer_; 449 base::CancelableTaskTracker history_lookup_consumer_;
422 450
423 // Copy of the data in the predictor tables. 451 // Copy of the data in the predictor tables.
424 std::unique_ptr<PrefetchDataMap> url_table_cache_; 452 std::unique_ptr<PrefetchDataMap> url_table_cache_;
425 std::unique_ptr<PrefetchDataMap> host_table_cache_; 453 std::unique_ptr<PrefetchDataMap> host_table_cache_;
426 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_; 454 std::unique_ptr<RedirectDataMap> url_redirect_table_cache_;
427 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_; 455 std::unique_ptr<RedirectDataMap> host_redirect_table_cache_;
428 std::unique_ptr<ManifestDataMap> manifest_table_cache_; 456 std::unique_ptr<ManifestDataMap> manifest_table_cache_;
457 std::unique_ptr<OriginDataMap> origin_table_cache_;
429 458
430 std::map<GURL, base::TimeTicks> inflight_prefetches_; 459 std::map<GURL, base::TimeTicks> inflight_prefetches_;
431 NavigationMap inflight_navigations_; 460 NavigationMap inflight_navigations_;
432 461
433 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>> 462 std::map<GURL, std::unique_ptr<ResourcePrefetcher::PrefetcherStats>>
434 prefetcher_stats_; 463 prefetcher_stats_;
435 464
436 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> 465 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
437 history_service_observer_; 466 history_service_observer_;
438 467
(...skipping 26 matching lines...) Expand all
465 494
466 private: 495 private:
467 ResourcePrefetchPredictor* predictor_; 496 ResourcePrefetchPredictor* predictor_;
468 497
469 DISALLOW_COPY_AND_ASSIGN(TestObserver); 498 DISALLOW_COPY_AND_ASSIGN(TestObserver);
470 }; 499 };
471 500
472 } // namespace predictors 501 } // namespace predictors
473 502
474 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_ 503 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698