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

Side by Side Diff: net/disk_cache/v3/block_bitmaps.cc

Issue 17816008: Disk cache: Introduce BlockBitmaps for V3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 5 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
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/disk_cache/block_files.h" 5 #include "net/disk_cache/v3/block_bitmaps.h"
6 6
7 #include "base/atomicops.h"
8 #include "base/file_util.h"
9 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_checker.h"
13 #include "base/time.h" 8 #include "base/time.h"
14 #include "net/disk_cache/cache_util.h" 9 #include "net/disk_cache/disk_format_base.h"
15 #include "net/disk_cache/file_lock.h"
16 #include "net/disk_cache/trace.h" 10 #include "net/disk_cache/trace.h"
17 11
18 using base::TimeTicks; 12 using base::TimeTicks;
19 13
20 namespace disk_cache { 14 namespace disk_cache {
21 15
22 BlockFiles::BlockFiles(const base::FilePath& path) 16 BlockBitmaps::BlockBitmaps() {
23 : init_(false), zero_buffer_(NULL), path_(path) {
24 } 17 }
25 18
26 BlockFiles::~BlockFiles() { 19 BlockBitmaps::~BlockBitmaps() {
27 if (zero_buffer_)
28 delete[] zero_buffer_;
29 CloseFiles();
30 } 20 }
31 21
32 bool BlockFiles::Init(bool create_files) { 22 void BlockBitmaps::Init(const BlockFilesBitmaps& bitmaps) {
33 DCHECK(!init_); 23 bitmaps_ = bitmaps;
34 if (init_) 24 }
25
26 bool BlockBitmaps::CreateBlock(FileType block_type,
27 int block_count,
28 Addr* block_address) {
29 if (block_type < BLOCK_256 || block_count < 1 || block_count > kMaxNumBlocks)
gavinp 2013/06/28 21:47:45 block_type < BLOCK_256 is hard for me to understan
35 return false; 30 return false;
36 31
37 thread_checker_.reset(new base::ThreadChecker); 32 int header_num = HeaderNumberForNewBlock(block_type, block_count);
38 33 if (header_num < 0)
39 block_files_.resize(kFirstAdditionalBlockFile);
40 for (int i = 0; i < kFirstAdditionalBlockFile; i++) {
41 if (create_files)
42 if (!CreateBlockFile(i, static_cast<FileType>(i + 1), true))
43 return false;
44
45 if (!OpenBlockFile(i))
46 return false;
47
48 // Walk this chain of files removing empty ones.
49 if (!RemoveEmptyFile(static_cast<FileType>(i + 1)))
50 return false;
51 }
52
53 init_ = true;
54 return true;
55 }
56
57 bool BlockFiles::CreateBlock(FileType block_type, int block_count,
58 Addr* block_address) {
59 DCHECK(thread_checker_->CalledOnValidThread());
60 if (block_type < RANKINGS || block_type > BLOCK_4K ||
61 block_count < 1 || block_count > 4)
62 return false;
63 if (!init_)
64 return false; 34 return false;
65 35
66 MappedFile* file = FileForNewBlock(block_type, block_count);
67 if (!file)
68 return false;
69
70 ScopedFlush flush(file);
71 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
72
73 int target_size = 0; 36 int target_size = 0;
74 for (int i = block_count; i <= 4; i++) { 37 for (int i = block_count; i <= kMaxNumBlocks; i++) {
75 if (header->empty[i - 1]) { 38 if (bitmaps_[header_num]->empty[i - 1]) {
76 target_size = i; 39 target_size = i;
77 break; 40 break;
78 } 41 }
79 } 42 }
80 43
81 DCHECK(target_size); 44 DCHECK(target_size);
82 int index; 45 int index;
83 if (!CreateMapBlock(target_size, block_count, header, &index)) 46 if (!bitmaps_[header_num].CreateMapBlock(target_size, block_count, &index))
84 return false; 47 return false;
85 48
86 Addr address(block_type, block_count, header->this_file, index); 49 if (!index && (block_type == BLOCK_ENTRIES || block_type == BLOCK_EVICTED) &&
50 !bitmaps_[header_num].CreateMapBlock(target_size, block_count, &index)) {
51 // index 0 for entries is a reserved value.
52 return false;
53 }
54
55 Addr address(block_type, block_count, bitmaps_[header_num]->this_file, index);
87 block_address->set_value(address.value()); 56 block_address->set_value(address.value());
88 Trace("CreateBlock 0x%x", address.value()); 57 Trace("CreateBlock 0x%x", address.value());
89 return true; 58 return true;
90 } 59 }
91 60
92 void BlockFiles::DeleteBlock(Addr address, bool deep) { 61 void BlockBitmaps::DeleteBlock(Addr address) {
93 DCHECK(thread_checker_->CalledOnValidThread());
94 if (!address.is_initialized() || address.is_separate_file()) 62 if (!address.is_initialized() || address.is_separate_file())
95 return; 63 return;
96 64
97 if (!zero_buffer_) { 65 int header_num = GetHeaderNumber(address);
98 zero_buffer_ = new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4]; 66 if (header_num < 0)
gavinp 2013/06/28 21:47:45 Why isn't this a DCHECK?
rvargas (doing something else) 2013/07/11 19:54:55 Same reason I mentioned before: (this way) DeleteB
99 memset(zero_buffer_, 0, Addr::BlockSizeForFileType(BLOCK_4K) * 4);
100 }
101 MappedFile* file = GetFile(address);
102 if (!file)
103 return; 67 return;
104 68
105 Trace("DeleteBlock 0x%x", address.value()); 69 Trace("DeleteBlock 0x%x", address.value());
106 70 bitmaps_[header_num].DeleteMapBlock(address.start_block(),
107 size_t size = address.BlockSize() * address.num_blocks(); 71 address.num_blocks());
108 size_t offset = address.start_block() * address.BlockSize() +
109 kBlockHeaderSize;
110 if (deep)
111 file->Write(zero_buffer_, size, offset);
112
113 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
114 DeleteMapBlock(address.start_block(), address.num_blocks(), header);
115 file->Flush();
116
117 if (!header->num_entries) {
118 // This file is now empty. Let's try to delete it.
119 FileType type = Addr::RequiredFileType(header->entry_size);
120 if (Addr::BlockSizeForFileType(RANKINGS) == header->entry_size)
121 type = RANKINGS;
122 RemoveEmptyFile(type); // Ignore failures.
123 }
124 } 72 }
125 73
126 void BlockFiles::CloseFiles() { 74 void BlockBitmaps::Clear() {
127 if (init_) { 75 bitmaps_.clear();
128 DCHECK(thread_checker_->CalledOnValidThread());
129 }
130 init_ = false;
131 for (unsigned int i = 0; i < block_files_.size(); i++) {
132 if (block_files_[i]) {
133 block_files_[i]->Release();
134 block_files_[i] = NULL;
135 }
136 }
137 block_files_.clear();
138 } 76 }
139 77
140 void BlockFiles::ReportStats() { 78 void BlockBitmaps::ReportStats() {
141 DCHECK(thread_checker_->CalledOnValidThread());
142 int used_blocks[kFirstAdditionalBlockFile]; 79 int used_blocks[kFirstAdditionalBlockFile];
143 int load[kFirstAdditionalBlockFile]; 80 int load[kFirstAdditionalBlockFile];
144 for (int i = 0; i < kFirstAdditionalBlockFile; i++) { 81 for (int i = 0; i < kFirstAdditionalBlockFile; i++) {
145 GetFileStats(i, &used_blocks[i], &load[i]); 82 GetFileStats(i, &used_blocks[i], &load[i]);
146 } 83 }
147 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_0", used_blocks[0]); 84 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_0", used_blocks[0]);
148 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_1", used_blocks[1]); 85 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_1", used_blocks[1]);
149 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_2", used_blocks[2]); 86 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_2", used_blocks[2]);
150 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_3", used_blocks[3]); 87 UMA_HISTOGRAM_COUNTS("DiskCache.Blocks_3", used_blocks[3]);
151 88
152 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_0", load[0], 101); 89 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_0", load[0], 101);
153 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_1", load[1], 101); 90 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_1", load[1], 101);
154 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_2", load[2], 101); 91 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_2", load[2], 101);
155 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_3", load[3], 101); 92 UMA_HISTOGRAM_ENUMERATION("DiskCache.BlockLoad_3", load[3], 101);
156 } 93 }
157 94
158 bool BlockFiles::IsValid(Addr address) { 95 bool BlockBitmaps::IsValid(Addr address) {
159 #ifdef NDEBUG 96 #ifdef NDEBUG
160 return true; 97 return true;
161 #else 98 #else
162 if (!address.is_initialized() || address.is_separate_file()) 99 if (!address.is_initialized() || address.is_separate_file())
163 return false; 100 return false;
164 101
165 MappedFile* file = GetFile(address); 102 int header_num = GetHeaderNumber(address);
166 if (!file) 103 if (header_num < 0)
167 return false; 104 return false;
168 105
169 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); 106 bool rv = bitmaps_[header_num].UsedMapBlock(address.start_block(),
170 bool rv = UsedMapBlock(address.start_block(), address.num_blocks(), header); 107 address.num_blocks());
171 DCHECK(rv); 108 DCHECK(rv);
172
173 static bool read_contents = false;
174 if (read_contents) {
175 scoped_ptr<char[]> buffer;
176 buffer.reset(new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4]);
177 size_t size = address.BlockSize() * address.num_blocks();
178 size_t offset = address.start_block() * address.BlockSize() +
179 kBlockHeaderSize;
180 bool ok = file->Read(buffer.get(), size, offset);
181 DCHECK(ok);
182 }
183
184 return rv; 109 return rv;
185 #endif 110 #endif
186 } 111 }
187 112
188 MappedFile* BlockFiles::GetFile(Addr address) { 113 int BlockBitmaps::GetHeaderNumber(Addr address) {
189 DCHECK(thread_checker_->CalledOnValidThread()); 114 DCHECK(bitmaps_.size() >= kFirstAdditionalBlockFileV3);
190 DCHECK(block_files_.size() >= 4);
191 DCHECK(address.is_block_file() || !address.is_initialized()); 115 DCHECK(address.is_block_file() || !address.is_initialized());
192 if (!address.is_initialized()) 116 if (!address.is_initialized())
193 return NULL; 117 return -1;
194 118
195 int file_index = address.FileNumber(); 119 int file_index = address.FileNumber();
196 if (static_cast<unsigned int>(file_index) >= block_files_.size() || 120 if (static_cast<unsigned int>(file_index) >= bitmaps_.size() ||
197 !block_files_[file_index]) { 121 !bitmaps_[file_index].Get()) {
198 // We need to open the file 122 return -1;
199 if (!OpenBlockFile(file_index))
200 return NULL;
201 } 123 }
202 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); 124 DCHECK(bitmaps_.size() >= static_cast<unsigned int>(file_index));
203 return block_files_[file_index]; 125 return file_index;
204 } 126 }
205 127
206 bool BlockFiles::GrowBlockFile(MappedFile* file, BlockFileHeader* header) { 128 int BlockBitmaps::HeaderNumberForNewBlock(FileType block_type,
207 if (kMaxBlocks == header->max_entries) 129 int block_count) {
208 return false; 130 COMPILE_ASSERT(RANKINGS == 1, invalid_file_type);
gavinp 2013/06/28 21:47:45 What does this do?
rvargas (doing something else) 2013/07/11 19:54:55 Makes sure that the next line doesn't result in a
gavinp 2013/07/15 18:23:35 Why not: DCHECK_GT(0, block_type); I'm at least
131 int header_num = block_type - 1;
132 bool found = true;
209 133
210 ScopedFlush flush(file); 134 TimeTicks start = TimeTicks::Now();
211 DCHECK(!header->empty[3]); 135 while (bitmaps_[header_num].NeedToGrowBlockFile(block_count)) {
212 int new_size = header->max_entries + 1024; 136 header_num = bitmaps_[header_num]->next_file;
213 if (new_size > kMaxBlocks) 137 if (!header_num) {
214 new_size = kMaxBlocks; 138 found = false;
215 139 break;
216 int new_size_bytes = new_size * header->entry_size + sizeof(*header);
217
218 if (!file->SetLength(new_size_bytes)) {
219 // Most likely we are trying to truncate the file, so the header is wrong.
220 if (header->updating < 10 && !FixBlockFileHeader(file)) {
221 // If we can't fix the file increase the lock guard so we'll pick it on
222 // the next start and replace it.
223 header->updating = 100;
224 return false;
225 } 140 }
226 return (header->max_entries >= new_size);
227 } 141 }
228 142
229 FileLock lock(header); 143 if (!found) {
230 header->empty[3] = (new_size - header->max_entries) / 4; // 4 blocks entries 144 // Restart the search, looking for any file with space.
231 header->max_entries = new_size; 145 header_num = block_type - 1;
232 146 do {
233 return true; 147 if (bitmaps_[header_num].CanAllocate(block_count))
234 } 148 found = true;
235 149 else
236 MappedFile* BlockFiles::FileForNewBlock(FileType block_type, int block_count) { 150 header_num = bitmaps_[header_num]->next_file;
237 COMPILE_ASSERT(RANKINGS == 1, invalid_file_type); 151 } while (header_num && !found);
238 MappedFile* file = block_files_[block_type - 1]; 152 if (!found) {
gavinp 2013/06/28 21:47:45 It took me a few passes to understand the logic of
rvargas (doing something else) 2013/07/11 19:54:55 Yes and no. In general it is not the same because
gavinp 2013/07/15 18:23:35 The two loops could be made a lot more clear if we
239 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); 153 NOTREACHED();
gavinp 2013/06/28 21:47:45 This scares me a bit. Is this an assertion, or an
rvargas (doing something else) 2013/07/11 19:54:55 Not that uncommon pattern. It is an assertion in t
gavinp 2013/07/15 18:23:35 Why not crash in the field? If this is impossible
rvargas (doing something else) 2013/08/05 19:50:45 I din't say it was impossible to get here. All is
gavinp 2013/08/06 01:11:02 Aha, so should this be a DLOG, then. Recall that t
240 154 header_num = -1;
241 TimeTicks start = TimeTicks::Now();
242 while (NeedToGrowBlockFile(header, block_count)) {
243 if (kMaxBlocks == header->max_entries) {
244 file = NextFile(file);
245 if (!file)
246 return NULL;
247 header = reinterpret_cast<BlockFileHeader*>(file->buffer());
248 continue;
249 } 155 }
250
251 if (!GrowBlockFile(file, header))
252 return NULL;
253 break;
254 }
255 HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", TimeTicks::Now() - start);
256 return file;
257 }
258
259 // Note that we expect to be called outside of a FileLock... however, we cannot
260 // DCHECK on header->updating because we may be fixing a crash.
261 bool BlockFiles::FixBlockFileHeader(MappedFile* file) {
262 ScopedFlush flush(file);
263 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer());
264 int file_size = static_cast<int>(file->GetLength());
265 if (file_size < static_cast<int>(sizeof(*header)))
266 return false; // file_size > 2GB is also an error.
267
268 const int kMinBlockSize = 36;
269 const int kMaxBlockSize = 4096;
270 if (header->entry_size < kMinBlockSize ||
271 header->entry_size > kMaxBlockSize || header->num_entries < 0)
272 return false;
273
274 // Make sure that we survive crashes.
275 header->updating = 1;
276 int expected = header->entry_size * header->max_entries + sizeof(*header);
277 if (file_size != expected) {
278 int max_expected = header->entry_size * kMaxBlocks + sizeof(*header);
279 if (file_size < expected || header->empty[3] || file_size > max_expected) {
280 NOTREACHED();
281 LOG(ERROR) << "Unexpected file size";
282 return false;
283 }
284 // We were in the middle of growing the file.
285 int num_entries = (file_size - sizeof(*header)) / header->entry_size;
286 header->max_entries = num_entries;
287 } 156 }
288 157
289 FixAllocationCounters(header); 158 HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", TimeTicks::Now() - start);
gavinp 2013/06/28 21:47:45 Should we be separating out v3 histograms by name?
rvargas (doing something else) 2013/07/11 19:54:55 This histogram is exactly the same one as before a
290 int empty_blocks = EmptyBlocks(header); 159 return header_num;
291 if (empty_blocks + header->num_entries > header->max_entries)
292 header->num_entries = header->max_entries - empty_blocks;
293
294 if (!ValidateCounters(header))
295 return false;
296
297 header->updating = 0;
298 return true;
299 } 160 }
300 161
301 // We are interested in the total number of blocks used by this file type, and 162 // We are interested in the total number of blocks used by this file type, and
302 // the max number of blocks that we can store (reported as the percentage of 163 // the max number of blocks that we can store (reported as the percentage of
303 // used blocks). In order to find out the number of used blocks, we have to 164 // used blocks). In order to find out the number of used blocks, we have to
304 // substract the empty blocks from the total blocks for each file in the chain. 165 // substract the empty blocks from the total blocks for each file in the chain.
305 void BlockFiles::GetFileStats(int index, int* used_count, int* load) { 166 void BlockBitmaps::GetFileStats(int index, int* used_count, int* load) {
306 int max_blocks = 0; 167 int max_blocks = 0;
307 *used_count = 0; 168 *used_count = 0;
308 *load = 0; 169 *load = 0;
309 for (;;) { 170 for (;;) {
310 if (!block_files_[index] && !OpenBlockFile(index)) 171 BlockFileHeader* header = bitmaps_[index].Get();
311 return;
312
313 BlockFileHeader* header =
314 reinterpret_cast<BlockFileHeader*>(block_files_[index]->buffer());
315 172
316 max_blocks += header->max_entries; 173 max_blocks += header->max_entries;
317 int used = header->max_entries; 174 int used = header->max_entries;
318 for (int i = 0; i < 4; i++) { 175 for (int i = 0; i < kMaxNumBlocks; i++) {
319 used -= header->empty[i] * (i + 1); 176 used -= header->empty[i] * (i + 1);
320 DCHECK_GE(used, 0); 177 DCHECK_GE(used, 0);
321 } 178 }
322 *used_count += used; 179 *used_count += used;
323 180
324 if (!header->next_file) 181 if (!header->next_file)
325 break; 182 break;
326 index = header->next_file; 183 index = header->next_file;
327 } 184 }
328 if (max_blocks) 185 if (max_blocks)
329 *load = *used_count * 100 / max_blocks; 186 *load = *used_count * 100 / max_blocks;
330 } 187 }
331 188
332 } // namespace disk_cache 189 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698