| OLD | NEW |
| 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/disk_cache/backend_impl.h" | 5 #include "net/disk_cache/backend_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 DCHECK(!new_eviction_); | 292 DCHECK(!new_eviction_); |
| 293 read_only_ = true; | 293 read_only_ = true; |
| 294 } else if (cache_type() == net::SHADER_CACHE) { | 294 } else if (cache_type() == net::SHADER_CACHE) { |
| 295 DCHECK(!new_eviction_); | 295 DCHECK(!new_eviction_); |
| 296 } | 296 } |
| 297 | 297 |
| 298 eviction_.Init(this); | 298 eviction_.Init(this); |
| 299 | 299 |
| 300 // stats_ and rankings_ may end up calling back to us so we better be enabled. | 300 // stats_ and rankings_ may end up calling back to us so we better be enabled. |
| 301 disabled_ = false; | 301 disabled_ = false; |
| 302 if (!stats_.Init(this, &data_->header.stats)) | 302 if (!InitStats()) |
| 303 return net::ERR_FAILED; | 303 return net::ERR_FAILED; |
| 304 | 304 |
| 305 disabled_ = !rankings_.Init(this, new_eviction_); | 305 disabled_ = !rankings_.Init(this, new_eviction_); |
| 306 | 306 |
| 307 #if defined(STRESS_CACHE_EXTENDED_VALIDATION) | 307 #if defined(STRESS_CACHE_EXTENDED_VALIDATION) |
| 308 trace_object_->EnableTracing(false); | 308 trace_object_->EnableTracing(false); |
| 309 int sc = SelfCheck(); | 309 int sc = SelfCheck(); |
| 310 if (sc < 0 && sc != ERR_NUM_ENTRIES_MISMATCH) | 310 if (sc < 0 && sc != ERR_NUM_ENTRIES_MISMATCH) |
| 311 NOTREACHED(); | 311 NOTREACHED(); |
| 312 trace_object_->EnableTracing(true); | 312 trace_object_->EnableTracing(true); |
| 313 #endif | 313 #endif |
| 314 | 314 |
| 315 if (previous_crash) { | 315 if (previous_crash) { |
| 316 ReportError(ERR_PREVIOUS_CRASH); | 316 ReportError(ERR_PREVIOUS_CRASH); |
| 317 } else if (!restarted_) { | 317 } else if (!restarted_) { |
| 318 ReportError(ERR_NO_ERROR); | 318 ReportError(ERR_NO_ERROR); |
| 319 } | 319 } |
| 320 | 320 |
| 321 FlushIndex(); | 321 FlushIndex(); |
| 322 | 322 |
| 323 return disabled_ ? net::ERR_FAILED : net::OK; | 323 return disabled_ ? net::ERR_FAILED : net::OK; |
| 324 } | 324 } |
| 325 | 325 |
| 326 void BackendImpl::CleanupCache() { | 326 void BackendImpl::CleanupCache() { |
| 327 Trace("Backend Cleanup"); | 327 Trace("Backend Cleanup"); |
| 328 eviction_.Stop(); | 328 eviction_.Stop(); |
| 329 timer_.reset(); | 329 timer_.reset(); |
| 330 | 330 |
| 331 if (init_) { | 331 if (init_) { |
| 332 stats_.Store(); | 332 StoreStats(); |
| 333 if (data_) | 333 if (data_) |
| 334 data_->header.crash = 0; | 334 data_->header.crash = 0; |
| 335 | 335 |
| 336 if (user_flags_ & kNoRandom) { | 336 if (user_flags_ & kNoRandom) { |
| 337 // This is a net_unittest, verify that we are not 'leaking' entries. | 337 // This is a net_unittest, verify that we are not 'leaking' entries. |
| 338 File::WaitForPendingIO(&num_pending_io_); | 338 File::WaitForPendingIO(&num_pending_io_); |
| 339 DCHECK(!num_refs_); | 339 DCHECK(!num_refs_); |
| 340 } else { | 340 } else { |
| 341 File::DropPendingIO(); | 341 File::DropPendingIO(); |
| 342 } | 342 } |
| (...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 if (!data_) | 1059 if (!data_) |
| 1060 first_timer_ = false; | 1060 first_timer_ = false; |
| 1061 if (first_timer_) { | 1061 if (first_timer_) { |
| 1062 first_timer_ = false; | 1062 first_timer_ = false; |
| 1063 if (ShouldReportAgain()) | 1063 if (ShouldReportAgain()) |
| 1064 ReportStats(); | 1064 ReportStats(); |
| 1065 } | 1065 } |
| 1066 | 1066 |
| 1067 // Save stats to disk at 5 min intervals. | 1067 // Save stats to disk at 5 min intervals. |
| 1068 if (time % 10 == 0) | 1068 if (time % 10 == 0) |
| 1069 stats_.Store(); | 1069 StoreStats(); |
| 1070 } | 1070 } |
| 1071 | 1071 |
| 1072 void BackendImpl::IncrementIoCount() { | 1072 void BackendImpl::IncrementIoCount() { |
| 1073 num_pending_io_++; | 1073 num_pending_io_++; |
| 1074 } | 1074 } |
| 1075 | 1075 |
| 1076 void BackendImpl::DecrementIoCount() { | 1076 void BackendImpl::DecrementIoCount() { |
| 1077 num_pending_io_--; | 1077 num_pending_io_--; |
| 1078 } | 1078 } |
| 1079 | 1079 |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1345 | 1345 |
| 1346 if (!table_len) | 1346 if (!table_len) |
| 1347 return; | 1347 return; |
| 1348 | 1348 |
| 1349 // If we already have a table, adjust the size to it. | 1349 // If we already have a table, adjust the size to it. |
| 1350 int current_max_size = MaxStorageSizeForTable(table_len); | 1350 int current_max_size = MaxStorageSizeForTable(table_len); |
| 1351 if (max_size_ > current_max_size) | 1351 if (max_size_ > current_max_size) |
| 1352 max_size_= current_max_size; | 1352 max_size_= current_max_size; |
| 1353 } | 1353 } |
| 1354 | 1354 |
| 1355 bool BackendImpl::InitStats() { |
| 1356 Addr address(data_->header.stats); |
| 1357 int size = stats_.StorageSize(); |
| 1358 |
| 1359 if (!address.is_initialized()) { |
| 1360 FileType file_type = Addr::RequiredFileType(size); |
| 1361 DCHECK_NE(file_type, EXTERNAL); |
| 1362 int num_blocks = Addr::RequiredBlocks(size, file_type); |
| 1363 |
| 1364 if (!CreateBlock(file_type, num_blocks, &address)) |
| 1365 return false; |
| 1366 return stats_.Init(NULL, 0, address); |
| 1367 } |
| 1368 |
| 1369 if (!address.is_block_file()) { |
| 1370 NOTREACHED(); |
| 1371 return false; |
| 1372 } |
| 1373 |
| 1374 // Load the required data. |
| 1375 size = address.num_blocks() * address.BlockSize(); |
| 1376 MappedFile* file = File(address); |
| 1377 if (!file) |
| 1378 return false; |
| 1379 |
| 1380 scoped_ptr<char[]> data(new char[size]); |
| 1381 size_t offset = address.start_block() * address.BlockSize() + |
| 1382 kBlockHeaderSize; |
| 1383 if (!file->Read(data.get(), size, offset)) |
| 1384 return false; |
| 1385 |
| 1386 if (!stats_.Init(data.get(), size, address)) |
| 1387 return false; |
| 1388 if (cache_type_ == net::DISK_CACHE && ShouldReportAgain()) |
| 1389 stats_.InitSizeHistogram(); |
| 1390 return true; |
| 1391 } |
| 1392 |
| 1393 void BackendImpl::StoreStats() { |
| 1394 int size = stats_.StorageSize(); |
| 1395 scoped_ptr<char[]> data(new char[size]); |
| 1396 Addr address; |
| 1397 bool rv = stats_.SerializeStats(data.get(), size, &address); |
| 1398 DCHECK(rv); |
| 1399 if (!address.is_initialized()) |
| 1400 return; |
| 1401 |
| 1402 MappedFile* file = File(address); |
| 1403 if (!file) |
| 1404 return; |
| 1405 |
| 1406 size_t offset = address.start_block() * address.BlockSize() + |
| 1407 kBlockHeaderSize; |
| 1408 rv = file->Write(data.get(), size, offset); // ignore result. |
| 1409 } |
| 1410 |
| 1355 void BackendImpl::RestartCache(bool failure) { | 1411 void BackendImpl::RestartCache(bool failure) { |
| 1356 int64 errors = stats_.GetCounter(Stats::FATAL_ERROR); | 1412 int64 errors = stats_.GetCounter(Stats::FATAL_ERROR); |
| 1357 int64 full_dooms = stats_.GetCounter(Stats::DOOM_CACHE); | 1413 int64 full_dooms = stats_.GetCounter(Stats::DOOM_CACHE); |
| 1358 int64 partial_dooms = stats_.GetCounter(Stats::DOOM_RECENT); | 1414 int64 partial_dooms = stats_.GetCounter(Stats::DOOM_RECENT); |
| 1359 int64 ga_evictions = stats_.GetCounter(Stats::GAJS_EVICTED); | 1415 int64 ga_evictions = stats_.GetCounter(Stats::GAJS_EVICTED); |
| 1360 int64 last_report = stats_.GetCounter(Stats::LAST_REPORT); | 1416 int64 last_report = stats_.GetCounter(Stats::LAST_REPORT); |
| 1361 | 1417 |
| 1362 PrepareForRestart(); | 1418 PrepareForRestart(); |
| 1363 if (failure) { | 1419 if (failure) { |
| 1364 DCHECK(!num_refs_); | 1420 DCHECK(!num_refs_); |
| (...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2054 if (total_memory > kMaxBuffersSize || total_memory <= 0) | 2110 if (total_memory > kMaxBuffersSize || total_memory <= 0) |
| 2055 total_memory = kMaxBuffersSize; | 2111 total_memory = kMaxBuffersSize; |
| 2056 | 2112 |
| 2057 done = true; | 2113 done = true; |
| 2058 } | 2114 } |
| 2059 | 2115 |
| 2060 return static_cast<int>(total_memory); | 2116 return static_cast<int>(total_memory); |
| 2061 } | 2117 } |
| 2062 | 2118 |
| 2063 } // namespace disk_cache | 2119 } // namespace disk_cache |
| OLD | NEW |