OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/entry_impl.h" | 5 #include "net/disk_cache/entry_impl.h" |
6 | 6 |
7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/disk_cache/backend_impl.h" | 12 #include "net/disk_cache/backend_impl.h" |
13 #include "net/disk_cache/cache_util.h" | 13 #include "net/disk_cache/cache_util.h" |
| 14 #include "net/disk_cache/histogram_macros.h" |
14 | 15 |
15 using base::Time; | 16 using base::Time; |
16 using base::TimeDelta; | 17 using base::TimeDelta; |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 // Index for the file used to store the key, if any (files_[kKeyFileIndex]). | 21 // Index for the file used to store the key, if any (files_[kKeyFileIndex]). |
21 const int kKeyFileIndex = 3; | 22 const int kKeyFileIndex = 3; |
22 | 23 |
23 // This class implements FileIOCallback to buffer the callback from a file IO | 24 // This class implements FileIOCallback to buffer the callback from a file IO |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 buf_len = entry_size - offset; | 202 buf_len = entry_size - offset; |
202 | 203 |
203 UpdateRank(false); | 204 UpdateRank(false); |
204 | 205 |
205 backend_->OnEvent(Stats::READ_DATA); | 206 backend_->OnEvent(Stats::READ_DATA); |
206 | 207 |
207 if (user_buffers_[index].get()) { | 208 if (user_buffers_[index].get()) { |
208 // Complete the operation locally. | 209 // Complete the operation locally. |
209 DCHECK(kMaxBlockSize >= offset + buf_len); | 210 DCHECK(kMaxBlockSize >= offset + buf_len); |
210 memcpy(buf->data() , user_buffers_[index].get() + offset, buf_len); | 211 memcpy(buf->data() , user_buffers_[index].get() + offset, buf_len); |
211 stats.AddTime(Time::Now() - start); | 212 if (backend_->cache_type() == net::DISK_CACHE) |
| 213 stats.AddTime(Time::Now() - start); |
212 return buf_len; | 214 return buf_len; |
213 } | 215 } |
214 | 216 |
215 Addr address(entry_.Data()->data_addr[index]); | 217 Addr address(entry_.Data()->data_addr[index]); |
216 DCHECK(address.is_initialized()); | 218 DCHECK(address.is_initialized()); |
217 if (!address.is_initialized()) | 219 if (!address.is_initialized()) |
218 return net::ERR_FAILED; | 220 return net::ERR_FAILED; |
219 | 221 |
220 File* file = GetBackingFile(address, index); | 222 File* file = GetBackingFile(address, index); |
221 if (!file) | 223 if (!file) |
(...skipping 11 matching lines...) Expand all Loading... |
233 bool completed; | 235 bool completed; |
234 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { | 236 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { |
235 if (io_callback) | 237 if (io_callback) |
236 io_callback->Discard(); | 238 io_callback->Discard(); |
237 return net::ERR_FAILED; | 239 return net::ERR_FAILED; |
238 } | 240 } |
239 | 241 |
240 if (io_callback && completed) | 242 if (io_callback && completed) |
241 io_callback->Discard(); | 243 io_callback->Discard(); |
242 | 244 |
243 stats.AddTime(Time::Now() - start); | 245 if (backend_->cache_type() == net::DISK_CACHE) |
| 246 stats.AddTime(Time::Now() - start); |
244 return (completed || !completion_callback) ? buf_len : net::ERR_IO_PENDING; | 247 return (completed || !completion_callback) ? buf_len : net::ERR_IO_PENDING; |
245 } | 248 } |
246 | 249 |
247 int EntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, | 250 int EntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, int buf_len, |
248 net::CompletionCallback* completion_callback, | 251 net::CompletionCallback* completion_callback, |
249 bool truncate) { | 252 bool truncate) { |
250 DCHECK(node_.Data()->dirty); | 253 DCHECK(node_.Data()->dirty); |
251 if (index < 0 || index >= NUM_STREAMS) | 254 if (index < 0 || index >= NUM_STREAMS) |
252 return net::ERR_INVALID_ARGUMENT; | 255 return net::ERR_INVALID_ARGUMENT; |
253 | 256 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 | 305 |
303 // If we have prepared the cache as an external file, we should never use | 306 // If we have prepared the cache as an external file, we should never use |
304 // user_buffers_ and always write to file directly. | 307 // user_buffers_ and always write to file directly. |
305 if (!need_file_[index] && user_buffers_[index].get()) { | 308 if (!need_file_[index] && user_buffers_[index].get()) { |
306 // Complete the operation locally. | 309 // Complete the operation locally. |
307 if (!buf_len) | 310 if (!buf_len) |
308 return 0; | 311 return 0; |
309 | 312 |
310 DCHECK(kMaxBlockSize >= offset + buf_len); | 313 DCHECK(kMaxBlockSize >= offset + buf_len); |
311 memcpy(user_buffers_[index].get() + offset, buf->data(), buf_len); | 314 memcpy(user_buffers_[index].get() + offset, buf->data(), buf_len); |
312 stats.AddTime(Time::Now() - start); | 315 if (backend_->cache_type() == net::DISK_CACHE) |
| 316 stats.AddTime(Time::Now() - start); |
313 return buf_len; | 317 return buf_len; |
314 } | 318 } |
315 | 319 |
316 Addr address(entry_.Data()->data_addr[index]); | 320 Addr address(entry_.Data()->data_addr[index]); |
317 File* file = GetBackingFile(address, index); | 321 File* file = GetBackingFile(address, index); |
318 if (!file) | 322 if (!file) |
319 return net::ERR_FAILED; | 323 return net::ERR_FAILED; |
320 | 324 |
321 size_t file_offset = offset; | 325 size_t file_offset = offset; |
322 if (address.is_block_file()) { | 326 if (address.is_block_file()) { |
(...skipping 15 matching lines...) Expand all Loading... |
338 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, | 342 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, |
339 &completed)) { | 343 &completed)) { |
340 if (io_callback) | 344 if (io_callback) |
341 io_callback->Discard(); | 345 io_callback->Discard(); |
342 return net::ERR_FAILED; | 346 return net::ERR_FAILED; |
343 } | 347 } |
344 | 348 |
345 if (io_callback && completed) | 349 if (io_callback && completed) |
346 io_callback->Discard(); | 350 io_callback->Discard(); |
347 | 351 |
348 stats.AddTime(Time::Now() - start); | 352 if (backend_->cache_type() == net::DISK_CACHE) |
| 353 stats.AddTime(Time::Now() - start); |
349 return (completed || !completion_callback) ? buf_len : net::ERR_IO_PENDING; | 354 return (completed || !completion_callback) ? buf_len : net::ERR_IO_PENDING; |
350 } | 355 } |
351 | 356 |
352 base::PlatformFile EntryImpl::UseExternalFile(int index) { | 357 base::PlatformFile EntryImpl::UseExternalFile(int index) { |
353 DCHECK(index >= 0 && index < NUM_STREAMS); | 358 DCHECK(index >= 0 && index < NUM_STREAMS); |
354 | 359 |
355 Addr address(entry_.Data()->data_addr[index]); | 360 Addr address(entry_.Data()->data_addr[index]); |
356 | 361 |
357 // We will not prepare the cache file since the entry is already initialized, | 362 // We will not prepare the cache file since the entry is already initialized, |
358 // just return the platform file backing the cache. | 363 // just return the platform file backing the cache. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 node_.Data()->dirty = backend_->GetCurrentEntryId(); | 456 node_.Data()->dirty = backend_->GetCurrentEntryId(); |
452 node_.Store(); | 457 node_.Store(); |
453 } | 458 } |
454 doomed_ = true; | 459 doomed_ = true; |
455 } | 460 } |
456 | 461 |
457 void EntryImpl::DeleteEntryData(bool everything) { | 462 void EntryImpl::DeleteEntryData(bool everything) { |
458 DCHECK(doomed_ || !everything); | 463 DCHECK(doomed_ || !everything); |
459 | 464 |
460 if (GetDataSize(0)) | 465 if (GetDataSize(0)) |
461 UMA_HISTOGRAM_COUNTS("DiskCache.DeleteHeader", GetDataSize(0)); | 466 CACHE_UMA(COUNTS, "DeleteHeader", 0, GetDataSize(0)); |
462 if (GetDataSize(1)) | 467 if (GetDataSize(1)) |
463 UMA_HISTOGRAM_COUNTS("DiskCache.DeleteData", GetDataSize(1)); | 468 CACHE_UMA(COUNTS, "DeleteData", 0, GetDataSize(1)); |
464 for (int index = 0; index < NUM_STREAMS; index++) { | 469 for (int index = 0; index < NUM_STREAMS; index++) { |
465 Addr address(entry_.Data()->data_addr[index]); | 470 Addr address(entry_.Data()->data_addr[index]); |
466 if (address.is_initialized()) { | 471 if (address.is_initialized()) { |
467 DeleteData(address, index); | 472 DeleteData(address, index); |
468 backend_->ModifyStorageSize(entry_.Data()->data_size[index] - | 473 backend_->ModifyStorageSize(entry_.Data()->data_size[index] - |
469 unreported_size_[index], 0); | 474 unreported_size_[index], 0); |
470 entry_.Data()->data_addr[index] = 0; | 475 entry_.Data()->data_addr[index] = 0; |
471 entry_.Data()->data_size[index] = 0; | 476 entry_.Data()->data_size[index] = 0; |
472 } | 477 } |
473 } | 478 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 } | 615 } |
611 | 616 |
612 void EntryImpl::DeleteData(Addr address, int index) { | 617 void EntryImpl::DeleteData(Addr address, int index) { |
613 if (!address.is_initialized()) | 618 if (!address.is_initialized()) |
614 return; | 619 return; |
615 if (address.is_separate_file()) { | 620 if (address.is_separate_file()) { |
616 if (files_[index]) | 621 if (files_[index]) |
617 files_[index] = NULL; // Releases the object. | 622 files_[index] = NULL; // Releases the object. |
618 | 623 |
619 if (!DeleteCacheFile(backend_->GetFileName(address))) { | 624 if (!DeleteCacheFile(backend_->GetFileName(address))) { |
620 UMA_HISTOGRAM_COUNTS("DiskCache.DeleteFailed", 1); | 625 CACHE_UMA(COUNTS, "DeleteFailed", 0, 1); |
621 LOG(ERROR) << "Failed to delete " << backend_->GetFileName(address) << | 626 LOG(ERROR) << "Failed to delete " << backend_->GetFileName(address) << |
622 " from the cache."; | 627 " from the cache."; |
623 } | 628 } |
624 } else { | 629 } else { |
625 backend_->DeleteBlock(address, true); | 630 backend_->DeleteBlock(address, true); |
626 } | 631 } |
627 } | 632 } |
628 | 633 |
629 void EntryImpl::UpdateRank(bool modified) { | 634 void EntryImpl::UpdateRank(bool modified) { |
630 if (!doomed_) { | 635 if (!doomed_) { |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
848 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), | 853 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), |
849 entry_.address().value(), node_.address().value()); | 854 entry_.address().value(), node_.address().value()); |
850 | 855 |
851 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], | 856 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], |
852 entry_.Data()->data_addr[1], entry_.Data()->long_key); | 857 entry_.Data()->data_addr[1], entry_.Data()->long_key); |
853 | 858 |
854 Trace(" doomed: %d 0x%p 0x%x", doomed_, pointer, dirty); | 859 Trace(" doomed: %d 0x%p 0x%x", doomed_, pointer, dirty); |
855 } | 860 } |
856 | 861 |
857 } // namespace disk_cache | 862 } // namespace disk_cache |
OLD | NEW |