OLD | NEW |
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 "net/dns/dns_session.h" | 5 #include "net/dns/dns_session.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 // Number of buckets in the histogram of observed RTTs. | 30 // Number of buckets in the histogram of observed RTTs. |
31 const size_t kRTTBucketCount = 100; | 31 const size_t kRTTBucketCount = 100; |
32 // Target percentile in the RTT histogram used for retransmission timeout. | 32 // Target percentile in the RTT histogram used for retransmission timeout. |
33 const unsigned kRTOPercentile = 99; | 33 const unsigned kRTOPercentile = 99; |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 // Runtime statistics of DNS server. | 36 // Runtime statistics of DNS server. |
37 struct DnsSession::ServerStats { | 37 struct DnsSession::ServerStats { |
38 ServerStats(base::TimeDelta rtt_estimate_param, RttBuckets* buckets) | 38 ServerStats(base::TimeDelta rtt_estimate_param, RttBuckets* buckets) |
39 : last_failure_count(0), rtt_estimate(rtt_estimate_param) { | 39 : last_failure_count(0), rtt_estimate(rtt_estimate_param) { |
40 rtt_histogram.reset(new base::SampleVector(buckets)); | 40 rtt_histogram.reset(new base::SampleVector(buckets)); |
41 // Seed histogram with 2 samples at |rtt_estimate| timeout. | 41 // Seed histogram with 2 samples at |rtt_estimate| timeout. |
42 rtt_histogram->Accumulate(rtt_estimate.InMilliseconds(), 2); | 42 rtt_histogram->Accumulate(rtt_estimate.InMilliseconds(), 2); |
43 } | 43 } |
44 | 44 |
45 // Count of consecutive failures after last success. | 45 // Count of consecutive failures after last success. |
46 int last_failure_count; | 46 int last_failure_count; |
47 | 47 |
48 // Last time when server returned failure or timeout. | 48 // Last time when server returned failure or timeout. |
49 base::Time last_failure; | 49 base::Time last_failure; |
(...skipping 15 matching lines...) Expand all Loading... |
65 base::LazyInstance<DnsSession::RttBuckets>::Leaky DnsSession::rtt_buckets_ = | 65 base::LazyInstance<DnsSession::RttBuckets>::Leaky DnsSession::rtt_buckets_ = |
66 LAZY_INSTANCE_INITIALIZER; | 66 LAZY_INSTANCE_INITIALIZER; |
67 | 67 |
68 DnsSession::RttBuckets::RttBuckets() : base::BucketRanges(kRTTBucketCount + 1) { | 68 DnsSession::RttBuckets::RttBuckets() : base::BucketRanges(kRTTBucketCount + 1) { |
69 base::Histogram::InitializeBucketRanges(1, 5000, this); | 69 base::Histogram::InitializeBucketRanges(1, 5000, this); |
70 } | 70 } |
71 | 71 |
72 DnsSession::SocketLease::SocketLease(scoped_refptr<DnsSession> session, | 72 DnsSession::SocketLease::SocketLease(scoped_refptr<DnsSession> session, |
73 unsigned server_index, | 73 unsigned server_index, |
74 scoped_ptr<DatagramClientSocket> socket) | 74 scoped_ptr<DatagramClientSocket> socket) |
75 : session_(session), server_index_(server_index), socket_(socket.Pass()) {} | 75 : session_(session), server_index_(server_index), socket_(socket.Pass()) { |
| 76 } |
76 | 77 |
77 DnsSession::SocketLease::~SocketLease() { | 78 DnsSession::SocketLease::~SocketLease() { |
78 session_->FreeSocket(server_index_, socket_.Pass()); | 79 session_->FreeSocket(server_index_, socket_.Pass()); |
79 } | 80 } |
80 | 81 |
81 DnsSession::DnsSession(const DnsConfig& config, | 82 DnsSession::DnsSession(const DnsConfig& config, |
82 scoped_ptr<DnsSocketPool> socket_pool, | 83 scoped_ptr<DnsSocketPool> socket_pool, |
83 const RandIntCallback& rand_int_callback, | 84 const RandIntCallback& rand_int_callback, |
84 NetLog* net_log) | 85 NetLog* net_log) |
85 : config_(config), | 86 : config_(config), |
86 socket_pool_(socket_pool.Pass()), | 87 socket_pool_(socket_pool.Pass()), |
87 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), | 88 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), |
88 net_log_(net_log), | 89 net_log_(net_log), |
89 server_index_(0) { | 90 server_index_(0) { |
90 socket_pool_->Initialize(&config_.nameservers, net_log); | 91 socket_pool_->Initialize(&config_.nameservers, net_log); |
91 UMA_HISTOGRAM_CUSTOM_COUNTS( | 92 UMA_HISTOGRAM_CUSTOM_COUNTS( |
92 "AsyncDNS.ServerCount", config_.nameservers.size(), 0, 10, 10); | 93 "AsyncDNS.ServerCount", config_.nameservers.size(), 0, 10, 10); |
93 for (size_t i = 0; i < config_.nameservers.size(); ++i) { | 94 for (size_t i = 0; i < config_.nameservers.size(); ++i) { |
94 server_stats_.push_back(new ServerStats(config_.timeout, | 95 server_stats_.push_back( |
95 rtt_buckets_.Pointer())); | 96 new ServerStats(config_.timeout, rtt_buckets_.Pointer())); |
96 } | 97 } |
97 } | 98 } |
98 | 99 |
99 DnsSession::~DnsSession() { | 100 DnsSession::~DnsSession() { |
100 RecordServerStats(); | 101 RecordServerStats(); |
101 } | 102 } |
102 | 103 |
103 int DnsSession::NextQueryId() const { return rand_callback_.Run(); } | 104 int DnsSession::NextQueryId() const { |
| 105 return rand_callback_.Run(); |
| 106 } |
104 | 107 |
105 unsigned DnsSession::NextFirstServerIndex() { | 108 unsigned DnsSession::NextFirstServerIndex() { |
106 unsigned index = NextGoodServerIndex(server_index_); | 109 unsigned index = NextGoodServerIndex(server_index_); |
107 if (config_.rotate) | 110 if (config_.rotate) |
108 server_index_ = (server_index_ + 1) % config_.nameservers.size(); | 111 server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
109 return index; | 112 return index; |
110 } | 113 } |
111 | 114 |
112 unsigned DnsSession::NextGoodServerIndex(unsigned server_index) { | 115 unsigned DnsSession::NextGoodServerIndex(unsigned server_index) { |
113 unsigned index = server_index; | 116 unsigned index = server_index; |
(...skipping 26 matching lines...) Expand all Loading... |
140 void DnsSession::RecordServerFailure(unsigned server_index) { | 143 void DnsSession::RecordServerFailure(unsigned server_index) { |
141 UMA_HISTOGRAM_CUSTOM_COUNTS( | 144 UMA_HISTOGRAM_CUSTOM_COUNTS( |
142 "AsyncDNS.ServerFailureIndex", server_index, 0, 10, 10); | 145 "AsyncDNS.ServerFailureIndex", server_index, 0, 10, 10); |
143 ++(server_stats_[server_index]->last_failure_count); | 146 ++(server_stats_[server_index]->last_failure_count); |
144 server_stats_[server_index]->last_failure = base::Time::Now(); | 147 server_stats_[server_index]->last_failure = base::Time::Now(); |
145 } | 148 } |
146 | 149 |
147 void DnsSession::RecordServerSuccess(unsigned server_index) { | 150 void DnsSession::RecordServerSuccess(unsigned server_index) { |
148 if (server_stats_[server_index]->last_success.is_null()) { | 151 if (server_stats_[server_index]->last_success.is_null()) { |
149 UMA_HISTOGRAM_COUNTS_100("AsyncDNS.ServerFailuresAfterNetworkChange", | 152 UMA_HISTOGRAM_COUNTS_100("AsyncDNS.ServerFailuresAfterNetworkChange", |
150 server_stats_[server_index]->last_failure_count); | 153 server_stats_[server_index]->last_failure_count); |
151 } else { | 154 } else { |
152 UMA_HISTOGRAM_COUNTS_100("AsyncDNS.ServerFailuresBeforeSuccess", | 155 UMA_HISTOGRAM_COUNTS_100("AsyncDNS.ServerFailuresBeforeSuccess", |
153 server_stats_[server_index]->last_failure_count); | 156 server_stats_[server_index]->last_failure_count); |
154 } | 157 } |
155 server_stats_[server_index]->last_failure_count = 0; | 158 server_stats_[server_index]->last_failure_count = 0; |
156 server_stats_[server_index]->last_failure = base::Time(); | 159 server_stats_[server_index]->last_failure = base::Time(); |
157 server_stats_[server_index]->last_success = base::Time::Now(); | 160 server_stats_[server_index]->last_success = base::Time::Now(); |
158 } | 161 } |
159 | 162 |
160 void DnsSession::RecordRTT(unsigned server_index, base::TimeDelta rtt) { | 163 void DnsSession::RecordRTT(unsigned server_index, base::TimeDelta rtt) { |
161 DCHECK_LT(server_index, server_stats_.size()); | 164 DCHECK_LT(server_index, server_stats_.size()); |
162 | 165 |
163 // For measurement, assume it is the first attempt (no backoff). | 166 // For measurement, assume it is the first attempt (no backoff). |
(...skipping 11 matching lines...) Expand all Loading... |
175 // Using parameters: alpha = 1/8, delta = 1/4, beta = 4 | 178 // Using parameters: alpha = 1/8, delta = 1/4, beta = 4 |
176 base::TimeDelta& estimate = server_stats_[server_index]->rtt_estimate; | 179 base::TimeDelta& estimate = server_stats_[server_index]->rtt_estimate; |
177 base::TimeDelta& deviation = server_stats_[server_index]->rtt_deviation; | 180 base::TimeDelta& deviation = server_stats_[server_index]->rtt_deviation; |
178 base::TimeDelta current_error = rtt - estimate; | 181 base::TimeDelta current_error = rtt - estimate; |
179 estimate += current_error / 8; // * alpha | 182 estimate += current_error / 8; // * alpha |
180 base::TimeDelta abs_error = base::TimeDelta::FromInternalValue( | 183 base::TimeDelta abs_error = base::TimeDelta::FromInternalValue( |
181 std::abs(current_error.ToInternalValue())); | 184 std::abs(current_error.ToInternalValue())); |
182 deviation += (abs_error - deviation) / 4; // * delta | 185 deviation += (abs_error - deviation) / 4; // * delta |
183 | 186 |
184 // Histogram-based method. | 187 // Histogram-based method. |
185 server_stats_[server_index]->rtt_histogram | 188 server_stats_[server_index]->rtt_histogram->Accumulate(rtt.InMilliseconds(), |
186 ->Accumulate(rtt.InMilliseconds(), 1); | 189 1); |
187 } | 190 } |
188 | 191 |
189 void DnsSession::RecordLostPacket(unsigned server_index, int attempt) { | 192 void DnsSession::RecordLostPacket(unsigned server_index, int attempt) { |
190 base::TimeDelta timeout_jacobson = | 193 base::TimeDelta timeout_jacobson = |
191 NextTimeoutFromJacobson(server_index, attempt); | 194 NextTimeoutFromJacobson(server_index, attempt); |
192 base::TimeDelta timeout_histogram = | 195 base::TimeDelta timeout_histogram = |
193 NextTimeoutFromHistogram(server_index, attempt); | 196 NextTimeoutFromHistogram(server_index, attempt); |
194 UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutSpentJacobson", timeout_jacobson); | 197 UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutSpentJacobson", timeout_jacobson); |
195 UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutSpentHistogram", timeout_histogram); | 198 UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutSpentHistogram", timeout_histogram); |
196 } | 199 } |
197 | 200 |
198 void DnsSession::RecordServerStats() { | 201 void DnsSession::RecordServerStats() { |
199 for (size_t index = 0; index < server_stats_.size(); ++index) { | 202 for (size_t index = 0; index < server_stats_.size(); ++index) { |
200 if (server_stats_[index]->last_failure_count) { | 203 if (server_stats_[index]->last_failure_count) { |
201 if (server_stats_[index]->last_success.is_null()) { | 204 if (server_stats_[index]->last_success.is_null()) { |
202 UMA_HISTOGRAM_COUNTS("AsyncDNS.ServerFailuresWithoutSuccess", | 205 UMA_HISTOGRAM_COUNTS("AsyncDNS.ServerFailuresWithoutSuccess", |
203 server_stats_[index]->last_failure_count); | 206 server_stats_[index]->last_failure_count); |
204 } else { | 207 } else { |
205 UMA_HISTOGRAM_COUNTS("AsyncDNS.ServerFailuresAfterSuccess", | 208 UMA_HISTOGRAM_COUNTS("AsyncDNS.ServerFailuresAfterSuccess", |
206 server_stats_[index]->last_failure_count); | 209 server_stats_[index]->last_failure_count); |
207 } | 210 } |
208 } | 211 } |
209 } | 212 } |
210 } | 213 } |
211 | 214 |
212 | |
213 base::TimeDelta DnsSession::NextTimeout(unsigned server_index, int attempt) { | 215 base::TimeDelta DnsSession::NextTimeout(unsigned server_index, int attempt) { |
214 // Respect config timeout if it exceeds |kMaxTimeoutMs|. | 216 // Respect config timeout if it exceeds |kMaxTimeoutMs|. |
215 if (config_.timeout.InMilliseconds() >= kMaxTimeoutMs) | 217 if (config_.timeout.InMilliseconds() >= kMaxTimeoutMs) |
216 return config_.timeout; | 218 return config_.timeout; |
217 return NextTimeoutFromHistogram(server_index, attempt); | 219 return NextTimeoutFromHistogram(server_index, attempt); |
218 } | 220 } |
219 | 221 |
220 // Allocate a socket, already connected to the server address. | 222 // Allocate a socket, already connected to the server address. |
221 scoped_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket( | 223 scoped_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket( |
222 unsigned server_index, const NetLog::Source& source) { | 224 unsigned server_index, |
| 225 const NetLog::Source& source) { |
223 scoped_ptr<DatagramClientSocket> socket; | 226 scoped_ptr<DatagramClientSocket> socket; |
224 | 227 |
225 socket = socket_pool_->AllocateSocket(server_index); | 228 socket = socket_pool_->AllocateSocket(server_index); |
226 if (!socket.get()) | 229 if (!socket.get()) |
227 return scoped_ptr<SocketLease>(); | 230 return scoped_ptr<SocketLease>(); |
228 | 231 |
229 socket->NetLog().BeginEvent(NetLog::TYPE_SOCKET_IN_USE, | 232 socket->NetLog().BeginEvent(NetLog::TYPE_SOCKET_IN_USE, |
230 source.ToEventParametersCallback()); | 233 source.ToEventParametersCallback()); |
231 | 234 |
232 SocketLease* lease = new SocketLease(this, server_index, socket.Pass()); | 235 SocketLease* lease = new SocketLease(this, server_index, socket.Pass()); |
233 return scoped_ptr<SocketLease>(lease); | 236 return scoped_ptr<SocketLease>(lease); |
234 } | 237 } |
235 | 238 |
236 scoped_ptr<StreamSocket> DnsSession::CreateTCPSocket( | 239 scoped_ptr<StreamSocket> DnsSession::CreateTCPSocket( |
237 unsigned server_index, const NetLog::Source& source) { | 240 unsigned server_index, |
| 241 const NetLog::Source& source) { |
238 return socket_pool_->CreateTCPSocket(server_index, source); | 242 return socket_pool_->CreateTCPSocket(server_index, source); |
239 } | 243 } |
240 | 244 |
241 // Release a socket. | 245 // Release a socket. |
242 void DnsSession::FreeSocket(unsigned server_index, | 246 void DnsSession::FreeSocket(unsigned server_index, |
243 scoped_ptr<DatagramClientSocket> socket) { | 247 scoped_ptr<DatagramClientSocket> socket) { |
244 DCHECK(socket.get()); | 248 DCHECK(socket.get()); |
245 | 249 |
246 socket->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE); | 250 socket->NetLog().EndEvent(NetLog::TYPE_SOCKET_IN_USE); |
247 | 251 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 timeout = std::max(timeout, base::TimeDelta::FromMilliseconds(kMinTimeoutMs)); | 293 timeout = std::max(timeout, base::TimeDelta::FromMilliseconds(kMinTimeoutMs)); |
290 | 294 |
291 // The timeout still doubles every full round. | 295 // The timeout still doubles every full round. |
292 unsigned num_backoffs = attempt / config_.nameservers.size(); | 296 unsigned num_backoffs = attempt / config_.nameservers.size(); |
293 | 297 |
294 return std::min(timeout * (1 << num_backoffs), | 298 return std::min(timeout * (1 << num_backoffs), |
295 base::TimeDelta::FromMilliseconds(kMaxTimeoutMs)); | 299 base::TimeDelta::FromMilliseconds(kMaxTimeoutMs)); |
296 } | 300 } |
297 | 301 |
298 } // namespace net | 302 } // namespace net |
OLD | NEW |