| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // The cache is stored on disk as a collection of block-files, plus an index | 5 // The cache is stored on disk as a collection of block-files, plus an index |
| 6 // file plus a collection of external files. | 6 // file plus a collection of external files. |
| 7 // | 7 // |
| 8 // Any data blob bigger than kMaxBlockSize (net/addr.h) will be stored on a | 8 // Any data blob bigger than kMaxBlockSize (disk_cache/addr.h) will be stored in |
| 9 // separate file named f_xxx where x is a hexadecimal number. Shorter data will | 9 // a separate file named f_xxx where x is a hexadecimal number. Shorter data |
| 10 // be stored as a series of blocks on a block-file. In any case, CacheAddr | 10 // will be stored as a series of blocks on a block-file. In any case, CacheAddr |
| 11 // represents the address of the data inside the cache. | 11 // represents the address of the data inside the cache. |
| 12 // | 12 // |
| 13 // The index file is just a simple hash table that maps a particular entry to | 13 // The index file is just a simple hash table that maps a particular entry to |
| 14 // a CacheAddr value. Linking for a given hash bucket is handled internally | 14 // a CacheAddr value. Linking for a given hash bucket is handled internally |
| 15 // by the cache entry. | 15 // by the cache entry. |
| 16 // | 16 // |
| 17 // The last element of the cache is the block-file. A block file is a file | 17 // The last element of the cache is the block-file. A block file is a file |
| 18 // designed to store blocks of data of a given size. It is able to store data | 18 // designed to store blocks of data of a given size. For more details see |
| 19 // that spans from one to four consecutive "blocks", and it grows as needed to | 19 // disk_cache/disk_format_base.h |
| 20 // store up to approximately 65000 blocks. It has a fixed size header used for | |
| 21 // book keeping such as tracking free of blocks on the file. For example, a | |
| 22 // block-file for 1KB blocks will grow from 8KB when totally empty to about 64MB | |
| 23 // when completely full. At that point, data blocks of 1KB will be stored on a | |
| 24 // second block file that will store the next set of 65000 blocks. The first | |
| 25 // file contains the number of the second file, and the second file contains the | |
| 26 // number of a third file, created when the second file reaches its limit. It is | |
| 27 // important to remember that no matter how long the chain of files is, any | |
| 28 // given block can be located directly by its address, which contains the file | |
| 29 // number and starting block inside the file. | |
| 30 // | 20 // |
| 31 // A new cache is initialized with four block files (named data_0 through | 21 // A new cache is initialized with four block files (named data_0 through |
| 32 // data_3), each one dedicated to store blocks of a given size. The number at | 22 // data_3), each one dedicated to store blocks of a given size. The number at |
| 33 // the end of the file name is the block file number (in decimal). | 23 // the end of the file name is the block file number (in decimal). |
| 34 // | 24 // |
| 35 // There are two "special" types of blocks: an entry and a rankings node. An | 25 // There are two "special" types of blocks: an entry and a rankings node. An |
| 36 // entry keeps track of all the information related to the same cache entry, | 26 // entry keeps track of all the information related to the same cache entry, |
| 37 // such as the key, hash value, data pointers etc. A rankings node keeps track | 27 // such as the key, hash value, data pointers etc. A rankings node keeps track |
| 38 // of the information that is updated frequently for a given entry, such as its | 28 // of the information that is updated frequently for a given entry, such as its |
| 39 // location on the LRU lists, last access time etc. | 29 // location on the LRU lists, last access time etc. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 50 // and all the data can be trusted, the dirty flag is cleared from the entry. | 40 // and all the data can be trusted, the dirty flag is cleared from the entry. |
| 51 // When the cache encounters an entry whose identifier is different than the one | 41 // When the cache encounters an entry whose identifier is different than the one |
| 52 // being currently used, it means that the entry was not properly closed on a | 42 // being currently used, it means that the entry was not properly closed on a |
| 53 // previous run, so it is discarded. | 43 // previous run, so it is discarded. |
| 54 | 44 |
| 55 #ifndef NET_DISK_CACHE_DISK_FORMAT_H_ | 45 #ifndef NET_DISK_CACHE_DISK_FORMAT_H_ |
| 56 #define NET_DISK_CACHE_DISK_FORMAT_H_ | 46 #define NET_DISK_CACHE_DISK_FORMAT_H_ |
| 57 | 47 |
| 58 #include "base/basictypes.h" | 48 #include "base/basictypes.h" |
| 59 #include "net/base/net_export.h" | 49 #include "net/base/net_export.h" |
| 50 #include "net/disk_cache/disk_format_base.h" |
| 60 | 51 |
| 61 namespace disk_cache { | 52 namespace disk_cache { |
| 62 | 53 |
| 63 typedef uint32 CacheAddr; | |
| 64 | |
| 65 const int kIndexTablesize = 0x10000; | 54 const int kIndexTablesize = 0x10000; |
| 66 const uint32 kIndexMagic = 0xC103CAC3; | 55 const uint32 kIndexMagic = 0xC103CAC3; |
| 67 const uint32 kCurrentVersion = 0x20000; // Version 2.0. | 56 const uint32 kCurrentVersion = 0x20000; // Version 2.0. |
| 68 | 57 |
| 69 struct LruData { | 58 struct LruData { |
| 70 int32 pad1[2]; | 59 int32 pad1[2]; |
| 71 int32 filled; // Flag to tell when we filled the cache. | 60 int32 filled; // Flag to tell when we filled the cache. |
| 72 int32 sizes[5]; | 61 int32 sizes[5]; |
| 73 CacheAddr heads[5]; | 62 CacheAddr heads[5]; |
| 74 CacheAddr tails[5]; | 63 CacheAddr tails[5]; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 CacheAddr next; // LRU list. | 141 CacheAddr next; // LRU list. |
| 153 CacheAddr prev; // LRU list. | 142 CacheAddr prev; // LRU list. |
| 154 CacheAddr contents; // Address of the EntryStore. | 143 CacheAddr contents; // Address of the EntryStore. |
| 155 int32 dirty; // The entry is being modifyied. | 144 int32 dirty; // The entry is being modifyied. |
| 156 uint32 self_hash; // RankingsNode's hash. | 145 uint32 self_hash; // RankingsNode's hash. |
| 157 }; | 146 }; |
| 158 #pragma pack(pop) | 147 #pragma pack(pop) |
| 159 | 148 |
| 160 COMPILE_ASSERT(sizeof(RankingsNode) == 36, bad_RankingsNode); | 149 COMPILE_ASSERT(sizeof(RankingsNode) == 36, bad_RankingsNode); |
| 161 | 150 |
| 162 const uint32 kBlockMagic = 0xC104CAC3; | |
| 163 const int kBlockHeaderSize = 8192; // Two pages: almost 64k entries | |
| 164 const int kMaxBlocks = (kBlockHeaderSize - 80) * 8; | |
| 165 | |
| 166 // Bitmap to track used blocks on a block-file. | |
| 167 typedef uint32 AllocBitmap[kMaxBlocks / 32]; | |
| 168 | |
| 169 // A block-file is the file used to store information in blocks (could be | |
| 170 // EntryStore blocks, RankingsNode blocks or user-data blocks). | |
| 171 // We store entries that can expand for up to 4 consecutive blocks, and keep | |
| 172 // counters of the number of blocks available for each type of entry. For | |
| 173 // instance, an entry of 3 blocks is an entry of type 3. We also keep track of | |
| 174 // where did we find the last entry of that type (to avoid searching the bitmap | |
| 175 // from the beginning every time). | |
| 176 // This Structure is the header of a block-file: | |
| 177 struct NET_EXPORT_PRIVATE BlockFileHeader { | |
| 178 BlockFileHeader(); | |
| 179 | |
| 180 uint32 magic; | |
| 181 uint32 version; | |
| 182 int16 this_file; // Index of this file. | |
| 183 int16 next_file; // Next file when this one is full. | |
| 184 int32 entry_size; // Size of the blocks of this file. | |
| 185 int32 num_entries; // Number of stored entries. | |
| 186 int32 max_entries; // Current maximum number of entries. | |
| 187 int32 empty[4]; // Counters of empty entries for each type. | |
| 188 int32 hints[4]; // Last used position for each entry type. | |
| 189 volatile int32 updating; // Keep track of updates to the header. | |
| 190 int32 user[5]; | |
| 191 AllocBitmap allocation_map; | |
| 192 }; | |
| 193 | |
| 194 COMPILE_ASSERT(sizeof(BlockFileHeader) == kBlockHeaderSize, bad_header); | |
| 195 | |
| 196 // Sparse data support: | |
| 197 // We keep a two level hierarchy to enable sparse data for an entry: the first | |
| 198 // level consists of using separate "child" entries to store ranges of 1 MB, | |
| 199 // and the second level stores blocks of 1 KB inside each child entry. | |
| 200 // | |
| 201 // Whenever we need to access a particular sparse offset, we first locate the | |
| 202 // child entry that stores that offset, so we discard the 20 least significant | |
| 203 // bits of the offset, and end up with the child id. For instance, the child id | |
| 204 // to store the first megabyte is 0, and the child that should store offset | |
| 205 // 0x410000 has an id of 4. | |
| 206 // | |
| 207 // The child entry is stored the same way as any other entry, so it also has a | |
| 208 // name (key). The key includes a signature to be able to identify children | |
| 209 // created for different generations of the same resource. In other words, given | |
| 210 // that a given sparse entry can have a large number of child entries, and the | |
| 211 // resource can be invalidated and replaced with a new version at any time, it | |
| 212 // is important to be sure that a given child actually belongs to certain entry. | |
| 213 // | |
| 214 // The full name of a child entry is composed with a prefix ("Range_"), and two | |
| 215 // hexadecimal 64-bit numbers at the end, separated by semicolons. The first | |
| 216 // number is the signature of the parent key, and the second number is the child | |
| 217 // id as described previously. The signature itself is also stored internally by | |
| 218 // the child and the parent entries. For example, a sparse entry with a key of | |
| 219 // "sparse entry name", and a signature of 0x052AF76, may have a child entry | |
| 220 // named "Range_sparse entry name:052af76:4", which stores data in the range | |
| 221 // 0x400000 to 0x4FFFFF. | |
| 222 // | |
| 223 // Each child entry keeps track of all the 1 KB blocks that have been written | |
| 224 // to the entry, but being a regular entry, it will happily return zeros for any | |
| 225 // read that spans data not written before. The actual sparse data is stored in | |
| 226 // one of the data streams of the child entry (at index 1), while the control | |
| 227 // information is stored in another stream (at index 2), both by parents and | |
| 228 // the children. | |
| 229 | |
| 230 // This structure contains the control information for parent and child entries. | |
| 231 // It is stored at offset 0 of the data stream with index 2. | |
| 232 // It is possible to write to a child entry in a way that causes the last block | |
| 233 // to be only partialy filled. In that case, last_block and last_block_len will | |
| 234 // keep track of that block. | |
| 235 struct SparseHeader { | |
| 236 int64 signature; // The parent and children signature. | |
| 237 uint32 magic; // Structure identifier (equal to kIndexMagic). | |
| 238 int32 parent_key_len; // Key length for the parent entry. | |
| 239 int32 last_block; // Index of the last written block. | |
| 240 int32 last_block_len; // Lenght of the last written block. | |
| 241 int32 dummy[10]; | |
| 242 }; | |
| 243 | |
| 244 // The SparseHeader will be followed by a bitmap, as described by this | |
| 245 // structure. | |
| 246 struct SparseData { | |
| 247 SparseHeader header; | |
| 248 uint32 bitmap[32]; // Bitmap representation of known children (if this | |
| 249 // is a parent entry), or used blocks (for child | |
| 250 // entries. The size is fixed for child entries but | |
| 251 // not for parents; it can be as small as 4 bytes | |
| 252 // and as large as 8 KB. | |
| 253 }; | |
| 254 | |
| 255 // The number of blocks stored by a child entry. | |
| 256 const int kNumSparseBits = 1024; | |
| 257 COMPILE_ASSERT(sizeof(SparseData) == sizeof(SparseHeader) + kNumSparseBits / 8, | |
| 258 Invalid_SparseData_bitmap); | |
| 259 | |
| 260 } // namespace disk_cache | 151 } // namespace disk_cache |
| 261 | 152 |
| 262 #endif // NET_DISK_CACHE_DISK_FORMAT_H_ | 153 #endif // NET_DISK_CACHE_DISK_FORMAT_H_ |
| OLD | NEW |