| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "content/renderer/scheduler/resource_dispatch_throttler.h" | 5 #include "content/renderer/scheduler/resource_dispatch_throttler.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/trace_event/trace_event.h" | 8 #include "base/trace_event/trace_event.h" |
| 9 #include "components/scheduler/renderer/renderer_scheduler.h" | 9 #include "components/scheduler/renderer/renderer_scheduler.h" |
| 10 #include "content/common/resource_messages.h" | 10 #include "content/common/resource_messages.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 if (!throttled_messages_.empty()) { | 59 if (!throttled_messages_.empty()) { |
| 60 TRACE_EVENT_INSTANT0("loader", "ResourceDispatchThrottler::ThrottleMessage", | 60 TRACE_EVENT_INSTANT0("loader", "ResourceDispatchThrottler::ThrottleMessage", |
| 61 TRACE_EVENT_SCOPE_THREAD); | 61 TRACE_EVENT_SCOPE_THREAD); |
| 62 throttled_messages_.push_back(msg); | 62 throttled_messages_.push_back(msg); |
| 63 return true; | 63 return true; |
| 64 } | 64 } |
| 65 | 65 |
| 66 if (!IsResourceRequest(*msg)) | 66 if (!IsResourceRequest(*msg)) |
| 67 return ForwardMessage(msg); | 67 return ForwardMessage(msg); |
| 68 | 68 |
| 69 if (!scheduler_->IsHighPriorityWorkAnticipated()) | 69 if (!scheduler_->IsHighPriorityWorkAnticipated()) { |
| 70 return ForwardMessage(msg); | 70 // Treat an unthrottled request as a flush. |
| 71 | 71 LogFlush(); |
| 72 if (Now() > (last_sent_request_time_ + flush_period_)) { | |
| 73 // If sufficient time has passed since the previous send, we can effectively | |
| 74 // mark the pipeline as flushed. | |
| 75 sent_requests_since_last_flush_ = 0; | |
| 76 return ForwardMessage(msg); | 72 return ForwardMessage(msg); |
| 77 } | 73 } |
| 78 | 74 |
| 75 if (Now() > (last_flush_time_ + flush_period_)) { |
| 76 // If sufficient time has passed since the previous flush, we can |
| 77 // effectively mark the pipeline as flushed. |
| 78 LogFlush(); |
| 79 return ForwardMessage(msg); |
| 80 } |
| 81 |
| 79 if (sent_requests_since_last_flush_ < max_requests_per_flush_) | 82 if (sent_requests_since_last_flush_ < max_requests_per_flush_) |
| 80 return ForwardMessage(msg); | 83 return ForwardMessage(msg); |
| 81 | 84 |
| 82 TRACE_EVENT_INSTANT0("loader", "ResourceDispatchThrottler::ThrottleRequest", | 85 TRACE_EVENT_INSTANT0("loader", "ResourceDispatchThrottler::ThrottleRequest", |
| 83 TRACE_EVENT_SCOPE_THREAD); | 86 TRACE_EVENT_SCOPE_THREAD); |
| 84 throttled_messages_.push_back(msg); | 87 throttled_messages_.push_back(msg); |
| 85 ScheduleFlush(); | 88 ScheduleFlush(); |
| 86 return true; | 89 return true; |
| 87 } | 90 } |
| 88 | 91 |
| 89 base::TimeTicks ResourceDispatchThrottler::Now() const { | 92 base::TimeTicks ResourceDispatchThrottler::Now() const { |
| 90 return base::TimeTicks::Now(); | 93 return base::TimeTicks::Now(); |
| 91 } | 94 } |
| 92 | 95 |
| 93 void ResourceDispatchThrottler::ScheduleFlush() { | 96 void ResourceDispatchThrottler::ScheduleFlush() { |
| 94 DCHECK(!flush_timer_.IsRunning()); | 97 DCHECK(!flush_timer_.IsRunning()); |
| 95 flush_timer_.Reset(); | 98 flush_timer_.Reset(); |
| 96 } | 99 } |
| 97 | 100 |
| 98 void ResourceDispatchThrottler::Flush() { | 101 void ResourceDispatchThrottler::Flush() { |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 TRACE_EVENT1("loader", "ResourceDispatchThrottler::Flush", | 103 TRACE_EVENT1("loader", "ResourceDispatchThrottler::Flush", |
| 101 "total_throttled_messages", throttled_messages_.size()); | 104 "total_throttled_messages", throttled_messages_.size()); |
| 102 sent_requests_since_last_flush_ = 0; | 105 LogFlush(); |
| 103 | 106 |
| 104 // If high-priority work is no longer anticipated, dispatch can be safely | 107 // If high-priority work is no longer anticipated, dispatch can be safely |
| 105 // accelerated. Avoid completely flushing in such case in the event that | 108 // accelerated. Avoid completely flushing in such case in the event that |
| 106 // a large number of requests have been throttled. | 109 // a large number of requests have been throttled. |
| 107 uint32_t max_requests = scheduler_->IsHighPriorityWorkAnticipated() | 110 uint32_t max_requests = scheduler_->IsHighPriorityWorkAnticipated() |
| 108 ? max_requests_per_flush_ | 111 ? max_requests_per_flush_ |
| 109 : max_requests_per_flush_ * 2; | 112 : max_requests_per_flush_ * 2; |
| 110 | 113 |
| 111 while (!throttled_messages_.empty() && | 114 while (!throttled_messages_.empty() && |
| 112 (sent_requests_since_last_flush_ < max_requests || | 115 (sent_requests_since_last_flush_ < max_requests || |
| 113 !IsResourceRequest(*throttled_messages_.front()))) { | 116 !IsResourceRequest(*throttled_messages_.front()))) { |
| 114 IPC::Message* msg = throttled_messages_.front(); | 117 IPC::Message* msg = throttled_messages_.front(); |
| 115 throttled_messages_.pop_front(); | 118 throttled_messages_.pop_front(); |
| 116 ForwardMessage(msg); | 119 ForwardMessage(msg); |
| 117 } | 120 } |
| 118 | 121 |
| 119 if (!throttled_messages_.empty()) | 122 if (!throttled_messages_.empty()) |
| 120 ScheduleFlush(); | 123 ScheduleFlush(); |
| 121 } | 124 } |
| 122 | 125 |
| 123 void ResourceDispatchThrottler::FlushAll() { | 126 void ResourceDispatchThrottler::FlushAll() { |
| 127 LogFlush(); |
| 124 if (throttled_messages_.empty()) | 128 if (throttled_messages_.empty()) |
| 125 return; | 129 return; |
| 126 | 130 |
| 127 TRACE_EVENT1("loader", "ResourceDispatchThrottler::FlushAll", | 131 TRACE_EVENT1("loader", "ResourceDispatchThrottler::FlushAll", |
| 128 "total_throttled_messages", throttled_messages_.size()); | 132 "total_throttled_messages", throttled_messages_.size()); |
| 129 std::deque<IPC::Message*> throttled_messages; | 133 std::deque<IPC::Message*> throttled_messages; |
| 130 throttled_messages.swap(throttled_messages_); | 134 throttled_messages.swap(throttled_messages_); |
| 131 for (auto& message : throttled_messages) | 135 for (auto& message : throttled_messages) |
| 132 ForwardMessage(message); | 136 ForwardMessage(message); |
| 133 // There shouldn't be re-entrancy issues when forwarding an IPC, but validate | 137 // There shouldn't be re-entrancy issues when forwarding an IPC, but validate |
| 134 // as a safeguard. | 138 // as a safeguard. |
| 135 DCHECK(throttled_messages_.empty()); | 139 DCHECK(throttled_messages_.empty()); |
| 136 } | 140 } |
| 137 | 141 |
| 142 void ResourceDispatchThrottler::LogFlush() { |
| 143 sent_requests_since_last_flush_ = 0; |
| 144 last_flush_time_ = Now(); |
| 145 } |
| 146 |
| 138 bool ResourceDispatchThrottler::ForwardMessage(IPC::Message* msg) { | 147 bool ResourceDispatchThrottler::ForwardMessage(IPC::Message* msg) { |
| 139 if (IsResourceRequest(*msg)) { | 148 if (IsResourceRequest(*msg)) |
| 140 last_sent_request_time_ = Now(); | |
| 141 ++sent_requests_since_last_flush_; | 149 ++sent_requests_since_last_flush_; |
| 142 } | 150 |
| 143 return proxied_sender_->Send(msg); | 151 return proxied_sender_->Send(msg); |
| 144 } | 152 } |
| 145 | 153 |
| 146 } // namespace content | 154 } // namespace content |
| OLD | NEW |