1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_
#define CHROME_BROWSER_NET_NETWORK_STATS_H_
#include <bitset>
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
#include "base/time/time.h"
#include "chrome/browser/io_thread.h"
#include "chrome/browser/net/probe_message.h"
#include "net/base/address_list.h"
#include "net/base/completion_callback.h"
#include "net/base/host_port_pair.h"
#include "net/base/io_buffer.h"
#include "net/proxy/proxy_info.h"
namespace net {
class ClientSocketFactory;
class DatagramClientSocket;
class HostResolver;
class SingleRequestHostResolver;
}
namespace chrome_browser_net {
// This class is used for live experiment of network connectivity (for UDP)
// metrics. A small percentage of users participate in this experiment. All
// users (who are in the experiment) must have enabled "UMA upload".
// In the experiments, clients request the server to send some probing packets,
// collect some stats, and send back the results via UMA reports.
//
// This class collects the following stats from users who have opted in.
// a) RTT.
// b) packet inter-arrival time.
// c) packet losses for correlation and FEC experiments.
// d) packet losses for NAT binding test after idling for a certain period.
//
// There are three tests in one experiment. Right before each test, a
// HelloRequest is sent to get an updated token.
// 1. |START_PACKET_TEST|: 21 packets are sent from the server to the client
// without pacing.
// 2. |PACED_PACKET_TEST| or |NON_PACED_PACKET_TEST|: After the first test,
// 21 packets are sent from the server to the client with or without pacing.
// If pacing, the pacing rate is computed from the first test.
// 3. |NAT_BIND_TEST|: 2 packets are sent from the server to the client with
// a randomly generated delay of 1~300 seconds.
// At the end of these tests, we send another HelloRequest to test whether
// the network is still connected and has not changed (e.g. from Wifi to 3g).
class NetworkStats {
public:
enum Status { // Used in UMA_HISTOGRAM_ENUMERATION.
SUCCESS, // Successfully received bytes from the server.
SOCKET_CREATE_FAILED, // Socket creation failed.
RESOLVE_FAILED, // Host resolution failed.
CONNECT_FAILED, // Connection to the server failed.
WRITE_FAILED, // Sending a message to the server failed.
READ_TIMED_OUT, // Reading the reply from the server timed out.
READ_FAILED, // Reading the reply from the server failed.
STATUS_MAX, // Bounding value.
};
enum ReadState { // Used to track if |socket_| has a pending read.
READ_STATE_IDLE,
READ_STATE_READ_PENDING,
};
enum WriteState { // Used to track if |socket_| has a pending write.
WRITE_STATE_IDLE,
WRITE_STATE_WRITE_PENDING,
};
// |TestType| specifies the possible tests we may run
// (except for the first and the last serving as boundaries).
enum TestType {
// The first one is for requesting token, not a probe test. Put it here
// because we use it as a symbol for sending the HelloRequest packet to
// acquire the token.
TOKEN_REQUEST,
START_PACKET_TEST, // First packet loss test (no pacing).
NON_PACED_PACKET_TEST, // Packet loss test with no pacing.
PACED_PACKET_TEST, // Packet loss test with pacing.
// Test whether NAT binding expires after some idle period.
NAT_BIND_TEST,
PACKET_SIZE_TEST,
TEST_TYPE_MAX,
};
// Pointer |socket_factory| is NOT deleted by this class.
explicit NetworkStats(net::ClientSocketFactory* socket_factory);
// NetworkStats is deleted in TestPhaseComplete() when all tests are done.
~NetworkStats();
// Start the client and connect to |server|.
// A client will request a token and then perform several tests.
// When the client finishes all tests, or when an error occurs causing the
// client to stop, |TestPhaseComplete| will be called with a net status code.
// |TestPhaseComplete| will collect histogram stats.
// Return true if successful in starting the client.
bool Start(net::HostResolver* host_resolver,
const net::HostPortPair& server,
uint16 histogram_port,
bool has_proxy_server,
uint32 probe_bytes,
uint32 bytes_for_packet_size_test,
const net::CompletionCallback& callback);
private:
friend class NetworkStatsTest;
// Start the test specified by the current_test_index_. It also resets all
// the book keeping data, before starting the new test.
void StartOneTest();
// Reset all the counters and the collected stats.
void ResetData();
// Callback that is called when host resolution is completed.
void OnResolveComplete(int result);
// Called after host is resolved. Creates UDPClientSocket and connects to the
// server. If successfully connected, then calls ConnectComplete() to start
// the network connectivity tests. Returns |false| if there is any error.
bool DoConnect(int result);
// This method is called after socket connection is completed. It will start
// the process of sending packets to |server| by calling SendHelloPacket().
// Return false if connection is not established (result is less than 0).
bool ConnectComplete(int result);
// Send a HelloRequest packet which asks for a token from the server. If
// a token is received, it will will add |START_PACKET_TEST| to the test
// queue.
void SendHelloRequest();
// Send a ProbeRequest packet which requests the server to send a set
// of Probing packets.
void SendProbeRequest();
// Read and process the data. Called from OnReadComplete() or ReadData().
// This function calls TestPhaseComplete() if there is a significant network
// error or if all packets in the current test are received.
// Return true if TestPhaseComplete() is called otherwise return false.
bool ReadComplete(int result);
// Callbacks when an internal IO (Read or Write) is completed.
void OnReadComplete(int result);
void OnWriteComplete(int result);
// Read data from server until an error or IO blocking occurs or reading is
// complete. Return the result value from socket reading and 0 if |socket_|
// is Null.
int ReadData();
// Send data contained in |str| to server.
// Return a negative value if IO blocking occurs or there is an error.
// Otherwise return net::OK.
int SendData(const std::string& str);
// Update the send buffer (telling it that |bytes_sent| has been sent).
// And reset |write_buffer_|.
void UpdateSendBuffer(int bytes_sent);
// Start a timer (with value |milliseconds|) for responses from the probe
// servers. |test_index| is the index of the test at vector |test_sequence_|
// and it is used as a parameter of the timer callback.
void StartReadDataTimer(uint32 milliseconds, uint32 test_index);
// Called when the StartReadDataTimer fires. |test_index| specifies
// the index of the test. If |current_test_index_| has changed to a
// different value, it indicates |test_index| has completed, then
// this method is a no-op.
void OnReadDataTimeout(uint32 test_index);
// Collect network connectivity stats. This is called when all the data from
// server is read or when there is a failure during connect/read/write. It
// will either start the next phase of the test, or it will self destruct
// at the end of this method. Returns true if a new test wasn't started and it
// was self destructed.
bool TestPhaseComplete(Status status, int result);
// This method is called from TestPhaseComplete() and calls
// |finished_callback_| callback to indicate that the test has finished.
void DoFinishCallback(int result);
// Update counters/metrics for the given |probe_packet|.
// Return true if all packets for the current test are received and
// false otherwise.
bool UpdateReception(const ProbePacket& probe_packet);
// Record all histograms for current test phase, which is assumed to be
// complete (i.e., we are no longer waiting for packets in this phase).
// |test_type| is the current test_type to be recorded. |status| is the
// status of the current test.
void RecordHistograms(TestType test_type);
// Collect the following network connectivity stats when
// kMaximumSequentialPackets (21) packets are sent from the server.
// a) Client received at least one packet.
// b) Client received the nth packet.
// c) The number of packets received for each subsequence of packets 1...n.
void RecordPacketsReceivedHistograms(TestType test_type);
// Collect the following network connectivity stats for the first
// kMaximumCorrelationPackets (6) packets in a test.
// Success/failure of each packet, to estimate reachability for users,
// and to estimate if there is a probabalistic dependency in packet loss when
// kMaximumCorrelationPackets packets are sent consecutively.
void RecordPacketLossSeriesHistograms(TestType test_type);
// Collect the average inter-arrival time (scaled up by 20 times because the
// minimum time value in a histogram is 1ms) of a sequence of probing packets.
void RecordInterArrivalHistograms(TestType test_type);
// Collect the RTT for the packet specified by the |index| in the current
// test.
void RecordRTTHistograms(TestType test_type, uint32 index);
// Collect whether the second packet in the NAT test is received for the
// given idle time.
void RecordNATTestReceivedHistograms(Status status);
// Collect whether we have the requested packet size was received or not in
// the PACKET_SIZE_TEST test.
void RecordPacketSizeTestReceivedHistograms(Status status);
// Record the time duration between sending the probe request and receiving
// the last probe packet excluding the pacing time requested by the client.
// This applies to both NAT bind test and paced/non-paced packet test.
void RecordSendToLastRecvDelayHistograms(TestType test_type);
// Return the next test type (internally increment |current_test_index_|)
// in |test_sequence_|;
TestType GetNextTest();
// These static variables are defined so that they can be changed in testing.
// Maximum number of tests in one activation of the experiment.
static uint32 maximum_tests_;
// Maximum number of packets for START/PACED/NON_PACED tests.
static uint32 maximum_sequential_packets_;
// Maximum number of packets for NAT binding test.
static uint32 maximum_NAT_packets_;
// Maximum time duration between the two packets for NAT Bind testing.
static uint32 maximum_NAT_idle_seconds_;
// Whether to start the probe test immediately after connect success.
// Used for unittest.
static bool start_test_after_connect_;
// The socket handler for this session.
scoped_ptr<net::DatagramClientSocket> socket_;
net::ClientSocketFactory* socket_factory_;
// The read buffer used to read data from the socket.
scoped_refptr<net::IOBuffer> read_buffer_;
// The write buffer used to write data to the socket.
scoped_refptr<net::DrainableIOBuffer> write_buffer_;
// Specify the port for which we are testing the network connectivity.
uint16 histogram_port_;
// Specify if there is a proxy server or not.
bool has_proxy_server_;
// HostResolver used to find the IP addresses.
scoped_ptr<net::SingleRequestHostResolver> resolver_;
// Addresses filled out by HostResolver after host resolution is completed.
net::AddressList addresses_;
// Callback to call when test is successefully finished or whenever
// there is an error (this will be used by unittests to check the result).
net::CompletionCallback finished_callback_;
// RTTs for each packet.
std::vector<base::TimeDelta> packet_rtt_;
// Time when sending probe_request, used for computing RTT.
base::TimeTicks probe_request_time_;
// Size of the probe packets requested to be sent from servers. We don't use
// |probe_packet_bytes_| during PACKET_SIZE_TEST.
uint32 probe_packet_bytes_;
// Size of the packet requested to be sent from servers for PACKET_SIZE_TEST.
uint32 bytes_for_packet_size_test_;
// bitmask indicating which packets are received.
std::bitset<21> packets_received_mask_;
// Arrival time of the first packet in the current test.
base::TimeTicks first_arrival_time_;
// Arrival time of the most recently received packet in the current test.
base::TimeTicks last_arrival_time_;
// Average time between two consecutive packets. It is updated when either all
// packets are received or timeout happens in the current test.
base::TimeDelta inter_arrival_time_;
// Target time duration for sending two consecutive packets at the server.
// It should be 0 for StartPacket test or NonPacedPacket test. For
// PacedPacket test, it is derived from the inter_arrival_time_ in the
// previous (StartPacket) test. For NATBind test, it is randomly generated
// between 1 second and |maximum_NAT_idle_seconds_| seconds.
base::TimeDelta pacing_interval_;
// A list of tests that will be performed in sequence.
std::vector<TestType> test_sequence_;
uint32 current_test_index_; // Index of the current test.
ProbeMessage probe_message_;
// Token received from server for authentication.
ProbePacket_Token token_;
// The state variables to track pending reads/writes.
ReadState read_state_;
WriteState write_state_;
// We use this factory to create timeout tasks for socket's ReadData.
base::WeakPtrFactory<NetworkStats> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(NetworkStats);
};
class ProxyDetector {
public:
// Used for the callback that is called from |OnResolveProxyComplete|.
typedef base::Callback<void(bool)> OnResolvedCallback;
// Construct a ProxyDetector object that finds out if access to
// |server_address| goes through a proxy server or not. Calls the |callback|
// after proxy resolution is completed by currying the proxy resolution
// status.
ProxyDetector(net::ProxyService* proxy_service,
const net::HostPortPair& server_address,
OnResolvedCallback callback);
// This method uses |proxy_service_| to resolve the proxy for
// |server_address_|.
void StartResolveProxy();
private:
// This object is deleted from |OnResolveProxyComplete|.
~ProxyDetector();
// Call the |callback_| by currying the proxy resolution status.
void OnResolveProxyComplete(int result);
// |proxy_service_| specifies the proxy service that is to be used to find
// if access to |server_address_| goes through proxy server or not.
net::ProxyService* proxy_service_;
// |server_address_| specifies the server host and port pair for which we are
// trying to see if access to it, goes through proxy or not.
net::HostPortPair server_address_;
// |callback_| will be called after proxy resolution is completed.
OnResolvedCallback callback_;
// |proxy_info_| holds proxy information returned by ResolveProxy.
net::ProxyInfo proxy_info_;
// Indicate if there is a pending a proxy resolution. We use this to assert
// that there is no in-progress proxy resolution request.
bool has_pending_proxy_resolution_;
DISALLOW_COPY_AND_ASSIGN(ProxyDetector);
};
// This collects the network connectivity stats for UDP protocol for small
// percentage of users who are participating in the experiment (by enabling
// "UMA upload"). This method gets called only if UMA upload to the
// server has succeeded.
void CollectNetworkStats(const std::string& network_stats_server_url,
IOThread* io_thread);
// This starts a series of tests randomly selected among one of the three
// choices of probe packet sizes: 100 Bytes, 500 Bytes, 1200 Bytes.
void StartNetworkStatsTest(net::HostResolver* host_resolver,
const net::HostPortPair& server_address,
uint16 histogram_port,
bool has_proxy_server);
} // namespace chrome_browser_net
#endif // CHROME_BROWSER_NET_NETWORK_STATS_H_
|