OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "chrome/browser/net/network_stats.h" | |
6 | |
7 #include "base/callback_old.h" | |
8 #include "base/logging.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/metrics/field_trial.h" | |
11 #include "base/metrics/histogram.h" | |
12 #include "base/task.h" | |
13 #include "base/threading/platform_thread.h" | |
14 #include "base/time.h" | |
15 #include "base/tuple.h" | |
16 #include "content/browser/browser_thread.h" | |
17 #include "net/base/address_list.h" | |
18 #include "net/base/host_resolver.h" | |
19 #include "net/base/net_errors.h" | |
20 #include "net/base/net_util.h" | |
21 #include "net/base/network_change_notifier.h" | |
22 #include "net/base/sys_addrinfo.h" | |
23 #include "net/base/test_completion_callback.h" | |
24 #include "net/socket/tcp_client_socket.h" | |
25 #include "net/udp/udp_client_socket.h" | |
26 #include "net/udp/udp_server_socket.h" | |
27 | |
28 namespace chrome_browser_net { | |
29 | |
30 // This specifies the number of bytes to be sent to the TCP/UDP servers as part | |
31 // of small packet size test. | |
32 static const int kSmallTestBytesToSend = 100; | |
33 | |
34 // This specifies the number of bytes to be sent to the TCP/UDP servers as part | |
35 // of large packet size test. | |
36 static const int kLargeTestBytesToSend = 1200; | |
37 | |
38 // NetworkStats methods and members. | |
39 NetworkStats::NetworkStats() | |
40 : bytes_to_read_(0), | |
41 bytes_to_send_(0), | |
42 ALLOW_THIS_IN_INITIALIZER_LIST( | |
43 read_callback_(this, &NetworkStats::OnReadComplete)), | |
44 ALLOW_THIS_IN_INITIALIZER_LIST( | |
45 write_callback_(this, &NetworkStats::OnWriteComplete)), | |
46 finished_callback_(NULL), | |
47 start_time_(base::TimeTicks::Now()) { | |
48 } | |
49 | |
50 NetworkStats::~NetworkStats() { | |
51 socket_.reset(); | |
52 read_buffer_ = NULL; | |
53 } | |
54 | |
55 void NetworkStats::Initialize(int bytes_to_send, | |
56 net::CompletionCallback* finished_callback) { | |
57 DCHECK(bytes_to_send); // We should have data to send. | |
58 | |
59 load_size_ = bytes_to_send; | |
60 bytes_to_send_ = bytes_to_send; | |
61 bytes_to_read_ = bytes_to_send; | |
62 finished_callback_ = finished_callback; | |
63 } | |
64 | |
65 void NetworkStats::set_socket(net::Socket* socket) { | |
66 DCHECK(socket); | |
67 DCHECK(!socket_.get()); | |
68 socket_.reset(socket); | |
69 } | |
70 | |
71 bool NetworkStats::ConnectComplete(int result) { | |
72 if (result < 0) { | |
73 Finish(CONNECT_FAILED, result); | |
74 return false; | |
75 } | |
76 | |
77 DCHECK(bytes_to_send_); // We should have data to send. | |
78 | |
79 start_time_ = base::TimeTicks::Now(); | |
80 | |
81 int rv = SendData(); | |
82 if (rv < 0) { | |
83 if (rv != net::ERR_IO_PENDING) { | |
84 Finish(WRITE_FAILED, rv); | |
85 return false; | |
86 } | |
87 } | |
88 | |
89 stream_.Reset(); | |
90 ReadData(); | |
91 | |
92 return true; | |
93 } | |
94 | |
95 void NetworkStats::DoFinishCallback(int result) { | |
96 if (finished_callback_ != NULL) { | |
97 net::CompletionCallback* callback = finished_callback_; | |
98 finished_callback_ = NULL; | |
99 callback->Run(result); | |
100 } | |
101 } | |
102 | |
103 bool NetworkStats::ReadComplete(int result) { | |
104 DCHECK(socket_.get()); | |
105 if (result < 0) { | |
106 Finish(READ_FAILED, result); | |
107 return true; | |
108 } | |
109 | |
110 if (!stream_.VerifyBytes(read_buffer_->data(), result)) { | |
111 Finish(READ_VERIFY_FAILED, net::ERR_INVALID_RESPONSE); | |
112 return true; | |
113 } | |
114 | |
115 read_buffer_ = NULL; | |
116 bytes_to_read_ -= result; | |
117 | |
118 // No more data to read. | |
119 if (!bytes_to_read_) { | |
120 Finish(SUCCESS, net::OK); | |
121 return true; | |
122 } | |
123 ReadData(); | |
124 return false; | |
125 } | |
126 | |
127 void NetworkStats::OnReadComplete(int result) { | |
128 ReadComplete(result); | |
129 } | |
130 | |
131 void NetworkStats::OnWriteComplete(int result) { | |
132 DCHECK(socket_.get()); | |
133 if (result < 0) { | |
134 Finish(WRITE_FAILED, result); | |
135 return; | |
136 } | |
137 | |
138 write_buffer_->DidConsume(result); | |
139 bytes_to_send_ -= result; | |
140 if (!write_buffer_->BytesRemaining()) | |
141 write_buffer_ = NULL; | |
142 | |
143 if (bytes_to_send_) { | |
144 int rv = SendData(); | |
145 if (rv < 0) { | |
146 if (rv != net::ERR_IO_PENDING) { | |
147 Finish(WRITE_FAILED, rv); | |
148 return; | |
149 } | |
150 } | |
151 } | |
152 } | |
153 | |
154 void NetworkStats::ReadData() { | |
155 DCHECK(!read_buffer_.get()); | |
156 // We release the read_buffer_ in the destructor if there is an error. | |
157 read_buffer_ = new net::IOBuffer(kMaxMessage); | |
158 | |
159 int rv; | |
160 do { | |
161 DCHECK(socket_.get()); | |
162 rv = socket_->Read(read_buffer_, kMaxMessage, &read_callback_); | |
163 if (rv == net::ERR_IO_PENDING) | |
164 return; | |
165 if (ReadComplete(rv)) // Complete the read manually. | |
166 return; | |
167 } while (rv > 0); | |
168 } | |
169 | |
170 int NetworkStats::SendData() { | |
171 DCHECK(bytes_to_send_); // We should have data to send. | |
172 do { | |
173 if (!write_buffer_.get()) { | |
174 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(bytes_to_send_)); | |
175 stream_.GetBytes(buffer->data(), bytes_to_send_); | |
176 write_buffer_ = new net::DrainableIOBuffer(buffer, bytes_to_send_); | |
177 } | |
178 | |
179 DCHECK(socket_.get()); | |
180 int rv = socket_->Write(write_buffer_, | |
181 write_buffer_->BytesRemaining(), | |
182 &write_callback_); | |
183 if (rv < 0) | |
184 return rv; | |
185 write_buffer_->DidConsume(rv); | |
186 bytes_to_send_ -= rv; | |
187 if (!write_buffer_->BytesRemaining()) | |
188 write_buffer_ = NULL; | |
189 } while (bytes_to_send_); | |
190 return net::OK; | |
191 } | |
192 | |
193 // UDPStatsClient methods and members. | |
194 UDPStatsClient::UDPStatsClient() | |
195 : NetworkStats() { | |
196 } | |
197 | |
198 UDPStatsClient::~UDPStatsClient() { | |
199 } | |
200 | |
201 bool UDPStatsClient::Start(const std::string& ip_str, | |
202 int port, | |
203 int bytes_to_send, | |
204 net::CompletionCallback* finished_callback) { | |
205 DCHECK(port); | |
206 DCHECK(bytes_to_send); // We should have data to send. | |
207 | |
208 AddRef(); // Released on Finish(). | |
209 | |
210 Initialize(bytes_to_send, finished_callback); | |
211 | |
212 net::IPAddressNumber ip_number; | |
213 if (!net::ParseIPLiteralToNumber(ip_str, &ip_number)) { | |
214 Finish(IP_STRING_PARSE_FAILED, net::ERR_INVALID_ARGUMENT); | |
215 return false; | |
216 } | |
217 net::IPEndPoint server_address = net::IPEndPoint(ip_number, port); | |
218 | |
219 net::UDPClientSocket* udp_socket = | |
220 new net::UDPClientSocket(NULL, net::NetLog::Source()); | |
221 DCHECK(udp_socket); | |
222 set_socket(udp_socket); | |
223 | |
224 int rv = udp_socket->Connect(server_address); | |
225 return ConnectComplete(rv); | |
226 } | |
227 | |
228 void UDPStatsClient::Finish(Status status, int result) { | |
229 base::TimeDelta duration = base::TimeTicks::Now() - start_time(); | |
230 if (load_size() == kSmallTestBytesToSend) { | |
231 if (result == net::OK) | |
232 UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Success.100B.RTT", duration); | |
233 else | |
234 UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Fail.100B.RTT", duration); | |
235 | |
236 UMA_HISTOGRAM_ENUMERATION( | |
237 "NetConnectivity.UDP.Status.100B", status, STATUS_MAX); | |
238 } else { | |
239 if (result == net::OK) | |
240 UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Success.1K.RTT", duration); | |
241 else | |
242 UMA_HISTOGRAM_TIMES("NetConnectivity.UDP.Fail.1K.RTT", duration); | |
243 | |
244 UMA_HISTOGRAM_ENUMERATION( | |
245 "NetConnectivity.UDP.Status.1K", status, STATUS_MAX); | |
246 } | |
247 | |
248 DoFinishCallback(result); | |
249 | |
250 // Close the socket so that there are no more IO operations. | |
251 net::UDPClientSocket* udp_socket = | |
252 static_cast<net::UDPClientSocket*>(socket().get()); | |
253 if (udp_socket) | |
254 udp_socket->Close(); | |
255 | |
256 // We may be holding the last reference. Do not access |this| after Release(). | |
257 Release(); // Acquired on Start(). | |
258 } | |
259 | |
260 // TCPStatsClient methods and members. | |
261 TCPStatsClient::TCPStatsClient() | |
262 : NetworkStats(), | |
263 ALLOW_THIS_IN_INITIALIZER_LIST( | |
264 connect_callback_(this, &TCPStatsClient::OnConnectComplete)) { | |
265 } | |
266 | |
267 TCPStatsClient::~TCPStatsClient() { | |
268 } | |
269 | |
270 bool TCPStatsClient::Start(const net::HostPortPair& server_host_port_pair, | |
271 int bytes_to_send, | |
272 net::CompletionCallback* finished_callback) { | |
273 DCHECK(bytes_to_send); // We should have data to send. | |
274 | |
275 AddRef(); // Released on Finish(). | |
276 | |
277 Initialize(bytes_to_send, finished_callback); | |
278 | |
279 scoped_ptr<net::HostResolver> system_host_resolver( | |
280 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
281 net::HostResolver::kDefaultRetryAttempts, | |
282 NULL)); | |
283 net::SingleRequestHostResolver host_resolver(system_host_resolver.get()); | |
284 net::HostResolver::RequestInfo request(server_host_port_pair); | |
285 net::AddressList addresses; | |
286 int rv = host_resolver.Resolve(request, &addresses, NULL, net::BoundNetLog()); | |
willchan no longer on Chromium
2011/06/06 09:25:49
Wait, is this a synchronous HostResolver call? I d
ramant (doing other things)
2011/06/07 20:54:00
Many many thanks for pointing this out. Added the
| |
287 if (rv != net::OK) { | |
288 Finish(RESOLVE_FAILED, rv); | |
289 return false; | |
290 } | |
291 | |
292 net::TCPClientSocket* tcp_socket = | |
293 new net::TCPClientSocket(addresses, NULL, net::NetLog::Source()); | |
294 DCHECK(tcp_socket); | |
295 set_socket(tcp_socket); | |
296 | |
297 rv = tcp_socket->Connect(&connect_callback_); | |
298 if (rv == net::ERR_IO_PENDING) | |
299 return true; | |
300 | |
301 return ConnectComplete(rv); | |
302 } | |
303 | |
304 void TCPStatsClient::OnConnectComplete(int result) { | |
305 ConnectComplete(result); | |
306 } | |
307 | |
308 void TCPStatsClient::Finish(Status status, int result) { | |
309 base::TimeDelta duration = base::TimeTicks::Now() - start_time(); | |
310 if (load_size() == kSmallTestBytesToSend) { | |
311 if (result == net::OK) | |
312 UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Success.100B.RTT", duration); | |
313 else | |
314 UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Fail.100B.RTT", duration); | |
315 | |
316 UMA_HISTOGRAM_ENUMERATION( | |
317 "NetConnectivity.TCP.Status.100B", status, STATUS_MAX); | |
318 } else { | |
319 if (result == net::OK) | |
320 UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Success.1K.RTT", duration); | |
321 else | |
322 UMA_HISTOGRAM_TIMES("NetConnectivity.TCP.Fail.1K.RTT", duration); | |
323 | |
324 UMA_HISTOGRAM_ENUMERATION( | |
325 "NetConnectivity.TCP.Status.1K", status, STATUS_MAX); | |
326 } | |
327 | |
328 DoFinishCallback(result); | |
329 | |
330 // Disconnect the socket so that there are no more IO operations. | |
331 net::TCPClientSocket* tcp_socket = | |
332 static_cast<net::TCPClientSocket*>(socket().get()); | |
333 if (tcp_socket) | |
334 tcp_socket->Disconnect(); | |
335 | |
336 // We may be holding the last reference. Do not access |this| after Release(). | |
337 Release(); // Acquired on Start(). | |
willchan no longer on Chromium
2011/06/06 09:25:49
Why is this refcounted? Who is acquiring a referen
ramant (doing other things)
2011/06/07 20:54:00
Done.
| |
338 } | |
339 | |
340 // static | |
341 void CollectNetworkStats(const std::string& network_stats_server) { | |
342 if (network_stats_server.empty()) | |
343 return; | |
344 | |
345 // If we are not on IO Thread, then post a task to call CollectNetworkStats on | |
346 // IO Thread. | |
347 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
348 BrowserThread::PostTask( | |
349 BrowserThread::IO, | |
350 FROM_HERE, | |
351 NewRunnableFunction(&CollectNetworkStats, network_stats_server)); | |
352 return; | |
353 } | |
354 | |
355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
356 | |
357 // Collect network stats is there is a network connection. | |
willchan no longer on Chromium
2011/06/06 09:25:49
if there is
ramant (doing other things)
2011/06/07 20:54:00
Done.
| |
358 if (net::NetworkChangeNotifier::IsOffline()) | |
willchan no longer on Chromium
2011/06/06 09:25:49
How often is this called? I've noticed that IsOffl
ramant (doing other things)
2011/06/07 20:54:00
We call this once in 30 mins and only when UMA upl
| |
359 return; | |
360 | |
361 static scoped_refptr<base::FieldTrial> trial = NULL; | |
362 static bool collect_stats = false; | |
363 | |
364 if (!trial.get()) { | |
365 // Set up a field trial to collect network stats for UDP and TCP. | |
366 base::FieldTrial::Probability kDivisor = 1000; | |
367 | |
368 // Enable the connectivity testing for 0.5% of the users. | |
369 base::FieldTrial::Probability kProbabilityPerGroup = 5; | |
370 | |
371 // After October 30, 2011 builds, it will always be in default group | |
372 // (disable_network_stats). | |
373 trial = new base::FieldTrial("NetworkConnectivity", kDivisor, | |
374 "disable_network_stats", 2011, 10, 30); | |
375 | |
376 // Add option to collect_stats for NetworkConnectivity. | |
377 int collect_stats_group = trial->AppendGroup("collect_stats", | |
378 kProbabilityPerGroup); | |
379 if (trial->group() == collect_stats_group) | |
380 collect_stats = true; | |
381 } | |
382 | |
383 if (!collect_stats) | |
384 return; | |
385 | |
386 // Run test once. | |
387 static const size_t kMaxNumberOfTests = INT_MAX; | |
willchan no longer on Chromium
2011/06/06 09:25:49
No need for the static. Just let the compiler allo
ramant (doing other things)
2011/06/07 20:54:00
Done.
| |
388 static size_t number_of_tests_done = 0; | |
389 | |
390 if (number_of_tests_done > kMaxNumberOfTests) | |
391 return; | |
392 | |
393 ++number_of_tests_done; | |
394 | |
395 scoped_refptr<UDPStatsClient> small_udp_stats(new UDPStatsClient()); | |
396 small_udp_stats->Start( | |
397 network_stats_server, 6121, kSmallTestBytesToSend, NULL); | |
willchan no longer on Chromium
2011/06/06 09:25:49
Use a named constant for 6121. Perhaps something l
ramant (doing other things)
2011/06/07 20:54:00
Done.
| |
398 | |
399 scoped_refptr<UDPStatsClient> large_udp_stats(new UDPStatsClient()); | |
400 large_udp_stats->Start( | |
401 network_stats_server, 6121, kLargeTestBytesToSend, NULL); | |
402 | |
403 net::HostPortPair server_address(network_stats_server, 80); | |
404 scoped_refptr<TCPStatsClient> small_tcp_client(new TCPStatsClient()); | |
405 small_tcp_client->Start(server_address, kSmallTestBytesToSend, NULL); | |
406 | |
407 scoped_refptr<TCPStatsClient> large_tcp_client(new TCPStatsClient()); | |
408 large_tcp_client->Start(server_address, kLargeTestBytesToSend, NULL); | |
409 } | |
410 | |
411 } // namespace chrome_browser_net | |
OLD | NEW |