| 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" |
| 11 #include "ipc/ipc_message_macros.h" | 11 #include "ipc/ipc_message_macros.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 bool IsResourceRequest(const IPC::Message& msg) { | 16 bool IsResourceRequest(const IPC::Message& msg) { |
| 17 return msg.type() == ResourceHostMsg_RequestResource::ID; | 17 return msg.type() == ResourceHostMsg_RequestResource::ID; |
| 18 } | 18 } |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 ResourceDispatchThrottler::ResourceDispatchThrottler( | 22 ResourceDispatchThrottler::ResourceDispatchThrottler( |
| 23 IPC::Sender* proxied_sender, | 23 IPC::Sender* proxied_sender, |
| 24 scheduler::RendererScheduler* scheduler, | 24 scheduler::RendererScheduler* scheduler, |
| 25 base::TimeDelta flush_period, | 25 base::TimeDelta flush_period, |
| 26 uint32 max_requests_per_flush) | 26 uint32_t max_requests_per_flush) |
| 27 : proxied_sender_(proxied_sender), | 27 : proxied_sender_(proxied_sender), |
| 28 scheduler_(scheduler), | 28 scheduler_(scheduler), |
| 29 flush_period_(flush_period), | 29 flush_period_(flush_period), |
| 30 max_requests_per_flush_(max_requests_per_flush), | 30 max_requests_per_flush_(max_requests_per_flush), |
| 31 flush_timer_( | 31 flush_timer_( |
| 32 FROM_HERE, | 32 FROM_HERE, |
| 33 flush_period_, | 33 flush_period_, |
| 34 base::Bind(&ResourceDispatchThrottler::Flush, base::Unretained(this)), | 34 base::Bind(&ResourceDispatchThrottler::Flush, base::Unretained(this)), |
| 35 false /* is_repeating */), | 35 false /* is_repeating */), |
| 36 sent_requests_since_last_flush_(0) { | 36 sent_requests_since_last_flush_(0) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 97 |
| 98 void ResourceDispatchThrottler::Flush() { | 98 void ResourceDispatchThrottler::Flush() { |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); | 99 DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 TRACE_EVENT1("loader", "ResourceDispatchThrottler::Flush", | 100 TRACE_EVENT1("loader", "ResourceDispatchThrottler::Flush", |
| 101 "total_throttled_messages", throttled_messages_.size()); | 101 "total_throttled_messages", throttled_messages_.size()); |
| 102 sent_requests_since_last_flush_ = 0; | 102 sent_requests_since_last_flush_ = 0; |
| 103 | 103 |
| 104 // If high-priority work is no longer anticipated, dispatch can be safely | 104 // If high-priority work is no longer anticipated, dispatch can be safely |
| 105 // accelerated. Avoid completely flushing in such case in the event that | 105 // accelerated. Avoid completely flushing in such case in the event that |
| 106 // a large number of requests have been throttled. | 106 // a large number of requests have been throttled. |
| 107 uint32 max_requests = scheduler_->IsHighPriorityWorkAnticipated() | 107 uint32_t max_requests = scheduler_->IsHighPriorityWorkAnticipated() |
| 108 ? max_requests_per_flush_ | 108 ? max_requests_per_flush_ |
| 109 : max_requests_per_flush_ * 2; | 109 : max_requests_per_flush_ * 2; |
| 110 | 110 |
| 111 while (!throttled_messages_.empty() && | 111 while (!throttled_messages_.empty() && |
| 112 (sent_requests_since_last_flush_ < max_requests || | 112 (sent_requests_since_last_flush_ < max_requests || |
| 113 !IsResourceRequest(*throttled_messages_.front()))) { | 113 !IsResourceRequest(*throttled_messages_.front()))) { |
| 114 IPC::Message* msg = throttled_messages_.front(); | 114 IPC::Message* msg = throttled_messages_.front(); |
| 115 throttled_messages_.pop_front(); | 115 throttled_messages_.pop_front(); |
| 116 ForwardMessage(msg); | 116 ForwardMessage(msg); |
| 117 } | 117 } |
| 118 | 118 |
| 119 if (!throttled_messages_.empty()) | 119 if (!throttled_messages_.empty()) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 137 | 137 |
| 138 bool ResourceDispatchThrottler::ForwardMessage(IPC::Message* msg) { | 138 bool ResourceDispatchThrottler::ForwardMessage(IPC::Message* msg) { |
| 139 if (IsResourceRequest(*msg)) { | 139 if (IsResourceRequest(*msg)) { |
| 140 last_sent_request_time_ = Now(); | 140 last_sent_request_time_ = Now(); |
| 141 ++sent_requests_since_last_flush_; | 141 ++sent_requests_since_last_flush_; |
| 142 } | 142 } |
| 143 return proxied_sender_->Send(msg); | 143 return proxied_sender_->Send(msg); |
| 144 } | 144 } |
| 145 | 145 |
| 146 } // namespace content | 146 } // namespace content |
| OLD | NEW |