Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "content/renderer/scheduler/resource_dispatch_throttler.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/trace_event/trace_event.h" | |
| 9 #include "content/common/resource_messages.h" | |
| 10 #include "content/renderer/scheduler/renderer_scheduler.h" | |
| 11 #include "ipc/ipc_message_macros.h" | |
| 12 | |
| 13 namespace content { | |
| 14 namespace { | |
| 15 | |
| 16 bool IsResourceRequest(const IPC::Message& msg) { | |
| 17 return msg.type() == ResourceHostMsg_RequestResource::ID; | |
| 18 } | |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 ResourceDispatchThrottler::ResourceDispatchThrottler( | |
| 23 IPC::Sender* proxied_sender, | |
| 24 RendererScheduler* scheduler, | |
| 25 base::TimeDelta flush_period, | |
| 26 uint32 max_requests_per_flush) | |
| 27 : proxied_sender_(proxied_sender), | |
| 28 scheduler_(scheduler), | |
| 29 flush_period_(flush_period), | |
| 30 max_requests_per_flush_(max_requests_per_flush), | |
| 31 flush_timer_( | |
| 32 FROM_HERE, | |
| 33 flush_period_, | |
| 34 base::Bind(&ResourceDispatchThrottler::Flush, base::Unretained(this)), | |
| 35 false /* is_repeating */), | |
| 36 sent_requests_since_last_flush_(0) { | |
| 37 DCHECK(proxied_sender); | |
| 38 DCHECK(scheduler); | |
| 39 DCHECK_NE(flush_period_, base::TimeDelta()); | |
| 40 DCHECK(max_requests_per_flush_); | |
| 41 flush_timer_.SetTaskRunner(scheduler->LoadingTaskRunner()); | |
| 42 } | |
| 43 | |
| 44 ResourceDispatchThrottler::~ResourceDispatchThrottler() { | |
| 45 std::deque<IPC::Message*> throttled_messages; | |
| 46 throttled_messages.swap(throttled_messages_); | |
| 47 for (auto& message : throttled_messages) | |
| 48 ForwardMessage(message); | |
| 49 // There shouldn't be re-entrancy issues when forwarding an IPC, but validate | |
| 50 // as a safeguard. | |
| 51 DCHECK(throttled_messages_.empty()); | |
| 52 } | |
| 53 | |
| 54 bool ResourceDispatchThrottler::Send(IPC::Message* msg) { | |
| 55 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 56 if (msg->is_sync()) | |
|
davidben
2015/02/04 17:10:41
Transplanting this comment; Rietveld doesn't do a
jdduke (slow)
2015/02/04 22:54:34
Oops, yeah I totally missed that comment. I change
| |
| 57 return ForwardMessage(msg); | |
| 58 | |
| 59 // Always defer message forwarding if there are pending messages, ensuring | |
| 60 // message dispatch ordering consistency. | |
| 61 if (!throttled_messages_.empty()) { | |
| 62 TRACE_EVENT_INSTANT0("loader", "ResourceDispatchThrottler::ThrottleMessage", | |
| 63 TRACE_EVENT_SCOPE_THREAD); | |
| 64 throttled_messages_.push_back(msg); | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 if (!IsResourceRequest(*msg)) | |
| 69 return ForwardMessage(msg); | |
| 70 | |
| 71 if (!scheduler_->IsHighPriorityWorkAnticipated()) | |
| 72 return ForwardMessage(msg); | |
| 73 | |
| 74 if (Now() > (last_sent_request_time_ + flush_period_)) { | |
| 75 // If sufficient time has passed since the previous send, we can effectively | |
| 76 // mark the pipeline as flushed. | |
| 77 sent_requests_since_last_flush_ = 0; | |
| 78 return ForwardMessage(msg); | |
| 79 } | |
| 80 | |
| 81 if (sent_requests_since_last_flush_ < max_requests_per_flush_) | |
| 82 return ForwardMessage(msg); | |
| 83 | |
| 84 TRACE_EVENT_INSTANT0("loader", "ResourceDispatchThrottler::ThrottleRequest", | |
| 85 TRACE_EVENT_SCOPE_THREAD); | |
| 86 throttled_messages_.push_back(msg); | |
| 87 ScheduleFlush(); | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 base::TimeTicks ResourceDispatchThrottler::Now() const { | |
| 92 return base::TimeTicks::Now(); | |
| 93 } | |
| 94 | |
| 95 void ResourceDispatchThrottler::ScheduleFlush() { | |
| 96 DCHECK(!flush_timer_.IsRunning()); | |
| 97 flush_timer_.Reset(); | |
| 98 } | |
| 99 | |
| 100 void ResourceDispatchThrottler::Flush() { | |
| 101 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 102 TRACE_EVENT1("loader", "ResourceDispatchThrottler::Flush", | |
| 103 "total_throttled_messages", throttled_messages_.size()); | |
| 104 sent_requests_since_last_flush_ = 0; | |
| 105 | |
| 106 // If high-priority work is no longer anticipated, dispatch can be safely | |
| 107 // accelerated. Avoid completely flushing in such case in the event that | |
| 108 // a large number of requests have been throttled. | |
| 109 uint32 max_requests = scheduler_->IsHighPriorityWorkAnticipated() | |
| 110 ? max_requests_per_flush_ | |
| 111 : max_requests_per_flush_ * 2; | |
| 112 | |
| 113 while (!throttled_messages_.empty() && | |
| 114 (sent_requests_since_last_flush_ < max_requests || | |
| 115 !IsResourceRequest(*throttled_messages_.front()))) { | |
| 116 IPC::Message* msg = throttled_messages_.front(); | |
| 117 throttled_messages_.pop_front(); | |
| 118 ForwardMessage(msg); | |
| 119 } | |
| 120 | |
| 121 if (!throttled_messages_.empty()) | |
| 122 ScheduleFlush(); | |
| 123 } | |
| 124 | |
| 125 bool ResourceDispatchThrottler::ForwardMessage(IPC::Message* msg) { | |
| 126 if (IsResourceRequest(*msg)) { | |
| 127 last_sent_request_time_ = Now(); | |
| 128 ++sent_requests_since_last_flush_; | |
| 129 } | |
| 130 return proxied_sender_->Send(msg); | |
| 131 } | |
| 132 | |
| 133 } // namespace content | |
| OLD | NEW |