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

Side by Side Diff: content/browser/loader/resource_scheduler.cc

Issue 146333004: Introduce an intra-priority level sorting value - Chromium Side (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes from review. Created 6 years, 10 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 <set>
6
5 #include "content/browser/loader/resource_scheduler.h" 7 #include "content/browser/loader/resource_scheduler.h"
6 8
7 #include "base/stl_util.h" 9 #include "base/stl_util.h"
8 #include "content/common/resource_messages.h" 10 #include "content/common/resource_messages.h"
9 #include "content/browser/loader/resource_message_delegate.h" 11 #include "content/browser/loader/resource_message_delegate.h"
10 #include "content/public/browser/resource_controller.h" 12 #include "content/public/browser/resource_controller.h"
11 #include "content/public/browser/resource_request_info.h" 13 #include "content/public/browser/resource_request_info.h"
12 #include "content/public/browser/resource_throttle.h" 14 #include "content/public/browser/resource_throttle.h"
13 #include "ipc/ipc_message_macros.h" 15 #include "ipc/ipc_message_macros.h"
14 #include "net/base/host_port_pair.h" 16 #include "net/base/host_port_pair.h"
15 #include "net/base/load_flags.h" 17 #include "net/base/load_flags.h"
16 #include "net/base/request_priority.h" 18 #include "net/base/request_priority.h"
17 #include "net/http/http_server_properties.h" 19 #include "net/http/http_server_properties.h"
18 #include "net/url_request/url_request.h" 20 #include "net/url_request/url_request.h"
19 #include "net/url_request/url_request_context.h" 21 #include "net/url_request/url_request_context.h"
20 22
21 namespace content { 23 namespace content {
22 24
23 static const size_t kMaxNumDelayableRequestsPerClient = 10; 25 static const size_t kMaxNumDelayableRequestsPerClient = 10;
24 static const size_t kMaxNumDelayableRequestsPerHost = 6; 26 static const size_t kMaxNumDelayableRequestsPerHost = 6;
25 27
26 // A thin wrapper around net::PriorityQueue that deals with 28
27 // ScheduledResourceRequests instead of PriorityQueue::Pointers.
28 class ResourceScheduler::RequestQueue { 29 class ResourceScheduler::RequestQueue {
29 private: 30 public:
30 typedef net::PriorityQueue<ScheduledResourceRequest*> NetQueue; 31 typedef std::multiset<ScheduledResourceRequest*, ScheduledResourceSorter>
32 NetQueue;
31 33
32 public: 34 RequestQueue() : fifo_ordering_ids_(0) {}
33 class Iterator {
34 public:
35 Iterator(NetQueue* queue) : queue_(queue) {
36 DCHECK(queue != NULL);
37 current_pointer_ = queue_->FirstMax();
38 }
39
40 Iterator& operator++() {
41 current_pointer_ = queue_->GetNextTowardsLastMin(current_pointer_);
42 return *this;
43 }
44
45 Iterator operator++(int) {
46 Iterator result(*this);
47 ++(*this);
48 return result;
49 }
50
51 ScheduledResourceRequest* value() {
52 return current_pointer_.value();
53 }
54
55 bool is_null() {
56 return current_pointer_.is_null();
57 }
58
59 private:
60 NetQueue* queue_;
61 NetQueue::Pointer current_pointer_;
62 };
63
64 RequestQueue() : queue_(net::NUM_PRIORITIES) {}
65 ~RequestQueue() {} 35 ~RequestQueue() {}
66 36
67 // Adds |request| to the queue with given |priority|. 37 // Adds |request| to the queue with given |priority|.
68 void Insert(ScheduledResourceRequest* request, 38 void Insert(ScheduledResourceRequest* request);
69 net::RequestPriority priority) {
70 DCHECK(!ContainsKey(pointers_, request));
71 NetQueue::Pointer pointer = queue_.Insert(request, priority);
72 pointers_[request] = pointer;
73 }
74 39
75 // Removes |request| from the queue. 40 // Removes |request| from the queue.
76 void Erase(ScheduledResourceRequest* request) { 41 void Erase(ScheduledResourceRequest* request) {
77 PointerMap::iterator it = pointers_.find(request); 42 PointerMap::iterator it = pointers_.find(request);
78 DCHECK(it != pointers_.end()); 43 DCHECK(it != pointers_.end());
79 if (it == pointers_.end()) 44 if (it == pointers_.end())
80 return; 45 return;
81 queue_.Erase(it->second); 46 queue_.erase(it->second);
82 pointers_.erase(it); 47 pointers_.erase(it);
83 } 48 }
84 49
85 // Returns the highest priority request that's queued, or NULL if none are. 50 NetQueue::iterator GetNextHighestIterator() {
86 ScheduledResourceRequest* FirstMax() { 51 return queue_.begin();
87 return queue_.FirstMax().value();
88 } 52 }
89 53
90 Iterator GetNextHighestIterator() { 54 NetQueue::iterator End() {
91 return Iterator(&queue_); 55 return queue_.end();
92 } 56 }
93 57
94 // Returns true if |request| is queued. 58 // Returns true if |request| is queued.
95 bool IsQueued(ScheduledResourceRequest* request) const { 59 bool IsQueued(ScheduledResourceRequest* request) const {
96 return ContainsKey(pointers_, request); 60 return ContainsKey(pointers_, request);
97 } 61 }
98 62
99 // Returns true if no requests are queued. 63 // Returns true if no requests are queued.
100 bool IsEmpty() const { return queue_.size() == 0; } 64 bool IsEmpty() const { return queue_.size() == 0; }
101 65
102 private: 66 private:
103 typedef std::map<ScheduledResourceRequest*, NetQueue::Pointer> PointerMap; 67 typedef std::map<ScheduledResourceRequest*, NetQueue::iterator> PointerMap;
68
69 uint32 MakeFifoOrderingId() {
70 fifo_ordering_ids_ += 1;
71 return fifo_ordering_ids_;
72 }
73
74 // Used to create an ordering ID for scheduled resources so that resources
75 // with same priority/intra_priority stay in fifo order.
76 uint32 fifo_ordering_ids_;
104 77
105 NetQueue queue_; 78 NetQueue queue_;
106 PointerMap pointers_; 79 PointerMap pointers_;
107 }; 80 };
108 81
109 // This is the handle we return to the ResourceDispatcherHostImpl so it can 82 // This is the handle we return to the ResourceDispatcherHostImpl so it can
110 // interact with the request. 83 // interact with the request.
111 class ResourceScheduler::ScheduledResourceRequest 84 class ResourceScheduler::ScheduledResourceRequest
112 : public ResourceMessageDelegate, 85 : public ResourceMessageDelegate,
113 public ResourceThrottle { 86 public ResourceThrottle {
114 public: 87 public:
115 ScheduledResourceRequest(const ClientId& client_id, 88 ScheduledResourceRequest(const ClientId& client_id,
116 net::URLRequest* request, 89 net::URLRequest* request,
117 ResourceScheduler* scheduler) 90 ResourceScheduler* scheduler)
118 : ResourceMessageDelegate(request), 91 : ResourceMessageDelegate(request),
119 client_id_(client_id), 92 client_id_(client_id),
120 request_(request), 93 request_(request),
121 ready_(false), 94 ready_(false),
122 deferred_(false), 95 deferred_(false),
123 scheduler_(scheduler) { 96 scheduler_(scheduler),
97 intra_priority_value_(0),
98 fifo_ordering_(0) {
124 TRACE_EVENT_ASYNC_BEGIN1("net", "URLRequest", request_, 99 TRACE_EVENT_ASYNC_BEGIN1("net", "URLRequest", request_,
125 "url", request->url().spec()); 100 "url", request->url().spec());
126 } 101 }
127 102
128 virtual ~ScheduledResourceRequest() { 103 virtual ~ScheduledResourceRequest() {
129 scheduler_->RemoveRequest(this); 104 scheduler_->RemoveRequest(this);
130 } 105 }
131 106
132 void Start() { 107 void Start() {
133 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request_, "Queued"); 108 TRACE_EVENT_ASYNC_STEP_PAST0("net", "URLRequest", request_, "Queued");
134 ready_ = true; 109 ready_ = true;
135 if (deferred_ && request_->status().is_success()) { 110 if (deferred_ && request_->status().is_success()) {
136 deferred_ = false; 111 deferred_ = false;
137 controller()->Resume(); 112 controller()->Resume();
138 } 113 }
139 } 114 }
140 115
141 const ClientId& client_id() const { return client_id_; } 116 const ClientId& client_id() const { return client_id_; }
142 net::URLRequest* url_request() { return request_; } 117 net::URLRequest* url_request() { return request_; }
143 const net::URLRequest* url_request() const { return request_; } 118 const net::URLRequest* url_request() const { return request_; }
119 net::RequestPriority priority() const { return request_->priority(); }
120 int intra_priority_value() const { return intra_priority_value_; }
121 void set_intra_priority_value(int intra_priority_value) {
122 intra_priority_value_ = intra_priority_value;
123 }
124 uint32 fifo_ordering() const { return fifo_ordering_; }
125 void set_fifo_ordering(uint32 fifo_ordering) {
126 fifo_ordering_ = fifo_ordering;
127 }
144 128
145 private: 129 private:
146 // ResourceMessageDelegate interface: 130 // ResourceMessageDelegate interface:
147 virtual bool OnMessageReceived(const IPC::Message& message, 131 virtual bool OnMessageReceived(const IPC::Message& message,
148 bool* message_was_ok) OVERRIDE { 132 bool* message_was_ok) OVERRIDE {
149 bool handled = true; 133 bool handled = true;
150 IPC_BEGIN_MESSAGE_MAP_EX(ScheduledResourceRequest, message, *message_was_ok) 134 IPC_BEGIN_MESSAGE_MAP_EX(ScheduledResourceRequest, message, *message_was_ok)
151 IPC_MESSAGE_HANDLER(ResourceHostMsg_DidChangePriority, DidChangePriority) 135 IPC_MESSAGE_HANDLER(ResourceHostMsg_DidChangePriority, DidChangePriority)
152 IPC_MESSAGE_UNHANDLED(handled = false) 136 IPC_MESSAGE_UNHANDLED(handled = false)
153 IPC_END_MESSAGE_MAP_EX() 137 IPC_END_MESSAGE_MAP_EX()
154 return handled; 138 return handled;
155 } 139 }
156 140
157 // ResourceThrottle interface: 141 // ResourceThrottle interface:
158 virtual void WillStartRequest(bool* defer) OVERRIDE { 142 virtual void WillStartRequest(bool* defer) OVERRIDE {
159 deferred_ = *defer = !ready_; 143 deferred_ = *defer = !ready_;
160 } 144 }
161 145
162 virtual const char* GetNameForLogging() const OVERRIDE { 146 virtual const char* GetNameForLogging() const OVERRIDE {
163 return "ResourceScheduler"; 147 return "ResourceScheduler";
164 } 148 }
165 149
166 void DidChangePriority(int request_id, net::RequestPriority new_priority) { 150 void DidChangePriority(int request_id, net::RequestPriority new_priority,
167 scheduler_->ReprioritizeRequest(this, new_priority); 151 int intra_priority_value) {
152 scheduler_->ReprioritizeRequest(this, new_priority, intra_priority_value);
168 } 153 }
169 154
170 ClientId client_id_; 155 ClientId client_id_;
171 net::URLRequest* request_; 156 net::URLRequest* request_;
172 bool ready_; 157 bool ready_;
173 bool deferred_; 158 bool deferred_;
174 ResourceScheduler* scheduler_; 159 ResourceScheduler* scheduler_;
160 int intra_priority_value_;
161 uint32 fifo_ordering_;
175 162
176 DISALLOW_COPY_AND_ASSIGN(ScheduledResourceRequest); 163 DISALLOW_COPY_AND_ASSIGN(ScheduledResourceRequest);
177 }; 164 };
178 165
166 bool ResourceScheduler::ScheduledResourceSorter::operator()(
167 const ScheduledResourceRequest* a,
168 const ScheduledResourceRequest* b) const {
169 // Want the set to be ordered first by decreasing priority, then by
170 // decreasing intra_priority.
171 // ie. with (priority, intra_priority)
172 // [(1, 0), (1, 0), (0, 100), (0, 0)]
173 if (a->priority() != b->priority())
174 return a->priority() > b->priority();
175
176 if (a->intra_priority_value() != b->intra_priority_value())
177 return a->intra_priority_value() > b->intra_priority_value();
178
179 // If priority/intra_priority is the same, fall back to fifo ordering.
180 // std::multiset doesn't guarantee this until c++11.
181 return a->fifo_ordering() < b->fifo_ordering();
182 }
183
184 void ResourceScheduler::RequestQueue::Insert(
185 ScheduledResourceRequest* request) {
186 DCHECK(!ContainsKey(pointers_, request));
187 request->set_fifo_ordering(MakeFifoOrderingId());
188 pointers_[request] = queue_.insert(request);
189 }
190
179 // Each client represents a tab. 191 // Each client represents a tab.
180 struct ResourceScheduler::Client { 192 struct ResourceScheduler::Client {
181 Client() : has_body(false), using_spdy_proxy(false) {} 193 Client() : has_body(false), using_spdy_proxy(false) {}
182 ~Client() {} 194 ~Client() {}
183 195
184 bool has_body; 196 bool has_body;
185 bool using_spdy_proxy; 197 bool using_spdy_proxy;
186 RequestQueue pending_requests; 198 RequestQueue pending_requests;
187 RequestSet in_flight_requests; 199 RequestSet in_flight_requests;
188 }; 200 };
(...skipping 23 matching lines...) Expand all
212 // 3. The tab is closed while a RequestResource IPC is in flight. 224 // 3. The tab is closed while a RequestResource IPC is in flight.
213 unowned_requests_.insert(request.get()); 225 unowned_requests_.insert(request.get());
214 request->Start(); 226 request->Start();
215 return request.PassAs<ResourceThrottle>(); 227 return request.PassAs<ResourceThrottle>();
216 } 228 }
217 229
218 Client* client = it->second; 230 Client* client = it->second;
219 if (ShouldStartRequest(request.get(), client) == START_REQUEST) { 231 if (ShouldStartRequest(request.get(), client) == START_REQUEST) {
220 StartRequest(request.get(), client); 232 StartRequest(request.get(), client);
221 } else { 233 } else {
222 client->pending_requests.Insert(request.get(), url_request->priority()); 234 client->pending_requests.Insert(request.get());
223 } 235 }
224 return request.PassAs<ResourceThrottle>(); 236 return request.PassAs<ResourceThrottle>();
225 } 237 }
226 238
227 void ResourceScheduler::RemoveRequest(ScheduledResourceRequest* request) { 239 void ResourceScheduler::RemoveRequest(ScheduledResourceRequest* request) {
228 DCHECK(CalledOnValidThread()); 240 DCHECK(CalledOnValidThread());
229 if (ContainsKey(unowned_requests_, request)) { 241 if (ContainsKey(unowned_requests_, request)) {
230 unowned_requests_.erase(request); 242 unowned_requests_.erase(request);
231 return; 243 return;
232 } 244 }
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 } 341 }
330 } 342 }
331 343
332 void ResourceScheduler::StartRequest(ScheduledResourceRequest* request, 344 void ResourceScheduler::StartRequest(ScheduledResourceRequest* request,
333 Client* client) { 345 Client* client) {
334 client->in_flight_requests.insert(request); 346 client->in_flight_requests.insert(request);
335 request->Start(); 347 request->Start();
336 } 348 }
337 349
338 void ResourceScheduler::ReprioritizeRequest(ScheduledResourceRequest* request, 350 void ResourceScheduler::ReprioritizeRequest(ScheduledResourceRequest* request,
339 net::RequestPriority new_priority) { 351 net::RequestPriority new_priority,
352 int new_intra_priority_value) {
340 if (request->url_request()->load_flags() & net::LOAD_IGNORE_LIMITS) { 353 if (request->url_request()->load_flags() & net::LOAD_IGNORE_LIMITS) {
341 // We should not be re-prioritizing requests with the 354 // We should not be re-prioritizing requests with the
342 // IGNORE_LIMITS flag. 355 // IGNORE_LIMITS flag.
343 NOTREACHED(); 356 NOTREACHED();
344 return; 357 return;
345 } 358 }
346 net::RequestPriority old_priority = request->url_request()->priority(); 359 net::RequestPriority old_priority = request->url_request()->priority();
347 DCHECK_NE(new_priority, old_priority); 360 int old_intra_priority = request->intra_priority_value();
361 DCHECK((new_priority != old_priority) ||
362 (old_intra_priority != new_intra_priority_value));
Chris Evans 2014/02/21 22:42:30 Note that a compromised renderer could cause the c
shatch 2014/02/25 18:48:34 Yeah it's just to check that we're not spamming th
348 request->url_request()->SetPriority(new_priority); 363 request->url_request()->SetPriority(new_priority);
364 request->set_intra_priority_value(new_intra_priority_value);
349 ClientMap::iterator client_it = client_map_.find(request->client_id()); 365 ClientMap::iterator client_it = client_map_.find(request->client_id());
350 if (client_it == client_map_.end()) { 366 if (client_it == client_map_.end()) {
351 // The client was likely deleted shortly before we received this IPC. 367 // The client was likely deleted shortly before we received this IPC.
352 return; 368 return;
353 } 369 }
354 370
355 Client *client = client_it->second; 371 Client *client = client_it->second;
356 if (!client->pending_requests.IsQueued(request)) { 372 if (!client->pending_requests.IsQueued(request)) {
357 DCHECK(ContainsKey(client->in_flight_requests, request)); 373 DCHECK(ContainsKey(client->in_flight_requests, request));
358 // Request has already started. 374 // Request has already started.
359 return; 375 return;
360 } 376 }
361 377
362 client->pending_requests.Erase(request); 378 client->pending_requests.Erase(request);
363 client->pending_requests.Insert(request, 379 client->pending_requests.Insert(request);
364 request->url_request()->priority());
365 380
366 if (new_priority > old_priority) { 381 if (new_priority > old_priority) {
367 // Check if this request is now able to load at its new priority. 382 // Check if this request is now able to load at its new priority.
368 LoadAnyStartablePendingRequests(client); 383 LoadAnyStartablePendingRequests(client);
369 } 384 }
370 } 385 }
371 386
372 void ResourceScheduler::LoadAnyStartablePendingRequests(Client* client) { 387 void ResourceScheduler::LoadAnyStartablePendingRequests(Client* client) {
373 // We iterate through all the pending requests, starting with the highest 388 // We iterate through all the pending requests, starting with the highest
374 // priority one. For each entry, one of three things can happen: 389 // priority one. For each entry, one of three things can happen:
375 // 1) We start the request, remove it from the list, and keep checking. 390 // 1) We start the request, remove it from the list, and keep checking.
376 // 2) We do NOT start the request, but ShouldStartRequest() signals us that 391 // 2) We do NOT start the request, but ShouldStartRequest() signals us that
377 // there may be room for other requests, so we keep checking and leave 392 // there may be room for other requests, so we keep checking and leave
378 // the previous request still in the list. 393 // the previous request still in the list.
379 // 3) We do not start the request, same as above, but StartRequest() tells 394 // 3) We do not start the request, same as above, but StartRequest() tells
380 // us there's no point in checking any further requests. 395 // us there's no point in checking any further requests.
381 396 RequestQueue::NetQueue::iterator request_iter =
382 RequestQueue::Iterator request_iter =
383 client->pending_requests.GetNextHighestIterator(); 397 client->pending_requests.GetNextHighestIterator();
384 398 while (request_iter != client->pending_requests.End()) {
385 while (!request_iter.is_null()) { 399 ScheduledResourceRequest* request = *request_iter;
386 ScheduledResourceRequest* request = request_iter.value();
387 ShouldStartReqResult query_result = ShouldStartRequest(request, client); 400 ShouldStartReqResult query_result = ShouldStartRequest(request, client);
388 401
389 if (query_result == START_REQUEST) { 402 if (query_result == START_REQUEST) {
390 client->pending_requests.Erase(request); 403 client->pending_requests.Erase(request);
391 StartRequest(request, client); 404 StartRequest(request, client);
392 405
393 // StartRequest can modify the pending list, so we (re)start evaluation 406 // StartRequest can modify the pending list, so we (re)start evaluation
394 // from the currently highest priority request. Avoid copying a singular 407 // from the currently highest priority request. Avoid copying a singular
395 // iterator, which would trigger undefined behavior. 408 // iterator, which would trigger undefined behavior.
396 if (client->pending_requests.GetNextHighestIterator().is_null()) 409 if (client->pending_requests.GetNextHighestIterator() ==
410 client->pending_requests.End())
397 break; 411 break;
398 request_iter = client->pending_requests.GetNextHighestIterator(); 412 request_iter = client->pending_requests.GetNextHighestIterator();
399 } else if (query_result == DO_NOT_START_REQUEST_AND_KEEP_SEARCHING) { 413 } else if (query_result == DO_NOT_START_REQUEST_AND_KEEP_SEARCHING) {
400 ++request_iter; 414 ++request_iter;
401 continue; 415 continue;
402 } else { 416 } else {
403 DCHECK(query_result == DO_NOT_START_REQUEST_AND_STOP_SEARCHING); 417 DCHECK(query_result == DO_NOT_START_REQUEST_AND_STOP_SEARCHING);
404 break; 418 break;
405 } 419 }
406 } 420 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 528
515 return START_REQUEST; 529 return START_REQUEST;
516 } 530 }
517 531
518 ResourceScheduler::ClientId ResourceScheduler::MakeClientId( 532 ResourceScheduler::ClientId ResourceScheduler::MakeClientId(
519 int child_id, int route_id) { 533 int child_id, int route_id) {
520 return (static_cast<ResourceScheduler::ClientId>(child_id) << 32) | route_id; 534 return (static_cast<ResourceScheduler::ClientId>(child_id) << 32) | route_id;
521 } 535 }
522 536
523 } // namespace content 537 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/resource_scheduler.h ('k') | content/browser/loader/resource_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698