OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/disk_cache/simple/simple_entry_impl.h" | 5 #include "net/disk_cache/simple/simple_entry_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <limits> | 9 #include <limits> |
10 #include <utility> | 10 #include <utility> |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
538 // I/O. Therefore, CancelSparseIO and ReadyForSparseIO succeed instantly. | 538 // I/O. Therefore, CancelSparseIO and ReadyForSparseIO succeed instantly. |
539 return net::OK; | 539 return net::OK; |
540 } | 540 } |
541 | 541 |
542 size_t SimpleEntryImpl::EstimateMemoryUsage() const { | 542 size_t SimpleEntryImpl::EstimateMemoryUsage() const { |
543 // TODO(xunjieli): crbug.com/669108. It'd be nice to have the rest of |entry| | 543 // TODO(xunjieli): crbug.com/669108. It'd be nice to have the rest of |entry| |
544 // measured, but the ownership of SimpleSynchronousEntry isn't straightforward | 544 // measured, but the ownership of SimpleSynchronousEntry isn't straightforward |
545 return sizeof(SimpleSynchronousEntry) + | 545 return sizeof(SimpleSynchronousEntry) + |
546 base::trace_event::EstimateMemoryUsage(pending_operations_) + | 546 base::trace_event::EstimateMemoryUsage(pending_operations_) + |
547 base::trace_event::EstimateMemoryUsage(executing_operation_) + | 547 base::trace_event::EstimateMemoryUsage(executing_operation_) + |
548 (stream_0_data_ ? stream_0_data_->capacity() : 0); | 548 (stream_0_data_ ? stream_0_data_->capacity() : 0) + |
549 (stream_1_prefetch_data_ ? stream_1_prefetch_data_->capacity() : 0); | |
549 } | 550 } |
550 | 551 |
551 SimpleEntryImpl::~SimpleEntryImpl() { | 552 SimpleEntryImpl::~SimpleEntryImpl() { |
552 DCHECK(io_thread_checker_.CalledOnValidThread()); | 553 DCHECK(io_thread_checker_.CalledOnValidThread()); |
553 DCHECK_EQ(0U, pending_operations_.size()); | 554 DCHECK_EQ(0U, pending_operations_.size()); |
554 DCHECK(state_ == STATE_UNINITIALIZED || state_ == STATE_FAILURE); | 555 DCHECK(state_ == STATE_UNINITIALIZED || state_ == STATE_FAILURE); |
555 DCHECK(!synchronous_entry_); | 556 DCHECK(!synchronous_entry_); |
556 net_log_.EndEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY); | 557 net_log_.EndEvent(net::NetLogEventType::SIMPLE_CACHE_ENTRY); |
557 } | 558 } |
558 | 559 |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
839 if (!callback.is_null()) | 840 if (!callback.is_null()) |
840 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 841 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
841 base::Bind(callback, 0)); | 842 base::Bind(callback, 0)); |
842 return; | 843 return; |
843 } | 844 } |
844 | 845 |
845 buf_len = std::min(buf_len, GetDataSize(stream_index) - offset); | 846 buf_len = std::min(buf_len, GetDataSize(stream_index) - offset); |
846 | 847 |
847 // Since stream 0 data is kept in memory, it is read immediately. | 848 // Since stream 0 data is kept in memory, it is read immediately. |
848 if (stream_index == 0) { | 849 if (stream_index == 0) { |
849 int ret_value = ReadStream0Data(buf, offset, buf_len); | 850 ReadInMemoryStreamData(stream_0_data_.get(), offset, buf_len, buf, |
850 if (!callback.is_null()) { | 851 callback); |
851 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
852 FROM_HERE, base::Bind(callback, ret_value)); | |
853 } | |
854 return; | 852 return; |
855 } | 853 } |
856 | 854 |
855 // Sometimes we can read in-ram prefetched stream 1 data immediately, too. | |
856 if (stream_index == 1 && stream_1_prefetch_data_) { | |
857 ReadInMemoryStreamData(stream_1_prefetch_data_.get(), offset, buf_len, buf, | |
pasko
2017/07/11 13:24:12
perhaps we could record how much time it takes to
Maks Orlovich
2017/07/11 14:34:06
It's a memcpy? Doesn't sound that interesting... T
pasko
2017/07/11 16:17:31
sorry, wrong pointer, I was asking whether we can
Maks Orlovich
2017/07/12 16:06:09
It's not really a separate event, since it replace
pasko
2017/07/18 13:46:30
lulz :) I was writing it without knowing that you
| |
858 callback); | |
859 return; | |
860 } | |
861 | |
857 state_ = STATE_IO_PENDING; | 862 state_ = STATE_IO_PENDING; |
858 if (!doomed_ && backend_.get()) | 863 if (!doomed_ && backend_.get()) |
859 backend_->index()->UseIfExists(entry_hash_); | 864 backend_->index()->UseIfExists(entry_hash_); |
860 | 865 |
861 // Figure out if we should be computing the checksum for this read, | 866 // Figure out if we should be computing the checksum for this read, |
862 // and whether we should be verifying it, too. | 867 // and whether we should be verifying it, too. |
863 std::unique_ptr<SimpleSynchronousEntry::CRCRequest> crc_request; | 868 std::unique_ptr<SimpleSynchronousEntry::CRCRequest> crc_request; |
864 if (crc32s_end_offset_[stream_index] == offset) { | 869 if (crc32s_end_offset_[stream_index] == offset) { |
865 crc_request.reset(new SimpleSynchronousEntry::CRCRequest()); | 870 crc_request.reset(new SimpleSynchronousEntry::CRCRequest()); |
866 | 871 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
938 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 943 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
939 base::Bind(callback, 0)); | 944 base::Bind(callback, 0)); |
940 } | 945 } |
941 return; | 946 return; |
942 } | 947 } |
943 } | 948 } |
944 state_ = STATE_IO_PENDING; | 949 state_ = STATE_IO_PENDING; |
945 if (!doomed_ && backend_.get()) | 950 if (!doomed_ && backend_.get()) |
946 backend_->index()->UseIfExists(entry_hash_); | 951 backend_->index()->UseIfExists(entry_hash_); |
947 | 952 |
953 // Any stream 1 write invalidates the prefetched data. | |
954 if (stream_index == 1) | |
955 stream_1_prefetch_data_ = nullptr; | |
956 | |
948 AdvanceCrc(buf, offset, buf_len, stream_index); | 957 AdvanceCrc(buf, offset, buf_len, stream_index); |
949 | 958 |
950 // |entry_stat| needs to be initialized before modifying |data_size_|. | 959 // |entry_stat| needs to be initialized before modifying |data_size_|. |
951 std::unique_ptr<SimpleEntryStat> entry_stat(new SimpleEntryStat( | 960 std::unique_ptr<SimpleEntryStat> entry_stat(new SimpleEntryStat( |
952 last_used_, last_modified_, data_size_, sparse_data_size_)); | 961 last_used_, last_modified_, data_size_, sparse_data_size_)); |
953 if (truncate) { | 962 if (truncate) { |
954 data_size_[stream_index] = offset + buf_len; | 963 data_size_[stream_index] = offset + buf_len; |
955 } else { | 964 } else { |
956 data_size_[stream_index] = std::max(offset + buf_len, | 965 data_size_[stream_index] = std::max(offset + buf_len, |
957 GetDataSize(stream_index)); | 966 GetDataSize(stream_index)); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1142 | 1151 |
1143 state_ = STATE_READY; | 1152 state_ = STATE_READY; |
1144 synchronous_entry_ = in_results->sync_entry; | 1153 synchronous_entry_ = in_results->sync_entry; |
1145 if (in_results->stream_0_data.get()) { | 1154 if (in_results->stream_0_data.get()) { |
1146 stream_0_data_ = in_results->stream_0_data; | 1155 stream_0_data_ = in_results->stream_0_data; |
1147 // The crc was read in SimpleSynchronousEntry. | 1156 // The crc was read in SimpleSynchronousEntry. |
1148 crc_check_state_[0] = CRC_CHECK_DONE; | 1157 crc_check_state_[0] = CRC_CHECK_DONE; |
1149 crc32s_[0] = in_results->stream_0_crc32; | 1158 crc32s_[0] = in_results->stream_0_crc32; |
1150 crc32s_end_offset_[0] = in_results->entry_stat.data_size(0); | 1159 crc32s_end_offset_[0] = in_results->entry_stat.data_size(0); |
1151 } | 1160 } |
1161 | |
1162 if (in_results->stream_1_data.get()) { | |
pasko
2017/07/18 13:46:30
almost a copy of the block above, perhaps make str
Maks Orlovich
2017/07/28 17:27:35
Done.
| |
1163 stream_1_prefetch_data_ = in_results->stream_1_data; | |
1164 // The crc was read in SimpleSynchronousEntry. | |
1165 crc_check_state_[1] = CRC_CHECK_DONE; | |
1166 crc32s_[1] = in_results->stream_1_crc32; | |
1167 crc32s_end_offset_[1] = in_results->entry_stat.data_size(1); | |
1168 } | |
1169 | |
1152 // If this entry was opened by hash, key_ could still be empty. If so, update | 1170 // If this entry was opened by hash, key_ could still be empty. If so, update |
1153 // it with the key read from the synchronous entry. | 1171 // it with the key read from the synchronous entry. |
1154 if (key_.empty()) { | 1172 if (key_.empty()) { |
1155 SetKey(synchronous_entry_->key()); | 1173 SetKey(synchronous_entry_->key()); |
1156 } else { | 1174 } else { |
1157 // This should only be triggered when creating an entry. In the open case | 1175 // This should only be triggered when creating an entry. In the open case |
1158 // the key is either copied from the arguments to open, or checked | 1176 // the key is either copied from the arguments to open, or checked |
1159 // in the synchronous entry. | 1177 // in the synchronous entry. |
1160 DCHECK_EQ(key_, synchronous_entry_->key()); | 1178 DCHECK_EQ(key_, synchronous_entry_->key()); |
1161 } | 1179 } |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1437 } else { | 1455 } else { |
1438 type = conflicting ? WRITE_FOLLOWS_CONFLICTING_WRITE | 1456 type = conflicting ? WRITE_FOLLOWS_CONFLICTING_WRITE |
1439 : WRITE_FOLLOWS_NON_CONFLICTING_WRITE; | 1457 : WRITE_FOLLOWS_NON_CONFLICTING_WRITE; |
1440 } | 1458 } |
1441 } | 1459 } |
1442 SIMPLE_CACHE_UMA(ENUMERATION, | 1460 SIMPLE_CACHE_UMA(ENUMERATION, |
1443 "WriteDependencyType", cache_type_, | 1461 "WriteDependencyType", cache_type_, |
1444 type, WRITE_DEPENDENCY_TYPE_MAX); | 1462 type, WRITE_DEPENDENCY_TYPE_MAX); |
1445 } | 1463 } |
1446 | 1464 |
1447 int SimpleEntryImpl::ReadStream0Data(net::IOBuffer* buf, | 1465 void SimpleEntryImpl::ReadInMemoryStreamData( |
1448 int offset, | 1466 net::GrowableIOBuffer* in_buf, |
1449 int buf_len) { | 1467 int offset, |
1468 int buf_len, | |
1469 net::IOBuffer* out_buf, | |
1470 const CompletionCallback& callback) { | |
1471 int rv; | |
1450 if (buf_len < 0) { | 1472 if (buf_len < 0) { |
1451 RecordReadResult(cache_type_, READ_RESULT_SYNC_READ_FAILURE); | 1473 RecordReadResult(cache_type_, READ_RESULT_SYNC_READ_FAILURE); |
1452 return 0; | 1474 rv = 0; |
1475 } else { | |
1476 memcpy(out_buf->data(), in_buf->data() + offset, buf_len); | |
1477 UpdateDataFromEntryStat(SimpleEntryStat(base::Time::Now(), last_modified_, | |
1478 data_size_, sparse_data_size_)); | |
1479 RecordReadResult(cache_type_, READ_RESULT_SUCCESS); | |
1480 rv = buf_len; | |
1453 } | 1481 } |
1454 memcpy(buf->data(), stream_0_data_->data() + offset, buf_len); | 1482 if (!callback.is_null()) { |
1455 UpdateDataFromEntryStat( | 1483 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
1456 SimpleEntryStat(base::Time::Now(), last_modified_, data_size_, | 1484 base::Bind(callback, rv)); |
1457 sparse_data_size_)); | 1485 } |
1458 RecordReadResult(cache_type_, READ_RESULT_SUCCESS); | |
1459 return buf_len; | |
1460 } | 1486 } |
1461 | 1487 |
1462 int SimpleEntryImpl::SetStream0Data(net::IOBuffer* buf, | 1488 int SimpleEntryImpl::SetStream0Data(net::IOBuffer* buf, |
1463 int offset, | 1489 int offset, |
1464 int buf_len, | 1490 int buf_len, |
1465 bool truncate) { | 1491 bool truncate) { |
1466 // Currently, stream 0 is only used for HTTP headers, and always writes them | 1492 // Currently, stream 0 is only used for HTTP headers, and always writes them |
1467 // with a single, truncating write. Detect these writes and record the size | 1493 // with a single, truncating write. Detect these writes and record the size |
1468 // changes of the headers. Also, support writes to stream 0 that have | 1494 // changes of the headers. Also, support writes to stream 0 that have |
1469 // different access patterns, as required by the API contract. | 1495 // different access patterns, as required by the API contract. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1516 } | 1542 } |
1517 crc32s_end_offset_[stream_index] = offset + length; | 1543 crc32s_end_offset_[stream_index] = offset + length; |
1518 } else if (offset < crc32s_end_offset_[stream_index]) { | 1544 } else if (offset < crc32s_end_offset_[stream_index]) { |
1519 // If a range for which the crc32 was already computed is rewritten, the | 1545 // If a range for which the crc32 was already computed is rewritten, the |
1520 // computation of the crc32 need to start from 0 again. | 1546 // computation of the crc32 need to start from 0 again. |
1521 crc32s_end_offset_[stream_index] = 0; | 1547 crc32s_end_offset_[stream_index] = 0; |
1522 } | 1548 } |
1523 } | 1549 } |
1524 | 1550 |
1525 } // namespace disk_cache | 1551 } // namespace disk_cache |
OLD | NEW |