Sort unlaunched apps on app list start page by apps grid order.
[chromium-blink-merge.git] / net / quic / quic_connection_logger.cc
blob747ddd639d239ce49d1653d51eb777630c58b872
1 // Copyright (c) 2013 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.
5 #include "net/quic/quic_connection_logger.h"
7 #include <algorithm>
8 #include <string>
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/sparse_histogram.h"
14 #include "base/profiler/scoped_tracker.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/values.h"
17 #include "net/base/net_log.h"
18 #include "net/base/net_util.h"
19 #include "net/cert/cert_verify_result.h"
20 #include "net/cert/x509_certificate.h"
21 #include "net/quic/crypto/crypto_handshake_message.h"
22 #include "net/quic/crypto/crypto_protocol.h"
23 #include "net/quic/quic_address_mismatch.h"
24 #include "net/quic/quic_socket_address_coder.h"
26 using base::StringPiece;
27 using std::string;
29 namespace net {
31 namespace {
33 // We have ranges-of-buckets in the cumulative histogram (covering 21 packet
34 // sequences) of length 2, 3, 4, ... 22.
35 // Hence the largest sample is bounded by the sum of those numbers.
36 const int kBoundingSampleInCumulativeHistogram = ((2 + 22) * 21) / 2;
38 base::Value* NetLogQuicPacketCallback(const IPEndPoint* self_address,
39 const IPEndPoint* peer_address,
40 size_t packet_size,
41 NetLog::LogLevel /* log_level */) {
42 base::DictionaryValue* dict = new base::DictionaryValue();
43 dict->SetString("self_address", self_address->ToString());
44 dict->SetString("peer_address", peer_address->ToString());
45 dict->SetInteger("size", packet_size);
46 return dict;
49 base::Value* NetLogQuicPacketSentCallback(
50 const SerializedPacket& serialized_packet,
51 EncryptionLevel level,
52 TransmissionType transmission_type,
53 size_t packet_size,
54 QuicTime sent_time,
55 NetLog::LogLevel /* log_level */) {
56 base::DictionaryValue* dict = new base::DictionaryValue();
57 dict->SetInteger("encryption_level", level);
58 dict->SetInteger("transmission_type", transmission_type);
59 dict->SetString("packet_sequence_number",
60 base::Uint64ToString(serialized_packet.sequence_number));
61 dict->SetInteger("size", packet_size);
62 dict->SetInteger("sent_time_us",
63 static_cast<int>(sent_time.ToDebuggingValue()));
64 return dict;
67 base::Value* NetLogQuicPacketRetransmittedCallback(
68 QuicPacketSequenceNumber old_sequence_number,
69 QuicPacketSequenceNumber new_sequence_number,
70 NetLog::LogLevel /* log_level */) {
71 base::DictionaryValue* dict = new base::DictionaryValue();
72 dict->SetString("old_packet_sequence_number",
73 base::Uint64ToString(old_sequence_number));
74 dict->SetString("new_packet_sequence_number",
75 base::Uint64ToString(new_sequence_number));
76 return dict;
79 base::Value* NetLogQuicPacketHeaderCallback(const QuicPacketHeader* header,
80 NetLog::LogLevel /* log_level */) {
81 base::DictionaryValue* dict = new base::DictionaryValue();
82 dict->SetString("connection_id",
83 base::Uint64ToString(header->public_header.connection_id));
84 dict->SetInteger("reset_flag", header->public_header.reset_flag);
85 dict->SetInteger("version_flag", header->public_header.version_flag);
86 dict->SetString("packet_sequence_number",
87 base::Uint64ToString(header->packet_sequence_number));
88 dict->SetInteger("entropy_flag", header->entropy_flag);
89 dict->SetInteger("fec_flag", header->fec_flag);
90 dict->SetInteger("fec_group", static_cast<int>(header->fec_group));
91 return dict;
94 base::Value* NetLogQuicStreamFrameCallback(const QuicStreamFrame* frame,
95 NetLog::LogLevel /* log_level */) {
96 base::DictionaryValue* dict = new base::DictionaryValue();
97 dict->SetInteger("stream_id", frame->stream_id);
98 dict->SetBoolean("fin", frame->fin);
99 dict->SetString("offset", base::Uint64ToString(frame->offset));
100 dict->SetInteger("length", frame->data.TotalBufferSize());
101 return dict;
104 base::Value* NetLogQuicAckFrameCallback(const QuicAckFrame* frame,
105 NetLog::LogLevel /* log_level */) {
106 base::DictionaryValue* dict = new base::DictionaryValue();
107 dict->SetString("largest_observed",
108 base::Uint64ToString(frame->largest_observed));
109 dict->SetInteger(
110 "delta_time_largest_observed_us",
111 static_cast<int>(frame->delta_time_largest_observed.ToMicroseconds()));
112 dict->SetInteger("entropy_hash",
113 frame->entropy_hash);
114 dict->SetBoolean("truncated", frame->is_truncated);
116 base::ListValue* missing = new base::ListValue();
117 dict->Set("missing_packets", missing);
118 const SequenceNumberSet& missing_packets = frame->missing_packets;
119 for (SequenceNumberSet::const_iterator it = missing_packets.begin();
120 it != missing_packets.end(); ++it) {
121 missing->AppendString(base::Uint64ToString(*it));
124 base::ListValue* revived = new base::ListValue();
125 dict->Set("revived_packets", revived);
126 const SequenceNumberSet& revived_packets = frame->revived_packets;
127 for (SequenceNumberSet::const_iterator it = revived_packets.begin();
128 it != revived_packets.end(); ++it) {
129 revived->AppendString(base::Uint64ToString(*it));
132 base::ListValue* received = new base::ListValue();
133 dict->Set("received_packet_times", received);
134 const PacketTimeList& received_times = frame->received_packet_times;
135 for (PacketTimeList::const_iterator it = received_times.begin();
136 it != received_times.end(); ++it) {
137 base::DictionaryValue* info = new base::DictionaryValue();
138 info->SetInteger("sequence_number", static_cast<int>(it->first));
139 info->SetInteger("received",
140 static_cast<int>(it->second.ToDebuggingValue()));
141 received->Append(info);
144 return dict;
147 base::Value* NetLogQuicRstStreamFrameCallback(
148 const QuicRstStreamFrame* frame,
149 NetLog::LogLevel /* log_level */) {
150 base::DictionaryValue* dict = new base::DictionaryValue();
151 dict->SetInteger("stream_id", frame->stream_id);
152 dict->SetInteger("quic_rst_stream_error", frame->error_code);
153 dict->SetString("details", frame->error_details);
154 return dict;
157 base::Value* NetLogQuicConnectionCloseFrameCallback(
158 const QuicConnectionCloseFrame* frame,
159 NetLog::LogLevel /* log_level */) {
160 base::DictionaryValue* dict = new base::DictionaryValue();
161 dict->SetInteger("quic_error", frame->error_code);
162 dict->SetString("details", frame->error_details);
163 return dict;
166 base::Value* NetLogQuicWindowUpdateFrameCallback(
167 const QuicWindowUpdateFrame* frame,
168 NetLog::LogLevel /* log_level */) {
169 base::DictionaryValue* dict = new base::DictionaryValue();
170 dict->SetInteger("stream_id", frame->stream_id);
171 dict->SetString("byte_offset", base::Uint64ToString(frame->byte_offset));
172 return dict;
175 base::Value* NetLogQuicBlockedFrameCallback(
176 const QuicBlockedFrame* frame,
177 NetLog::LogLevel /* log_level */) {
178 base::DictionaryValue* dict = new base::DictionaryValue();
179 dict->SetInteger("stream_id", frame->stream_id);
180 return dict;
183 base::Value* NetLogQuicGoAwayFrameCallback(
184 const QuicGoAwayFrame* frame,
185 NetLog::LogLevel /* log_level */) {
186 base::DictionaryValue* dict = new base::DictionaryValue();
187 dict->SetInteger("quic_error", frame->error_code);
188 dict->SetInteger("last_good_stream_id", frame->last_good_stream_id);
189 dict->SetString("reason_phrase", frame->reason_phrase);
190 return dict;
193 base::Value* NetLogQuicStopWaitingFrameCallback(
194 const QuicStopWaitingFrame* frame,
195 NetLog::LogLevel /* log_level */) {
196 base::DictionaryValue* dict = new base::DictionaryValue();
197 base::DictionaryValue* sent_info = new base::DictionaryValue();
198 dict->Set("sent_info", sent_info);
199 sent_info->SetString("least_unacked",
200 base::Uint64ToString(frame->least_unacked));
201 return dict;
204 base::Value* NetLogQuicVersionNegotiationPacketCallback(
205 const QuicVersionNegotiationPacket* packet,
206 NetLog::LogLevel /* log_level */) {
207 base::DictionaryValue* dict = new base::DictionaryValue();
208 base::ListValue* versions = new base::ListValue();
209 dict->Set("versions", versions);
210 for (QuicVersionVector::const_iterator it = packet->versions.begin();
211 it != packet->versions.end(); ++it) {
212 versions->AppendString(QuicVersionToString(*it));
214 return dict;
217 base::Value* NetLogQuicCryptoHandshakeMessageCallback(
218 const CryptoHandshakeMessage* message,
219 NetLog::LogLevel /* log_level */) {
220 base::DictionaryValue* dict = new base::DictionaryValue();
221 dict->SetString("quic_crypto_handshake_message", message->DebugString());
222 return dict;
225 base::Value* NetLogQuicOnConnectionClosedCallback(
226 QuicErrorCode error,
227 bool from_peer,
228 NetLog::LogLevel /* log_level */) {
229 base::DictionaryValue* dict = new base::DictionaryValue();
230 dict->SetInteger("quic_error", error);
231 dict->SetBoolean("from_peer", from_peer);
232 return dict;
235 base::Value* NetLogQuicCertificateVerifiedCallback(
236 scoped_refptr<X509Certificate> cert,
237 NetLog::LogLevel /* log_level */) {
238 // Only the subjects are logged so that we can investigate connection pooling.
239 // More fields could be logged in the future.
240 std::vector<std::string> dns_names;
241 cert->GetDNSNames(&dns_names);
242 base::DictionaryValue* dict = new base::DictionaryValue();
243 base::ListValue* subjects = new base::ListValue();
244 for (std::vector<std::string>::const_iterator it = dns_names.begin();
245 it != dns_names.end(); it++) {
246 subjects->Append(new base::StringValue(*it));
248 dict->Set("subjects", subjects);
249 return dict;
252 void UpdatePacketGapSentHistogram(size_t num_consecutive_missing_packets) {
253 UMA_HISTOGRAM_COUNTS("Net.QuicSession.PacketGapSent",
254 num_consecutive_missing_packets);
257 void UpdatePublicResetAddressMismatchHistogram(
258 const IPEndPoint& server_hello_address,
259 const IPEndPoint& public_reset_address) {
260 int sample = GetAddressMismatch(server_hello_address, public_reset_address);
261 // We are seemingly talking to an older server that does not support the
262 // feature, so we can't report the results in the histogram.
263 if (sample < 0) {
264 return;
266 UMA_HISTOGRAM_ENUMERATION("Net.QuicSession.PublicResetAddressMismatch2",
267 sample, QUIC_ADDRESS_MISMATCH_MAX);
270 const char* GetConnectionDescriptionString() {
271 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
272 tracked_objects::ScopedTracker tracking_profile(
273 FROM_HERE_WITH_EXPLICIT_FUNCTION(
274 "422516 QuicConnectionLogger GetConnectionDescriptionString"));
276 NetworkChangeNotifier::ConnectionType type =
277 NetworkChangeNotifier::GetConnectionType();
278 const char* description = NetworkChangeNotifier::ConnectionTypeToString(type);
279 // Most platforms don't distingish Wifi vs Etherenet, and call everything
280 // CONNECTION_UNKNOWN :-(. We'll tease out some details when we are on WiFi,
281 // and hopefully leave only ethernet (with no WiFi available) in the
282 // CONNECTION_UNKNOWN category. This *might* err if there is both ethernet,
283 // as well as WiFi, where WiFi was not being used that much.
284 // This function only seems usefully defined on Windows currently.
285 if (type == NetworkChangeNotifier::CONNECTION_UNKNOWN ||
286 type == NetworkChangeNotifier::CONNECTION_WIFI) {
287 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is
288 // fixed.
289 tracked_objects::ScopedTracker tracking_profile1(
290 FROM_HERE_WITH_EXPLICIT_FUNCTION(
291 "422516 QuicConnectionLogger GetConnectionDescriptionString1"));
293 WifiPHYLayerProtocol wifi_type = GetWifiPHYLayerProtocol();
294 switch (wifi_type) {
295 case WIFI_PHY_LAYER_PROTOCOL_NONE:
296 // No wifi support or no associated AP.
297 break;
298 case WIFI_PHY_LAYER_PROTOCOL_ANCIENT:
299 // An obsolete modes introduced by the original 802.11, e.g. IR, FHSS.
300 description = "CONNECTION_WIFI_ANCIENT";
301 break;
302 case WIFI_PHY_LAYER_PROTOCOL_A:
303 // 802.11a, OFDM-based rates.
304 description = "CONNECTION_WIFI_802.11a";
305 break;
306 case WIFI_PHY_LAYER_PROTOCOL_B:
307 // 802.11b, DSSS or HR DSSS.
308 description = "CONNECTION_WIFI_802.11b";
309 break;
310 case WIFI_PHY_LAYER_PROTOCOL_G:
311 // 802.11g, same rates as 802.11a but compatible with 802.11b.
312 description = "CONNECTION_WIFI_802.11g";
313 break;
314 case WIFI_PHY_LAYER_PROTOCOL_N:
315 // 802.11n, HT rates.
316 description = "CONNECTION_WIFI_802.11n";
317 break;
318 case WIFI_PHY_LAYER_PROTOCOL_UNKNOWN:
319 // Unclassified mode or failure to identify.
320 break;
323 return description;
326 // If |address| is an IPv4-mapped IPv6 address, returns ADDRESS_FAMILY_IPV4
327 // instead of ADDRESS_FAMILY_IPV6. Othewise, behaves like GetAddressFamily().
328 AddressFamily GetRealAddressFamily(const IPAddressNumber& address) {
329 return IsIPv4Mapped(address) ? ADDRESS_FAMILY_IPV4 :
330 GetAddressFamily(address);
333 } // namespace
335 QuicConnectionLogger::QuicConnectionLogger(QuicSession* session,
336 const BoundNetLog& net_log)
337 : net_log_(net_log),
338 session_(session),
339 last_received_packet_sequence_number_(0),
340 last_received_packet_size_(0),
341 previous_received_packet_size_(0),
342 largest_received_packet_sequence_number_(0),
343 largest_received_missing_packet_sequence_number_(0),
344 num_out_of_order_received_packets_(0),
345 num_out_of_order_large_received_packets_(0),
346 num_packets_received_(0),
347 num_truncated_acks_sent_(0),
348 num_truncated_acks_received_(0),
349 num_frames_received_(0),
350 num_duplicate_frames_received_(0),
351 num_incorrect_connection_ids_(0),
352 num_undecryptable_packets_(0),
353 num_duplicate_packets_(0),
354 num_blocked_frames_received_(0),
355 num_blocked_frames_sent_(0),
356 connection_description_(GetConnectionDescriptionString()) {
359 QuicConnectionLogger::~QuicConnectionLogger() {
360 UMA_HISTOGRAM_COUNTS("Net.QuicSession.OutOfOrderPacketsReceived",
361 num_out_of_order_received_packets_);
362 UMA_HISTOGRAM_COUNTS("Net.QuicSession.OutOfOrderLargePacketsReceived",
363 num_out_of_order_large_received_packets_);
364 UMA_HISTOGRAM_COUNTS("Net.QuicSession.TruncatedAcksSent",
365 num_truncated_acks_sent_);
366 UMA_HISTOGRAM_COUNTS("Net.QuicSession.TruncatedAcksReceived",
367 num_truncated_acks_received_);
368 UMA_HISTOGRAM_COUNTS("Net.QuicSession.IncorrectConnectionIDsReceived",
369 num_incorrect_connection_ids_);
370 UMA_HISTOGRAM_COUNTS("Net.QuicSession.UndecryptablePacketsReceived",
371 num_undecryptable_packets_);
372 UMA_HISTOGRAM_COUNTS("Net.QuicSession.DuplicatePacketsReceived",
373 num_duplicate_packets_);
374 UMA_HISTOGRAM_COUNTS("Net.QuicSession.BlockedFrames.Received",
375 num_blocked_frames_received_);
376 UMA_HISTOGRAM_COUNTS("Net.QuicSession.BlockedFrames.Sent",
377 num_blocked_frames_sent_);
378 UMA_HISTOGRAM_COUNTS("Net.QuicSession.HeadersStream.EarlyFramesReceived",
379 session_->headers_stream()->num_early_frames_received());
381 if (num_frames_received_ > 0) {
382 int duplicate_stream_frame_per_thousand =
383 num_duplicate_frames_received_ * 1000 / num_frames_received_;
384 if (num_packets_received_ < 100) {
385 UMA_HISTOGRAM_CUSTOM_COUNTS(
386 "Net.QuicSession.StreamFrameDuplicatedShortConnection",
387 duplicate_stream_frame_per_thousand, 1, 1000, 75);
388 } else {
389 UMA_HISTOGRAM_CUSTOM_COUNTS(
390 "Net.QuicSession.StreamFrameDuplicatedLongConnection",
391 duplicate_stream_frame_per_thousand, 1, 1000, 75);
396 RecordLossHistograms();
399 void QuicConnectionLogger::OnFrameAddedToPacket(const QuicFrame& frame) {
400 switch (frame.type) {
401 case PADDING_FRAME:
402 break;
403 case STREAM_FRAME:
404 net_log_.AddEvent(
405 NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_SENT,
406 base::Bind(&NetLogQuicStreamFrameCallback, frame.stream_frame));
407 break;
408 case ACK_FRAME: {
409 net_log_.AddEvent(
410 NetLog::TYPE_QUIC_SESSION_ACK_FRAME_SENT,
411 base::Bind(&NetLogQuicAckFrameCallback, frame.ack_frame));
412 const SequenceNumberSet& missing_packets =
413 frame.ack_frame->missing_packets;
414 const uint8 max_ranges = std::numeric_limits<uint8>::max();
415 // Compute an upper bound on the number of NACK ranges. If the bound
416 // is below the max, then it clearly isn't truncated.
417 if (missing_packets.size() < max_ranges ||
418 (*missing_packets.rbegin() - *missing_packets.begin() -
419 missing_packets.size() + 1) < max_ranges) {
420 break;
422 size_t num_ranges = 0;
423 QuicPacketSequenceNumber last_missing = 0;
424 for (SequenceNumberSet::const_iterator it = missing_packets.begin();
425 it != missing_packets.end(); ++it) {
426 if (*it != last_missing + 1 && ++num_ranges >= max_ranges) {
427 ++num_truncated_acks_sent_;
428 break;
430 last_missing = *it;
432 break;
434 case RST_STREAM_FRAME:
435 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.RstStreamErrorCodeClient",
436 frame.rst_stream_frame->error_code);
437 net_log_.AddEvent(
438 NetLog::TYPE_QUIC_SESSION_RST_STREAM_FRAME_SENT,
439 base::Bind(&NetLogQuicRstStreamFrameCallback,
440 frame.rst_stream_frame));
441 break;
442 case CONNECTION_CLOSE_FRAME:
443 net_log_.AddEvent(
444 NetLog::TYPE_QUIC_SESSION_CONNECTION_CLOSE_FRAME_SENT,
445 base::Bind(&NetLogQuicConnectionCloseFrameCallback,
446 frame.connection_close_frame));
447 break;
448 case GOAWAY_FRAME:
449 net_log_.AddEvent(
450 NetLog::TYPE_QUIC_SESSION_GOAWAY_FRAME_SENT,
451 base::Bind(&NetLogQuicGoAwayFrameCallback,
452 frame.goaway_frame));
453 break;
454 case WINDOW_UPDATE_FRAME:
455 net_log_.AddEvent(
456 NetLog::TYPE_QUIC_SESSION_WINDOW_UPDATE_FRAME_SENT,
457 base::Bind(&NetLogQuicWindowUpdateFrameCallback,
458 frame.window_update_frame));
459 break;
460 case BLOCKED_FRAME:
461 ++num_blocked_frames_sent_;
462 net_log_.AddEvent(
463 NetLog::TYPE_QUIC_SESSION_BLOCKED_FRAME_SENT,
464 base::Bind(&NetLogQuicBlockedFrameCallback,
465 frame.blocked_frame));
466 break;
467 case STOP_WAITING_FRAME:
468 net_log_.AddEvent(
469 NetLog::TYPE_QUIC_SESSION_STOP_WAITING_FRAME_SENT,
470 base::Bind(&NetLogQuicStopWaitingFrameCallback,
471 frame.stop_waiting_frame));
472 break;
473 case PING_FRAME:
474 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.ConnectionFlowControlBlocked",
475 session_->IsConnectionFlowControlBlocked());
476 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.StreamFlowControlBlocked",
477 session_->IsStreamFlowControlBlocked());
478 // PingFrame has no contents to log, so just record that it was sent.
479 net_log_.AddEvent(NetLog::TYPE_QUIC_SESSION_PING_FRAME_SENT);
480 break;
481 default:
482 DCHECK(false) << "Illegal frame type: " << frame.type;
486 void QuicConnectionLogger::OnPacketSent(
487 const SerializedPacket& serialized_packet,
488 QuicPacketSequenceNumber original_sequence_number,
489 EncryptionLevel level,
490 TransmissionType transmission_type,
491 const QuicEncryptedPacket& packet,
492 QuicTime sent_time) {
493 if (original_sequence_number == 0) {
494 net_log_.AddEvent(
495 NetLog::TYPE_QUIC_SESSION_PACKET_SENT,
496 base::Bind(&NetLogQuicPacketSentCallback, serialized_packet,
497 level, transmission_type, packet.length(), sent_time));
498 } else {
499 net_log_.AddEvent(
500 NetLog::TYPE_QUIC_SESSION_PACKET_RETRANSMITTED,
501 base::Bind(&NetLogQuicPacketRetransmittedCallback,
502 original_sequence_number,
503 serialized_packet.sequence_number));
507 void QuicConnectionLogger::OnPacketReceived(const IPEndPoint& self_address,
508 const IPEndPoint& peer_address,
509 const QuicEncryptedPacket& packet) {
510 if (local_address_from_self_.GetFamily() == ADDRESS_FAMILY_UNSPECIFIED) {
511 local_address_from_self_ = self_address;
512 UMA_HISTOGRAM_ENUMERATION("Net.QuicSession.ConnectionTypeFromSelf",
513 GetRealAddressFamily(self_address.address()),
514 ADDRESS_FAMILY_LAST);
517 previous_received_packet_size_ = last_received_packet_size_;
518 last_received_packet_size_ = packet.length();
519 net_log_.AddEvent(
520 NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED,
521 base::Bind(&NetLogQuicPacketCallback, &self_address, &peer_address,
522 packet.length()));
525 void QuicConnectionLogger::OnIncorrectConnectionId(
526 QuicConnectionId connection_id) {
527 ++num_incorrect_connection_ids_;
530 void QuicConnectionLogger::OnUndecryptablePacket() {
531 ++num_undecryptable_packets_;
534 void QuicConnectionLogger::OnDuplicatePacket(
535 QuicPacketSequenceNumber sequence_number) {
536 ++num_duplicate_packets_;
539 void QuicConnectionLogger::OnProtocolVersionMismatch(
540 QuicVersion received_version) {
541 // TODO(rtenneti): Add logging.
544 void QuicConnectionLogger::OnPacketHeader(const QuicPacketHeader& header) {
545 net_log_.AddEvent(
546 NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED,
547 base::Bind(&NetLogQuicPacketHeaderCallback, &header));
548 ++num_packets_received_;
549 if (largest_received_packet_sequence_number_ <
550 header.packet_sequence_number) {
551 QuicPacketSequenceNumber delta = header.packet_sequence_number -
552 largest_received_packet_sequence_number_;
553 if (delta > 1) {
554 // There is a gap between the largest packet previously received and
555 // the current packet. This indicates either loss, or out-of-order
556 // delivery.
557 UMA_HISTOGRAM_COUNTS("Net.QuicSession.PacketGapReceived",
558 static_cast<base::HistogramBase::Sample>(delta - 1));
560 largest_received_packet_sequence_number_ = header.packet_sequence_number;
562 if (header.packet_sequence_number < received_packets_.size()) {
563 received_packets_[static_cast<size_t>(header.packet_sequence_number)] =
564 true;
566 if (header.packet_sequence_number < last_received_packet_sequence_number_) {
567 ++num_out_of_order_received_packets_;
568 if (previous_received_packet_size_ < last_received_packet_size_)
569 ++num_out_of_order_large_received_packets_;
570 UMA_HISTOGRAM_COUNTS(
571 "Net.QuicSession.OutOfOrderGapReceived",
572 static_cast<base::HistogramBase::Sample>(
573 last_received_packet_sequence_number_ -
574 header.packet_sequence_number));
576 last_received_packet_sequence_number_ = header.packet_sequence_number;
579 void QuicConnectionLogger::OnStreamFrame(const QuicStreamFrame& frame) {
580 net_log_.AddEvent(
581 NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED,
582 base::Bind(&NetLogQuicStreamFrameCallback, &frame));
585 void QuicConnectionLogger::OnAckFrame(const QuicAckFrame& frame) {
586 net_log_.AddEvent(
587 NetLog::TYPE_QUIC_SESSION_ACK_FRAME_RECEIVED,
588 base::Bind(&NetLogQuicAckFrameCallback, &frame));
590 const size_t kApproximateLargestSoloAckBytes = 100;
591 if (last_received_packet_sequence_number_ < received_acks_.size() &&
592 last_received_packet_size_ < kApproximateLargestSoloAckBytes) {
593 received_acks_[static_cast<size_t>(last_received_packet_sequence_number_)] =
594 true;
597 if (frame.is_truncated)
598 ++num_truncated_acks_received_;
600 if (frame.missing_packets.empty())
601 return;
603 SequenceNumberSet missing_packets = frame.missing_packets;
604 SequenceNumberSet::const_iterator it = missing_packets.lower_bound(
605 largest_received_missing_packet_sequence_number_);
606 if (it == missing_packets.end())
607 return;
609 if (*it == largest_received_missing_packet_sequence_number_) {
610 ++it;
611 if (it == missing_packets.end())
612 return;
614 // Scan through the list and log consecutive ranges of missing packets.
615 size_t num_consecutive_missing_packets = 0;
616 QuicPacketSequenceNumber previous_missing_packet = *it - 1;
617 while (it != missing_packets.end()) {
618 if (previous_missing_packet == *it - 1) {
619 ++num_consecutive_missing_packets;
620 } else {
621 DCHECK_NE(0u, num_consecutive_missing_packets);
622 UpdatePacketGapSentHistogram(num_consecutive_missing_packets);
623 // Make sure this packet it included in the count.
624 num_consecutive_missing_packets = 1;
626 previous_missing_packet = *it;
627 ++it;
629 if (num_consecutive_missing_packets != 0) {
630 UpdatePacketGapSentHistogram(num_consecutive_missing_packets);
632 largest_received_missing_packet_sequence_number_ =
633 *missing_packets.rbegin();
636 void QuicConnectionLogger::OnStopWaitingFrame(
637 const QuicStopWaitingFrame& frame) {
638 net_log_.AddEvent(
639 NetLog::TYPE_QUIC_SESSION_STOP_WAITING_FRAME_RECEIVED,
640 base::Bind(&NetLogQuicStopWaitingFrameCallback, &frame));
643 void QuicConnectionLogger::OnRstStreamFrame(const QuicRstStreamFrame& frame) {
644 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.RstStreamErrorCodeServer",
645 frame.error_code);
646 net_log_.AddEvent(
647 NetLog::TYPE_QUIC_SESSION_RST_STREAM_FRAME_RECEIVED,
648 base::Bind(&NetLogQuicRstStreamFrameCallback, &frame));
651 void QuicConnectionLogger::OnConnectionCloseFrame(
652 const QuicConnectionCloseFrame& frame) {
653 net_log_.AddEvent(
654 NetLog::TYPE_QUIC_SESSION_CONNECTION_CLOSE_FRAME_RECEIVED,
655 base::Bind(&NetLogQuicConnectionCloseFrameCallback, &frame));
658 void QuicConnectionLogger::OnWindowUpdateFrame(
659 const QuicWindowUpdateFrame& frame) {
660 net_log_.AddEvent(
661 NetLog::TYPE_QUIC_SESSION_WINDOW_UPDATE_FRAME_RECEIVED,
662 base::Bind(&NetLogQuicWindowUpdateFrameCallback, &frame));
665 void QuicConnectionLogger::OnBlockedFrame(const QuicBlockedFrame& frame) {
666 ++num_blocked_frames_received_;
667 net_log_.AddEvent(
668 NetLog::TYPE_QUIC_SESSION_BLOCKED_FRAME_RECEIVED,
669 base::Bind(&NetLogQuicBlockedFrameCallback, &frame));
672 void QuicConnectionLogger::OnGoAwayFrame(const QuicGoAwayFrame& frame) {
673 net_log_.AddEvent(
674 NetLog::TYPE_QUIC_SESSION_GOAWAY_FRAME_RECEIVED,
675 base::Bind(&NetLogQuicGoAwayFrameCallback, &frame));
678 void QuicConnectionLogger::OnPingFrame(const QuicPingFrame& frame) {
679 // PingFrame has no contents to log, so just record that it was received.
680 net_log_.AddEvent(NetLog::TYPE_QUIC_SESSION_PING_FRAME_RECEIVED);
683 void QuicConnectionLogger::OnPublicResetPacket(
684 const QuicPublicResetPacket& packet) {
685 net_log_.AddEvent(NetLog::TYPE_QUIC_SESSION_PUBLIC_RESET_PACKET_RECEIVED);
686 UpdatePublicResetAddressMismatchHistogram(local_address_from_shlo_,
687 packet.client_address);
690 void QuicConnectionLogger::OnVersionNegotiationPacket(
691 const QuicVersionNegotiationPacket& packet) {
692 net_log_.AddEvent(
693 NetLog::TYPE_QUIC_SESSION_VERSION_NEGOTIATION_PACKET_RECEIVED,
694 base::Bind(&NetLogQuicVersionNegotiationPacketCallback, &packet));
697 void QuicConnectionLogger::OnRevivedPacket(
698 const QuicPacketHeader& revived_header,
699 base::StringPiece payload) {
700 net_log_.AddEvent(
701 NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_REVIVED,
702 base::Bind(&NetLogQuicPacketHeaderCallback, &revived_header));
705 void QuicConnectionLogger::OnCryptoHandshakeMessageReceived(
706 const CryptoHandshakeMessage& message) {
707 net_log_.AddEvent(
708 NetLog::TYPE_QUIC_SESSION_CRYPTO_HANDSHAKE_MESSAGE_RECEIVED,
709 base::Bind(&NetLogQuicCryptoHandshakeMessageCallback, &message));
711 if (message.tag() == kSHLO) {
712 StringPiece address;
713 QuicSocketAddressCoder decoder;
714 if (message.GetStringPiece(kCADR, &address) &&
715 decoder.Decode(address.data(), address.size())) {
716 local_address_from_shlo_ = IPEndPoint(decoder.ip(), decoder.port());
717 UMA_HISTOGRAM_ENUMERATION("Net.QuicSession.ConnectionTypeFromPeer",
718 GetRealAddressFamily(
719 local_address_from_shlo_.address()),
720 ADDRESS_FAMILY_LAST);
725 void QuicConnectionLogger::OnCryptoHandshakeMessageSent(
726 const CryptoHandshakeMessage& message) {
727 net_log_.AddEvent(
728 NetLog::TYPE_QUIC_SESSION_CRYPTO_HANDSHAKE_MESSAGE_SENT,
729 base::Bind(&NetLogQuicCryptoHandshakeMessageCallback, &message));
732 void QuicConnectionLogger::OnConnectionClosed(QuicErrorCode error,
733 bool from_peer) {
734 net_log_.AddEvent(
735 NetLog::TYPE_QUIC_SESSION_CLOSED,
736 base::Bind(&NetLogQuicOnConnectionClosedCallback, error, from_peer));
739 void QuicConnectionLogger::OnSuccessfulVersionNegotiation(
740 const QuicVersion& version) {
741 string quic_version = QuicVersionToString(version);
742 net_log_.AddEvent(NetLog::TYPE_QUIC_SESSION_VERSION_NEGOTIATED,
743 NetLog::StringCallback("version", &quic_version));
746 void QuicConnectionLogger::UpdateReceivedFrameCounts(
747 QuicStreamId stream_id,
748 int num_frames_received,
749 int num_duplicate_frames_received) {
750 if (stream_id != kCryptoStreamId) {
751 num_frames_received_ += num_frames_received;
752 num_duplicate_frames_received_ += num_duplicate_frames_received;
756 void QuicConnectionLogger::OnCertificateVerified(
757 const CertVerifyResult& result) {
758 net_log_.AddEvent(
759 NetLog::TYPE_QUIC_SESSION_CERTIFICATE_VERIFIED,
760 base::Bind(&NetLogQuicCertificateVerifiedCallback, result.verified_cert));
763 base::HistogramBase* QuicConnectionLogger::GetPacketSequenceNumberHistogram(
764 const char* statistic_name) const {
765 string prefix("Net.QuicSession.PacketReceived_");
766 return base::LinearHistogram::FactoryGet(
767 prefix + statistic_name + connection_description_,
768 1, received_packets_.size(), received_packets_.size() + 1,
769 base::HistogramBase::kUmaTargetedHistogramFlag);
772 base::HistogramBase* QuicConnectionLogger::Get6PacketHistogram(
773 const char* which_6) const {
774 // This histogram takes a binary encoding of the 6 consecutive packets
775 // received. As a result, there are 64 possible sample-patterns.
776 string prefix("Net.QuicSession.6PacketsPatternsReceived_");
777 return base::LinearHistogram::FactoryGet(
778 prefix + which_6 + connection_description_, 1, 64, 65,
779 base::HistogramBase::kUmaTargetedHistogramFlag);
782 base::HistogramBase* QuicConnectionLogger::Get21CumulativeHistogram(
783 const char* which_21) const {
784 // This histogram contains, for each sequence of 21 packets, the results from
785 // 21 distinct questions about that sequence. Conceptually the histogtram is
786 // broken into 21 distinct ranges, and one sample is added into each of those
787 // ranges whenever we process a set of 21 packets.
788 // There is a little rendundancy, as each "range" must have the same number
789 // of samples, all told, but the histogram is a tad easier to read this way.
790 // The questions are:
791 // Was the first packet present (bucket 0==>no; bucket 1==>yes)
792 // Of the first two packets, how many were present? (bucket 2==> none;
793 // bucket 3==> 1 of 2; bucket 4==> 2 of 2)
794 // Of the first three packets, how many were present? (bucket 5==>none;
795 // bucket 6==> 1 of 3; bucket 7==> 2 of 3; bucket 8==> 3 of 3).
796 // etc.
797 string prefix("Net.QuicSession.21CumulativePacketsReceived_");
798 return base::LinearHistogram::FactoryGet(
799 prefix + which_21 + connection_description_,
800 1, kBoundingSampleInCumulativeHistogram,
801 kBoundingSampleInCumulativeHistogram + 1,
802 base::HistogramBase::kUmaTargetedHistogramFlag);
805 // static
806 void QuicConnectionLogger::AddTo21CumulativeHistogram(
807 base::HistogramBase* histogram,
808 int bit_mask_of_packets,
809 int valid_bits_in_mask) {
810 DCHECK_LE(valid_bits_in_mask, 21);
811 DCHECK_LT(bit_mask_of_packets, 1 << 21);
812 const int blank_bits_in_mask = 21 - valid_bits_in_mask;
813 DCHECK_EQ(bit_mask_of_packets & ((1 << blank_bits_in_mask) - 1), 0);
814 bit_mask_of_packets >>= blank_bits_in_mask;
815 int bits_so_far = 0;
816 int range_start = 0;
817 for (int i = 1; i <= valid_bits_in_mask; ++i) {
818 bits_so_far += bit_mask_of_packets & 1;
819 bit_mask_of_packets >>= 1;
820 DCHECK_LT(range_start + bits_so_far, kBoundingSampleInCumulativeHistogram);
821 histogram->Add(range_start + bits_so_far);
822 range_start += i + 1;
826 void QuicConnectionLogger::RecordAggregatePacketLossRate() const {
827 // For short connections under 22 packets in length, we'll rely on the
828 // Net.QuicSession.21CumulativePacketsReceived_* histogram to indicate packet
829 // loss rates. This way we avoid tremendously anomalous contributions to our
830 // histogram. (e.g., if we only got 5 packets, but lost 1, we'd otherwise
831 // record a 20% loss in this histogram!). We may still get some strange data
832 // (1 loss in 22 is still high :-/).
833 if (largest_received_packet_sequence_number_ <= 21)
834 return;
836 QuicPacketSequenceNumber divisor = largest_received_packet_sequence_number_;
837 QuicPacketSequenceNumber numerator = divisor - num_packets_received_;
838 if (divisor < 100000)
839 numerator *= 1000;
840 else
841 divisor /= 1000;
842 string prefix("Net.QuicSession.PacketLossRate_");
843 base::HistogramBase* histogram = base::Histogram::FactoryGet(
844 prefix + connection_description_, 1, 1000, 75,
845 base::HistogramBase::kUmaTargetedHistogramFlag);
846 histogram->Add(static_cast<base::HistogramBase::Sample>(numerator / divisor));
849 void QuicConnectionLogger::RecordLossHistograms() const {
850 if (largest_received_packet_sequence_number_ == 0)
851 return; // Connection was never used.
852 RecordAggregatePacketLossRate();
854 base::HistogramBase* is_not_ack_histogram =
855 GetPacketSequenceNumberHistogram("IsNotAck_");
856 base::HistogramBase* is_an_ack_histogram =
857 GetPacketSequenceNumberHistogram("IsAnAck_");
858 base::HistogramBase* packet_arrived_histogram =
859 GetPacketSequenceNumberHistogram("Ack_");
860 base::HistogramBase* packet_missing_histogram =
861 GetPacketSequenceNumberHistogram("Nack_");
862 base::HistogramBase* ongoing_cumulative_packet_histogram =
863 Get21CumulativeHistogram("Some21s_");
864 base::HistogramBase* first_cumulative_packet_histogram =
865 Get21CumulativeHistogram("First21_");
866 base::HistogramBase* six_packet_histogram = Get6PacketHistogram("Some6s_");
868 DCHECK_EQ(received_packets_.size(), received_acks_.size());
869 const QuicPacketSequenceNumber last_index =
870 std::min<QuicPacketSequenceNumber>(received_packets_.size() - 1,
871 largest_received_packet_sequence_number_);
872 const QuicPacketSequenceNumber index_of_first_21_contribution =
873 std::min<QuicPacketSequenceNumber>(21, last_index);
874 // Bit pattern of consecutively received packets that is maintained as we scan
875 // through the received_packets_ vector. Less significant bits correspond to
876 // less recent packets, and only the low order 21 bits are ever defined.
877 // Bit is 1 iff corresponding packet was received.
878 int packet_pattern_21 = 0;
879 // Zero is an invalid packet sequence number.
880 DCHECK(!received_packets_[0]);
881 for (size_t i = 1; i <= last_index; ++i) {
882 if (received_acks_[i])
883 is_an_ack_histogram->Add(i);
884 else
885 is_not_ack_histogram->Add(i);
887 packet_pattern_21 >>= 1;
888 if (received_packets_[i]) {
889 packet_arrived_histogram->Add(i);
890 packet_pattern_21 |= (1 << 20); // Turn on the 21st bit.
891 } else {
892 packet_missing_histogram->Add(i);
895 if (i == index_of_first_21_contribution) {
896 AddTo21CumulativeHistogram(first_cumulative_packet_histogram,
897 packet_pattern_21, i);
899 // We'll just record for non-overlapping ranges, to reduce histogramming
900 // cost for now. Each call does 21 separate histogram additions.
901 if (i > 21 || i % 21 == 0) {
902 AddTo21CumulativeHistogram(ongoing_cumulative_packet_histogram,
903 packet_pattern_21, 21);
906 if (i < 6)
907 continue; // Not enough packets to do any pattern recording.
908 int recent_6_mask = packet_pattern_21 >> 15;
909 DCHECK_LT(recent_6_mask, 64);
910 if (i == 6) {
911 Get6PacketHistogram("First6_")->Add(recent_6_mask);
912 continue;
914 // Record some overlapping patterns, to get a better picture, since this is
915 // not very expensive.
916 if (i % 3 == 0)
917 six_packet_histogram->Add(recent_6_mask);
921 } // namespace net