Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: net/http/http_cache_transaction.cc

Issue 10808047: Add HttpCache histograms focussed on blocking and transaction type. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rename parameter Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/http/http_cache_transaction.h ('k') | net/http/mock_http_cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/http/http_cache_transaction.h" 5 #include "net/http/http_cache_transaction.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_POSIX) 9 #if defined(OS_POSIX)
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 21 matching lines...) Expand all
32 #include "net/http/http_request_info.h" 32 #include "net/http/http_request_info.h"
33 #include "net/http/http_response_headers.h" 33 #include "net/http/http_response_headers.h"
34 #include "net/http/http_transaction.h" 34 #include "net/http/http_transaction.h"
35 #include "net/http/http_util.h" 35 #include "net/http/http_util.h"
36 #include "net/http/partial_data.h" 36 #include "net/http/partial_data.h"
37 37
38 using base::Time; 38 using base::Time;
39 using base::TimeDelta; 39 using base::TimeDelta;
40 using base::TimeTicks; 40 using base::TimeTicks;
41 41
42 namespace {
43
44 // The cutoff for tagging small transactions in histograms; this size was chosen
45 // to cover resources likely to be received in a single TCP window. With an
46 // initial CWND of 10, and an MTU of 1500 bytes, with TCP and HTTP framing
47 // overhead this is a size relatively likely to take only one RTT.
48 const int kSmallResourceMaxBytes = 14 * 1024;
49
50 } // namespace
51
42 namespace net { 52 namespace net {
43 53
44 struct HeaderNameAndValue { 54 struct HeaderNameAndValue {
45 const char* name; 55 const char* name;
46 const char* value; 56 const char* value;
47 }; 57 };
48 58
49 // If the request includes one of these request headers, then avoid caching 59 // If the request includes one of these request headers, then avoid caching
50 // to avoid getting confused. 60 // to avoid getting confused.
51 static const HeaderNameAndValue kPassThroughHeaders[] = { 61 static const HeaderNameAndValue kPassThroughHeaders[] = {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 handling_206_(false), 129 handling_206_(false),
120 cache_pending_(false), 130 cache_pending_(false),
121 done_reading_(false), 131 done_reading_(false),
122 read_offset_(0), 132 read_offset_(0),
123 effective_load_flags_(0), 133 effective_load_flags_(0),
124 write_len_(0), 134 write_len_(0),
125 final_upload_progress_(0), 135 final_upload_progress_(0),
126 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 136 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
127 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( 137 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_(
128 base::Bind(&Transaction::OnIOComplete, 138 base::Bind(&Transaction::OnIOComplete,
129 weak_factory_.GetWeakPtr()))) { 139 weak_factory_.GetWeakPtr()))),
140 transaction_pattern_(PATTERN_UNDEFINED),
141 bytes_read_from_cache_(0),
142 bytes_read_from_network_(0) {
130 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders == 143 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders ==
131 arraysize(kValidationHeaders), 144 arraysize(kValidationHeaders),
132 Invalid_number_of_validation_headers); 145 Invalid_number_of_validation_headers);
133 } 146 }
134 147
135 HttpCache::Transaction::~Transaction() { 148 HttpCache::Transaction::~Transaction() {
136 // We may have to issue another IO, but we should never invoke the callback_ 149 // We may have to issue another IO, but we should never invoke the callback_
137 // after this point. 150 // after this point.
138 callback_.Reset(); 151 callback_.Reset();
139 152
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 result); 676 result);
664 cache_pending_ = false; 677 cache_pending_ = false;
665 678
666 if (!ShouldPassThrough()) { 679 if (!ShouldPassThrough()) {
667 cache_key_ = cache_->GenerateCacheKey(request_); 680 cache_key_ = cache_->GenerateCacheKey(request_);
668 681
669 // Requested cache access mode. 682 // Requested cache access mode.
670 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { 683 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
671 mode_ = READ; 684 mode_ = READ;
672 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) { 685 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
686 UpdateTransactionPattern(PATTERN_NOT_COVERED);
673 mode_ = WRITE; 687 mode_ = WRITE;
674 } else { 688 } else {
675 mode_ = READ_WRITE; 689 mode_ = READ_WRITE;
676 } 690 }
677 691
678 // Downgrade to UPDATE if the request has been externally conditionalized. 692 // Downgrade to UPDATE if the request has been externally conditionalized.
679 if (external_validation_.initialized) { 693 if (external_validation_.initialized) {
680 if (mode_ & WRITE) { 694 if (mode_ & WRITE) {
681 // Strip off the READ_DATA bit (and maybe add back a READ_META bit 695 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
682 // in case READ was off). 696 // in case READ was off).
(...skipping 28 matching lines...) Expand all
711 // This is only set if we have something to do with the response. 725 // This is only set if we have something to do with the response.
712 range_requested_ = (partial_.get() != NULL); 726 range_requested_ = (partial_.get() != NULL);
713 727
714 return OK; 728 return OK;
715 } 729 }
716 730
717 int HttpCache::Transaction::DoSendRequest() { 731 int HttpCache::Transaction::DoSendRequest() {
718 DCHECK(mode_ & WRITE || mode_ == NONE); 732 DCHECK(mode_ & WRITE || mode_ == NONE);
719 DCHECK(!network_trans_.get()); 733 DCHECK(!network_trans_.get());
720 734
735 send_request_since_ = TimeTicks::Now();
736
721 // Create a network transaction. 737 // Create a network transaction.
722 int rv = cache_->network_layer_->CreateTransaction(&network_trans_); 738 int rv = cache_->network_layer_->CreateTransaction(&network_trans_);
723 if (rv != OK) 739 if (rv != OK)
724 return rv; 740 return rv;
725 741
726 next_state_ = STATE_SEND_REQUEST_COMPLETE; 742 next_state_ = STATE_SEND_REQUEST_COMPLETE;
727 rv = network_trans_->Start(request_, io_callback_, net_log_); 743 rv = network_trans_->Start(request_, io_callback_, net_log_);
728 return rv; 744 return rv;
729 } 745 }
730 746
731 int HttpCache::Transaction::DoSendRequestComplete(int result) { 747 int HttpCache::Transaction::DoSendRequestComplete(int result) {
732 if (!cache_) 748 if (!cache_)
733 return ERR_UNEXPECTED; 749 return ERR_UNEXPECTED;
734 750
735 if (result == OK) { 751 if (result == OK) {
736 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST; 752 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
737 return OK; 753 return OK;
738 } 754 }
739 755
756 // Do not record requests that have network errors or restarts.
757 UpdateTransactionPattern(PATTERN_NOT_COVERED);
740 if (IsCertificateError(result)) { 758 if (IsCertificateError(result)) {
741 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); 759 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
742 // If we get a certificate error, then there is a certificate in ssl_info, 760 // If we get a certificate error, then there is a certificate in ssl_info,
743 // so GetResponseInfo() should never return NULL here. 761 // so GetResponseInfo() should never return NULL here.
744 DCHECK(response); 762 DCHECK(response);
745 response_.ssl_info = response->ssl_info; 763 response_.ssl_info = response->ssl_info;
746 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { 764 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
747 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); 765 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
748 DCHECK(response); 766 DCHECK(response);
749 response_.cert_request_info = response->cert_request_info; 767 response_.cert_request_info = response->cert_request_info;
(...skipping 10 matching lines...) Expand all
760 auth_response_ = *new_response; 778 auth_response_ = *new_response;
761 return OK; 779 return OK;
762 } 780 }
763 781
764 new_response_ = new_response; 782 new_response_ = new_response;
765 if (!ValidatePartialResponse() && !auth_response_.headers) { 783 if (!ValidatePartialResponse() && !auth_response_.headers) {
766 // Something went wrong with this request and we have to restart it. 784 // Something went wrong with this request and we have to restart it.
767 // If we have an authentication response, we are exposed to weird things 785 // If we have an authentication response, we are exposed to weird things
768 // hapenning if the user cancels the authentication before we receive 786 // hapenning if the user cancels the authentication before we receive
769 // the new response. 787 // the new response.
788 UpdateTransactionPattern(PATTERN_NOT_COVERED);
770 response_ = HttpResponseInfo(); 789 response_ = HttpResponseInfo();
771 network_trans_.reset(); 790 network_trans_.reset();
772 new_response_ = NULL; 791 new_response_ = NULL;
773 next_state_ = STATE_SEND_REQUEST; 792 next_state_ = STATE_SEND_REQUEST;
774 return OK; 793 return OK;
775 } 794 }
795 bytes_read_from_network_ += new_response_->headers->raw_headers().size();
776 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) { 796 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
777 // We have stored the full entry, but it changed and the server is 797 // We have stored the full entry, but it changed and the server is
778 // sending a range. We have to delete the old entry. 798 // sending a range. We have to delete the old entry.
779 DoneWritingToEntry(false); 799 DoneWritingToEntry(false);
780 } 800 }
781
782 if (new_response_->headers->response_code() == 416) { 801 if (new_response_->headers->response_code() == 416) {
783 DCHECK_EQ(NONE, mode_); 802 DCHECK_EQ(NONE, mode_);
784 response_ = *new_response_; 803 response_ = *new_response_;
785 return OK; 804 return OK;
786 } 805 }
787 806
788 if (mode_ == WRITE && 807 if (mode_ == WRITE &&
789 (request_->method == "PUT" || request_->method == "DELETE")) { 808 (request_->method == "PUT" || request_->method == "DELETE")) {
790 if (new_response->headers->response_code() == 200) { 809 if (new_response->headers->response_code() == 200) {
791 int ret = cache_->DoomEntry(cache_key_, NULL); 810 int ret = cache_->DoomEntry(cache_key_, NULL);
792 DCHECK_EQ(OK, ret); 811 DCHECK_EQ(OK, ret);
793 } 812 }
794 mode_ = NONE; 813 mode_ = NONE;
795 } 814 }
796 815
797 // Are we expecting a response to a conditional query? 816 // Are we expecting a response to a conditional query?
798 if (mode_ == READ_WRITE || mode_ == UPDATE) { 817 if (mode_ == READ_WRITE || mode_ == UPDATE) {
799 if (new_response->headers->response_code() == 304 || handling_206_) { 818 if (new_response->headers->response_code() == 304 || handling_206_) {
819 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
800 next_state_ = STATE_UPDATE_CACHED_RESPONSE; 820 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
801 return OK; 821 return OK;
802 } 822 }
823 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
803 mode_ = WRITE; 824 mode_ = WRITE;
804 } 825 }
805 826
806 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE; 827 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
807 return OK; 828 return OK;
808 } 829 }
809 830
810 int HttpCache::Transaction::DoNetworkRead() { 831 int HttpCache::Transaction::DoNetworkRead() {
811 next_state_ = STATE_NETWORK_READ_COMPLETE; 832 next_state_ = STATE_NETWORK_READ_COMPLETE;
812 return network_trans_->Read(read_buf_, io_buf_len_, io_callback_); 833 return network_trans_->Read(read_buf_, io_buf_len_, io_callback_);
813 } 834 }
814 835
815 int HttpCache::Transaction::DoNetworkReadComplete(int result) { 836 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
816 DCHECK(mode_ & WRITE || mode_ == NONE); 837 DCHECK(mode_ & WRITE || mode_ == NONE);
817 838
818 if (!cache_) 839 if (!cache_)
819 return ERR_UNEXPECTED; 840 return ERR_UNEXPECTED;
820 841
842 if (result > 0)
843 bytes_read_from_network_ += result;
844
821 // If there is an error or we aren't saving the data, we are done; just wait 845 // If there is an error or we aren't saving the data, we are done; just wait
822 // until the destructor runs to see if we can keep the data. 846 // until the destructor runs to see if we can keep the data.
823 if (mode_ == NONE || result < 0) 847 if (mode_ == NONE || result < 0)
824 return result; 848 return result;
825 849
826 next_state_ = STATE_CACHE_WRITE_DATA; 850 next_state_ = STATE_CACHE_WRITE_DATA;
827 return result; 851 return result;
828 } 852 }
829 853
830 int HttpCache::Transaction::DoInitEntry() { 854 int HttpCache::Transaction::DoInitEntry() {
831 DCHECK(!new_entry_); 855 DCHECK(!new_entry_);
832 856
833 if (!cache_) 857 if (!cache_)
834 return ERR_UNEXPECTED; 858 return ERR_UNEXPECTED;
835 859
836 if (mode_ == WRITE) { 860 if (mode_ == WRITE) {
837 next_state_ = STATE_DOOM_ENTRY; 861 next_state_ = STATE_DOOM_ENTRY;
838 return OK; 862 return OK;
839 } 863 }
840 864
841 next_state_ = STATE_OPEN_ENTRY; 865 next_state_ = STATE_OPEN_ENTRY;
842 return OK; 866 return OK;
843 } 867 }
844 868
845 int HttpCache::Transaction::DoOpenEntry() { 869 int HttpCache::Transaction::DoOpenEntry() {
846 DCHECK(!new_entry_); 870 DCHECK(!new_entry_);
847 next_state_ = STATE_OPEN_ENTRY_COMPLETE; 871 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
848 cache_pending_ = true; 872 cache_pending_ = true;
849 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY); 873 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
874 open_entry_since_ = TimeTicks::Now();
850 return cache_->OpenEntry(cache_key_, &new_entry_, this); 875 return cache_->OpenEntry(cache_key_, &new_entry_, this);
851 } 876 }
852 877
853 int HttpCache::Transaction::DoOpenEntryComplete(int result) { 878 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
854 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is 879 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
855 // OK, otherwise the cache will end up with an active entry without any 880 // OK, otherwise the cache will end up with an active entry without any
856 // transaction attached. 881 // transaction attached.
857 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result); 882 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
858 cache_pending_ = false; 883 cache_pending_ = false;
859 if (result == OK) { 884 if (result == OK) {
860 next_state_ = STATE_ADD_TO_ENTRY; 885 next_state_ = STATE_ADD_TO_ENTRY;
861 return OK; 886 return OK;
862 } 887 }
863 888
864 if (result == ERR_CACHE_RACE) { 889 if (result == ERR_CACHE_RACE) {
865 next_state_ = STATE_INIT_ENTRY; 890 next_state_ = STATE_INIT_ENTRY;
866 return OK; 891 return OK;
867 } 892 }
868 893
894 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
869 if (request_->method == "PUT" || request_->method == "DELETE") { 895 if (request_->method == "PUT" || request_->method == "DELETE") {
870 DCHECK(mode_ == READ_WRITE || mode_ == WRITE); 896 DCHECK(mode_ == READ_WRITE || mode_ == WRITE);
871 mode_ = NONE; 897 mode_ = NONE;
872 next_state_ = STATE_SEND_REQUEST; 898 next_state_ = STATE_SEND_REQUEST;
873 return OK; 899 return OK;
874 } 900 }
875 901
876 if (mode_ == READ_WRITE) { 902 if (mode_ == READ_WRITE) {
877 mode_ = WRITE; 903 mode_ = WRITE;
878 next_state_ = STATE_CREATE_ENTRY; 904 next_state_ = STATE_CREATE_ENTRY;
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
1211 io_buf_len_, io_callback_); 1237 io_buf_len_, io_callback_);
1212 } 1238 }
1213 1239
1214 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { 1240 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1215 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); 1241 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1216 if (result != io_buf_len_ || 1242 if (result != io_buf_len_ ||
1217 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, 1243 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1218 &response_, &truncated_)) { 1244 &response_, &truncated_)) {
1219 return OnCacheReadError(result, true); 1245 return OnCacheReadError(result, true);
1220 } 1246 }
1247 bytes_read_from_cache_ += result;
1221 1248
1222 // Some resources may have slipped in as truncated when they're not. 1249 // Some resources may have slipped in as truncated when they're not.
1223 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); 1250 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1224 if (response_.headers->GetContentLength() == current_size) 1251 if (response_.headers->GetContentLength() == current_size)
1225 truncated_ = false; 1252 truncated_ = false;
1226 1253
1227 // We now have access to the cache entry. 1254 // We now have access to the cache entry.
1228 // 1255 //
1229 // o if we are a reader for the transaction, then we can start reading the 1256 // o if we are a reader for the transaction, then we can start reading the
1230 // cache entry. 1257 // cache entry.
1231 // 1258 //
1232 // o if we can read or write, then we should check if the cache entry needs 1259 // o if we can read or write, then we should check if the cache entry needs
1233 // to be validated and then issue a network request if needed or just read 1260 // to be validated and then issue a network request if needed or just read
1234 // from the cache if the cache entry is already valid. 1261 // from the cache if the cache entry is already valid.
1235 // 1262 //
1236 // o if we are set to UPDATE, then we are handling an externally 1263 // o if we are set to UPDATE, then we are handling an externally
1237 // conditionalized request (if-modified-since / if-none-match). We check 1264 // conditionalized request (if-modified-since / if-none-match). We check
1238 // if the request headers define a validation request. 1265 // if the request headers define a validation request.
1239 // 1266 //
1240 switch (mode_) { 1267 switch (mode_) {
1241 case READ: 1268 case READ:
1269 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1242 result = BeginCacheRead(); 1270 result = BeginCacheRead();
1243 break; 1271 break;
1244 case READ_WRITE: 1272 case READ_WRITE:
1245 result = BeginPartialCacheValidation(); 1273 result = BeginPartialCacheValidation();
1246 break; 1274 break;
1247 case UPDATE: 1275 case UPDATE:
1248 result = BeginExternallyConditionalizedRequest(); 1276 result = BeginExternallyConditionalizedRequest();
1249 break; 1277 break;
1250 case WRITE: 1278 case WRITE:
1251 default: 1279 default:
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); 1324 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1297 return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata, 1325 return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata,
1298 response_.metadata->size(), 1326 response_.metadata->size(),
1299 io_callback_); 1327 io_callback_);
1300 } 1328 }
1301 1329
1302 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { 1330 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1303 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); 1331 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1304 if (result != response_.metadata->size()) 1332 if (result != response_.metadata->size())
1305 return OnCacheReadError(result, false); 1333 return OnCacheReadError(result, false);
1306
1307 return OK; 1334 return OK;
1308 } 1335 }
1309 1336
1310 int HttpCache::Transaction::DoCacheQueryData() { 1337 int HttpCache::Transaction::DoCacheQueryData() {
1311 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE; 1338 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1312 1339
1313 // Balanced in DoCacheQueryDataComplete. 1340 // Balanced in DoCacheQueryDataComplete.
1314 return entry_->disk_entry->ReadyForSparseIO(io_callback_); 1341 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1315 } 1342 }
1316 1343
(...skipping 22 matching lines...) Expand all
1339 1366
1340 int HttpCache::Transaction::DoCacheReadDataComplete(int result) { 1367 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1341 if (net_log_.IsLoggingAllEvents()) { 1368 if (net_log_.IsLoggingAllEvents()) {
1342 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA, 1369 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1343 result); 1370 result);
1344 } 1371 }
1345 1372
1346 if (!cache_) 1373 if (!cache_)
1347 return ERR_UNEXPECTED; 1374 return ERR_UNEXPECTED;
1348 1375
1349 if (partial_.get()) 1376 if (partial_.get()) {
1377 // Partial requests are confusing to report in histograms because they may
1378 // have multiple underlying requests.
1379 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1350 return DoPartialCacheReadCompleted(result); 1380 return DoPartialCacheReadCompleted(result);
1381 }
1351 1382
1352 if (result > 0) { 1383 if (result > 0) {
1353 read_offset_ += result; 1384 read_offset_ += result;
1385 bytes_read_from_cache_ += result;
1354 } else if (result == 0) { // End of file. 1386 } else if (result == 0) { // End of file.
1387 RecordHistograms();
1355 cache_->DoneReadingFromEntry(entry_, this); 1388 cache_->DoneReadingFromEntry(entry_, this);
1356 entry_ = NULL; 1389 entry_ = NULL;
1357 } else { 1390 } else {
1358 return OnCacheReadError(result, false); 1391 return OnCacheReadError(result, false);
1359 } 1392 }
1360 return result; 1393 return result;
1361 } 1394 }
1362 1395
1363 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) { 1396 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1364 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE; 1397 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
1492 1525
1493 // If there is more than one validation header, we can't treat this request as 1526 // If there is more than one validation header, we can't treat this request as
1494 // a cache validation, since we don't know for sure which header the server 1527 // a cache validation, since we don't know for sure which header the server
1495 // will give us a response for (and they could be contradictory). 1528 // will give us a response for (and they could be contradictory).
1496 if (external_validation_error) { 1529 if (external_validation_error) {
1497 LOG(WARNING) << "Multiple or malformed validation headers found."; 1530 LOG(WARNING) << "Multiple or malformed validation headers found.";
1498 effective_load_flags_ |= LOAD_DISABLE_CACHE; 1531 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1499 } 1532 }
1500 1533
1501 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) { 1534 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
1535 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1502 partial_.reset(new PartialData); 1536 partial_.reset(new PartialData);
1503 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) { 1537 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
1504 // We will be modifying the actual range requested to the server, so 1538 // We will be modifying the actual range requested to the server, so
1505 // let's remove the header here. 1539 // let's remove the header here.
1506 custom_request_.reset(new HttpRequestInfo(*request_)); 1540 custom_request_.reset(new HttpRequestInfo(*request_));
1507 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange); 1541 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
1508 request_ = custom_request_.get(); 1542 request_ = custom_request_.get();
1509 partial_->SetHeaders(custom_request_->extra_headers); 1543 partial_->SetHeaders(custom_request_->extra_headers);
1510 } else { 1544 } else {
1511 // The range is invalid or we cannot handle it properly. 1545 // The range is invalid or we cannot handle it properly.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1563 1597
1564 return OK; 1598 return OK;
1565 } 1599 }
1566 1600
1567 int HttpCache::Transaction::BeginCacheValidation() { 1601 int HttpCache::Transaction::BeginCacheValidation() {
1568 DCHECK(mode_ == READ_WRITE); 1602 DCHECK(mode_ == READ_WRITE);
1569 1603
1570 bool skip_validation = effective_load_flags_ & LOAD_PREFERRING_CACHE || 1604 bool skip_validation = effective_load_flags_ & LOAD_PREFERRING_CACHE ||
1571 !RequiresValidation(); 1605 !RequiresValidation();
1572 1606
1573 if (truncated_) 1607 if (truncated_) {
1608 // Truncated entries can cause partial gets, so we shouldn't record this
1609 // load in histograms.
1610 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1574 skip_validation = !partial_->initial_validation(); 1611 skip_validation = !partial_->initial_validation();
1612 }
1575 1613
1576 if ((partial_.get() && !partial_->IsCurrentRangeCached()) || invalid_range_) 1614 if ((partial_.get() && !partial_->IsCurrentRangeCached()) || invalid_range_)
1577 skip_validation = false; 1615 skip_validation = false;
1578 1616
1579 if (skip_validation) { 1617 if (skip_validation) {
1618 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1580 if (partial_.get()) { 1619 if (partial_.get()) {
1581 // We are going to return the saved response headers to the caller, so 1620 // We are going to return the saved response headers to the caller, so
1582 // we may need to adjust them first. 1621 // we may need to adjust them first.
1583 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; 1622 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1584 return OK; 1623 return OK;
1585 } 1624 }
1586 cache_->ConvertWriterToReader(entry_); 1625 cache_->ConvertWriterToReader(entry_);
1587 mode_ = READ; 1626 mode_ = READ;
1588 1627
1589 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 1628 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
1590 next_state_ = STATE_CACHE_READ_METADATA; 1629 next_state_ = STATE_CACHE_READ_METADATA;
1591 } else { 1630 } else {
1592 // Make the network request conditional, to see if we may reuse our cached 1631 // Make the network request conditional, to see if we may reuse our cached
1593 // response. If we cannot do so, then we just resort to a normal fetch. 1632 // response. If we cannot do so, then we just resort to a normal fetch.
1594 // Our mode remains READ_WRITE for a conditional request. We'll switch to 1633 // Our mode remains READ_WRITE for a conditional request. We'll switch to
1595 // either READ or WRITE mode once we hear back from the server. 1634 // either READ or WRITE mode once we hear back from the server.
1596 if (!ConditionalizeRequest()) { 1635 if (!ConditionalizeRequest()) {
1636 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1597 DCHECK(!partial_.get()); 1637 DCHECK(!partial_.get());
1598 DCHECK_NE(206, response_.headers->response_code()); 1638 DCHECK_NE(206, response_.headers->response_code());
1599 mode_ = WRITE; 1639 mode_ = WRITE;
1600 } 1640 }
1601 next_state_ = STATE_SEND_REQUEST; 1641 next_state_ = STATE_SEND_REQUEST;
1602 } 1642 }
1603 return OK; 1643 return OK;
1604 } 1644 }
1605 1645
1606 int HttpCache::Transaction::BeginPartialCacheValidation() { 1646 int HttpCache::Transaction::BeginPartialCacheValidation() {
1607 DCHECK(mode_ == READ_WRITE); 1647 DCHECK(mode_ == READ_WRITE);
1608 1648
1609 if (response_.headers->response_code() != 206 && !partial_.get() && 1649 if (response_.headers->response_code() != 206 && !partial_.get() &&
1610 !truncated_) 1650 !truncated_)
1611 return BeginCacheValidation(); 1651 return BeginCacheValidation();
1612 1652
1653 // Partial requests should not be recorded in histograms.
1654 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1613 if (range_requested_) { 1655 if (range_requested_) {
1614 next_state_ = STATE_CACHE_QUERY_DATA; 1656 next_state_ = STATE_CACHE_QUERY_DATA;
1615 return OK; 1657 return OK;
1616 } 1658 }
1617 // The request is not for a range, but we have stored just ranges. 1659 // The request is not for a range, but we have stored just ranges.
1618 partial_.reset(new PartialData()); 1660 partial_.reset(new PartialData());
1619 partial_->SetHeaders(request_->extra_headers); 1661 partial_->SetHeaders(request_->extra_headers);
1620 if (!custom_request_.get()) { 1662 if (!custom_request_.get()) {
1621 custom_request_.reset(new HttpRequestInfo(*request_)); 1663 custom_request_.reset(new HttpRequestInfo(*request_));
1622 request_ = custom_request_.get(); 1664 request_ = custom_request_.get();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1663 std::string validator; 1705 std::string validator;
1664 response_.headers->EnumerateHeader( 1706 response_.headers->EnumerateHeader(
1665 NULL, 1707 NULL,
1666 kValidationHeaders[i].related_response_header_name, 1708 kValidationHeaders[i].related_response_header_name,
1667 &validator); 1709 &validator);
1668 1710
1669 if (response_.headers->response_code() != 200 || truncated_ || 1711 if (response_.headers->response_code() != 200 || truncated_ ||
1670 validator.empty() || validator != external_validation_.values[i]) { 1712 validator.empty() || validator != external_validation_.values[i]) {
1671 // The externally conditionalized request is not a validation request 1713 // The externally conditionalized request is not a validation request
1672 // for our existing cache entry. Proceed with caching disabled. 1714 // for our existing cache entry. Proceed with caching disabled.
1715 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1673 DoneWritingToEntry(true); 1716 DoneWritingToEntry(true);
1674 } 1717 }
1675 } 1718 }
1676 1719
1677 next_state_ = STATE_SEND_REQUEST; 1720 next_state_ = STATE_SEND_REQUEST;
1678 return OK; 1721 return OK;
1679 } 1722 }
1680 1723
1681 int HttpCache::Transaction::RestartNetworkRequest() { 1724 int HttpCache::Transaction::RestartNetworkRequest() {
1682 DCHECK(mode_ & WRITE || mode_ == NONE); 1725 DCHECK(mode_ & WRITE || mode_ == NONE);
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
1886 } 1929 }
1887 1930
1888 // 304 is not expected here, but we'll spare the entry (unless it was 1931 // 304 is not expected here, but we'll spare the entry (unless it was
1889 // truncated). 1932 // truncated).
1890 if (truncated_) 1933 if (truncated_)
1891 failure = true; 1934 failure = true;
1892 } 1935 }
1893 1936
1894 if (failure) { 1937 if (failure) {
1895 // We cannot truncate this entry, it has to be deleted. 1938 // We cannot truncate this entry, it has to be deleted.
1939 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1896 DoomPartialEntry(false); 1940 DoomPartialEntry(false);
1897 mode_ = NONE; 1941 mode_ = NONE;
1898 if (!reading_ && !partial_->IsLastRange()) { 1942 if (!reading_ && !partial_->IsLastRange()) {
1899 // We'll attempt to issue another network request, this time without us 1943 // We'll attempt to issue another network request, this time without us
1900 // messing up the headers. 1944 // messing up the headers.
1901 partial_->RestoreHeaders(&custom_request_->extra_headers); 1945 partial_->RestoreHeaders(&custom_request_->extra_headers);
1902 partial_.reset(); 1946 partial_.reset();
1903 truncated_ = false; 1947 truncated_ = false;
1904 return false; 1948 return false;
1905 } 1949 }
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
2008 2052
2009 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); 2053 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2010 return WriteToEntry(kResponseContentIndex, current_size, data, data_len, 2054 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2011 callback); 2055 callback);
2012 } 2056 }
2013 2057
2014 void HttpCache::Transaction::DoneWritingToEntry(bool success) { 2058 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2015 if (!entry_) 2059 if (!entry_)
2016 return; 2060 return;
2017 2061
2018 if (cache_->mode() == RECORD) 2062 RecordHistograms();
2019 DVLOG(1) << "Recorded: " << request_->method << request_->url
2020 << " status: " << response_.headers->response_code();
2021 2063
2022 cache_->DoneWritingToEntry(entry_, success); 2064 cache_->DoneWritingToEntry(entry_, success);
2023 entry_ = NULL; 2065 entry_ = NULL;
2024 mode_ = NONE; // switch to 'pass through' mode 2066 mode_ = NONE; // switch to 'pass through' mode
2025 } 2067 }
2026 2068
2027 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { 2069 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2028 DLOG(ERROR) << "ReadData failed: " << result; 2070 DLOG(ERROR) << "ReadData failed: " << result;
2029 2071
2030 // Avoid using this entry in the future. 2072 // Avoid using this entry in the future.
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
2105 !response_.headers->HasStrongValidators()) 2147 !response_.headers->HasStrongValidators())
2106 return false; 2148 return false;
2107 2149
2108 return true; 2150 return true;
2109 } 2151 }
2110 2152
2111 void HttpCache::Transaction::OnIOComplete(int result) { 2153 void HttpCache::Transaction::OnIOComplete(int result) {
2112 DoLoop(result); 2154 DoLoop(result);
2113 } 2155 }
2114 2156
2157 void HttpCache::Transaction::UpdateTransactionPattern(
2158 TransactionPattern new_transaction_pattern) {
2159 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2160 return;
2161 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2162 new_transaction_pattern == PATTERN_NOT_COVERED);
2163 transaction_pattern_ = new_transaction_pattern;
2164 }
2165
2166 void HttpCache::Transaction::RecordHistograms() {
2167 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2168 if (!cache_ || !cache_->GetCurrentBackend() ||
2169 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2170 cache_->mode() != NORMAL || request_->method != "GET") {
2171 return;
2172 }
2173 UMA_HISTOGRAM_BOOLEAN("HttpCache.HasPattern",
2174 transaction_pattern_ != PATTERN_NOT_COVERED);
2175 if (transaction_pattern_ == PATTERN_NOT_COVERED)
2176 return;
2177 DCHECK(!range_requested_);
2178 DCHECK(!open_entry_since_.is_null());
2179
2180 TimeDelta total_time = base::TimeTicks::Now() - open_entry_since_;
2181
2182 UMA_HISTOGRAM_TIMES("HttpCache.OpenToEnd", total_time);
2183
2184 bool did_send_request = !send_request_since_.is_null();
2185 DCHECK(
2186 (did_send_request && (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2187 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2188 transaction_pattern_ == PATTERN_ENTRY_UPDATED)) ||
2189 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2190
2191 int resource_size = -1;
2192 if (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2193 transaction_pattern_ == PATTERN_ENTRY_UPDATED) {
2194 resource_size = bytes_read_from_network_;
2195 } else if (transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
rvargas (doing something else) 2012/07/30 21:09:31 nit: simple else and DCHECK the pattern.
gavinp 2012/07/31 15:58:48 Done.
2196 transaction_pattern_ == PATTERN_ENTRY_USED) {
2197 resource_size = bytes_read_from_cache_;
2198 } else {
2199 NOTREACHED();
2200 }
2201 bool is_small_resource = resource_size < kSmallResourceMaxBytes;
2202 if (is_small_resource)
2203 UMA_HISTOGRAM_TIMES("HttpCache.OpenToEnd.SmallResource", total_time);
2204
2205 if (!did_send_request) {
2206 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2207 UMA_HISTOGRAM_TIMES("HttpCache.OpenToEnd.Used", total_time);
2208 if (is_small_resource)
2209 UMA_HISTOGRAM_TIMES("HttpCache.OpenToEnd.Used.SmallResource", total_time);
2210 return;
2211 }
2212
2213 TimeDelta before_send_time = send_request_since_ - open_entry_since_;
2214 if (is_small_resource) {
2215 UMA_HISTOGRAM_TIMES("HttpCache.SentRequest.OpenToEnd.SmallResource",
2216 total_time);
2217 UMA_HISTOGRAM_TIMES("HttpCache.SentRequest.BeforeSend.SmallResource",
2218 before_send_time);
2219 }
2220
2221 int before_send_percent = before_send_time * 100 / total_time;
2222 DCHECK_LE(0, before_send_percent);
2223 DCHECK_GE(100, before_send_percent);
2224
2225 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent);
2226 UMA_HISTOGRAM_TIMES("HttpCache.SentRequest.OpenToEnd", total_time);
2227 UMA_HISTOGRAM_TIMES("HttpCache.SentRequest.BeforeSend", before_send_time);
2228 if (is_small_resource) {
2229 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.SmallResource",
2230 before_send_percent);
2231 }
2232
2233 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2234 // below this comment after we have received initial data.
2235 switch (transaction_pattern_) {
2236 case PATTERN_ENTRY_NOT_CACHED: {
2237 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2238 UMA_HISTOGRAM_PERCENTAGE(
2239 "HttpCache.PercentBeforeSend.NotCached", before_send_percent);
2240 if (is_small_resource) {
2241 UMA_HISTOGRAM_PERCENTAGE(
2242 "HttpCache.PercentBeforeSend.NotCached.SmallResource",
2243 before_send_percent);
2244 }
2245 break;
2246 }
2247 case PATTERN_ENTRY_VALIDATED: {
2248 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2249 UMA_HISTOGRAM_PERCENTAGE(
2250 "HttpCache.PercentBeforeSend.Validated", before_send_percent);
2251 if (is_small_resource) {
2252 UMA_HISTOGRAM_PERCENTAGE(
2253 "HttpCache.PercentBeforeSend.Validated.SmallResource",
2254 before_send_percent);
2255 }
2256 break;
2257 }
2258 case PATTERN_ENTRY_UPDATED: {
2259 UMA_HISTOGRAM_TIMES("HttpCache.TimeBlocked.Updated", before_send_time);
2260 UMA_HISTOGRAM_PERCENTAGE(
2261 "HttpCache.PercentBeforeSend.Updated", before_send_percent);
2262 if (is_small_resource) {
2263 UMA_HISTOGRAM_PERCENTAGE(
2264 "HttpCache.PercentBeforeSend.Updated.SmallResource",
2265 before_send_percent);
2266 }
2267 break;
2268 }
2269 default:
2270 NOTREACHED();
2271 }
2272 }
2273
2115 } // namespace net 2274 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_cache_transaction.h ('k') | net/http/mock_http_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698